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