diff options
author | Andre Fischer <af@openoffice.org> | 2002-02-15 12:53:43 +0000 |
---|---|---|
committer | Andre Fischer <af@openoffice.org> | 2002-02-15 12:53:43 +0000 |
commit | 5a58fb16159c07b7d3a074da35609f94d4dbbe39 (patch) | |
tree | 56f8b162483d6de76c292cfd36ff2b36fdf9337a /toolkit/test | |
parent | f80849595b2bbea53cc35e0018c00a6b242baeec (diff) |
Initial revision of Uno Accessibility API test tool.
Diffstat (limited to 'toolkit/test')
-rwxr-xr-x | toolkit/test/accessibility/AccessibilityTree.java | 414 | ||||
-rwxr-xr-x | toolkit/test/accessibility/AccessibilityWorkBench.java | 605 | ||||
-rwxr-xr-x | toolkit/test/accessibility/Canvas.java | 181 | ||||
-rwxr-xr-x | toolkit/test/accessibility/InformationWriter.java | 420 | ||||
-rwxr-xr-x | toolkit/test/accessibility/Print.java | 5 | ||||
-rwxr-xr-x | toolkit/test/accessibility/SimpleOffice.java | 352 |
6 files changed, 1977 insertions, 0 deletions
diff --git a/toolkit/test/accessibility/AccessibilityTree.java b/toolkit/test/accessibility/AccessibilityTree.java new file mode 100755 index 000000000000..a0610dddb69b --- /dev/null +++ b/toolkit/test/accessibility/AccessibilityTree.java @@ -0,0 +1,414 @@ +import AccessibleObject; +import MessageInterface; + +import drafts.com.sun.star.accessibility.XAccessible; +import drafts.com.sun.star.accessibility.XAccessibleContext; +import drafts.com.sun.star.accessibility.XAccessibleComponent; +import drafts.com.sun.star.accessibility.XAccessibleExtendedComponent; +import drafts.com.sun.star.accessibility.XAccessibleAction; +import drafts.com.sun.star.accessibility.XAccessibleImage; +import drafts.com.sun.star.accessibility.XAccessibleRelationSet; +import drafts.com.sun.star.accessibility.XAccessibleStateSet; + +import com.sun.star.lang.XServiceInfo; +import com.sun.star.uno.UnoRuntime; + +import java.util.Vector; +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; +import javax.swing.tree.*; +import javax.swing.event.*; + + + +/** This class is a start to collect the handling of a JTree and a DefaultTreeModel. +*/ +public class AccessibilityTree extends JTree +{ + public static void main (String args[]) + { + new AccessibilityWorkBench (); + } + + + + + /** Local class used to store the accessible object with every tree node. + */ + protected class TreeNode extends DefaultMutableTreeNode + { + public AccessibleObject aAccessibleObject; + public TreeNode (String sName) + { + super (sName); + aAccessibleObject = null; + } + } + + /** Create a new accessibility tree. Use the specified message display + for displaying messages and the specified canvas to draw the + graphical representations of accessible objects on. + */ + public AccessibilityTree ( + MessageInterface aMessageDisplay, + Canvas aCanvas) + { + maMessageDisplay = aMessageDisplay; + maCanvas = aCanvas; + + maRoot = new DefaultMutableTreeNode ("Accessibility Tree"); + maTreeModel = new DefaultTreeModel (maRoot); + setModel (maTreeModel); + + setEditable (true); + + MouseListener ml = new MouseAdapter() { + public void mousePressed(MouseEvent e) { + int selRow = getRowForLocation(e.getX(), e.getY()); + TreePath selPath = getPathForLocation(e.getX(), e.getY()); + if(selRow != -1) { + if (e.getClickCount() == 2) + { + System.out.println ("double click : " + selRow + " , " + selPath); + Object aObject = selPath.getLastPathComponent(); + if (TreeNode.class.isInstance (aObject)) + { + TreeNode aNode = (TreeNode)aObject; + createTree (aNode.aAccessibleObject.getAccessible(), + selPath); + expandShapes (); + } + } + } + } + }; + addMouseListener (ml); + } + + + + + public void createTree (XAccessible xRoot, TreePath aPath) + { + System.out.println ("creating accessibility tree for root " + xRoot + " under path " + + aPath); + TreeNode aTree = createAccessibilityTree ( + xRoot, + 0, + new java.awt.Point(0,0), + aPath); + DefaultMutableTreeNode aRoot = (DefaultMutableTreeNode)aPath.getLastPathComponent(); + aRoot.insert (aTree, aRoot.getChildCount()); + maTreeModel.reload (); + } + + + + public void createTree (XAccessible xRoot) + { + createTree (xRoot, new TreePath (maRoot)); + } + + + + + public DefaultMutableTreeNode getRoot () + { + return maRoot; + } + + + + + protected TreeNode createAccessibilityTree ( + XAccessible xRoot, int depth, java.awt.Point aOrigin, TreePath aPath) + { + TreeNode aRoot = null; + + try + { + aRoot = newTreeNode (xRoot, depth); + TreePath aNewPath = aPath.pathByAddingChild (aRoot); + + if (depth >= 0) + { + AccessibleObject aObject = new AccessibleObject (xRoot, aNewPath); + aRoot.aAccessibleObject = aObject; + message ("creating accessibility tree at depth " + depth + ": " + + aObject.toString()); + + if (maCanvas != null) + { + if (aObject.getOrigin().x != 0 || aObject.getOrigin().y != 0) + { + maCanvas.addAccessible (aObject); + System.out.println ("adding object " + aObject.toString() + " to canvas"); + } + } + else + System.out.println ("no canvas"); + + aOrigin = aObject.getOrigin (); + } + + // Iterate over children and show them. + XAccessibleContext xContext = xRoot.getAccessibleContext(); + if (xContext != null) + { + int n = xContext.getAccessibleChildCount(); + for (int i=0; i<n; i++) + { + DefaultMutableTreeNode aNode = + createAccessibilityTree (xContext.getAccessibleChild(i), + depth+1, aOrigin, aNewPath); + aRoot.insert (aNode, aRoot.getChildCount()); + } + } + else + { + // println ("object is not accessible"); + } + } + catch (Exception e) + { + System.out.println ("caught exception while creating accessibility tree: " + e); + } + + return aRoot; + } + + + + + /** Create a new node for the JTree which represents the specified accessible object. + The node displays selected attributes of the accessible object. + */ + protected TreeNode newTreeNode (XAccessible xAccessible, int depth) + { + TreeNode aNode = null; + try + { + if (xAccessible == null) + { + aNode = new TreeNode ("not accessible"); + } + else + { + XAccessibleContext xContext = xAccessible.getAccessibleContext(); + + if (xContext == null) + { + aNode = new TreeNode ("no context"); + } + else + { + aNode = new TreeNode ( + xContext.getAccessibleName()); + aNode.add (new DefaultMutableTreeNode ( + "Description: "+ xContext.getAccessibleDescription())); + aNode.add (new DefaultMutableTreeNode ( + "Role: "+ xContext.getAccessibleRole())); + aNode.add (new DefaultMutableTreeNode ( + "Has parent: "+ (xContext.getAccessibleParent()!=null? + "yes" : "no"))); + aNode.add (new DefaultMutableTreeNode ( + "Child count: "+ xContext.getAccessibleChildCount())); + + XAccessibleComponent xComponent = + (XAccessibleComponent) UnoRuntime.queryInterface ( + XAccessibleComponent.class, xContext); + if (xComponent != null) + { + aNode.add (new DefaultMutableTreeNode ( + "Location: "+ xComponent.getLocation().X + ", " + + xComponent.getLocation().Y)); + aNode.add (new DefaultMutableTreeNode ( + "Location on Screen: "+ xComponent.getLocationOnScreen().X + + ", "+ xComponent.getLocationOnScreen().Y)); + aNode.add (new DefaultMutableTreeNode ( + "Size: "+ xComponent.getSize().Width + ", " + + xComponent.getSize().Height)); + + } + else + aNode.add (new DefaultMutableTreeNode ( + "XAccessibleComponent not supported")); + + XAccessibleExtendedComponent xEComponent = + (XAccessibleExtendedComponent) UnoRuntime.queryInterface ( + XAccessibleExtendedComponent.class, xContext); + if (xEComponent != null) + { + int nColor = xEComponent.getForeground(); + aNode.add (new DefaultMutableTreeNode ( + "Foreground color: R" + + (nColor>>16&0xff) + +"G"+ (nColor>>8&0xff) + +"B"+ (nColor>>0&0xff))); + nColor = xEComponent.getBackground(); + aNode.add (new DefaultMutableTreeNode ( + "Background color: R" + + (nColor>>16&0xff) + +"G"+ (nColor>>8&0xff) + +"B"+ (nColor>>0&0xff))); + } + else + aNode.add (new DefaultMutableTreeNode ( + "XAccessibleExtendedComponent not supported")); + + XAccessibleAction xAction = + (XAccessibleAction) UnoRuntime.queryInterface ( + XAccessibleAction.class, xContext); + if (xAction != null) + { + int nActions = xAction.getAccessibleActionCount(); + aNode.add ( + new DefaultMutableTreeNode ( + "Actions: " + nActions)); + for (int i=0; i<nActions; i++) + aNode.add ( + new DefaultMutableTreeNode ( + "Action " + i + " : " + xAction.getAccessibleActionDescription(i))); + } + + XAccessibleImage xImage = + (XAccessibleImage) UnoRuntime.queryInterface ( + XAccessibleImage.class, xContext); + if (xImage != null) + { + aNode.add ( + new DefaultMutableTreeNode ( + "Image description: " + xImage.getAccessibleImageDescription())); + } + } + } + + // Use interface XServiceInfo to retrieve information about + // supported services. + XServiceInfo xSI = (XServiceInfo) UnoRuntime.queryInterface ( + XServiceInfo.class, xAccessible); + if (xSI == null) + aNode.add (new DefaultMutableTreeNode ( + "XServiceInfo not supported")); + else + aNode.add (new DefaultMutableTreeNode ( + "Service name: " + xSI.getImplementationName ())); + } + catch (Exception e) + { + System.out.println ("caught exception in newTreeNode: " + e); + } + + return aNode; + } + + + + + /** Expand all nodes and their subtrees that represent shapes. Call + this method from the outside. + */ + public void expandShapes () + { + mbFirstShapeSeen = false; + expandShapes (maRoot); + + addTreeSelectionListener( new TreeSelectionListener() { + public void valueChanged(TreeSelectionEvent e) { + System.out.println ("TreeSelectionEvent = " + e); + } + } + ); + + } + + + + /** Expand all nodes and their subtrees that represent shapes. + */ + protected void expandShapes (Object aRoot) + { + message ("Expanding shapes."); + + try + { + int nChildCount = maTreeModel.getChildCount(aRoot); + // Ignore leafs. + if (nChildCount > 0) + { + for (int i=0; i<nChildCount; i++) + { + Object aChild = maTreeModel.getChild (aRoot, i); + if (TreeNode.class.isInstance (aChild)) + { + TreeNode aNode = (TreeNode)aChild; + AccessibleObject aAccessibleObject = (AccessibleObject)aNode.aAccessibleObject; + if (aAccessibleObject != null) + // Expand every node that has one of the new OO roles. + if (aAccessibleObject.getRole() >= 100) + { + TreePath aPath = new TreePath (aNode.getPath()); + expandPath (aPath); + if ( ! mbFirstShapeSeen) + { + mbFirstShapeSeen = true; + makeVisible (aPath); + } + } + } + // Descent into tree strucuture. + expandShapes (aChild); + } + } + } + catch (Exception e) + { + System.out.println ("caught exception while expanding shape nodes: " + e); + } + } + + + + + public void addDocument (XAccessible xAccessible, String sURL) + { + if (maTreeModel != null && maRoot != null) + { + System.out.println ("Trying to add node at position " + maRoot.getChildCount() + + " into root node"); + if (sURL.length() == 0) + sURL = "<unnamed>"; + TreeNode aNode = new TreeNode ("Document: " + sURL); + aNode.aAccessibleObject = new AccessibleObject (xAccessible, + new TreePath (maRoot).pathByAddingChild(aNode)); + maRoot.insert (aNode, maRoot.getChildCount()); + } + maTreeModel.reload (); + } + + + public void clear () + { + maRoot.removeAllChildren(); + } + + + protected void message (String message) + { + maMessageDisplay.message (message); + } + + + protected MessageInterface + maMessageDisplay; + + private DefaultMutableTreeNode + maRoot; + private DefaultTreeModel + maTreeModel; + private Canvas + maCanvas; + private boolean + mbFirstShapeSeen; +} diff --git a/toolkit/test/accessibility/AccessibilityWorkBench.java b/toolkit/test/accessibility/AccessibilityWorkBench.java new file mode 100755 index 000000000000..80240afb025b --- /dev/null +++ b/toolkit/test/accessibility/AccessibilityWorkBench.java @@ -0,0 +1,605 @@ +import SimpleOffice; +import InformationWriter; +import Print; +import Canvas; +import AccessibilityTree; + +import com.sun.star.awt.XWindow; +import com.sun.star.beans.XPropertySet; +import com.sun.star.beans.XPropertyChangeListener; +import com.sun.star.beans.PropertyChangeEvent; +import com.sun.star.container.XEnumerationAccess; +import com.sun.star.container.XEnumeration; +import com.sun.star.document.XEventListener; +import com.sun.star.drawing.XDrawPage; +import com.sun.star.drawing.XDrawView; +import com.sun.star.frame.XController; +import com.sun.star.frame.XFrame; +import com.sun.star.frame.XFrameActionListener; +import com.sun.star.frame.FrameActionEvent; +import com.sun.star.frame.FrameAction; +import com.sun.star.lang.XComponent; +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.lang.XServiceInfo; +import com.sun.star.frame.XDesktop; +import com.sun.star.frame.XModel; +import com.sun.star.uno.UnoRuntime; + +import drafts.com.sun.star.accessibility.XAccessible; +import drafts.com.sun.star.accessibility.XAccessibleContext; +import drafts.com.sun.star.accessibility.XAccessibleComponent; +import drafts.com.sun.star.accessibility.XAccessibleExtendedComponent; +import drafts.com.sun.star.accessibility.XAccessibleRelationSet; +import drafts.com.sun.star.accessibility.XAccessibleStateSet; + + +import java.util.Vector; +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; +import javax.swing.tree.*; + + +public class AccessibilityWorkBench + extends JFrame + implements ActionListener, + Print, + MessageInterface, + XEventListener, + XFrameActionListener, + XPropertyChangeListener +{ + public String msFileName; + /*WinFilename + // = "file:///d|/tmp/impress-test-document.sxi"; + = "file:///d|/tmp/writer-test-document.sxw"; + // = "file:///d|/tmp/calc-test-document.sxc"; + final public String sUnxFilename + = "file:///tmp/impress-test-document.sxi"; + //= "file:///tmp/draw-test-document.sxd"; + */ + + public static void main (String args[]) + { + int nPortNumber = 5678; + String sFileName = "file:///tmp/impress-test-document.sxi"; + + for (int i=0; i<args.length; i++) + { + if (args[i].equals ("-h") || args[i].equals ("--help") || args[i].equals ("-?")) + { + System.out.println ("usage: AccessibilityWorkBench <option>*"); + System.out.println ("options:"); + System.out.println (" -p <port-number> Port on which to connect to StarOffice."); + System.out.println (" Defaults to 5678."); + System.out.println (" -f <file-name-URL> URL of document file which is loaded when"); + System.out.println (" clicking on the Load button. Don't forget"); + System.out.println (" the file:// prefix!"); + System.exit (0); + } + else if (args[i].equals ("-p")) + { + nPortNumber = Integer.parseInt (args[++i]); + } + else if (args[i].equals ("-f")) + { + sFileName = args[++i]; + } + } + + new AccessibilityWorkBench (nPortNumber, sFileName); + } + + + + + public AccessibilityWorkBench (int nPortNumber, String sFileName) + { + msFileName = sFileName; + + Layout (); + + println (System.getProperty ("os.name") + " / " + + System.getProperty ("os.arch") + " / " + + System.getProperty ("os.version")); + println ("Using port " + nPortNumber + " and document file name " + msFileName); + office = new SimpleOffice (this, nPortNumber); + info = new InformationWriter (this); + + addWindowListener (new WindowAdapter () + { public void windowClosing (WindowEvent e) + { System.exit(0); } + }); + + initialize (); + } + + + + + /** Create and arrange the widgets of the GUI. + */ + public void Layout () + { + setSize (new Dimension (1024,768)); + maMainPanel = new JPanel(); + this.getContentPane().add (maMainPanel); + GridBagLayout aLayout = new GridBagLayout (); + JScrollPane aScrollPane; + + // Text output area. + maOutputArea = new JTextArea (5,50); + maScrollPane = new JScrollPane(maOutputArea, + JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, + JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); + GridBagConstraints constraints = new GridBagConstraints (); + constraints.gridx = 1; + constraints.gridy = 1; + constraints.gridwidth = 1; + constraints.gridheight = 1; + constraints.weightx = 3; + constraints.weighty = 1; + constraints.fill = GridBagConstraints.BOTH; + aLayout.addLayoutComponent (maScrollPane, constraints); + maMainPanel.add (maScrollPane); + + // Message output area. + maMessageArea = new JTextArea (5,20); + constraints = new GridBagConstraints (); + constraints.gridx = 0; + constraints.gridy = 2; + constraints.gridwidth = 2; + constraints.gridheight = 1; + constraints.weightx = 3; + constraints.weighty = 0; + constraints.fill = GridBagConstraints.BOTH; + aLayout.addLayoutComponent (maMessageArea, constraints); + maMainPanel.add (maMessageArea); + + // Canvas. + maCanvas = new Canvas (this, maTree); + maCanvas.setPreferredSize (new Dimension (1050,1050)); + aScrollPane = new JScrollPane(maCanvas, + JScrollPane.VERTICAL_SCROLLBAR_ALWAYS/*AS_NEEDED*/, + JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS/*AS_NEEDED*/); + constraints = new GridBagConstraints (); + constraints.gridx = 1; + constraints.gridy = 0; + constraints.gridwidth = 1; + constraints.gridheight = 1; + constraints.weightx = 3; + constraints.weighty = 3; + constraints.fill = GridBagConstraints.BOTH; + aLayout.addLayoutComponent (aScrollPane, constraints); + maMainPanel.add (aScrollPane); + + // Accessible Tree. + maTree = new AccessibilityTree (this, maCanvas); + aScrollPane = new JScrollPane(maTree, + JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, + JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); + constraints = new GridBagConstraints (); + constraints.gridx = 0; + constraints.gridy = 0; + constraints.gridwidth = 1; + constraints.gridheight = 2; + constraints.weightx = 2; + constraints.weighty = 1; + constraints.fill = GridBagConstraints.BOTH; + aLayout.addLayoutComponent (aScrollPane, constraints); + maMainPanel.add (aScrollPane); + + // Button bar. + maButtonBar = new JPanel(); + GridBagLayout aButtonLayout = new GridBagLayout (); + maButtonBar.setLayout (aLayout); + constraints = new GridBagConstraints (); + constraints.gridx = 0; + constraints.gridy = 3; + constraints.gridwidth = 3; + constraints.weightx = 1; + constraints.anchor = GridBagConstraints.WEST; + constraints.fill = GridBagConstraints.BOTH; + aLayout.setConstraints (maButtonBar, constraints); + maMainPanel.add (maButtonBar); + + // Buttons. + aConnectButton = createButton ("Connect", "connect"); + aLoadButton = createButton ("Load", "load"); + aRunButton = createButton ("Run", "run"); + aUpdateButton = createButton ("Update", "update"); + aQuitButton = createButton ("Quit", "quit"); + + maMainPanel.setLayout (aLayout); + getContentPane().add ("Center", maMainPanel); + maMainPanel.setVisible (true); + setVisible (true); + setTitle("Accessibility Workbench"); + } + + + + /** Create a new button and place at the right most position into the + button bar. + */ + public JButton createButton (String title, String command) + { + JButton aButton = new JButton (title); + aButton.setActionCommand (command); + aButton.addActionListener (this); + GridBagConstraints constraints = new GridBagConstraints (); + constraints.gridx = maButtonBar.getComponentCount(); + constraints.gridy = 0; + GridBagLayout aLayout = (GridBagLayout)maButtonBar.getLayout(); + + aLayout.setConstraints (aButton, constraints); + maButtonBar.add (aButton); + return aButton; + } + + + protected void initialize () + { + // Clear the accessibility tree. + maTree.clear (); + + // Delete the graphical representations. + maCanvas.clear (); + + // Add entries for open documents to tree. + addOpenDocumentsToTree (); + } + + + /** Callback for GUI actions from the buttons. + */ + public void actionPerformed (java.awt.event.ActionEvent e) + { + if (e.getActionCommand().equals("connect")) + { + office.connect(); + initialize (); + } + else if (e.getActionCommand().equals("quit")) + { + System.exit (0); + } + else if (e.getActionCommand().equals("load")) + { + print ("Loading file " + msFileName); + mxModel = office.loadDocument (msFileName); + if (mxModel == null) + println (": could not be loaded"); + else + { + println ("."); + XWindow xWindow = office.getCurrentWindow (mxModel); + XAccessible xRoot = office.getAccessibleRoot (xWindow); + maTree.addDocument (xRoot, msFileName); + } + } + else if (e.getActionCommand().equals("run")) + { + run (mxModel); + } + else if (e.getActionCommand().equals("update")) + { + initialize (); + run (mxModel); + } + else + { + System.err.println("unknown command " + e.getActionCommand()); + } + } + + + + + public void test (XModel xModel) + { + println ("Test:"); + try + { + if (xModel == null) + { + XDrawView xView = office.getCurrentView (); + if (xView == null) + println ("no current view"); + else + xModel = office.getModel (xView); + } + info.showProperties (xModel); + + XWindow xWindow = office.getCurrentWindow (xModel); + if (xWindow != null) + { + println ("current window:"); + com.sun.star.awt.Rectangle aRectangle = xWindow.getPosSize(); + println (aRectangle.X + " " + aRectangle.Y + " " + + aRectangle.Width + " " + aRectangle.Height); + } + else + println ("Can't get window"); + + XPropertySet xSet = (XPropertySet) UnoRuntime.queryInterface ( + XPropertySet.class, xModel); + if (xSet != null) + { + com.sun.star.awt.Rectangle aRectangle = + (com.sun.star.awt.Rectangle) xSet.getPropertyValue ( + "VisibleArea"); + println (aRectangle.X + " " + aRectangle.Y + " " + + aRectangle.Width + " " + aRectangle.Height); + } + else + println ("model does not support XPropertySet"); + } + catch (Exception e) + { + System.out.println ("caught exception in test: " + e); + } + println ("Test finished."); + } + + + + + /** Get the accessibility tree for the specified model. + */ + protected void run (XModel xModel) + { + try + { + addListeners (xModel); + XWindow xWindow = office.getCurrentWindow (xModel); + XAccessible xRoot = office.getAccessibleRoot (xWindow); + if (xRoot != null) + { + println ("window is accessible"); + } + else + { + println ("window is not accessible."); + return; + } + + message ("creating accessibility tree"); + maTree.createTree (xRoot); + maTree.expandShapes (); + } + catch (Exception e) + { + System.out.println ("caught exception in run: " + e); + } + } + + + + + + /** Experimental. Add list of currently open (and named) documents to + the tree widget. + */ + public void addOpenDocumentsToTree () + { + try + { + XDesktop xDesktop = office.getDesktop(); + if (xDesktop == null) + { + println ("can't get desktop to retrieve open documents"); + return; + } + + XEnumerationAccess xEA = xDesktop.getComponents(); + if (xEA == null) + { + println ("Can't get list of components from desktop"); + return; + } + XEnumeration xE = xEA.createEnumeration(); + while (xE.hasMoreElements()) + { + XComponent xComponent = (XComponent) UnoRuntime.queryInterface( + XComponent.class, xE.nextElement()); + XModel xModel = (XModel) UnoRuntime.queryInterface( + XModel.class, xComponent); + if (xModel != null) + { + println (xModel.getURL()); + XWindow xWindow = office.getCurrentWindow (xModel); + XAccessible xRoot = office.getAccessibleRoot (xWindow); + maTree.addDocument (xRoot, xModel.getURL()); + } + else + println ("can't cast component to model"); + } + println ("finished getting named documents"); + } + catch (Exception e) + { + System.out.println ("caught exception while getting document names: " + e); + } + } + + + + + /** Add various listeners to the model and other Office objects. + */ + protected void addListeners (XModel xModel) + { + + com.sun.star.document.XEventBroadcaster xBr = + (com.sun.star.document.XEventBroadcaster)UnoRuntime.queryInterface( + com.sun.star.document.XEventBroadcaster.class, xModel); + if( xBr != null ) + xBr.addEventListener (this); + + XController xController = xModel.getCurrentController(); + if (xController != null) + { + XFrame xFrame = xController.getFrame(); + if (xFrame != null) + { + xFrame.addFrameActionListener( this ); + System.out.println("[DONE]"); + } + connectListener( xController ); + } + } + + + + public boolean connectListener( XController xController ) + { + System.out.println("connecting to controller..."); + + XServiceInfo oObj = (XServiceInfo)UnoRuntime.queryInterface(XServiceInfo.class, xController); + String[] names = oObj.getSupportedServiceNames(); + for (int i=0;i<names.length;i++) + { + System.out.println("Supported Service is "+names[i]); + } + + System.out.print("add property listener "); + + XPropertySet xPropSet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xController); + + try + { + + xPropSet.addPropertyChangeListener ("", this); + + System.out.println("[DONE]"); + + } + catch (Exception exp) + { + System.out.println("[FAILED]"); + System.exit(0); + } + + System.out.print("add dispose listener "); + + XComponent xComponent = (XComponent)UnoRuntime.queryInterface(XComponent.class, xController); + // com.sun.star.lang.XEventListener xEventListener = (com.sun.star.lang.XEventListener)UnoRuntime.queryInterface(com.sun.star.lang.XEventListener.class, this); + + xComponent.addEventListener (this); + + System.out.println("[DONE]"); + + return true; + } + + public void propertyChange( final PropertyChangeEvent evt ) throws RuntimeException + { + System.out.println("property " + evt.PropertyName + "(" + evt.PropertyHandle + ") changed!"); + + if( evt.PropertyHandle == 3 ) + { + System.out.print("old value: "); + com.sun.star.awt.Rectangle aRect; + aRect = (com.sun.star.awt.Rectangle)evt.OldValue; + System.out.println( "old value: (" + aRect.X + "," + aRect.Y + "," + aRect.Width + "," + aRect.Height + ")" ); + + aRect = (com.sun.star.awt.Rectangle)evt.NewValue; + System.out.println( "new value: (" + aRect.X + "," + aRect.Y + "," + aRect.Width + "," + aRect.Height + ")" ); + } + } + + public void frameAction( final FrameActionEvent aEvent ) throws RuntimeException + { + System.out.println("frame action event " + aEvent.Action ); + + if( aEvent.Action == FrameAction.COMPONENT_REATTACHED ) + { + System.out.println("FrameAction.COMPONENT_REATTACHED"); + XController xController = aEvent.Frame.getController(); + connectListener( xController ); + } + } + + + // XEventListener + public void disposing( com.sun.star.lang.EventObject aSourceObj ) + { + XFrame xFrame = (XFrame)UnoRuntime.queryInterface(XFrame.class, aSourceObj.Source); + + if( xFrame != null ) + { + System.out.println("frame disposed"); + System.exit(0); + } + else + { + System.out.println("controller disposed"); + + } + } + + // XEventListener + public void notifyEvent (final com.sun.star.document.EventObject aEvent) throws RuntimeException + { + System.out.println ("Event : " + aEvent.EventName + " at " + aEvent.Source); + } + + + /** Write message into message area. + */ + public void message (String message) + { + maMessageArea.setText (message); + System.out.println (message); + + // Show the new message string immediately. + maMessageArea.paintImmediately (maMessageArea.getVisibleRect()); + } + + + + + public void print (String text) + { + maOutputArea.append (text); + } + + + + + public void println (String text) + { + maOutputArea.append (text + "\n"); + JScrollBar aBar = maScrollPane.getVerticalScrollBar(); + aBar.setValue (aBar.getMaximum()); + } + + + + protected SimpleOffice + office; + protected InformationWriter + info; + + private XModel + mxModel; + private JPanel + maMainPanel, + maButtonBar; + private Canvas + maCanvas; + private AccessibilityTree + maTree; + private JScrollPane + maScrollPane; + private JTextArea + maOutputArea, + maMessageArea; + private JButton + aConnectButton, + aQuitButton, + aLoadButton, + aUpdateButton, + aRunButton; +} diff --git a/toolkit/test/accessibility/Canvas.java b/toolkit/test/accessibility/Canvas.java new file mode 100755 index 000000000000..cd4ac0e072e5 --- /dev/null +++ b/toolkit/test/accessibility/Canvas.java @@ -0,0 +1,181 @@ +import java.util.Vector; +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; +import javax.swing.tree.*; + +/** This canvas displays accessible objects graphically. Each accessible + object with graphical representation is represented by an + AccessibleObject object and has to be added by the + <member>addAccessible</member> member function. +*/ +class Canvas + extends JPanel + implements MouseListener, MouseMotionListener +{ + public MessageInterface maMessageDisplay; + public final int nMaximumWidth = 1000; + public final int nMaximumHeight = 1000; + + public Canvas (MessageInterface aMessageDisplay, JTree aTree) + { + super (true); + maObjects = new Vector (); + addMouseListener (this); + addMouseMotionListener (this); + maBoundingBox = new Rectangle (0,0,100,100); + setPreferredSize (maBoundingBox.getSize()); + setSize (nMaximumWidth,nMaximumHeight); + maMessageDisplay = aMessageDisplay; + maTree = aTree; + mnXOffset = 0; + mnYOffset = 0; + mnScaleFactor = 1; + } + + public void addAccessible (AccessibleObject aObject) + { + maObjects.add (aObject); + maBoundingBox = maBoundingBox.union (aObject.getBBox()); + } + + public void clear () + { + maObjects.clear(); + } + + public void paintComponent (Graphics g) + { + Rectangle r = g.getClipBounds(); + g.clearRect (r.x,r.y,r.width,r.height); + + // Recalculate scale and offset so that all accessible objects fit + // into the area specified by nMaximum(Width,Height) + double nXScale = 1, + nYScale = 1; + if (maBoundingBox.getWidth() > nMaximumWidth) + nXScale = 1.0 * nMaximumWidth / maBoundingBox.getWidth(); + if (maBoundingBox.getHeight() > nMaximumHeight) + nYScale = 1.0 * nMaximumHeight / maBoundingBox.getHeight(); + if (nXScale < nYScale) + mnScaleFactor = nXScale; + else + mnScaleFactor = nYScale; + + int n = maObjects.size(); + for (int i=0; i<n; i++) + ((AccessibleObject)(maObjects.elementAt(i))).paint ( + g, + (int)-maBoundingBox.getX(), + (int)-maBoundingBox.getY(), + mnScaleFactor); + } + + public void mouseClicked (MouseEvent e) + { + } + + public void mousePressed (MouseEvent e) + { + mnXAnchor = e.getX(); + mnYAnchor = e.getY(); + int n = maObjects.size(); + for (int i=0; i<n; i++) + { + AccessibleObject aObject = (AccessibleObject)(maObjects.elementAt(i)); + if (aObject != null && aObject.contains (e.getX(),e.getY())) + { + maActiveObject = aObject; + maResizeFlag = aObject.getResizeFlag (e.getX(),e.getY()); + } + } + } + + public void mouseReleased (MouseEvent e) + { + } + + public void mouseEntered (MouseEvent e) + { + } + + public void mouseExited (MouseEvent e) + { + int n = maObjects.size(); + for (int i=0; i<n; i++) + { + AccessibleObject aObject = (AccessibleObject)(maObjects.elementAt(i)); + if (aObject != null) + aObject.deselect (); + } + repaint (); + } + + public void mouseDragged (MouseEvent e) + { + if (maActiveObject != null) + { + int dx = e.getX() - mnXAnchor; + int dy = e.getY() - mnYAnchor; + + if ((e.getModifiers() & MouseEvent.BUTTON1_MASK) != 0) + { + maActiveObject.translate (dx,dy); + } + else if ((e.getModifiers() & MouseEvent.BUTTON2_MASK) != 0) + { + maActiveObject.resize (maResizeFlag, dx,dy); + } + + mnXAnchor = e.getX(); + mnYAnchor = e.getY(); + + repaint (); + } + } + + public void mouseMoved (MouseEvent e) + { + maActiveObject = null; + for (int i=0; i<maObjects.size(); i++) + { + AccessibleObject aObject = (AccessibleObject)(maObjects.elementAt(i)); + if (aObject != null) + { + aObject.deselect (); + if (aObject.contains (e.getX(),e.getY())) + { + maActiveObject = aObject; + } + } + } + if (maActiveObject != null) + { + maActiveObject.select (); + maMessageDisplay.message ("mouse moved to " + e.getX() + "," + e.getY() + ": " + +maActiveObject.toString()); + System.out.println (maActiveObject.getPath()); + + maTree.scrollPathToVisible (maActiveObject.getPath()); + maTree.repaint (); + } + repaint (); + } + + protected int + mnXAnchor, + mnYAnchor, + maResizeFlag, + mnXOffset, + mnYOffset; + protected double + mnScaleFactor; + protected AccessibleObject + maActiveObject; + protected Vector + maObjects; + protected Rectangle + maBoundingBox; + protected JTree + maTree; +} diff --git a/toolkit/test/accessibility/InformationWriter.java b/toolkit/test/accessibility/InformationWriter.java new file mode 100755 index 000000000000..29dac4c2cf01 --- /dev/null +++ b/toolkit/test/accessibility/InformationWriter.java @@ -0,0 +1,420 @@ +import Print; + +import java.lang.Thread; + +import com.sun.star.awt.Rectangle; +import com.sun.star.awt.XWindow; + +import com.sun.star.beans.Property; +import com.sun.star.beans.PropertyValue; +import com.sun.star.beans.XPropertySet; +import com.sun.star.beans.XPropertySetInfo; + +import com.sun.star.container.XIndexAccess; +import com.sun.star.container.XChild; +import com.sun.star.container.XEnumerationAccess; +import com.sun.star.container.XEnumeration; + +import com.sun.star.frame.XComponentLoader; +import com.sun.star.frame.XController; +import com.sun.star.frame.XDesktop; +import com.sun.star.frame.XFrame; +import com.sun.star.frame.XTasksSupplier; +import com.sun.star.frame.XTask; + +import com.sun.star.lang.XComponent; +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.lang.XServiceInfo; +import com.sun.star.lang.XServiceName; +import com.sun.star.lang.XTypeProvider; + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.XInterface; +import com.sun.star.uno.Type; + +import com.sun.star.drawing.XDrawView; +import com.sun.star.drawing.XDrawPage; +import com.sun.star.drawing.XShapes; +import com.sun.star.drawing.XShape; +import com.sun.star.drawing.XShapeDescriptor; + +import drafts.com.sun.star.accessibility.XAccessible; +import drafts.com.sun.star.accessibility.XAccessibleContext; +import drafts.com.sun.star.accessibility.XAccessibleComponent; +import drafts.com.sun.star.accessibility.XAccessibleRelationSet; +import drafts.com.sun.star.accessibility.XAccessibleStateSet; + +public class InformationWriter + implements Print +{ + Print maPrinter; + + public InformationWriter (Print aPrinter) + { + maPrinter = aPrinter; + } + + + public void drawPageTest (XInterface xPage) + { + try + { + printProperty (xPage, "BorderBottom ", "BorderBottom"); + printProperty (xPage, "BorderLeft ", "BorderLeft"); + printProperty (xPage, "BorderRight ", "BorderRight"); + printProperty (xPage, "BorderTop ", "BorderTop"); + printProperty (xPage, "Height ", "Height"); + printProperty (xPage, "Width ", "Width"); + printProperty (xPage, "Number ", "Number"); + } + catch (Exception e) + { + System.out.println ("caught exception while testing draw page:" + e); + } + } + + public void printProperty (XInterface xObject, String prefix, String name) + { + try + { + XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface( + XPropertySet.class, xObject); + println (prefix + + xPropertySet.getPropertyValue (name)); + } + catch (Exception e) + { + println ("caught exception while getting property " + + name + " : " + e); + } + } + + + + public void showShapes (XDrawPage xPage) + { + try + { + XIndexAccess xShapeList = (XIndexAccess) UnoRuntime.queryInterface( + XIndexAccess.class, xPage); + + println ("There are " + xShapeList.getCount() + + " shapes"); + for (int i=0; i<xShapeList.getCount(); i++) + { + XShape xShape = (XShape) UnoRuntime.queryInterface( + XShape.class, xShapeList.getByIndex (i)); + + XShapeDescriptor xShapeDescriptor = + (XShapeDescriptor) UnoRuntime.queryInterface( + XShapeDescriptor.class, xShape); + String sName = xShapeDescriptor.getShapeType (); + println (" shape " + i + " : " + sName); + + XPropertySet xPropertySet = + (XPropertySet) UnoRuntime.queryInterface( + XPropertySet.class, xShape); + Integer nZOrder = + (Integer) xPropertySet.getPropertyValue ("ZOrder"); + println (" zorder = " + nZOrder); + } + } + catch (Exception e) + { + println ("caught exception in showShapes: " + e); + } + } + + + + + /** @descr Print all available services of the given object to the + standard output. + */ + public void showServices (XInterface xObject) + { + try + { + println ("Services:"); + XMultiServiceFactory xMSF = (XMultiServiceFactory) UnoRuntime.queryInterface ( + XMultiServiceFactory.class, + xObject + ); + if (xMSF == null) + println (" object does not support interface XMultiServiceFactory"); + else + { + String[] sServiceNames = xMSF.getAvailableServiceNames (); + println (" object can create " + + sServiceNames.length + " services"); + for (int i=0; i<sServiceNames.length; i++) + println (" service " + i + " : " + sServiceNames[i]); + } + } + catch (Exception e) + { + println ("caught exception in showServices : " + e); + } + } + + /** @descr Print the service and implementation name of the given + object. + */ + public void showInfo (XInterface xObject) + { + try + { + System.out.println ("Info:"); + // Use interface XServiceName to retrieve name of (main) service. + XServiceName xSN = (XServiceName) UnoRuntime.queryInterface ( + XServiceName.class, xObject); + if (xSN == null) + println (" interface XServiceName not supported"); + else + { + println (" Service name : " + xSN.getServiceName ()); + } + + // Use interface XServiceInfo to retrieve information about + // supported services. + XServiceInfo xSI = (XServiceInfo) UnoRuntime.queryInterface ( + XServiceInfo.class, xObject); + if (xSI == null) + println (" interface XServiceInfo not supported"); + else + { + println (" Implementation name : " + + xSI.getImplementationName ()); + } + } + catch (Exception e) + { + println ("caught exception in showInfo : " + e); + } + } + + + + + /** @descr Print information about supported interfaces. + */ + public void showInterfaces (XInterface xObject) + { + try + { + println ("Interfaces:"); + // Use interface XTypeProvider to retrieve a list of supported + // interfaces. + XTypeProvider xTP = (XTypeProvider) UnoRuntime.queryInterface ( + XTypeProvider.class, xObject); + if (xTP == null) + println (" interface XTypeProvider not supported"); + else + { + Type[] aTypeList = xTP.getTypes (); + println (" object supports " + aTypeList.length + + " interfaces"); + for (int i=0; i<aTypeList.length; i++) + println (" " + i + " : " + + aTypeList[i].getTypeName()); + } + } + catch (Exception e) + { + println ("caught exception in showInterfaces : " + e); + } + } + + + /** @descr Print information concerning the accessibility of the given + object. + */ + public boolean showAccessibility (XInterface xObject, int depth) + { + try + { + // Create indentation string. + String sIndent = ""; + while (depth-- > 0) + sIndent += " "; + + // Get XAccessibleContext object if given object does not + // already support this interface. + XAccessibleContext xContext + = (XAccessibleContext) UnoRuntime.queryInterface ( + XAccessibleContext.class, xObject); + if (xContext == null) + { + XAccessible xAccessible + = (XAccessible) UnoRuntime.queryInterface ( + XAccessible.class, xObject); + if (xAccessible == null) + { + println (sIndent + "given object " + xObject + + " is not accessible"); + return false; + } + else + xContext = xAccessible.getAccessibleContext(); + } + + // Print information about the accessible context. + if (xContext != null) + { + println (sIndent + "Name : " + + xContext.getAccessibleName()); + println (sIndent + "Description : " + + xContext.getAccessibleDescription()); + println (sIndent + "Role : " + + xContext.getAccessibleRole()); + String sHasParent; + if (xContext.getAccessibleParent() != null) + { + println (sIndent + "Has parent : yes"); + println (sIndent + "Parent index : " + + xContext.getAccessibleIndexInParent()); + } + else + println (sIndent + "Has parent : no"); + println (sIndent + "Child count : " + + xContext.getAccessibleChildCount()); + print (sIndent + "Relation set : "); + XAccessibleRelationSet xRelationSet + = xContext.getAccessibleRelationSet(); + if (xRelationSet != null) + { + print (xRelationSet.getRelationCount() + " ("); + for (int i=0; i<xRelationSet.getRelationCount(); i++) + { + if (i > 0) + print (", "); + print (xRelationSet.getRelation(i).toString()); + } + println (")"); + } + else + println ("no relation set"); + + print (sIndent + "State set : "); + XAccessibleStateSet xStateSet = + xContext.getAccessibleStateSet(); + if (xStateSet != null) + { + XIndexAccess xStates = + (XIndexAccess) UnoRuntime.queryInterface ( + XIndexAccess.class, xStateSet); + print (xStates.getCount() + " ("); + for (int i=0; i<xStates.getCount(); i++) + { + if (i > 0) + print (", "); + print (xStates.getByIndex(i).toString()); + } + println (")"); + } + else + println ("no state set"); + + showAccessibleComponent (xContext, sIndent); + } + else + println ("object has no accessible context."); + + // showInfo (xContext); + // showServices (xContext); + // showInterfaces (xContext); + } + catch (Exception e) + { + System.out.println ("caught exception in showAccessibility :" + e); + } + return true; + } + + + + + /** @descr Print information about the given accessible component. + */ + public void showAccessibleComponent (XInterface xObject, String sIndent) + { + try + { + XAccessibleComponent xComponent = + (XAccessibleComponent) UnoRuntime.queryInterface ( + XAccessibleComponent.class, xObject); + + // Print information about the accessible context. + if (xComponent != null) + { + println (sIndent + "Position : " + + xComponent.getLocation().X+", " + + xComponent.getLocation().Y); + println (sIndent + "Screen position : " + + xComponent.getLocationOnScreen().X+", " + + xComponent.getLocationOnScreen().Y); + println (sIndent + "Size : " + + xComponent.getSize().Width+", " + + xComponent.getSize().Height); + } + } + catch (Exception e) + { + System.out.println ( + "caught exception in showAccessibleComponent : " + e); + } + } + + + public boolean showAccessibilityTree (XAccessible xRoot, int depth) + { + if ( ! showAccessibility (xRoot, depth)) + return false; + + String sIndent = ""; + for (int i=0; i<depth; i++) + sIndent += " "; + + // Iterate over children and show them. + XAccessibleContext xContext = xRoot.getAccessibleContext(); + if (xContext != null) + { + int n = xContext.getAccessibleChildCount(); + for (int i=0; i<n; i++) + { + println (sIndent + "child " + i + " :"); + showAccessibilityTree (xContext.getAccessibleChild(i),depth+1); + } + } + else + println ("Accessible object has no context"); + + return true; + } + + public void showProperties (XInterface xObject) + { + XPropertySet xSet = (XPropertySet) UnoRuntime.queryInterface ( + XPropertySet.class, xObject); + if (xSet == null) + println ("object does not support XPropertySet"); + else + { + XPropertySetInfo xInfo = xSet.getPropertySetInfo (); + Property[] aProperties = xInfo.getProperties (); + int n = aProperties.length; + for (int i=0; i<n; i++) + println (i + " : " + aProperties[i].Name +", " + aProperties[i].Type); + } + } + + public void print (String text) + { + maPrinter.print (text); + } + + public void println (String text) + { + maPrinter.println (text); + } +} diff --git a/toolkit/test/accessibility/Print.java b/toolkit/test/accessibility/Print.java new file mode 100755 index 000000000000..e4d21dc4e092 --- /dev/null +++ b/toolkit/test/accessibility/Print.java @@ -0,0 +1,5 @@ +interface Print +{ + void print (String text); + void println (String text); +} diff --git a/toolkit/test/accessibility/SimpleOffice.java b/toolkit/test/accessibility/SimpleOffice.java new file mode 100755 index 000000000000..aee86a897b29 --- /dev/null +++ b/toolkit/test/accessibility/SimpleOffice.java @@ -0,0 +1,352 @@ +import OfficeConnection; +import FrameActionListener; +import Print; + +import java.lang.Thread; + +import com.sun.star.awt.Rectangle; +import com.sun.star.awt.XWindow; + +import com.sun.star.beans.PropertyValue; +import com.sun.star.beans.XPropertySet; + +import com.sun.star.container.XIndexAccess; +import com.sun.star.container.XChild; +import com.sun.star.container.XEnumerationAccess; +import com.sun.star.container.XEnumeration; + +import com.sun.star.frame.XComponentLoader; +import com.sun.star.frame.XController; +import com.sun.star.frame.XDesktop; +import com.sun.star.frame.XFrame; +import com.sun.star.frame.XModel; +import com.sun.star.frame.XTasksSupplier; +import com.sun.star.frame.XTask; + +import com.sun.star.lang.XComponent; +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.lang.XServiceInfo; +import com.sun.star.lang.XServiceName; +import com.sun.star.lang.XTypeProvider; + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.XInterface; +import com.sun.star.uno.Type; + +import com.sun.star.drawing.XDrawView; +import com.sun.star.drawing.XDrawPage; +import com.sun.star.drawing.XShapes; +import com.sun.star.drawing.XShape; +import com.sun.star.drawing.XShapeDescriptor; + +import drafts.com.sun.star.accessibility.XAccessible; +import drafts.com.sun.star.accessibility.XAccessibleContext; +import drafts.com.sun.star.accessibility.XAccessibleComponent; +import drafts.com.sun.star.accessibility.XAccessibleRelationSet; +import drafts.com.sun.star.accessibility.XAccessibleStateSet; + + +/** This class tries to simplify some tasks like loading a document or + getting various objects. +*/ +public class SimpleOffice + implements Print +{ + XDesktop mxDesktop = null; + Print maPrinter; + OfficeConnection aConnection; + int mnPortNumber; + + public SimpleOffice (Print aPrinter, int nPortNumber) + { + mnPortNumber = nPortNumber; + maPrinter = aPrinter; + connect (); + getDesktop (); + } + + public void connect () + { + aConnection = new OfficeConnection (maPrinter, mnPortNumber); + mxDesktop = null; + getDesktop (); + } + + public XModel loadDocument (String URL) + { + XModel xModel = null; + try + { + // Load the document from the specified URL. + XComponentLoader xLoader = + (XComponentLoader)UnoRuntime.queryInterface( + XComponentLoader.class, mxDesktop); + + XComponent xComponent = xLoader.loadComponentFromURL ( + URL, + "_blank", + 0, + new PropertyValue[0] + ); + + xModel = (XModel) UnoRuntime.queryInterface( + XModel.class, xComponent); + } + catch (java.lang.NullPointerException e) + { + println ("caught exception while loading " + + URL + " : " + e); + } + catch (Exception e) + { + println ("caught exception while loading " + + URL + " : " + e); + } + return xModel; + } + + + + + public XModel getModel (String name) + { + XModel xModel = null; + try + { + XTasksSupplier xTasksSupplier = + (XTasksSupplier) UnoRuntime.queryInterface( + XTasksSupplier.class, mxDesktop); + XEnumerationAccess xEA = xTasksSupplier.getTasks(); + XEnumeration xE = xEA.createEnumeration(); + while (xE.hasMoreElements()) + { + XTask xTask = (XTask) UnoRuntime.queryInterface( + XTask.class, xE.nextElement()); + print (xTask.getName()); + } + } + catch (Exception e) + { + println ("caught exception while getting Model " + name + + ": " + e); + } + return xModel; + } + + + public XModel getModel (XDrawView xView) + { + XController xController = (XController) UnoRuntime.queryInterface( + XController.class, xView); + if (xController != null) + return xController.getModel(); + else + { + println ("can't cast view to controller"); + return null; + } + } + + public XDesktop getDesktop () + { + if (mxDesktop != null) + return mxDesktop; + try + { + // Get the factory of the connected office. + XMultiServiceFactory xMSF = aConnection.getServiceManager (); + if (xMSF == null) + { + println ("can't connect to office"); + return null; + } + else + println ("Connected successfully."); + + // Create a new desktop. + mxDesktop = (XDesktop) UnoRuntime.queryInterface( + XDesktop.class, + xMSF.createInstance ("com.sun.star.frame.Desktop") + ); + } + catch (Exception e) + { + println ("caught exception while creating desktop: " + + e); + } + + return mxDesktop; + } + + + + + public XAccessible getAccessibleRoot (XInterface xObject) + { + XAccessible xAccessible = null; + try + { + xAccessible = (XAccessible) UnoRuntime.queryInterface( + XAccessible.class, xObject); + } + catch (Exception e) + { + println ( + "caught exception while getting accessible root" + e); + } + return xAccessible; + } + + + + + /** @descr Return the current window associated with the given + model. + */ + public XWindow getCurrentWindow () + { + return getCurrentWindow ((XModel) UnoRuntime.queryInterface( + XModel.class, getDesktop())); + } + + + + + + public XWindow getCurrentWindow (XModel xModel) + { + XWindow xWindow = null; + try + { + if (xModel == null) + println ("invalid model (==null)"); + XController xController = xModel.getCurrentController(); + if (xController == null) + println ("can't get controller from model"); + XFrame xFrame = xController.getFrame(); + if (xFrame == null) + println ("can't get frame from controller"); + xWindow = xFrame.getComponentWindow (); + if (xWindow == null) + println ("can't get window from frame"); + } + catch (Exception e) + { + println ("caught exception while getting current window" + e); + } + + return xWindow; + } + + + + + /** @descr Return the current draw page of the given desktop. + */ + public XDrawPage getCurrentDrawPage () + { + return getCurrentDrawPage ((XDrawView) UnoRuntime.queryInterface( + XDrawView.class, getDesktop())); + } + + + + + public XDrawPage getCurrentDrawPage (XDrawView xView) + { + try + { + XDrawPage xPage = xView.getCurrentPage(); + return xPage; + } + catch (Exception e) + { + println ("caught exception while getting current draw page : " + e); + } + + return null; + } + + + + + /** @descr Return the current view of the given desktop. + */ + public XDrawView getCurrentView () + { + return getCurrentView (null); + } + + public XDrawView getCurrentView (XDesktop xDesktop) + { + getDesktop(); + if (mxDesktop == null) + println ("can't get desktop to retrieve current view"); + + XDrawView xView = null; + try + { + XComponent xComponent = mxDesktop.getCurrentComponent(); + if (xComponent == null) + println ("can't get component to retrieve current view"); + + XFrame xFrame = mxDesktop.getCurrentFrame(); + if (xFrame == null) + println ("can't get frame to retrieve current view"); + + XController xController = xFrame.getController(); + if (xController == null) + println ("can't get controller to retrieve current view"); + + xView = (XDrawView) UnoRuntime.queryInterface( + XDrawView.class, xController); + } + catch (Exception e) + { + println ("caught exception while getting current view : " + e); + } + + return xView; + } + + + + + // Return the accessible object of the document window. + public static XAccessible getAccessibleDocumentWindow (XDrawPage xPage) + { + XIndexAccess xShapeList = (XIndexAccess) UnoRuntime.queryInterface( + XIndexAccess.class, xPage); + if (xShapeList.getCount() > 0) + { + // All shapes return as accessible object the document window's + // accessible object. This is, of course, a hack and will be + // removed as soon as the missing infrastructure for obtaining + // the object directly is implemented. + XShape xShape = null; + try{ + xShape = (XShape) UnoRuntime.queryInterface( + XShape.class, xShapeList.getByIndex (0)); + } catch (Exception e) + {} + XAccessible xAccessible = (XAccessible) UnoRuntime.queryInterface ( + XAccessible.class, xShape); + return xAccessible; + } + else + return null; + } + + + + + public void print (String text) + { + maPrinter.print (text); + } + + public void println (String text) + { + maPrinter.println (text); + } +} |