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.frame.XTerminateListener; 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, XTerminateListener { 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"; // = "file:///tmp/draw-test-document.sxd"; for (int i=0; i*"); System.out.println ("options:"); System.out.println (" -p Port on which to connect to StarOffice."); System.out.println (" Defaults to 5678."); System.out.println (" -f 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"); aUpdateButton = createButton ("Update", "update"); aShapesButton = createButton ("Shapes", "shapes"); aExpandButton = createButton ("Expand", "expand"); aTextButton = createButton("Text", "text"); 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 () { // Delete the graphical representations. if (maCanvas != null) maCanvas.clear (); // create new model (with new documents) AccessibilityTreeModel aModel = new AccessibilityTreeModel( createTreeModelRoot() ); aModel.setCanvas( maCanvas ); maTree.setModel( aModel ); // if (office != null && office.getDesktop() != null) // office.getDesktop().addTerminateListener (this); } /** 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 ("."); initialize(); } } else if (e.getActionCommand().equals("update")) { initialize (); } else if (e.getActionCommand().equals("shapes")) { maTree.expandShapes(); } else if (e.getActionCommand().equals("expand")) { maTree.expandAll(); } else if (e.getActionCommand().equals("text")) { Canvas.bPaintText = ! Canvas.bPaintText; } else { System.err.println("unknown command " + e.getActionCommand()); } } /** Create an AccessibilityTreeModel root which contains the documents */ private Object createTreeModelRoot() { Vector aRoots = new Vector(); try { XDesktop xDesktop = office.getDesktop(); if (xDesktop == null) { return "ERROR: Can't connect. (No desktop)"; } XEnumerationAccess xEA = xDesktop.getComponents(); if (xEA == null) { return "ERROR: Can't get components"; } 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); // create document node aRoots.add( AccessibilityTreeModel.createDefaultNode(xRoot) ); } else aRoots.add( "can't cast component to model" ); } println ("finished getting named documents"); } catch (Exception e) { System.out.println ("caught exception while getting document names: " + e); } // create root node AccTreeNode aNode = new AccTreeNode( aRoots, "Accessibility Tree" ); AccessibilityTreeModel.addDefaultHandlers( aNode ); return aNode; } /** 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