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