merge the formfield patch from ooo-build
[ooovba.git] / toolkit / test / accessibility / ov / ObjectViewContainer.java
blob96eb1c7b5363a4e603f4a58b10807895cfb349c9
1 package ov;
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;
9 import java.util.Vector;
11 import java.lang.reflect.Method;
12 import java.lang.NoSuchMethodException;
13 import java.lang.IllegalAccessException;
14 import java.lang.reflect.InvocationTargetException;
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;
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;
28 public class ObjectViewContainer
29 extends JPanel
31 public ObjectViewContainer ()
33 maViewTemplates = new Vector ();
34 maViewBorder = BorderFactory.createBevelBorder (BevelBorder.RAISED);
35 setLayout (new GridBagLayout ());
37 System.out.println ("ObjectViewContainer");
38 RegisterView (ContextView.class);
39 // RegisterView (StateSetView.class);
40 RegisterView (FocusView.class);
41 RegisterView (TextView.class);
46 /** Remove all existing views and create new ones according to the
47 interfaces supported by the given object.
49 public void SetObject (XAccessibleContext xContext)
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 ();
59 // Add new views.
60 for (int i=0; i<maViewTemplates.size(); i++)
62 try
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)
71 ObjectView aView = (ObjectView)
72 aCreateMethod.invoke (null, new Object[] {this, xContext});
73 Add (aView);
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);}
84 UpdateLayoutManager ();
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);
91 setPreferredSize (getLayout().preferredLayoutSize (this));
95 /** Add the given class to the list of classes which will be
96 instantiated the next time an accessible object is set.
98 public void RegisterView (Class aObjectViewClass)
100 System.out.println ("registering " + aObjectViewClass);
101 maViewTemplates.addElement (aObjectViewClass);
104 /** Replace one view class with another.
106 public void ReplaceView (Class aObjectViewClass, Class aSubstitution)
108 int nIndex = maViewTemplates.indexOf (aObjectViewClass);
109 if (nIndex >= 0)
110 maViewTemplates.setElementAt (aSubstitution, nIndex);
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.
117 private void Add (ObjectView aView)
119 if (aView != null)
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;
134 aView.setBorder (
135 BorderFactory.createTitledBorder (
136 maViewBorder, aView.GetTitle()));
138 add (aView, constraints);
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.
147 private void UpdateLayoutManager ()
149 // Adapt the layout manager.
150 if (getComponentCount() > 0)
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);
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;