1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 package ov;
23 
24 import java.awt.Color;
25 import java.awt.Component;
26 import java.awt.GridBagLayout;
27 import java.awt.GridBagConstraints;
28 import java.awt.Insets;
29 
30 import java.util.Vector;
31 
32 import java.lang.reflect.Method;
33 import java.lang.NoSuchMethodException;
34 import java.lang.IllegalAccessException;
35 import java.lang.reflect.InvocationTargetException;
36 
37 import javax.swing.JPanel;
38 import javax.swing.JTree;
39 import javax.swing.BorderFactory;
40 import javax.swing.border.Border;
41 import javax.swing.border.BevelBorder;
42 
43 import com.sun.star.accessibility.XAccessibleContext;
44 import com.sun.star.accessibility.XAccessibleComponent;
45 import com.sun.star.accessibility.XAccessibleSelection;
46 import com.sun.star.uno.UnoRuntime;
47 
48 
49 public class ObjectViewContainer
50     extends JPanel
51 {
ObjectViewContainer()52     public ObjectViewContainer ()
53     {
54         maViewTemplates = new Vector ();
55         maViewBorder = BorderFactory.createBevelBorder (BevelBorder.RAISED);
56         setLayout (new GridBagLayout ());
57 
58         System.out.println ("ObjectViewContainer");
59         RegisterView (ContextView.class);
60         //        RegisterView (StateSetView.class);
61         RegisterView (FocusView.class);
62         RegisterView (TextView.class);
63     }
64 
65 
66 
67     /** Remove all existing views and create new ones according to the
68         interfaces supported by the given object.
69     */
SetObject(XAccessibleContext xContext)70     public void SetObject (XAccessibleContext xContext)
71     {
72         // Call Destroy at all views to give them a chance to release their
73         // resources.
74         int n = getComponentCount();
75         for (int i=0; i<n; i++)
76             ((ObjectView)getComponent(i)).Destroy();
77         // Remove existing views.
78         removeAll ();
79 
80         // Add new views.
81         for (int i=0; i<maViewTemplates.size(); i++)
82         {
83             try
84             {
85                 Class aViewClass = (Class)maViewTemplates.elementAt (i);
86                 Method aCreateMethod = aViewClass.getDeclaredMethod (
87                     "Create", new Class[] {
88                         ObjectViewContainer.class,
89                         XAccessibleContext.class});
90                 if (aCreateMethod != null)
91                 {
92                     ObjectView aView = (ObjectView)
93                         aCreateMethod.invoke (null, new Object[] {this, xContext});
94                     Add (aView);
95                 }
96             }
97             catch (NoSuchMethodException e)
98             {System.err.println ("Caught exception while creating view " + i + " : " + e);}
99             catch (IllegalAccessException e)
100             {System.err.println ("Caught exception while creating view " + i + " : " + e);}
101             catch (InvocationTargetException e)
102             {System.err.println ("Caught exception while creating view " + i + " : " + e);}
103         }
104 
105         UpdateLayoutManager ();
106 
107         // Now set the object at all views.
108         n = getComponentCount();
109         for (int i=0; i<n; i++)
110             ((ObjectView)getComponent(i)).SetObject (xContext);
111 
112         setPreferredSize (getLayout().preferredLayoutSize (this));
113     }
114 
115 
116     /** Add the given class to the list of classes which will be
117         instantiated the next time an accessible object is set.
118     */
RegisterView(Class aObjectViewClass)119     public void RegisterView (Class aObjectViewClass)
120     {
121         System.out.println ("registering " + aObjectViewClass);
122         maViewTemplates.addElement (aObjectViewClass);
123     }
124 
125     /** Replace one view class with another.
126     */
ReplaceView(Class aObjectViewClass, Class aSubstitution)127     public void ReplaceView (Class aObjectViewClass, Class aSubstitution)
128     {
129         int nIndex = maViewTemplates.indexOf (aObjectViewClass);
130         if (nIndex >= 0)
131             maViewTemplates.setElementAt (aSubstitution, nIndex);
132     }
133 
134     /** Add an object view and place it below all previously added views.
135         @param aView
136             This argument may be null.  In this case nothing happens.
137     */
Add(ObjectView aView)138     private void Add (ObjectView aView)
139     {
140         if (aView != null)
141         {
142             GridBagConstraints constraints = new GridBagConstraints ();
143             constraints.gridx = 0;
144             constraints.gridy = getComponentCount();
145             constraints.gridwidth = 1;
146             constraints.gridheight = 1;
147             constraints.weightx = 1;
148             constraints.weighty = 0;
149             constraints.ipadx = 2;
150             constraints.ipady = 5;
151             constraints.insets = new Insets (5,5,5,5);
152             constraints.anchor = GridBagConstraints.NORTH;
153             constraints.fill = GridBagConstraints.HORIZONTAL;
154 
155             aView.setBorder (
156                 BorderFactory.createTitledBorder (
157                     maViewBorder, aView.GetTitle()));
158 
159             add (aView, constraints);
160         }
161     }
162 
163     /** Update the layout manager by setting the vertical weight of the
164         bottom entry to 1 and so make it strech to over the available
165         space.
166 
167     */
UpdateLayoutManager()168     private void UpdateLayoutManager ()
169     {
170         // Adapt the layout manager.
171         if (getComponentCount() > 0)
172         {
173             Component aComponent = getComponent (getComponentCount()-1);
174             GridBagLayout aLayout = (GridBagLayout)getLayout();
175             GridBagConstraints aConstraints = aLayout.getConstraints (aComponent);
176             aConstraints.weighty = 1;
177             aLayout.setConstraints (aComponent, aConstraints);
178         }
179     }
180 
181     /// Observe this tree for selection changes and notify them to all
182     /// children.
183     private JTree maTree;
184     private Border maViewBorder;
185     /// List of view templates which are instantiated when new object is set.
186     private Vector maViewTemplates;
187 }
188