1 package ov;
2 
3 import java.awt.Color;
4 import java.awt.BorderLayout;
5 import java.awt.Dimension;
6 import java.awt.Graphics;
7 import java.awt.Graphics2D;
8 import java.awt.Insets;
9 import java.awt.Rectangle;
10 import java.awt.RenderingHints;
11 import java.awt.Shape;
12 
13 import java.awt.event.MouseListener;
14 import java.awt.event.MouseEvent;
15 
16 import java.awt.geom.Rectangle2D;
17 import java.awt.geom.AffineTransform;
18 
19 
20 import javax.swing.JLabel;
21 import javax.swing.JPanel;
22 import javax.swing.border.Border;
23 
24 import com.sun.star.accessibility.AccessibleEventObject;
25 import com.sun.star.accessibility.AccessibleEventId;
26 import com.sun.star.accessibility.AccessibleStateType;
27 import com.sun.star.accessibility.XAccessibleContext;
28 import com.sun.star.accessibility.XAccessibleStateSet;
29 
30 import tools.NameProvider;
31 
32 public class StateSetView
33     extends ListeningObjectView
34     implements MouseListener
35 {
36     /** Create a FocusView when the given object supports the
37         XAccessibleComponent interface.
38     */
39     static public ObjectView Create (
40         ObjectViewContainer aContainer,
41         XAccessibleContext xContext)
42     {
43         ObjectView aView = null;
44         if (xContext != null)
45             if (mnViewMode == SHOW_ALL_STATES)
46                 aView = StateSetAllView.Create (aContainer, xContext);
47             else
48                 aView = StateSetSetView.Create (aContainer, xContext);
49         return aView;
50     }
51 
52     public StateSetView (ObjectViewContainer aContainer)
53     {
54         super (aContainer);
55 
56         addMouseListener (this);
57     }
58 
59     private void SetViewMode (int nViewMode)
60     {
61         mnViewMode = nViewMode;
62         switch (mnViewMode)
63         {
64             case SHOW_SET_STATES :
65                 maContainer.ReplaceView (
66                     getClass(),
67                     StateSetSetView.class);
68                 break;
69             case SHOW_ALL_STATES :
70                 maContainer.ReplaceView (
71                     getClass(),
72                     StateSetAllView.class);
73                 break;
74         }
75         aContainer.SetObject (mxContext);
76     }
77 
78 
79 
80     public String GetTitle ()
81     {
82         return ("StateSet");
83     }
84 
85     public void notifyEvent (AccessibleEventObject aEvent)
86     {
87         if (aEvent.EventId == AccessibleEventId.STATE_CHANGED)
88             Update();
89     }
90 
91     public void mouseClicked(MouseEvent e)
92     {
93         switch (mnViewMode)
94         {
95             case SHOW_SET_STATES :
96                 SetViewMode (SHOW_ALL_STATES);
97                 break;
98             case SHOW_ALL_STATES :
99                 SetViewMode (SHOW_SET_STATES);
100                 break;
101         }
102     }
103     public void mouseEntered (MouseEvent e) {}
104     public void mouseExited (MouseEvent e) {}
105     public void mousePressed (MouseEvent e) {}
106     public void mouseReleased(MouseEvent e) {}
107 
108     private static int mnViewMode = SHOW_ALL_STATES;
109     private final static int SHOW_SET_STATES = 0;
110     private final static int SHOW_ALL_STATES = 1;
111 
112 
113 
114 public class StateSetAllView
115     extends StateSetView
116 {
117     /** Create a FocusView when the given object supports the
118         XAccessibleComponent interface.
119     */
120     static public ObjectView Create (
121         ObjectViewContainer aContainer,
122         XAccessibleContext xContext)
123     {
124         if (xContext != null)
125             return new StateSetAllView (aContainer);
126         else
127             return null;
128     }
129 
130     public StateSetAllView (ObjectViewContainer aContainer)
131     {
132         super (aContainer);
133 
134         setPreferredSize (new Dimension(300,90));
135         setMinimumSize (new Dimension(200,80));
136     }
137 
138     public void paintChildren (Graphics g)
139     {
140         synchronized (g)
141         {
142             super.paintChildren (g);
143 
144             // Calculcate the are inside the border.
145             Insets aInsets = getInsets ();
146             Dimension aSize = getSize();
147             Rectangle aWidgetArea = new Rectangle (
148                 aInsets.left,
149                 aInsets.top,
150                 aSize.width-aInsets.left-aInsets.right,
151                 aSize.height-aInsets.top-aInsets.bottom);
152 
153             PaintAllStates ((Graphics2D)g, aWidgetArea);
154         }
155     }
156 
157     private void PaintAllStates (Graphics2D g, Rectangle aWidgetArea)
158     {
159         Color aTextColor = g.getColor();
160 
161         g.setRenderingHint (
162             RenderingHints.KEY_ANTIALIASING,
163             RenderingHints.VALUE_ANTIALIAS_ON);
164 
165         XAccessibleStateSet xStateSet = mxContext.getAccessibleStateSet();
166         if (xStateSet != null)
167         {
168             short aStates[] = xStateSet.getStates ();
169             final int nMaxStateIndex = AccessibleStateType.MANAGES_DESCENDANTS;
170             int nStateWidth = (aWidgetArea.width-12) / (nMaxStateIndex+1);
171             AffineTransform aTransform = g.getTransform ();
172             g.setColor (aTextColor);
173             int y = aWidgetArea.y+aWidgetArea.height - 12;
174             double nTextRotation = -0.9;//-java.lang.Math.PI/2;
175             double nScale = 0.6;
176 
177             // Create a shape for the boxes.
178             int nBoxWidth = nStateWidth-2;
179             if (nBoxWidth > 8)
180                 nBoxWidth = 8;
181             Rectangle aCheckBox = new Rectangle (-nBoxWidth/2,0,nBoxWidth,nBoxWidth);
182 
183             for (short i=0; i<=nMaxStateIndex; i++)
184             {
185                 int x = nStateWidth + i * nStateWidth;
186                 String sStateName = NameProvider.getStateName (i);
187                 boolean bStateSet = xStateSet.contains (i);
188                 g.setTransform (aTransform);
189                 g.translate (x,y);
190                 if (bStateSet)
191                 {
192                     g.setColor (Color.GREEN);
193                     g.fill (aCheckBox);
194                     g.setColor (aTextColor);
195                 }
196                 g.draw (aCheckBox);
197                 g.rotate (nTextRotation);
198                 g.scale (nScale, nScale);
199                 g.translate (2,-2);
200                 g.drawString (sStateName, 0,0);
201             }
202         }
203     }
204 }
205 
206 
207 public class StateSetSetView
208     extends StateSetView
209 {
210     static public ObjectView Create (
211         ObjectViewContainer aContainer,
212         XAccessibleContext xContext)
213     {
214         if (xContext != null)
215             return new StateSetSetView (aContainer);
216         else
217             return null;
218     }
219 
220     public StateSetSetView (ObjectViewContainer aContainer)
221     {
222         super (aContainer);
223 
224         maStates = null;
225         setPreferredSize (new Dimension(300,90));
226     }
227 
228 
229     synchronized public void Update ()
230     {
231         XAccessibleStateSet xStateSet = mxContext.getAccessibleStateSet();
232         if (xStateSet != null)
233         {
234             String sStates = new String ();
235             short aStates[] = xStateSet.getStates();
236             for (int i=0; i<aStates.length; i++)
237             {
238                 if (i > 0)
239                     sStates = sStates + ", ";
240                 sStates = sStates + NameProvider.getStateName(aStates[i]);
241             }
242             maStates.setText (sStates);
243         }
244     }
245 
246     private JLabel maStates;
247 }
248 
249 }
250