1 11 package org.eclipse.ui.internal.navigator.resources.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.actions.ActionFactory; 29 30 46 public class TextActionHandler extends org.eclipse.ui.actions.TextActionHandler { 47 private DeleteActionHandler textDeleteAction = new DeleteActionHandler(); 48 49 private CutActionHandler textCutAction = new CutActionHandler(); 50 51 private CopyActionHandler textCopyAction = new CopyActionHandler(); 52 53 private PasteActionHandler textPasteAction = new PasteActionHandler(); 54 55 private SelectAllActionHandler textSelectAllAction = new SelectAllActionHandler(); 56 57 private IAction deleteAction; 58 59 private IAction cutAction; 60 61 private IAction copyAction; 62 63 private IAction pasteAction; 64 65 private IAction selectAllAction; 66 67 private IPropertyChangeListener deleteActionListener = new PropertyChangeListener( 68 textDeleteAction); 69 70 private IPropertyChangeListener cutActionListener = new PropertyChangeListener( 71 textCutAction); 72 73 private IPropertyChangeListener copyActionListener = new PropertyChangeListener( 74 textCopyAction); 75 76 private IPropertyChangeListener pasteActionListener = new PropertyChangeListener( 77 textPasteAction); 78 79 private IPropertyChangeListener selectAllActionListener = new PropertyChangeListener( 80 textSelectAllAction); 81 82 private Listener textControlListener = new TextControlListener(); 83 84 private Text activeTextControl; 85 86 private MouseAdapter mouseAdapter = new MouseAdapter() { 87 public void mouseUp(MouseEvent e) { 88 updateActionsEnableState(); 89 } 90 }; 91 92 private KeyAdapter keyAdapter = new KeyAdapter() { 93 public void keyReleased(KeyEvent e) { 94 updateActionsEnableState(); 95 } 96 }; 97 98 private IActionBars actionBars; 99 100 private class TextControlListener implements Listener { 101 public void handleEvent(Event event) { 102 switch (event.type) { 103 case SWT.Activate: 104 activeTextControl = (Text) event.widget; 105 updateActionsEnableState(); 106 break; 107 case SWT.Deactivate: 108 activeTextControl = null; 109 updateActionsEnableState(); 110 break; 111 default: 112 break; 113 } 114 } 115 } 116 117 private class PropertyChangeListener implements IPropertyChangeListener { 118 private IAction actionHandler; 119 120 protected PropertyChangeListener(IAction actionHandler) { 121 super(); 122 this.actionHandler = actionHandler; 123 } 124 125 public void propertyChange(PropertyChangeEvent event) { 126 if (activeTextControl != null) { 127 return; 128 } 129 if (event.getProperty().equals(IAction.ENABLED)) { 130 Boolean bool = (Boolean ) event.getNewValue(); 131 actionHandler.setEnabled(bool.booleanValue()); 132 } 133 } 134 } 135 136 private class DeleteActionHandler extends Action { 137 protected DeleteActionHandler() { 138 super("Delete"); setId("TextDeleteActionHandler"); setEnabled(false); 141 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, "DeleteHelpId"); } 144 145 public void runWithEvent(Event event) { 146 if (activeTextControl != null && !activeTextControl.isDisposed()) { 147 String text = activeTextControl.getText(); 148 Point selection = activeTextControl.getSelection(); 149 if (selection.y == selection.x) { 150 ++selection.y; 151 } 152 if (selection.y > text.length()) { 153 return; 154 } 155 StringBuffer buf = new StringBuffer (text.substring(0, 156 selection.x)); 157 buf.append(text.substring(selection.y)); 158 activeTextControl.setText(buf.toString()); 159 activeTextControl.setSelection(selection.x, selection.x); 160 updateActionsEnableState(); 161 return; 162 } 163 if (deleteAction != null) { 164 deleteAction.runWithEvent(event); 165 return; 166 } 167 } 168 169 172 public void updateEnabledState() { 173 if (activeTextControl != null && !activeTextControl.isDisposed()) { 174 setEnabled(activeTextControl.getSelectionCount() > 0 175 || activeTextControl.getCaretPosition() < activeTextControl 176 .getCharCount()); 177 return; 178 } 179 if (deleteAction != null) { 180 setEnabled(deleteAction.isEnabled()); 181 return; 182 } 183 setEnabled(false); 184 } 185 } 186 187 private class CutActionHandler extends Action { 188 protected CutActionHandler() { 189 super("Cut"); setId("TextCutActionHandler"); setEnabled(false); 192 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, "CutHelpId"); } 195 196 public void runWithEvent(Event event) { 197 if (activeTextControl != null && !activeTextControl.isDisposed()) { 198 activeTextControl.cut(); 199 updateActionsEnableState(); 200 return; 201 } 202 if (cutAction != null) { 203 cutAction.runWithEvent(event); 204 return; 205 } 206 } 207 208 211 public void updateEnabledState() { 212 if (activeTextControl != null && !activeTextControl.isDisposed()) { 213 setEnabled(activeTextControl.getSelectionCount() > 0); 214 return; 215 } 216 if (cutAction != null) { 217 setEnabled(cutAction.isEnabled()); 218 return; 219 } 220 setEnabled(false); 221 } 222 } 223 224 private class CopyActionHandler extends Action { 225 protected CopyActionHandler() { 226 super("Copy"); setId("TextCopyActionHandler"); setEnabled(false); 229 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, "CopyHelpId"); } 232 233 public void runWithEvent(Event event) { 234 if (activeTextControl != null && !activeTextControl.isDisposed()) { 235 activeTextControl.copy(); 236 updateActionsEnableState(); 237 return; 238 } 239 if (copyAction != null) { 240 copyAction.runWithEvent(event); 241 return; 242 } 243 } 244 245 248 public void updateEnabledState() { 249 if (activeTextControl != null && !activeTextControl.isDisposed()) { 250 setEnabled(activeTextControl.getSelectionCount() > 0); 251 return; 252 } 253 if (copyAction != null) { 254 setEnabled(copyAction.isEnabled()); 255 return; 256 } 257 setEnabled(false); 258 } 259 } 260 261 private class PasteActionHandler extends Action { 262 protected PasteActionHandler() { 263 super("Paste"); setId("TextPasteActionHandler"); setEnabled(false); 266 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, "PasteHelpId"); } 269 270 public void runWithEvent(Event event) { 271 if (activeTextControl != null && !activeTextControl.isDisposed()) { 272 activeTextControl.paste(); 273 updateActionsEnableState(); 274 return; 275 } 276 if (pasteAction != null) { 277 pasteAction.runWithEvent(event); 278 return; 279 } 280 } 281 282 285 public void updateEnabledState() { 286 if (activeTextControl != null && !activeTextControl.isDisposed()) { 287 setEnabled(true); 288 return; 289 } 290 if (pasteAction != null) { 291 setEnabled(pasteAction.isEnabled()); 292 return; 293 } 294 setEnabled(false); 295 } 296 } 297 298 private class SelectAllActionHandler extends Action { 299 protected SelectAllActionHandler() { 300 super("Select All"); setId("TextSelectAllActionHandler"); setEnabled(false); 303 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, "SelectAllHelpId"); } 306 307 public void runWithEvent(Event event) { 308 if (activeTextControl != null && !activeTextControl.isDisposed()) { 309 activeTextControl.selectAll(); 310 updateActionsEnableState(); 311 return; 312 } 313 if (selectAllAction != null) { 314 selectAllAction.runWithEvent(event); 315 return; 316 } 317 } 318 319 322 public void updateEnabledState() { 323 if (activeTextControl != null && !activeTextControl.isDisposed()) { 324 setEnabled(true); 325 return; 326 } 327 if (selectAllAction != null) { 328 setEnabled(selectAllAction.isEnabled()); 329 return; 330 } 331 setEnabled(false); 332 } 333 } 334 335 344 public TextActionHandler(IActionBars theActionBars) { 345 super(theActionBars); 346 actionBars = theActionBars; 347 updateActionBars(); 348 } 349 350 353 public void updateActionBars() { 354 actionBars.setGlobalActionHandler(ActionFactory.CUT.getId(), 355 textCutAction); 356 actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), 357 textCopyAction); 358 actionBars.setGlobalActionHandler(ActionFactory.PASTE.getId(), 359 textPasteAction); 360 actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), 361 textSelectAllAction); 362 actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(), 363 textDeleteAction); 364 } 365 366 373 public void addText(Text textControl) { 374 if (textControl == null) { 375 return; 376 } 377 378 activeTextControl = textControl; 379 textControl.addListener(SWT.Activate, textControlListener); 380 textControl.addListener(SWT.Deactivate, textControlListener); 381 382 textControl.addKeyListener(keyAdapter); 386 textControl.addMouseListener(mouseAdapter); 387 388 } 389 390 393 public void dispose() { 394 setCutAction(null); 395 setCopyAction(null); 396 setPasteAction(null); 397 setSelectAllAction(null); 398 setDeleteAction(null); 399 } 400 401 408 public void removeText(Text textControl) { 409 if (textControl == null) { 410 return; 411 } 412 413 textControl.removeListener(SWT.Activate, textControlListener); 414 textControl.removeListener(SWT.Deactivate, textControlListener); 415 416 textControl.removeMouseListener(mouseAdapter); 417 textControl.removeKeyListener(keyAdapter); 418 419 activeTextControl = null; 420 updateActionsEnableState(); 421 } 422 423 431 public void setCopyAction(IAction action) { 432 if (copyAction == action) { 433 return; 434 } 435 436 if (copyAction != null) { 437 copyAction.removePropertyChangeListener(copyActionListener); 438 } 439 440 copyAction = action; 441 442 if (copyAction != null) { 443 copyAction.addPropertyChangeListener(copyActionListener); 444 } 445 446 textCopyAction.updateEnabledState(); 447 } 448 449 457 public void setCutAction(IAction action) { 458 if (cutAction == action) { 459 return; 460 } 461 462 if (cutAction != null) { 463 cutAction.removePropertyChangeListener(cutActionListener); 464 } 465 466 cutAction = action; 467 468 if (cutAction != null) { 469 cutAction.addPropertyChangeListener(cutActionListener); 470 } 471 472 textCutAction.updateEnabledState(); 473 } 474 475 483 public void setPasteAction(IAction action) { 484 if (pasteAction == action) { 485 return; 486 } 487 488 if (pasteAction != null) { 489 pasteAction.removePropertyChangeListener(pasteActionListener); 490 } 491 492 pasteAction = action; 493 494 if (pasteAction != null) { 495 pasteAction.addPropertyChangeListener(pasteActionListener); 496 } 497 498 textPasteAction.updateEnabledState(); 499 } 500 501 509 public void setSelectAllAction(IAction action) { 510 if (selectAllAction == action) { 511 return; 512 } 513 514 if (selectAllAction != null) { 515 selectAllAction 516 .removePropertyChangeListener(selectAllActionListener); 517 } 518 519 selectAllAction = action; 520 521 if (selectAllAction != null) { 522 selectAllAction.addPropertyChangeListener(selectAllActionListener); 523 } 524 525 textSelectAllAction.updateEnabledState(); 526 } 527 528 536 public void setDeleteAction(IAction action) { 537 if (deleteAction == action) { 538 return; 539 } 540 541 if (deleteAction != null) { 542 deleteAction.removePropertyChangeListener(deleteActionListener); 543 } 544 545 deleteAction = action; 546 547 if (deleteAction != null) { 548 deleteAction.addPropertyChangeListener(deleteActionListener); 549 } 550 551 textDeleteAction.updateEnabledState(); 552 } 553 554 558 private void updateActionsEnableState() { 559 textCutAction.updateEnabledState(); 560 textCopyAction.updateEnabledState(); 561 textPasteAction.updateEnabledState(); 562 textSelectAllAction.updateEnabledState(); 563 textDeleteAction.updateEnabledState(); 564 } 565 } 566 | Popular Tags |