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 
23 
24 package org.openoffice.accessibility.awb.view;
25 
26 import java.awt.Color;
27 import java.awt.Dimension;
28 import java.awt.Graphics;
29 import java.awt.Graphics2D;
30 import java.awt.Insets;
31 import java.awt.Rectangle;
32 import java.awt.RenderingHints;
33 import java.awt.geom.AffineTransform;
34 
35 
36 
37 import com.sun.star.accessibility.AccessibleEventObject;
38 import com.sun.star.accessibility.AccessibleEventId;
39 import com.sun.star.accessibility.AccessibleStateType;
40 import com.sun.star.accessibility.XAccessibleContext;
41 import com.sun.star.accessibility.XAccessibleStateSet;
42 
43 import org.openoffice.accessibility.misc.NameProvider;
44 
45 public class StateSetView
46     extends ObjectView
47 {
48     /** Create a FocusView when the given object supports the
49         XAccessibleComponent interface.
50     */
Create( ObjectViewContainer aContainer, XAccessibleContext xContext)51     static public ObjectView Create (
52         ObjectViewContainer aContainer,
53         XAccessibleContext xContext)
54     {
55         ObjectView aView = null;
56         if (xContext != null)
57             aView = new StateSetView (aContainer);
58 
59         return aView;
60     }
61 
StateSetView(ObjectViewContainer aContainer)62     public StateSetView (ObjectViewContainer aContainer)
63     {
64         super (aContainer);
65         setPreferredSize (new Dimension(300,110));
66         setMinimumSize (new Dimension(200,80));
67     }
68 
GetTitle()69     public String GetTitle ()
70     {
71         return ("StateSet");
72     }
73 
notifyEvent(AccessibleEventObject aEvent)74     public void notifyEvent (AccessibleEventObject aEvent)
75     {
76         if (aEvent.EventId == AccessibleEventId.STATE_CHANGED)
77             Update();
78     }
79 
80 
Update()81     public void Update ()
82     {
83         repaint ();
84     }
85 
paintChildren(Graphics g)86     public void paintChildren (Graphics g)
87     {
88         if (g != null)
89             synchronized (g)
90             {
91                 super.paintChildren (g);
92 
93                 // Calculcate the are inside the border.
94                 Insets aInsets = getInsets ();
95                 Dimension aSize = getSize();
96                 Rectangle aWidgetArea = new Rectangle (
97                     aInsets.left,
98                     aInsets.top,
99                     aSize.width-aInsets.left-aInsets.right,
100                     aSize.height-aInsets.top-aInsets.bottom);
101 
102                 PaintAllStates ((Graphics2D)g, aWidgetArea);
103             }
104     }
105 
PaintAllStates(Graphics2D g, Rectangle aWidgetArea)106     private void PaintAllStates (Graphics2D g, Rectangle aWidgetArea)
107     {
108         Color aTextColor = g.getColor();
109 
110         g.setRenderingHint (
111             RenderingHints.KEY_ANTIALIASING,
112             RenderingHints.VALUE_ANTIALIAS_ON);
113 
114         XAccessibleStateSet xStateSet = ( mxContext != null ) ? mxContext.getAccessibleStateSet() : null;
115         if (xStateSet != null)
116         {
117             short aStates[] = xStateSet.getStates ();
118             final int nMaxStateIndex = AccessibleStateType.VISIBLE;//MANAGES_DESCENDANTS;
119             int nStateWidth = (aWidgetArea.width-12) / (nMaxStateIndex+1);
120             AffineTransform aTransform = g.getTransform ();
121             g.setColor (aTextColor);
122             int y = aWidgetArea.y+aWidgetArea.height - 25;
123             double nTextRotation = -0.9;//-java.lang.Math.PI/2;
124             double nScale = 0.6;
125 
126             // Create a shape for the boxes.
127             int nBoxWidth = 8;
128             Rectangle aCheckBox = new Rectangle (-nBoxWidth/2,0,nBoxWidth,nBoxWidth);
129 
130             // For each state draw a box, fill it appropriately, and draw
131             // thre states name.
132             for (short i=0; i<=nMaxStateIndex; i++)
133             {
134                 int x = nStateWidth + i * nStateWidth;
135                 String sStateName = NameProvider.getStateName (i);
136                 if (sStateName == null)
137                     sStateName = new String ("<unknown state " + i + ">");
138                 boolean bStateSet = xStateSet.contains (i);
139                 g.setTransform (aTransform);
140                 g.translate (x,y);
141                 if (bStateSet)
142                 {
143                     switch (i)
144                     {
145                         case AccessibleStateType.INVALID:
146                         case AccessibleStateType.DEFUNC:
147                             g.setColor (saInvalidColor);
148                             break;
149                         case AccessibleStateType.FOCUSED:
150                             g.setColor (saFocusColor);
151                             break;
152                         case AccessibleStateType.SELECTED:
153                             g.setColor (saSelectionColor);
154                             break;
155                         case AccessibleStateType.EDITABLE:
156                             g.setColor (saEditColor);
157                             break;
158                         default:
159                             g.setColor (saDefaultColor);
160                             break;
161                     }
162                     g.fill (aCheckBox);
163                     g.setColor (aTextColor);
164                 }
165                 g.draw (aCheckBox);
166                 g.rotate (nTextRotation);
167                 g.scale (nScale, nScale);
168                 g.translate (2,-2);
169                 g.drawString (sStateName, 0,0);
170             }
171 
172             // Draw string of set states.
173             String sStates = new String ();
174             for (int i=0; i<aStates.length; i++)
175             {
176                 if (i > 0)
177                     sStates = sStates + ", ";
178                 sStates = sStates + NameProvider.getStateName(aStates[i]);
179             }
180             g.setTransform (aTransform);
181             g.translate (10,aWidgetArea.y+aWidgetArea.height-3);
182             g.scale (0.9,0.9);
183             g.drawString (sStates,0,0);
184         }
185     }
186 
187     static private Color
188         saInvalidColor = new Color (255,0,255),
189         saFocusColor = new Color (100,100,255),
190         saSelectionColor = Color.GREEN,
191         saDefaultColor = new Color (90,90,90),
192         saEditColor = new Color (240,240,0);
193 }
194 
195 
196