1*cdf0e10cSrcweir package ov;
2*cdf0e10cSrcweir 
3*cdf0e10cSrcweir import java.util.Vector;
4*cdf0e10cSrcweir 
5*cdf0e10cSrcweir import java.awt.event.ActionListener;
6*cdf0e10cSrcweir import java.awt.event.ActionEvent;
7*cdf0e10cSrcweir 
8*cdf0e10cSrcweir import java.awt.BorderLayout;
9*cdf0e10cSrcweir import java.awt.Dimension;
10*cdf0e10cSrcweir import java.awt.GridBagLayout;
11*cdf0e10cSrcweir import java.awt.GridBagConstraints;
12*cdf0e10cSrcweir 
13*cdf0e10cSrcweir import javax.swing.BoxLayout;
14*cdf0e10cSrcweir import javax.swing.ButtonGroup;
15*cdf0e10cSrcweir import javax.swing.JButton;
16*cdf0e10cSrcweir import javax.swing.JCheckBox;
17*cdf0e10cSrcweir import javax.swing.JLabel;
18*cdf0e10cSrcweir import javax.swing.JList;
19*cdf0e10cSrcweir import javax.swing.JPanel;
20*cdf0e10cSrcweir import javax.swing.JOptionPane;
21*cdf0e10cSrcweir import javax.swing.JRadioButton;
22*cdf0e10cSrcweir import javax.swing.JScrollPane;
23*cdf0e10cSrcweir import javax.swing.JToggleButton;
24*cdf0e10cSrcweir import javax.swing.ListSelectionModel;
25*cdf0e10cSrcweir 
26*cdf0e10cSrcweir 
27*cdf0e10cSrcweir import com.sun.star.accessibility.AccessibleEventId;
28*cdf0e10cSrcweir import com.sun.star.accessibility.AccessibleEventObject;
29*cdf0e10cSrcweir import com.sun.star.accessibility.AccessibleStateType;
30*cdf0e10cSrcweir import com.sun.star.accessibility.XAccessible;
31*cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleContext;
32*cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleSelection;
33*cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleStateSet;
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
36*cdf0e10cSrcweir import com.sun.star.lang.IndexOutOfBoundsException;
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir /** Display a list of children and select/deselect buttons
40*cdf0e10cSrcweir */
41*cdf0e10cSrcweir class SelectionView
42*cdf0e10cSrcweir     extends ListeningObjectView
43*cdf0e10cSrcweir     implements ActionListener
44*cdf0e10cSrcweir {
45*cdf0e10cSrcweir     static public ObjectView Create (
46*cdf0e10cSrcweir         ObjectViewContainer aContainer,
47*cdf0e10cSrcweir         XAccessibleContext xContext)
48*cdf0e10cSrcweir     {
49*cdf0e10cSrcweir         XAccessibleSelection xSelection = (XAccessibleSelection)UnoRuntime.queryInterface(
50*cdf0e10cSrcweir                 XAccessibleSelection.class, xContext);
51*cdf0e10cSrcweir         if (xSelection != null)
52*cdf0e10cSrcweir             return new SelectionView(aContainer);
53*cdf0e10cSrcweir         else
54*cdf0e10cSrcweir             return null;
55*cdf0e10cSrcweir     }
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir     public SelectionView (ObjectViewContainer aContainer)
58*cdf0e10cSrcweir     {
59*cdf0e10cSrcweir         super (aContainer);
60*cdf0e10cSrcweir         Layout();
61*cdf0e10cSrcweir     }
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir     public String GetTitle ()
64*cdf0e10cSrcweir     {
65*cdf0e10cSrcweir         return "Selection";
66*cdf0e10cSrcweir     }
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir     /** Create and arrange the widgets for this view.
69*cdf0e10cSrcweir     */
70*cdf0e10cSrcweir     private void Layout ()
71*cdf0e10cSrcweir     {
72*cdf0e10cSrcweir         setLayout (new GridBagLayout());
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir         GridBagConstraints aConstraints = new GridBagConstraints();
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir         // Label that shows wheter the selection is multi selectable.
77*cdf0e10cSrcweir         aConstraints.gridx = 0;
78*cdf0e10cSrcweir         aConstraints.gridy = 0;
79*cdf0e10cSrcweir         aConstraints.anchor = GridBagConstraints.WEST;
80*cdf0e10cSrcweir         maTypeLabel = new JLabel ();
81*cdf0e10cSrcweir         add (maTypeLabel, aConstraints);
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir         // the JListBox
84*cdf0e10cSrcweir         maChildrenSelector = new JPanel ();
85*cdf0e10cSrcweir         maChildrenSelector.setPreferredSize (new Dimension (100,100));
86*cdf0e10cSrcweir         maChildrenSelector.setLayout (new BoxLayout (maChildrenSelector, BoxLayout.Y_AXIS));
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir         aConstraints.gridx = 0;
89*cdf0e10cSrcweir         aConstraints.gridwidth = 4;
90*cdf0e10cSrcweir         aConstraints.gridy = 1;
91*cdf0e10cSrcweir         aConstraints.fill = GridBagConstraints.HORIZONTAL;
92*cdf0e10cSrcweir         add (new JScrollPane (maChildrenSelector,
93*cdf0e10cSrcweir                  JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
94*cdf0e10cSrcweir                  JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),
95*cdf0e10cSrcweir             aConstraints);
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir         JButton aButton;
98*cdf0e10cSrcweir         aButton = new JButton( "Select all" );
99*cdf0e10cSrcweir         aButton.setActionCommand( "Select all" );
100*cdf0e10cSrcweir         aButton.addActionListener( this );
101*cdf0e10cSrcweir         aConstraints.gridx = 0;
102*cdf0e10cSrcweir         aConstraints.gridwidth = 1;
103*cdf0e10cSrcweir         aConstraints.gridy = 2;
104*cdf0e10cSrcweir         aConstraints.fill = GridBagConstraints.NONE;
105*cdf0e10cSrcweir         aConstraints.anchor = GridBagConstraints.WEST;
106*cdf0e10cSrcweir         add (aButton, aConstraints);
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir         aButton = new JButton( "Clear Selection" );
109*cdf0e10cSrcweir         aButton.setActionCommand( "Clear Selection" );
110*cdf0e10cSrcweir         aButton.addActionListener( this );
111*cdf0e10cSrcweir         aConstraints.gridx = 1;
112*cdf0e10cSrcweir         aConstraints.gridy = 2;
113*cdf0e10cSrcweir         aConstraints.weightx = 1;
114*cdf0e10cSrcweir         add (aButton, aConstraints);
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir         setSize (getPreferredSize());
117*cdf0e10cSrcweir     }
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir     public void SetObject (XAccessibleContext xContext)
121*cdf0e10cSrcweir     {
122*cdf0e10cSrcweir         mxSelection = (XAccessibleSelection)UnoRuntime.queryInterface(
123*cdf0e10cSrcweir             XAccessibleSelection.class, xContext);
124*cdf0e10cSrcweir         super.SetObject (xContext);
125*cdf0e10cSrcweir     }
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir     public void Update ()
129*cdf0e10cSrcweir     {
130*cdf0e10cSrcweir         maChildrenSelector.removeAll ();
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir         // Determine whether multi selection is possible.
133*cdf0e10cSrcweir         XAccessibleStateSet aStateSet = mxContext.getAccessibleStateSet();
134*cdf0e10cSrcweir         boolean bMultiSelectable = false;
135*cdf0e10cSrcweir         ButtonGroup aButtonGroup = null;
136*cdf0e10cSrcweir         if (aStateSet!=null && aStateSet.contains(AccessibleStateType.MULTI_SELECTABLE))
137*cdf0e10cSrcweir         {
138*cdf0e10cSrcweir             bMultiSelectable = true;
139*cdf0e10cSrcweir             maTypeLabel.setText ("multi selectable");
140*cdf0e10cSrcweir         }
141*cdf0e10cSrcweir         else
142*cdf0e10cSrcweir         {
143*cdf0e10cSrcweir             maTypeLabel.setText ("single selectable");
144*cdf0e10cSrcweir             aButtonGroup = new ButtonGroup ();
145*cdf0e10cSrcweir         }
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir         int nCount = mxContext.getAccessibleChildCount();
148*cdf0e10cSrcweir         for (int i=0; i<nCount; i++)
149*cdf0e10cSrcweir         {
150*cdf0e10cSrcweir             try
151*cdf0e10cSrcweir             {
152*cdf0e10cSrcweir                 XAccessible xChild = mxContext.getAccessibleChild(i);
153*cdf0e10cSrcweir                 XAccessibleContext xChildContext = xChild.getAccessibleContext();
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir                 String sName = i + " " + xChildContext.getAccessibleName();
156*cdf0e10cSrcweir                 JToggleButton aChild;
157*cdf0e10cSrcweir                 if (bMultiSelectable)
158*cdf0e10cSrcweir                     aChild = new JCheckBox (sName);
159*cdf0e10cSrcweir                 else
160*cdf0e10cSrcweir                 {
161*cdf0e10cSrcweir                     aChild = new JRadioButton (sName);
162*cdf0e10cSrcweir                     aButtonGroup.add (aChild);
163*cdf0e10cSrcweir                 }
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir                 XAccessibleStateSet aChildStateSet = mxContext.getAccessibleStateSet();
166*cdf0e10cSrcweir                 aChild.setSelected (aChildStateSet!=null
167*cdf0e10cSrcweir                     && aChildStateSet.contains(AccessibleStateType.SELECTED));
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir                 aChild.addActionListener (this);
170*cdf0e10cSrcweir                 maChildrenSelector.add (aChild);
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir             }
173*cdf0e10cSrcweir             catch (IndexOutOfBoundsException e)
174*cdf0e10cSrcweir             {
175*cdf0e10cSrcweir             }
176*cdf0e10cSrcweir         }
177*cdf0e10cSrcweir     }
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir     void SelectAll()
181*cdf0e10cSrcweir     {
182*cdf0e10cSrcweir         mxSelection.selectAllAccessibleChildren();
183*cdf0e10cSrcweir     }
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir     void ClearSelection()
186*cdf0e10cSrcweir     {
187*cdf0e10cSrcweir         mxSelection.clearAccessibleSelection();
188*cdf0e10cSrcweir     }
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir     /** Call the function associated with the pressed button.
192*cdf0e10cSrcweir     */
193*cdf0e10cSrcweir     public void actionPerformed (ActionEvent aEvent)
194*cdf0e10cSrcweir     {
195*cdf0e10cSrcweir         String sCommand = aEvent.getActionCommand();
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir         if (sCommand.equals ("Clear Selection"))
198*cdf0e10cSrcweir             ClearSelection();
199*cdf0e10cSrcweir         else if (sCommand.equals ("Select all"))
200*cdf0e10cSrcweir             SelectAll();
201*cdf0e10cSrcweir         else
202*cdf0e10cSrcweir         {
203*cdf0e10cSrcweir             // Extract the child index from the widget text.
204*cdf0e10cSrcweir             String[] aWords = sCommand.split (" ");
205*cdf0e10cSrcweir             int nIndex = Integer.parseInt(aWords[0]);
206*cdf0e10cSrcweir             try
207*cdf0e10cSrcweir             {
208*cdf0e10cSrcweir                 if (((JToggleButton)aEvent.getSource()).isSelected())
209*cdf0e10cSrcweir                     mxSelection.selectAccessibleChild (nIndex);
210*cdf0e10cSrcweir                 else
211*cdf0e10cSrcweir                     mxSelection.deselectAccessibleChild (nIndex);
212*cdf0e10cSrcweir             }
213*cdf0e10cSrcweir             catch (IndexOutOfBoundsException e)
214*cdf0e10cSrcweir             {
215*cdf0e10cSrcweir                 System.err.println ("caught exception while changing selection: " + e);
216*cdf0e10cSrcweir             }
217*cdf0e10cSrcweir         }
218*cdf0e10cSrcweir     }
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir     public void notifyEvent (AccessibleEventObject aEvent)
222*cdf0e10cSrcweir     {
223*cdf0e10cSrcweir         if (aEvent.EventId == AccessibleEventId.SELECTION_CHANGED)
224*cdf0e10cSrcweir             Update ();
225*cdf0e10cSrcweir     }
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir     private JPanel maChildrenSelector;
228*cdf0e10cSrcweir     private XAccessibleSelection mxSelection;
229*cdf0e10cSrcweir     private JLabel maTypeLabel;
230*cdf0e10cSrcweir }
231