bump product version to 7.2.5.1
[LibreOffice.git] / toolkit / test / accessibility / ov / ObjectViewContainer.java
blobc7e647d67350980779680761c506da97093ec7e2
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package ov;
21 import java.awt.Component;
22 import java.awt.GridBagConstraints;
23 import java.awt.GridBagLayout;
24 import java.awt.Insets;
25 import java.util.ArrayList;
27 import javax.swing.BorderFactory;
28 import javax.swing.JPanel;
29 import javax.swing.border.BevelBorder;
30 import javax.swing.border.Border;
32 import com.sun.star.accessibility.XAccessibleContext;
35 public class ObjectViewContainer
36 extends JPanel
38 private static interface IViewFactory {
39 ObjectView Create (
40 ObjectViewContainer aContainer,
41 XAccessibleContext xContext);
44 public ObjectViewContainer ()
46 maViewTemplates = new ArrayList<IViewFactory>();
47 maViewBorder = BorderFactory.createBevelBorder (BevelBorder.RAISED);
48 setLayout (new GridBagLayout ());
50 maViewTemplates.add(new IViewFactory() {
51 public ObjectView Create(ObjectViewContainer aContainer,
52 XAccessibleContext xContext) {
53 return ContextView.Create(aContainer, xContext);
55 });
56 maViewTemplates.add(new IViewFactory() {
57 public ObjectView Create(ObjectViewContainer aContainer,
58 XAccessibleContext xContext) {
59 return FocusView.Create(aContainer, xContext);
61 });
62 maViewTemplates.add(new IViewFactory() {
63 public ObjectView Create(ObjectViewContainer aContainer,
64 XAccessibleContext xContext) {
65 return TextView.Create(aContainer, xContext);
67 });
70 /** Remove all existing views and create new ones according to the
71 interfaces supported by the given object.
73 public void SetObject (XAccessibleContext xContext)
75 // Call Destroy at all views to give them a chance to release their
76 // resources.
77 int n = getComponentCount();
78 for (int i=0; i<n; i++)
79 ((ObjectView)getComponent(i)).Destroy();
80 // Remove existing views.
81 removeAll ();
83 // Add new views.
84 for (int i=0; i<maViewTemplates.size(); i++)
86 IViewFactory aViewFactory = maViewTemplates.get(i);
87 ObjectView aView = aViewFactory.Create(this, xContext);
88 Add (aView);
91 UpdateLayoutManager ();
93 // Now set the object at all views.
94 n = getComponentCount();
95 for (int i=0; i<n; i++)
96 ((ObjectView)getComponent(i)).SetObject (xContext);
98 setPreferredSize (getLayout().preferredLayoutSize (this));
102 /** Add an object view and place it below all previously added views.
103 @param aView
104 This argument may be null. In this case nothing happens.
106 private void Add (ObjectView aView)
108 if (aView != null)
110 GridBagConstraints constraints = new GridBagConstraints ();
111 constraints.gridx = 0;
112 constraints.gridy = getComponentCount();
113 constraints.gridwidth = 1;
114 constraints.gridheight = 1;
115 constraints.weightx = 1;
116 constraints.weighty = 0;
117 constraints.ipadx = 2;
118 constraints.ipady = 5;
119 constraints.insets = new Insets (5,5,5,5);
120 constraints.anchor = GridBagConstraints.NORTH;
121 constraints.fill = GridBagConstraints.HORIZONTAL;
123 aView.setBorder (
124 BorderFactory.createTitledBorder (
125 maViewBorder, aView.GetTitle()));
127 add (aView, constraints);
131 /** Update the layout manager by setting the vertical weight of the
132 bottom entry to 1 and so make it stretch to over the available
133 space.
136 private void UpdateLayoutManager ()
138 // Adapt the layout manager.
139 if (getComponentCount() > 0)
141 Component aComponent = getComponent (getComponentCount()-1);
142 GridBagLayout aLayout = (GridBagLayout)getLayout();
143 GridBagConstraints aConstraints = aLayout.getConstraints (aComponent);
144 aConstraints.weighty = 1;
145 aLayout.setConstraints (aComponent, aConstraints);
149 private final Border maViewBorder;
150 /// List of view templates which are instantiated when new object is set.
151 private final ArrayList<IViewFactory> maViewTemplates;