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