1 import com.sun.star.uno.UnoRuntime;
2 import com.sun.star.accessibility.XAccessible;
3 import com.sun.star.accessibility.XAccessibleContext;
4 import com.sun.star.accessibility.XAccessibleSelection;
5 import com.sun.star.lang.IndexOutOfBoundsException;
6 
7 import javax.swing.*;
8 import java.awt.*;
9 import java.util.Vector;
10 import java.awt.event.ActionListener;
11 import java.awt.event.ActionEvent;
12 
13 
14 
15 
16 /**
17  * Display a dialog with a list-box of children and select/deselect buttons
18  */
19 class SelectionDialog extends JDialog
20     implements ActionListener
21 {
22     public SelectionDialog (AccTreeNode aNode)
23     {
24         super (AccessibilityWorkBench.Instance());
25 
26         maNode = aNode;
27 
28         Layout();
29     }
30 
31     /** build dialog */
32     protected void Layout ()
33     {
34         setTitle( "Select" );
35 
36         // vertical stacking of the elements
37         Container aContent = getContentPane();
38 
39         // label with explanation
40         aContent.add( new JLabel( "Select/Deselect child elements" ),
41                       BorderLayout.NORTH );
42 
43         // the JListBox
44         maChildrenSelector = new JList (GetChildrenList());
45         maChildrenSelector.setPreferredSize (new Dimension (500,300));
46         aContent.add (maChildrenSelector, BorderLayout.CENTER);
47         maChildrenSelector.setSelectionMode (ListSelectionModel.SINGLE_SELECTION);
48 
49         JPanel aButtons = new JPanel();
50         aButtons.setLayout( new FlowLayout() );
51 
52         JButton aButton;
53 
54         aButton = new JButton( "Select" );
55         aButton.setActionCommand( "Select" );
56         aButton.addActionListener( this );
57         aButtons.add( aButton );
58 
59         aButton = new JButton( "Deselect" );
60         aButton.setActionCommand( "Deselect" );
61         aButton.addActionListener( this );
62         aButtons.add( aButton );
63 
64         aButton = new JButton( "Select all" );
65         aButton.setActionCommand( "Select all" );
66         aButton.addActionListener( this );
67         aButtons.add( aButton );
68 
69         aButton = new JButton( "Clear Selection" );
70         aButton.setActionCommand( "Clear Selection" );
71         aButton.addActionListener( this );
72         aButtons.add( aButton );
73 
74         aButton = new JButton( "Close" );
75         aButton.setActionCommand( "Close" );
76         aButton.addActionListener( this );
77         aButtons.add( aButton );
78 
79         // add Panel with buttons
80         aContent.add( aButtons, BorderLayout.SOUTH );
81 
82         setSize( getPreferredSize() );
83     }
84 
85     /** Get a list of all children
86     */
87     private Vector GetChildrenList ()
88     {
89         mxSelection = maNode.getSelection();
90 
91         XAccessibleContext xContext = maNode.getContext();
92         int nCount = xContext.getAccessibleChildCount();
93         Vector aChildVector = new Vector();
94         for(int i = 0; i < nCount; i++)
95         {
96             try
97             {
98                 XAccessible xChild = xContext.getAccessibleChild(i);
99                 XAccessibleContext xChildContext = xChild.getAccessibleContext();
100                 aChildVector.add( i + " " + xChildContext.getAccessibleName());
101             }
102             catch( IndexOutOfBoundsException e )
103             {
104                 aChildVector.add( "ERROR: IndexOutOfBoundsException" );
105             }
106         }
107         return aChildVector;
108     }
109 
110 
111     void close ()
112     {
113         hide();
114         dispose();
115     }
116 
117     void select()
118     {
119         try
120         {
121             mxSelection.selectAccessibleChild (maChildrenSelector.getSelectedIndex());
122         }
123         catch( IndexOutOfBoundsException e )
124         {
125             JOptionPane.showMessageDialog( AccessibilityWorkBench.Instance(),
126                                            "Can't select: IndexOutofBounds",
127                                            "Error in selectAccessibleChild",
128                                            JOptionPane.ERROR_MESSAGE);
129         }
130     }
131 
132     void deselect()
133     {
134         try
135         {
136             mxSelection.deselectAccessibleChild(
137                 maChildrenSelector.getSelectedIndex());
138         }
139         catch( IndexOutOfBoundsException e )
140         {
141             JOptionPane.showMessageDialog( AccessibilityWorkBench.Instance(),
142                                            "Can't deselect: IndexOutofBounds",
143                                            "Error in deselectAccessibleChild",
144                                            JOptionPane.ERROR_MESSAGE);
145         }
146     }
147 
148     void selectAll()
149     {
150         mxSelection.selectAllAccessibleChildren();
151     }
152 
153     void clearSelection()
154     {
155         mxSelection.clearAccessibleSelection();
156     }
157 
158 
159 
160     public void actionPerformed(ActionEvent e)
161     {
162         String sCommand = e.getActionCommand();
163 
164         if( "Close".equals( sCommand ) )
165             close();
166         else if ( "Select".equals( sCommand ) )
167             select();
168         else if ( "Deselect".equals( sCommand ) )
169             deselect();
170         else if ( "Clear Selection".equals( sCommand ) )
171             clearSelection();
172         else if ( "Select all".equals( sCommand ) )
173             selectAll();
174     }
175 
176     private JList maChildrenSelector;
177     private XAccessibleSelection mxSelection;
178     private AccTreeNode maNode;
179 }
180