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