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.text; 25 26 import java.awt.BorderLayout; 27 import java.awt.Container; 28 import java.awt.FlowLayout; 29 import java.awt.event.ActionListener; 30 import java.awt.event.ActionEvent; 31 import javax.swing.JButton; 32 import javax.swing.JCheckBox; 33 import javax.swing.JDialog; 34 import javax.swing.JLabel; 35 import javax.swing.JOptionPane; 36 import javax.swing.JPanel; 37 import javax.swing.JTextArea; 38 import javax.swing.text.JTextComponent; 39 40 import com.sun.star.accessibility.XAccessibleContext; 41 import com.sun.star.accessibility.XAccessibleText; 42 import com.sun.star.accessibility.XAccessibleEditableText; 43 import com.sun.star.lang.IndexOutOfBoundsException; 44 import com.sun.star.uno.UnoRuntime; 45 46 47 /** 48 * Display a dialog with a text field and a pair of cancel/do-it buttons 49 */ 50 class TextActionDialog 51 extends JDialog 52 implements ActionListener 53 { TextActionDialog( XAccessibleContext xContext, String sExplanation, String sTitle)54 public TextActionDialog ( 55 XAccessibleContext xContext, 56 String sExplanation, 57 String sTitle) 58 { 59 super();// AccessibilityWorkBench.Instance() ); 60 61 mxContext = xContext; 62 msTitle = sTitle; 63 msExplanation = sExplanation; 64 Layout (); 65 setSize (350, 225); 66 67 } 68 69 70 /** build dialog */ Layout()71 protected void Layout() 72 { 73 setTitle (msTitle); 74 75 // vertical stacking of the elements 76 Container aContent = getContentPane(); 77 // aContent.setLayout( new BorderLayout() ); 78 79 // Label with explanation. 80 if (msExplanation.length() > 0) 81 aContent.add (new JLabel (msExplanation), BorderLayout.NORTH); 82 83 // the text field 84 maText = new JTextArea(); 85 maText.setLineWrap (true); 86 maText.setEditable (false); 87 aContent.add (maText, BorderLayout.CENTER); 88 89 XAccessibleText xText = (XAccessibleText)UnoRuntime.queryInterface( 90 XAccessibleText.class, mxContext); 91 String sText = xText.getText(); 92 maText.setText (sText); 93 maText.setRows (sText.length() / 40 + 1); 94 maText.setColumns (Math.min (Math.max (40, sText.length()), 20)); 95 96 JPanel aButtons = new JPanel(); 97 aButtons.setLayout (new FlowLayout()); 98 maIndexToggle = new JCheckBox ("reverse selection"); 99 aButtons.add (maIndexToggle); 100 101 JButton aActionButton = new JButton (msTitle); 102 aActionButton.setActionCommand ("Action"); 103 aActionButton.addActionListener (this); 104 aButtons.add (aActionButton); 105 106 JButton aCancelButton = new JButton ("cancel"); 107 aCancelButton.setActionCommand ("Cancel"); 108 aCancelButton.addActionListener (this); 109 aButtons.add (aCancelButton); 110 111 // add Panel with buttons 112 aContent.add (aButtons, BorderLayout.SOUTH); 113 } 114 Cancel()115 protected void Cancel() 116 { 117 hide(); 118 dispose(); 119 } 120 actionPerformed(ActionEvent e)121 public void actionPerformed(ActionEvent e) 122 { 123 String sCommand = e.getActionCommand(); 124 125 if( "Cancel".equals( sCommand ) ) 126 Cancel(); 127 else if( "Action".equals( sCommand ) ) 128 Action(); 129 } 130 131 GetSelectionStart()132 protected int GetSelectionStart() 133 { 134 return GetSelection(true); 135 } GetSelectionEnd()136 protected int GetSelectionEnd() 137 { 138 return GetSelection(false); 139 } GetSelection(boolean bStart)140 private int GetSelection (boolean bStart) 141 { 142 if (bStart ^ maIndexToggle.isSelected()) 143 return maText.getSelectionStart(); 144 else 145 return maText.getSelectionEnd(); 146 } 147 148 149 Action()150 protected void Action () 151 { 152 String sError = null; 153 boolean bSuccess = true; 154 try 155 { 156 XAccessibleText xText = 157 (XAccessibleText)UnoRuntime.queryInterface( 158 XAccessibleText.class, mxContext); 159 if (xText != null) 160 bSuccess = bSuccess && TextAction (xText); 161 162 XAccessibleEditableText xEditableText = 163 (XAccessibleEditableText)UnoRuntime.queryInterface( 164 XAccessibleEditableText.class, mxContext); 165 if (xEditableText != null) 166 bSuccess = bSuccess && EditableTextAction (xEditableText); 167 168 if ( ! bSuccess) 169 sError = "Can't execute"; 170 } 171 catch (IndexOutOfBoundsException e) 172 { 173 sError = "Index out of bounds"; 174 } 175 176 if (sError != null) 177 JOptionPane.showMessageDialog ( 178 this,// AccessibilityWorkBench.Instance(), 179 sError, 180 msTitle, 181 JOptionPane.ERROR_MESSAGE); 182 183 Cancel(); 184 } 185 186 /** override this for dialog-specific action */ TextAction(XAccessibleText xText)187 boolean TextAction (XAccessibleText xText) 188 throws IndexOutOfBoundsException 189 { 190 return true; 191 } 192 EditableTextAction(XAccessibleEditableText xText)193 boolean EditableTextAction (XAccessibleEditableText xText) 194 throws IndexOutOfBoundsException 195 { 196 return true; 197 } 198 199 private XAccessibleContext mxContext; 200 protected JTextArea maText; 201 private String msTitle; 202 private String msExplanation; 203 private JCheckBox maIndexToggle; 204 } 205