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