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