1 11 package org.eclipse.ui.internal.navigator; 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.widgets.Event; 23 import org.eclipse.swt.widgets.Listener; 24 import org.eclipse.swt.widgets.Text; 25 import org.eclipse.ui.IActionBars; 26 import org.eclipse.ui.PlatformUI; 27 import org.eclipse.ui.actions.ActionFactory; 28 29 45 public class TextActionHandler { 46 private DeleteActionHandler textDeleteAction = new DeleteActionHandler(); 47 48 private CutActionHandler textCutAction = new CutActionHandler(); 49 50 private CopyActionHandler textCopyAction = new CopyActionHandler(); 51 52 private PasteActionHandler textPasteAction = new PasteActionHandler(); 53 54 private SelectAllActionHandler textSelectAllAction = new SelectAllActionHandler(); 55 56 private IAction deleteAction; 57 58 private IAction cutAction; 59 60 private IAction copyAction; 61 62 private IAction pasteAction; 63 64 private IAction selectAllAction; 65 66 private IPropertyChangeListener deleteActionListener = new PropertyChangeListener( 67 textDeleteAction); 68 69 private IPropertyChangeListener cutActionListener = new PropertyChangeListener( 70 textCutAction); 71 72 private IPropertyChangeListener copyActionListener = new PropertyChangeListener( 73 textCopyAction); 74 75 private IPropertyChangeListener pasteActionListener = new PropertyChangeListener( 76 textPasteAction); 77 78 private IPropertyChangeListener selectAllActionListener = new PropertyChangeListener( 79 textSelectAllAction); 80 81 private Listener textControlListener = new TextControlListener(); 82 83 private Text activeTextControl; 84 85 private MouseAdapter mouseAdapter = new MouseAdapter() { 86 public void mouseUp(MouseEvent e) { 87 updateActionsEnableState(); 88 } 89 }; 90 91 private KeyAdapter keyAdapter = new KeyAdapter() { 92 public void keyReleased(KeyEvent e) { 93 updateActionsEnableState(); 94 } 95 }; 96 97 private class TextControlListener implements Listener { 98 public void handleEvent(Event event) { 99 switch (event.type) { 100 case SWT.Activate: 101 activeTextControl = (Text) event.widget; 102 updateActionsEnableState(); 103 break; 104 case SWT.Deactivate: 105 activeTextControl = null; 106 updateActionsEnableState(); 107 break; 108 default: 109 break; 110 } 111 } 112 } 113 114 private class PropertyChangeListener implements IPropertyChangeListener { 115 private IAction actionHandler; 116 117 protected PropertyChangeListener(IAction actionHandler) { 118 super(); 119 this.actionHandler = actionHandler; 120 } 121 122 public void propertyChange(PropertyChangeEvent event) { 123 if (activeTextControl != null) { 124 return; 125 } 126 if (event.getProperty().equals(IAction.ENABLED)) { 127 Boolean bool = (Boolean ) event.getNewValue(); 128 actionHandler.setEnabled(bool.booleanValue()); 129 } 130 } 131 } 132 133 private class DeleteActionHandler extends Action { 134 protected DeleteActionHandler() { 135 super(CommonNavigatorMessages.Delete); 136 setId("TextDeleteActionHandler"); setEnabled(false); 138 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, 139 INavigatorHelpContextIds.TEXT_DELETE_ACTION); 140 } 141 142 public void runWithEvent(Event event) { 143 if (activeTextControl != null && !activeTextControl.isDisposed()) { 144 activeTextControl.clearSelection(); 145 return; 146 } 147 if (deleteAction != null) { 148 deleteAction.runWithEvent(event); 149 return; 150 } 151 } 152 153 156 public void updateEnabledState() { 157 if (activeTextControl != null && !activeTextControl.isDisposed()) { 158 setEnabled(activeTextControl.getSelectionCount() > 0 159 || activeTextControl.getCaretPosition() < activeTextControl 160 .getCharCount()); 161 return; 162 } 163 if (deleteAction != null) { 164 setEnabled(deleteAction.isEnabled()); 165 return; 166 } 167 setEnabled(false); 168 } 169 } 170 171 private class CutActionHandler extends Action { 172 protected CutActionHandler() { 173 super(CommonNavigatorMessages.Cut); 174 setId("TextCutActionHandler"); setEnabled(false); 176 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, 177 INavigatorHelpContextIds.TEXT_CUT_ACTION); 178 } 179 180 public void runWithEvent(Event event) { 181 if (activeTextControl != null && !activeTextControl.isDisposed()) { 182 activeTextControl.cut(); 183 return; 184 } 185 if (cutAction != null) { 186 cutAction.runWithEvent(event); 187 return; 188 } 189 } 190 191 194 public void updateEnabledState() { 195 if (activeTextControl != null && !activeTextControl.isDisposed()) { 196 setEnabled(activeTextControl.getSelectionCount() > 0); 197 return; 198 } 199 if (cutAction != null) { 200 setEnabled(cutAction.isEnabled()); 201 return; 202 } 203 setEnabled(false); 204 } 205 } 206 207 private class CopyActionHandler extends Action { 208 protected CopyActionHandler() { 209 super(CommonNavigatorMessages.Copy); 210 setId("TextCopyActionHandler"); setEnabled(false); 212 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, 213 INavigatorHelpContextIds.TEXT_COPY_ACTION); 214 } 215 216 public void runWithEvent(Event event) { 217 if (activeTextControl != null && !activeTextControl.isDisposed()) { 218 activeTextControl.copy(); 219 return; 220 } 221 if (copyAction != null) { 222 copyAction.runWithEvent(event); 223 return; 224 } 225 } 226 227 230 public void updateEnabledState() { 231 if (activeTextControl != null && !activeTextControl.isDisposed()) { 232 setEnabled(activeTextControl.getSelectionCount() > 0); 233 return; 234 } 235 if (copyAction != null) { 236 setEnabled(copyAction.isEnabled()); 237 return; 238 } 239 setEnabled(false); 240 } 241 } 242 243 private class PasteActionHandler extends Action { 244 protected PasteActionHandler() { 245 super(CommonNavigatorMessages.Paste); 246 setId("TextPasteActionHandler"); setEnabled(false); 248 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, 249 INavigatorHelpContextIds.TEXT_PASTE_ACTION); 250 } 251 252 public void runWithEvent(Event event) { 253 if (activeTextControl != null && !activeTextControl.isDisposed()) { 254 activeTextControl.paste(); 255 return; 256 } 257 if (pasteAction != null) { 258 pasteAction.runWithEvent(event); 259 return; 260 } 261 } 262 263 266 public void updateEnabledState() { 267 if (activeTextControl != null && !activeTextControl.isDisposed()) { 268 setEnabled(true); 269 return; 270 } 271 if (pasteAction != null) { 272 setEnabled(pasteAction.isEnabled()); 273 return; 274 } 275 setEnabled(false); 276 } 277 } 278 279 private class SelectAllActionHandler extends Action { 280 protected SelectAllActionHandler() { 281 super(CommonNavigatorMessages.TextAction_selectAll); 282 setId("TextSelectAllActionHandler"); setEnabled(false); 284 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, 285 INavigatorHelpContextIds.TEXT_SELECT_ALL_ACTION); 286 } 287 288 public void runWithEvent(Event event) { 289 if (activeTextControl != null && !activeTextControl.isDisposed()) { 290 activeTextControl.selectAll(); 291 return; 292 } 293 if (selectAllAction != null) { 294 selectAllAction.runWithEvent(event); 295 return; 296 } 297 } 298 299 302 public void updateEnabledState() { 303 if (activeTextControl != null && !activeTextControl.isDisposed()) { 304 setEnabled(true); 305 return; 306 } 307 if (selectAllAction != null) { 308 setEnabled(selectAllAction.isEnabled()); 309 return; 310 } 311 setEnabled(false); 312 } 313 } 314 315 324 public TextActionHandler(IActionBars actionBar) { 325 super(); 326 actionBar.setGlobalActionHandler(ActionFactory.CUT.getId(), 327 textCutAction); 328 actionBar.setGlobalActionHandler(ActionFactory.COPY.getId(), 329 textCopyAction); 330 actionBar.setGlobalActionHandler(ActionFactory.PASTE.getId(), 331 textPasteAction); 332 actionBar.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), 333 textSelectAllAction); 334 actionBar.setGlobalActionHandler(ActionFactory.DELETE.getId(), 335 textDeleteAction); 336 } 337 338 345 public void addText(Text textControl) { 346 if (textControl == null) { 347 return; 348 } 349 350 activeTextControl = textControl; 351 textControl.addListener(SWT.Activate, textControlListener); 352 textControl.addListener(SWT.Deactivate, textControlListener); 353 354 textControl.addKeyListener(keyAdapter); 358 textControl.addMouseListener(mouseAdapter); 359 360 } 361 362 365 public void dispose() { 366 setCutAction(null); 367 setCopyAction(null); 368 setPasteAction(null); 369 setSelectAllAction(null); 370 setDeleteAction(null); 371 } 372 373 380 public void removeText(Text textControl) { 381 if (textControl == null) { 382 return; 383 } 384 385 textControl.removeListener(SWT.Activate, textControlListener); 386 textControl.removeListener(SWT.Deactivate, textControlListener); 387 388 textControl.removeMouseListener(mouseAdapter); 389 textControl.removeKeyListener(keyAdapter); 390 391 activeTextControl = null; 392 updateActionsEnableState(); 393 } 394 395 403 public void setCopyAction(IAction action) { 404 if (copyAction == action) { 405 return; 406 } 407 408 if (copyAction != null) { 409 copyAction.removePropertyChangeListener(copyActionListener); 410 } 411 412 copyAction = action; 413 414 if (copyAction != null) { 415 copyAction.addPropertyChangeListener(copyActionListener); 416 } 417 418 textCopyAction.updateEnabledState(); 419 } 420 421 429 public void setCutAction(IAction action) { 430 if (cutAction == action) { 431 return; 432 } 433 434 if (cutAction != null) { 435 cutAction.removePropertyChangeListener(cutActionListener); 436 } 437 438 cutAction = action; 439 440 if (cutAction != null) { 441 cutAction.addPropertyChangeListener(cutActionListener); 442 } 443 444 textCutAction.updateEnabledState(); 445 } 446 447 455 public void setPasteAction(IAction action) { 456 if (pasteAction == action) { 457 return; 458 } 459 460 if (pasteAction != null) { 461 pasteAction.removePropertyChangeListener(pasteActionListener); 462 } 463 464 pasteAction = action; 465 466 if (pasteAction != null) { 467 pasteAction.addPropertyChangeListener(pasteActionListener); 468 } 469 470 textPasteAction.updateEnabledState(); 471 } 472 473 481 public void setSelectAllAction(IAction action) { 482 if (selectAllAction == action) { 483 return; 484 } 485 486 if (selectAllAction != null) { 487 selectAllAction 488 .removePropertyChangeListener(selectAllActionListener); 489 } 490 491 selectAllAction = action; 492 493 if (selectAllAction != null) { 494 selectAllAction.addPropertyChangeListener(selectAllActionListener); 495 } 496 497 textSelectAllAction.updateEnabledState(); 498 } 499 500 508 public void setDeleteAction(IAction action) { 509 if (deleteAction == action) { 510 return; 511 } 512 513 if (deleteAction != null) { 514 deleteAction.removePropertyChangeListener(deleteActionListener); 515 } 516 517 deleteAction = action; 518 519 if (deleteAction != null) { 520 deleteAction.addPropertyChangeListener(deleteActionListener); 521 } 522 523 textDeleteAction.updateEnabledState(); 524 } 525 526 530 private void updateActionsEnableState() { 531 textCutAction.updateEnabledState(); 532 textCopyAction.updateEnabledState(); 533 textPasteAction.updateEnabledState(); 534 textSelectAllAction.updateEnabledState(); 535 textDeleteAction.updateEnabledState(); 536 } 537 } 538 539 | Popular Tags |