1 11 package org.eclipse.ui.actions; 12 13 import org.eclipse.jface.action.Action; 14 import org.eclipse.jface.action.IAction; 15 import org.eclipse.jface.util.IPropertyChangeListener; 16 import org.eclipse.jface.util.PropertyChangeEvent; 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.events.KeyAdapter; 19 import org.eclipse.swt.events.KeyEvent; 20 import org.eclipse.swt.events.MouseAdapter; 21 import org.eclipse.swt.events.MouseEvent; 22 import org.eclipse.swt.graphics.Point; 23 import org.eclipse.swt.widgets.Event; 24 import org.eclipse.swt.widgets.Listener; 25 import org.eclipse.swt.widgets.Text; 26 import org.eclipse.ui.IActionBars; 27 import org.eclipse.ui.PlatformUI; 28 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 29 import org.eclipse.ui.internal.ide.IIDEHelpContextIds; 30 31 47 public class TextActionHandler { 48 private DeleteActionHandler textDeleteAction = new DeleteActionHandler(); 49 50 private CutActionHandler textCutAction = new CutActionHandler(); 51 52 private CopyActionHandler textCopyAction = new CopyActionHandler(); 53 54 private PasteActionHandler textPasteAction = new PasteActionHandler(); 55 56 private SelectAllActionHandler textSelectAllAction = new SelectAllActionHandler(); 57 58 private IAction deleteAction; 59 60 private IAction cutAction; 61 62 private IAction copyAction; 63 64 private IAction pasteAction; 65 66 private IAction selectAllAction; 67 68 private IPropertyChangeListener deleteActionListener = new PropertyChangeListener( 69 textDeleteAction); 70 71 private IPropertyChangeListener cutActionListener = new PropertyChangeListener( 72 textCutAction); 73 74 private IPropertyChangeListener copyActionListener = new PropertyChangeListener( 75 textCopyAction); 76 77 private IPropertyChangeListener pasteActionListener = new PropertyChangeListener( 78 textPasteAction); 79 80 private IPropertyChangeListener selectAllActionListener = new PropertyChangeListener( 81 textSelectAllAction); 82 83 private Listener textControlListener = new TextControlListener(); 84 85 private Text activeTextControl; 86 87 private MouseAdapter mouseAdapter = new MouseAdapter() { 88 public void mouseUp(MouseEvent e) { 89 updateActionsEnableState(); 90 } 91 }; 92 93 private KeyAdapter keyAdapter = new KeyAdapter() { 94 public void keyReleased(KeyEvent e) { 95 updateActionsEnableState(); 96 } 97 }; 98 99 private class TextControlListener implements Listener { 100 public void handleEvent(Event event) { 101 switch (event.type) { 102 case SWT.Activate: 103 activeTextControl = (Text) event.widget; 104 updateActionsEnableState(); 105 break; 106 case SWT.Deactivate: 107 activeTextControl = null; 108 updateActionsEnableState(); 109 break; 110 default: 111 break; 112 } 113 } 114 } 115 116 private class PropertyChangeListener implements IPropertyChangeListener { 117 private IAction actionHandler; 118 119 protected PropertyChangeListener(IAction actionHandler) { 120 super(); 121 this.actionHandler = actionHandler; 122 } 123 124 public void propertyChange(PropertyChangeEvent event) { 125 if (activeTextControl != null) { 126 return; 127 } 128 if (event.getProperty().equals(IAction.ENABLED)) { 129 Boolean bool = (Boolean ) event.getNewValue(); 130 actionHandler.setEnabled(bool.booleanValue()); 131 } 132 } 133 } 134 135 private class DeleteActionHandler extends Action { 136 protected DeleteActionHandler() { 137 super(IDEWorkbenchMessages.Delete); 138 setId("TextDeleteActionHandler"); setEnabled(false); 140 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, 141 IIDEHelpContextIds.TEXT_DELETE_ACTION); 142 } 143 144 public void runWithEvent(Event event) { 145 if (activeTextControl != null && !activeTextControl.isDisposed()) { 146 String text = activeTextControl.getText(); 147 Point selection = activeTextControl.getSelection(); 148 if (selection.y == selection.x) { 149 ++selection.y; 150 } 151 if (selection.y > text.length()) { 152 return; 153 } 154 StringBuffer buf = new StringBuffer (text.substring(0, 155 selection.x)); 156 buf.append(text.substring(selection.y)); 157 activeTextControl.setText(buf.toString()); 158 activeTextControl.setSelection(selection.x, selection.x); 159 updateActionsEnableState(); 160 return; 161 } 162 if (deleteAction != null) { 163 deleteAction.runWithEvent(event); 164 return; 165 } 166 } 167 168 171 public void updateEnabledState() { 172 if (activeTextControl != null && !activeTextControl.isDisposed()) { 173 setEnabled(activeTextControl.getSelectionCount() > 0 174 || activeTextControl.getCaretPosition() < activeTextControl 175 .getCharCount()); 176 return; 177 } 178 if (deleteAction != null) { 179 setEnabled(deleteAction.isEnabled()); 180 return; 181 } 182 setEnabled(false); 183 } 184 } 185 186 private class CutActionHandler extends Action { 187 protected CutActionHandler() { 188 super(IDEWorkbenchMessages.Cut); 189 setId("TextCutActionHandler"); setEnabled(false); 191 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, 192 IIDEHelpContextIds.TEXT_CUT_ACTION); 193 } 194 195 public void runWithEvent(Event event) { 196 if (activeTextControl != null && !activeTextControl.isDisposed()) { 197 activeTextControl.cut(); 198 updateActionsEnableState(); 199 return; 200 } 201 if (cutAction != null) { 202 cutAction.runWithEvent(event); 203 return; 204 } 205 } 206 207 210 public void updateEnabledState() { 211 if (activeTextControl != null && !activeTextControl.isDisposed()) { 212 setEnabled(activeTextControl.getSelectionCount() > 0); 213 return; 214 } 215 if (cutAction != null) { 216 setEnabled(cutAction.isEnabled()); 217 return; 218 } 219 setEnabled(false); 220 } 221 } 222 223 private class CopyActionHandler extends Action { 224 protected CopyActionHandler() { 225 super(IDEWorkbenchMessages.Copy); 226 setId("TextCopyActionHandler"); setEnabled(false); 228 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, 229 IIDEHelpContextIds.TEXT_COPY_ACTION); 230 } 231 232 public void runWithEvent(Event event) { 233 if (activeTextControl != null && !activeTextControl.isDisposed()) { 234 activeTextControl.copy(); 235 updateActionsEnableState(); 236 return; 237 } 238 if (copyAction != null) { 239 copyAction.runWithEvent(event); 240 return; 241 } 242 } 243 244 247 public void updateEnabledState() { 248 if (activeTextControl != null && !activeTextControl.isDisposed()) { 249 setEnabled(activeTextControl.getSelectionCount() > 0); 250 return; 251 } 252 if (copyAction != null) { 253 setEnabled(copyAction.isEnabled()); 254 return; 255 } 256 setEnabled(false); 257 } 258 } 259 260 private class PasteActionHandler extends Action { 261 protected PasteActionHandler() { 262 super(IDEWorkbenchMessages.Paste); 263 setId("TextPasteActionHandler"); setEnabled(false); 265 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, 266 IIDEHelpContextIds.TEXT_PASTE_ACTION); 267 } 268 269 public void runWithEvent(Event event) { 270 if (activeTextControl != null && !activeTextControl.isDisposed()) { 271 activeTextControl.paste(); 272 updateActionsEnableState(); 273 return; 274 } 275 if (pasteAction != null) { 276 pasteAction.runWithEvent(event); 277 return; 278 } 279 } 280 281 284 public void updateEnabledState() { 285 if (activeTextControl != null && !activeTextControl.isDisposed()) { 286 setEnabled(true); 287 return; 288 } 289 if (pasteAction != null) { 290 setEnabled(pasteAction.isEnabled()); 291 return; 292 } 293 setEnabled(false); 294 } 295 } 296 297 private class SelectAllActionHandler extends Action { 298 protected SelectAllActionHandler() { 299 super(IDEWorkbenchMessages.TextAction_selectAll); 300 setId("TextSelectAllActionHandler"); setEnabled(false); 302 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, 303 IIDEHelpContextIds.TEXT_SELECT_ALL_ACTION); 304 } 305 306 public void runWithEvent(Event event) { 307 if (activeTextControl != null && !activeTextControl.isDisposed()) { 308 activeTextControl.selectAll(); 309 updateActionsEnableState(); 310 return; 311 } 312 if (selectAllAction != null) { 313 selectAllAction.runWithEvent(event); 314 return; 315 } 316 } 317 318 321 public void updateEnabledState() { 322 if (activeTextControl != null && !activeTextControl.isDisposed()) { 323 setEnabled(true); 324 return; 325 } 326 if (selectAllAction != null) { 327 setEnabled(selectAllAction.isEnabled()); 328 return; 329 } 330 setEnabled(false); 331 } 332 } 333 334 343 public TextActionHandler(IActionBars actionBar) { 344 super(); 345 actionBar.setGlobalActionHandler(ActionFactory.CUT.getId(), 346 textCutAction); 347 actionBar.setGlobalActionHandler(ActionFactory.COPY.getId(), 348 textCopyAction); 349 actionBar.setGlobalActionHandler(ActionFactory.PASTE.getId(), 350 textPasteAction); 351 actionBar.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), 352 textSelectAllAction); 353 actionBar.setGlobalActionHandler(ActionFactory.DELETE.getId(), 354 textDeleteAction); 355 } 356 357 364 public void addText(Text textControl) { 365 if (textControl == null) { 366 return; 367 } 368 369 activeTextControl = textControl; 370 textControl.addListener(SWT.Activate, textControlListener); 371 textControl.addListener(SWT.Deactivate, textControlListener); 372 373 textControl.addKeyListener(keyAdapter); 377 textControl.addMouseListener(mouseAdapter); 378 379 } 380 381 384 public void dispose() { 385 setCutAction(null); 386 setCopyAction(null); 387 setPasteAction(null); 388 setSelectAllAction(null); 389 setDeleteAction(null); 390 } 391 392 399 public void removeText(Text textControl) { 400 if (textControl == null) { 401 return; 402 } 403 404 textControl.removeListener(SWT.Activate, textControlListener); 405 textControl.removeListener(SWT.Deactivate, textControlListener); 406 407 textControl.removeMouseListener(mouseAdapter); 408 textControl.removeKeyListener(keyAdapter); 409 410 activeTextControl = null; 411 updateActionsEnableState(); 412 } 413 414 422 public void setCopyAction(IAction action) { 423 if (copyAction == action) { 424 return; 425 } 426 427 if (copyAction != null) { 428 copyAction.removePropertyChangeListener(copyActionListener); 429 } 430 431 copyAction = action; 432 433 if (copyAction != null) { 434 copyAction.addPropertyChangeListener(copyActionListener); 435 } 436 437 textCopyAction.updateEnabledState(); 438 } 439 440 448 public void setCutAction(IAction action) { 449 if (cutAction == action) { 450 return; 451 } 452 453 if (cutAction != null) { 454 cutAction.removePropertyChangeListener(cutActionListener); 455 } 456 457 cutAction = action; 458 459 if (cutAction != null) { 460 cutAction.addPropertyChangeListener(cutActionListener); 461 } 462 463 textCutAction.updateEnabledState(); 464 } 465 466 474 public void setPasteAction(IAction action) { 475 if (pasteAction == action) { 476 return; 477 } 478 479 if (pasteAction != null) { 480 pasteAction.removePropertyChangeListener(pasteActionListener); 481 } 482 483 pasteAction = action; 484 485 if (pasteAction != null) { 486 pasteAction.addPropertyChangeListener(pasteActionListener); 487 } 488 489 textPasteAction.updateEnabledState(); 490 } 491 492 500 public void setSelectAllAction(IAction action) { 501 if (selectAllAction == action) { 502 return; 503 } 504 505 if (selectAllAction != null) { 506 selectAllAction 507 .removePropertyChangeListener(selectAllActionListener); 508 } 509 510 selectAllAction = action; 511 512 if (selectAllAction != null) { 513 selectAllAction.addPropertyChangeListener(selectAllActionListener); 514 } 515 516 textSelectAllAction.updateEnabledState(); 517 } 518 519 527 public void setDeleteAction(IAction action) { 528 if (deleteAction == action) { 529 return; 530 } 531 532 if (deleteAction != null) { 533 deleteAction.removePropertyChangeListener(deleteActionListener); 534 } 535 536 deleteAction = action; 537 538 if (deleteAction != null) { 539 deleteAction.addPropertyChangeListener(deleteActionListener); 540 } 541 542 textDeleteAction.updateEnabledState(); 543 } 544 545 549 private void updateActionsEnableState() { 550 textCutAction.updateEnabledState(); 551 textCopyAction.updateEnabledState(); 552 textPasteAction.updateEnabledState(); 553 textSelectAllAction.updateEnabledState(); 554 textDeleteAction.updateEnabledState(); 555 } 556 } 557 | Popular Tags |