merge the formfield patch from ooo-build
[ooovba.git] / toolkit / test / accessibility / ov / StateSetView.java
bloba2e4e28a49b3cbea92c73bf548c8d797193d6a2e
1 package ov;
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;
13 import java.awt.event.MouseListener;
14 import java.awt.event.MouseEvent;
16 import java.awt.geom.Rectangle2D;
17 import java.awt.geom.AffineTransform;
20 import javax.swing.JLabel;
21 import javax.swing.JPanel;
22 import javax.swing.border.Border;
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;
30 import tools.NameProvider;
32 public class StateSetView
33 extends ListeningObjectView
34 implements MouseListener
36 /** Create a FocusView when the given object supports the
37 XAccessibleComponent interface.
39 static public ObjectView Create (
40 ObjectViewContainer aContainer,
41 XAccessibleContext xContext)
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;
52 public StateSetView (ObjectViewContainer aContainer)
54 super (aContainer);
56 addMouseListener (this);
59 private void SetViewMode (int nViewMode)
61 mnViewMode = nViewMode;
62 switch (mnViewMode)
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;
75 aContainer.SetObject (mxContext);
80 public String GetTitle ()
82 return ("StateSet");
85 public void notifyEvent (AccessibleEventObject aEvent)
87 if (aEvent.EventId == AccessibleEventId.STATE_CHANGED)
88 Update();
91 public void mouseClicked(MouseEvent e)
93 switch (mnViewMode)
95 case SHOW_SET_STATES :
96 SetViewMode (SHOW_ALL_STATES);
97 break;
98 case SHOW_ALL_STATES :
99 SetViewMode (SHOW_SET_STATES);
100 break;
103 public void mouseEntered (MouseEvent e) {}
104 public void mouseExited (MouseEvent e) {}
105 public void mousePressed (MouseEvent e) {}
106 public void mouseReleased(MouseEvent e) {}
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;
114 public class StateSetAllView
115 extends StateSetView
117 /** Create a FocusView when the given object supports the
118 XAccessibleComponent interface.
120 static public ObjectView Create (
121 ObjectViewContainer aContainer,
122 XAccessibleContext xContext)
124 if (xContext != null)
125 return new StateSetAllView (aContainer);
126 else
127 return null;
130 public StateSetAllView (ObjectViewContainer aContainer)
132 super (aContainer);
134 setPreferredSize (new Dimension(300,90));
135 setMinimumSize (new Dimension(200,80));
138 public void paintChildren (Graphics g)
140 synchronized (g)
142 super.paintChildren (g);
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);
153 PaintAllStates ((Graphics2D)g, aWidgetArea);
157 private void PaintAllStates (Graphics2D g, Rectangle aWidgetArea)
159 Color aTextColor = g.getColor();
161 g.setRenderingHint (
162 RenderingHints.KEY_ANTIALIASING,
163 RenderingHints.VALUE_ANTIALIAS_ON);
165 XAccessibleStateSet xStateSet = mxContext.getAccessibleStateSet();
166 if (xStateSet != null)
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;
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);
183 for (short i=0; i<=nMaxStateIndex; i++)
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)
192 g.setColor (Color.GREEN);
193 g.fill (aCheckBox);
194 g.setColor (aTextColor);
196 g.draw (aCheckBox);
197 g.rotate (nTextRotation);
198 g.scale (nScale, nScale);
199 g.translate (2,-2);
200 g.drawString (sStateName, 0,0);
207 public class StateSetSetView
208 extends StateSetView
210 static public ObjectView Create (
211 ObjectViewContainer aContainer,
212 XAccessibleContext xContext)
214 if (xContext != null)
215 return new StateSetSetView (aContainer);
216 else
217 return null;
220 public StateSetSetView (ObjectViewContainer aContainer)
222 super (aContainer);
224 maStates = null;
225 setPreferredSize (new Dimension(300,90));
229 synchronized public void Update ()
231 XAccessibleStateSet xStateSet = mxContext.getAccessibleStateSet();
232 if (xStateSet != null)
234 String sStates = new String ();
235 short aStates[] = xStateSet.getStates();
236 for (int i=0; i<aStates.length; i++)
238 if (i > 0)
239 sStates = sStates + ", ";
240 sStates = sStates + NameProvider.getStateName(aStates[i]);
242 maStates.setText (sStates);
246 private JLabel maStates;