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