1*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 2*cdf0e10cSrcweir import com.sun.star.accessibility.*; 3*cdf0e10cSrcweir import java.util.Vector; 4*cdf0e10cSrcweir 5*cdf0e10cSrcweir /** 6*cdf0e10cSrcweir * The node type for the AccessibleTreeModel. 7*cdf0e10cSrcweir * This implements all the child-handling based on the appropriate 8*cdf0e10cSrcweir * NodeHandlers. Trivial nodes can be implemented by any Object 9*cdf0e10cSrcweir * type. 10*cdf0e10cSrcweir */ 11*cdf0e10cSrcweir class AccTreeNode 12*cdf0e10cSrcweir extends AccessibleTreeNode 13*cdf0e10cSrcweir { 14*cdf0e10cSrcweir class HandlerDescriptor 15*cdf0e10cSrcweir { 16*cdf0e10cSrcweir public HandlerDescriptor (NodeHandler aHandler) 17*cdf0e10cSrcweir { 18*cdf0e10cSrcweir maHandler = aHandler; 19*cdf0e10cSrcweir mnChildCount = -1; 20*cdf0e10cSrcweir } 21*cdf0e10cSrcweir public NodeHandler maHandler; 22*cdf0e10cSrcweir public int mnChildCount; 23*cdf0e10cSrcweir } 24*cdf0e10cSrcweir /// NodeHandlers for this node 25*cdf0e10cSrcweir private Vector maHandlers; 26*cdf0e10cSrcweir 27*cdf0e10cSrcweir // The accessible context of this node. 28*cdf0e10cSrcweir private XAccessible mxAccessible; 29*cdf0e10cSrcweir private XAccessibleContext mxContext; 30*cdf0e10cSrcweir private XAccessibleComponent mxComponent; 31*cdf0e10cSrcweir private XAccessibleText mxText; 32*cdf0e10cSrcweir private XAccessibleTable mxTable; 33*cdf0e10cSrcweir 34*cdf0e10cSrcweir public AccTreeNode (XAccessible xAccessible, XAccessibleContext xContext, AccessibleTreeNode aParent) 35*cdf0e10cSrcweir { 36*cdf0e10cSrcweir this (xAccessible, xContext, xContext, aParent); 37*cdf0e10cSrcweir } 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir public AccTreeNode (XAccessible xAccessible, XAccessibleContext xContext, Object aDisplay, AccessibleTreeNode aParent) 40*cdf0e10cSrcweir { 41*cdf0e10cSrcweir super (aDisplay, aParent); 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir maHandlers = new Vector(5); 44*cdf0e10cSrcweir mxContext = xContext; 45*cdf0e10cSrcweir mxAccessible = xAccessible; 46*cdf0e10cSrcweir } 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir /** Update the internal data extracted from the corresponding accessible 49*cdf0e10cSrcweir object. This is done by replacing every handler by a new one. An 50*cdf0e10cSrcweir update method at each handler would be better of course. 51*cdf0e10cSrcweir */ 52*cdf0e10cSrcweir public void update () 53*cdf0e10cSrcweir { 54*cdf0e10cSrcweir for (int i=0; i<maHandlers.size(); i++) 55*cdf0e10cSrcweir { 56*cdf0e10cSrcweir System.out.println ("replacing handler " + i); 57*cdf0e10cSrcweir HandlerDescriptor aDescriptor = (HandlerDescriptor)maHandlers.get(i); 58*cdf0e10cSrcweir aDescriptor.maHandler = aDescriptor.maHandler.createHandler (mxContext); 59*cdf0e10cSrcweir aDescriptor.mnChildCount = 60*cdf0e10cSrcweir aDescriptor.maHandler.getChildCount (this); 61*cdf0e10cSrcweir } 62*cdf0e10cSrcweir } 63*cdf0e10cSrcweir 64*cdf0e10cSrcweir public XAccessibleContext getContext () 65*cdf0e10cSrcweir { 66*cdf0e10cSrcweir return mxContext; 67*cdf0e10cSrcweir } 68*cdf0e10cSrcweir 69*cdf0e10cSrcweir public XAccessibleComponent getComponent () 70*cdf0e10cSrcweir { 71*cdf0e10cSrcweir if (mxComponent == null && mxContext != null) 72*cdf0e10cSrcweir mxComponent = (XAccessibleComponent)UnoRuntime.queryInterface( 73*cdf0e10cSrcweir XAccessibleComponent.class, mxContext); 74*cdf0e10cSrcweir return mxComponent; 75*cdf0e10cSrcweir } 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir public XAccessibleExtendedComponent getExtendedComponent () 78*cdf0e10cSrcweir { 79*cdf0e10cSrcweir if (mxComponent == null) 80*cdf0e10cSrcweir getComponent(); 81*cdf0e10cSrcweir if (mxComponent != null) 82*cdf0e10cSrcweir return (XAccessibleExtendedComponent)UnoRuntime.queryInterface( 83*cdf0e10cSrcweir XAccessibleExtendedComponent.class, mxComponent); 84*cdf0e10cSrcweir else 85*cdf0e10cSrcweir return null; 86*cdf0e10cSrcweir } 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir public XAccessibleText getText () 89*cdf0e10cSrcweir { 90*cdf0e10cSrcweir if (mxText == null && mxContext != null) 91*cdf0e10cSrcweir mxText = (XAccessibleText)UnoRuntime.queryInterface( 92*cdf0e10cSrcweir XAccessibleText.class, mxContext); 93*cdf0e10cSrcweir return mxText; 94*cdf0e10cSrcweir } 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir public XAccessibleEditableText getEditText () 97*cdf0e10cSrcweir { 98*cdf0e10cSrcweir return (XAccessibleEditableText)UnoRuntime.queryInterface( 99*cdf0e10cSrcweir XAccessibleEditableText.class, mxContext); 100*cdf0e10cSrcweir } 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir public XAccessibleTable getTable () 103*cdf0e10cSrcweir { 104*cdf0e10cSrcweir if (mxTable == null && mxContext != null) 105*cdf0e10cSrcweir mxTable = (XAccessibleTable)UnoRuntime.queryInterface( 106*cdf0e10cSrcweir XAccessibleTable.class, mxContext); 107*cdf0e10cSrcweir return mxTable; 108*cdf0e10cSrcweir } 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir 111*cdf0e10cSrcweir public XAccessible getAccessible() 112*cdf0e10cSrcweir { 113*cdf0e10cSrcweir if ((mxAccessible == null) && (mxContext != null)) 114*cdf0e10cSrcweir mxAccessible = (XAccessible)UnoRuntime.queryInterface( 115*cdf0e10cSrcweir XAccessible.class, mxContext); 116*cdf0e10cSrcweir return mxAccessible; 117*cdf0e10cSrcweir } 118*cdf0e10cSrcweir 119*cdf0e10cSrcweir public XAccessibleSelection getSelection () 120*cdf0e10cSrcweir { 121*cdf0e10cSrcweir return (XAccessibleSelection)UnoRuntime.queryInterface( 122*cdf0e10cSrcweir XAccessibleSelection.class, mxContext); 123*cdf0e10cSrcweir } 124*cdf0e10cSrcweir 125*cdf0e10cSrcweir public void addHandler( NodeHandler aHandler ) 126*cdf0e10cSrcweir { 127*cdf0e10cSrcweir if (aHandler != null) 128*cdf0e10cSrcweir maHandlers.add (new HandlerDescriptor (aHandler)); 129*cdf0e10cSrcweir } 130*cdf0e10cSrcweir 131*cdf0e10cSrcweir 132*cdf0e10cSrcweir /** iterate over handlers and return child sum */ 133*cdf0e10cSrcweir protected HandlerDescriptor getHandlerDescriptor (int i) 134*cdf0e10cSrcweir { 135*cdf0e10cSrcweir HandlerDescriptor aDescriptor = (HandlerDescriptor)maHandlers.get(i); 136*cdf0e10cSrcweir if (aDescriptor.mnChildCount < 0) 137*cdf0e10cSrcweir aDescriptor.mnChildCount = 138*cdf0e10cSrcweir aDescriptor.maHandler.getChildCount (this); 139*cdf0e10cSrcweir return aDescriptor; 140*cdf0e10cSrcweir } 141*cdf0e10cSrcweir 142*cdf0e10cSrcweir public int getChildCount() 143*cdf0e10cSrcweir { 144*cdf0e10cSrcweir int nChildCount = 0; 145*cdf0e10cSrcweir for (int i = 0; i < maHandlers.size(); i++) 146*cdf0e10cSrcweir { 147*cdf0e10cSrcweir HandlerDescriptor aDescriptor = getHandlerDescriptor (i); 148*cdf0e10cSrcweir nChildCount += aDescriptor.mnChildCount; 149*cdf0e10cSrcweir } 150*cdf0e10cSrcweir return nChildCount; 151*cdf0e10cSrcweir } 152*cdf0e10cSrcweir 153*cdf0e10cSrcweir /** iterate over handlers until the child is found */ 154*cdf0e10cSrcweir public AccessibleTreeNode getChild (int nIndex) 155*cdf0e10cSrcweir throws IndexOutOfBoundsException 156*cdf0e10cSrcweir { 157*cdf0e10cSrcweir if( nIndex >= 0 ) 158*cdf0e10cSrcweir { 159*cdf0e10cSrcweir for(int i = 0; i < maHandlers.size(); i++) 160*cdf0e10cSrcweir { 161*cdf0e10cSrcweir // check if this handler has the child, and if not 162*cdf0e10cSrcweir // search with next handler 163*cdf0e10cSrcweir HandlerDescriptor aDescriptor = getHandlerDescriptor (i); 164*cdf0e10cSrcweir if (nIndex < aDescriptor.mnChildCount) 165*cdf0e10cSrcweir return aDescriptor.maHandler.getChild (this, nIndex); 166*cdf0e10cSrcweir else 167*cdf0e10cSrcweir nIndex -= aDescriptor.mnChildCount; 168*cdf0e10cSrcweir } 169*cdf0e10cSrcweir } 170*cdf0e10cSrcweir else 171*cdf0e10cSrcweir throw new IndexOutOfBoundsException(); 172*cdf0e10cSrcweir 173*cdf0e10cSrcweir // nothing found? 174*cdf0e10cSrcweir return null; 175*cdf0e10cSrcweir } 176*cdf0e10cSrcweir 177*cdf0e10cSrcweir public AccessibleTreeNode getChildNoCreate (int nIndex) 178*cdf0e10cSrcweir throws IndexOutOfBoundsException 179*cdf0e10cSrcweir { 180*cdf0e10cSrcweir if( nIndex >= 0 ) 181*cdf0e10cSrcweir { 182*cdf0e10cSrcweir for(int i = 0; i < maHandlers.size(); i++) 183*cdf0e10cSrcweir { 184*cdf0e10cSrcweir // check if this handler has the child, and if not 185*cdf0e10cSrcweir // search with next handler 186*cdf0e10cSrcweir HandlerDescriptor aDescriptor = getHandlerDescriptor (i); 187*cdf0e10cSrcweir if (nIndex < aDescriptor.mnChildCount) 188*cdf0e10cSrcweir return aDescriptor.maHandler.getChildNoCreate (this, nIndex); 189*cdf0e10cSrcweir else 190*cdf0e10cSrcweir nIndex -= aDescriptor.mnChildCount; 191*cdf0e10cSrcweir } 192*cdf0e10cSrcweir } 193*cdf0e10cSrcweir else 194*cdf0e10cSrcweir throw new IndexOutOfBoundsException(); 195*cdf0e10cSrcweir 196*cdf0e10cSrcweir // nothing found? 197*cdf0e10cSrcweir return null; 198*cdf0e10cSrcweir } 199*cdf0e10cSrcweir 200*cdf0e10cSrcweir public boolean removeChild (int nIndex) 201*cdf0e10cSrcweir throws IndexOutOfBoundsException 202*cdf0e10cSrcweir { 203*cdf0e10cSrcweir boolean bStatus = false; 204*cdf0e10cSrcweir if (nIndex >= 0) 205*cdf0e10cSrcweir { 206*cdf0e10cSrcweir for (int i=0; i<maHandlers.size(); i++) 207*cdf0e10cSrcweir { 208*cdf0e10cSrcweir // check if this handler has the child, and if not 209*cdf0e10cSrcweir // search with next handler 210*cdf0e10cSrcweir HandlerDescriptor aDescriptor = getHandlerDescriptor (i); 211*cdf0e10cSrcweir if (nIndex < aDescriptor.mnChildCount) 212*cdf0e10cSrcweir { 213*cdf0e10cSrcweir bStatus = aDescriptor.maHandler.removeChild (this, nIndex); 214*cdf0e10cSrcweir aDescriptor.mnChildCount = aDescriptor.maHandler.getChildCount (this); 215*cdf0e10cSrcweir break; 216*cdf0e10cSrcweir } 217*cdf0e10cSrcweir else 218*cdf0e10cSrcweir nIndex -= aDescriptor.mnChildCount; 219*cdf0e10cSrcweir } 220*cdf0e10cSrcweir } 221*cdf0e10cSrcweir else 222*cdf0e10cSrcweir throw new IndexOutOfBoundsException(); 223*cdf0e10cSrcweir 224*cdf0e10cSrcweir return bStatus; 225*cdf0e10cSrcweir } 226*cdf0e10cSrcweir 227*cdf0e10cSrcweir 228*cdf0e10cSrcweir public int indexOf (AccessibleTreeNode aNode) 229*cdf0e10cSrcweir { 230*cdf0e10cSrcweir int nBaseIndex = 0; 231*cdf0e10cSrcweir if (aNode != null) 232*cdf0e10cSrcweir { 233*cdf0e10cSrcweir for (int i=0; i<maHandlers.size(); i++) 234*cdf0e10cSrcweir { 235*cdf0e10cSrcweir HandlerDescriptor aDescriptor = getHandlerDescriptor (i); 236*cdf0e10cSrcweir int nIndex = aDescriptor.maHandler.indexOf (aNode); 237*cdf0e10cSrcweir if (nIndex >= 0) 238*cdf0e10cSrcweir return nBaseIndex + nIndex; 239*cdf0e10cSrcweir else 240*cdf0e10cSrcweir nBaseIndex += aDescriptor.mnChildCount; 241*cdf0e10cSrcweir } 242*cdf0e10cSrcweir } 243*cdf0e10cSrcweir 244*cdf0e10cSrcweir return -1; 245*cdf0e10cSrcweir } 246*cdf0e10cSrcweir 247*cdf0e10cSrcweir /** this node is a leaf if have no handlers, or is those 248*cdf0e10cSrcweir handlers show no children */ 249*cdf0e10cSrcweir public boolean isLeaf() 250*cdf0e10cSrcweir { 251*cdf0e10cSrcweir return (maHandlers.size() == 0);// || (getChildCount() == 0); 252*cdf0e10cSrcweir } 253*cdf0e10cSrcweir 254*cdf0e10cSrcweir public boolean equals (Object aOther) 255*cdf0e10cSrcweir { 256*cdf0e10cSrcweir return (this == aOther) || (aOther!=null && aOther.equals(mxContext)); 257*cdf0e10cSrcweir } 258*cdf0e10cSrcweir 259*cdf0e10cSrcweir 260*cdf0e10cSrcweir /** iterate over handlers until the child is found */ 261*cdf0e10cSrcweir public void getActions(Vector aActions) 262*cdf0e10cSrcweir { 263*cdf0e10cSrcweir for(int i = 0; i < maHandlers.size(); i++) 264*cdf0e10cSrcweir { 265*cdf0e10cSrcweir HandlerDescriptor aDescriptor = getHandlerDescriptor (i); 266*cdf0e10cSrcweir NodeHandler aHandler = aDescriptor.maHandler; 267*cdf0e10cSrcweir String[] aHandlerActions = aHandler.getActions (this); 268*cdf0e10cSrcweir for(int j = 0; j < aHandlerActions.length; j++ ) 269*cdf0e10cSrcweir { 270*cdf0e10cSrcweir aActions.add( aHandlerActions[j] ); 271*cdf0e10cSrcweir } 272*cdf0e10cSrcweir } 273*cdf0e10cSrcweir } 274*cdf0e10cSrcweir 275*cdf0e10cSrcweir public void performAction( int nIndex ) 276*cdf0e10cSrcweir { 277*cdf0e10cSrcweir if( nIndex >= 0 ) 278*cdf0e10cSrcweir { 279*cdf0e10cSrcweir for(int i = 0; i < maHandlers.size(); i++) 280*cdf0e10cSrcweir { 281*cdf0e10cSrcweir // check if this handler has the child, and if not 282*cdf0e10cSrcweir // search with next handler 283*cdf0e10cSrcweir HandlerDescriptor aDescriptor = getHandlerDescriptor (i); 284*cdf0e10cSrcweir NodeHandler aHandler = aDescriptor.maHandler; 285*cdf0e10cSrcweir int nCount = aHandler.getActions(this).length; 286*cdf0e10cSrcweir if( nCount > nIndex ) 287*cdf0e10cSrcweir { 288*cdf0e10cSrcweir aHandler.performAction(this, nIndex ); 289*cdf0e10cSrcweir return; 290*cdf0e10cSrcweir } 291*cdf0e10cSrcweir else 292*cdf0e10cSrcweir nIndex -= nCount; 293*cdf0e10cSrcweir } 294*cdf0e10cSrcweir } 295*cdf0e10cSrcweir } 296*cdf0e10cSrcweir 297*cdf0e10cSrcweir /** Try to add the specified accessible object as new accessible child of the 298*cdf0e10cSrcweir AccessibleTreeHandler. 299*cdf0e10cSrcweir Note that child is used in another context than 300*cdf0e10cSrcweir it is used in the other methods of this class. 301*cdf0e10cSrcweir */ 302*cdf0e10cSrcweir public AccessibleTreeNode addAccessibleChild (XAccessible xChild) 303*cdf0e10cSrcweir { 304*cdf0e10cSrcweir for(int i = 0; i < maHandlers.size(); i++) 305*cdf0e10cSrcweir { 306*cdf0e10cSrcweir HandlerDescriptor aDescriptor = getHandlerDescriptor (i); 307*cdf0e10cSrcweir if (aDescriptor.maHandler instanceof AccessibleTreeHandler) 308*cdf0e10cSrcweir { 309*cdf0e10cSrcweir AccessibleTreeHandler aHandler = (AccessibleTreeHandler)aDescriptor.maHandler; 310*cdf0e10cSrcweir AccessibleTreeNode aNode = aHandler.addAccessibleChild (this, xChild); 311*cdf0e10cSrcweir aDescriptor.mnChildCount = aHandler.getChildCount (this); 312*cdf0e10cSrcweir return aNode; 313*cdf0e10cSrcweir } 314*cdf0e10cSrcweir } 315*cdf0e10cSrcweir return null; 316*cdf0e10cSrcweir } 317*cdf0e10cSrcweir 318*cdf0e10cSrcweir /** Update the specified handlers. 319*cdf0e10cSrcweir @return 320*cdf0e10cSrcweir The returned array containes the indices of the updated children 321*cdf0e10cSrcweir and can be used to create a TreeModelEvent. 322*cdf0e10cSrcweir */ 323*cdf0e10cSrcweir public Vector updateChildren (java.lang.Class class1) 324*cdf0e10cSrcweir { 325*cdf0e10cSrcweir return updateChildren (class1, null); 326*cdf0e10cSrcweir } 327*cdf0e10cSrcweir 328*cdf0e10cSrcweir public Vector updateChildren (java.lang.Class class1, java.lang.Class class2) 329*cdf0e10cSrcweir { 330*cdf0e10cSrcweir Vector aChildIndices = new Vector(); 331*cdf0e10cSrcweir int nOffset = 0; 332*cdf0e10cSrcweir for(int i=0; i < maHandlers.size(); i++) 333*cdf0e10cSrcweir { 334*cdf0e10cSrcweir HandlerDescriptor aDescriptor = getHandlerDescriptor (i); 335*cdf0e10cSrcweir if ((class1.isInstance(aDescriptor.maHandler)) 336*cdf0e10cSrcweir || (class2 !=null && class2.isInstance(aDescriptor.maHandler))) 337*cdf0e10cSrcweir { 338*cdf0e10cSrcweir aDescriptor.maHandler.update(this); 339*cdf0e10cSrcweir // Get updated number of children. 340*cdf0e10cSrcweir int nChildCount = aDescriptor.maHandler.getChildCount (this); 341*cdf0e10cSrcweir aDescriptor.mnChildCount = nChildCount; 342*cdf0e10cSrcweir // Fill in the indices of the updated children. 343*cdf0e10cSrcweir for (int j=0; j<nChildCount; j++) 344*cdf0e10cSrcweir aChildIndices.add(new Integer(j+nOffset)); 345*cdf0e10cSrcweir } 346*cdf0e10cSrcweir nOffset += aDescriptor.mnChildCount; 347*cdf0e10cSrcweir } 348*cdf0e10cSrcweir return aChildIndices; 349*cdf0e10cSrcweir } 350*cdf0e10cSrcweir } 351