comparison org.eclipse.jface/src/org/eclipse/jface/action/Action.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children 12b890a6392a
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
1 /*******************************************************************************
2 * Copyright (c) 2000, 2007 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module org.eclipse.jface.action.Action;
14
15 import org.eclipse.jface.action.AbstractAction;
16 import org.eclipse.jface.action.IAction;
17 import org.eclipse.jface.action.IMenuCreator;
18 import org.eclipse.jface.action.LegacyActionTools;
19
20 import org.eclipse.jface.util.IPropertyChangeListener;
21
22 import org.eclipse.swt.events.HelpListener;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.Event;
25 import org.eclipse.swt.widgets.Menu;
26 import org.eclipse.jface.resource.ImageDescriptor;
27
28 import java.lang.all;
29 import java.util.Set;
30
31 /**
32 * The standard abstract implementation of an action.
33 * <p>
34 * Subclasses must implement the <code>IAction.run</code> method to carry out
35 * the action's semantics.
36 * </p>
37 */
38 public abstract class Action : AbstractAction, IAction {
39
40 public void addPropertyChangeListener(IPropertyChangeListener listener) {
41 super.addPropertyChangeListener(listener);
42 }
43 public void removePropertyChangeListener( IPropertyChangeListener listener) {
44 super.removePropertyChangeListener(listener);
45 }
46
47
48 private static const IMenuCreator VAL_DROP_DOWN_MENU;
49 static this(){
50 VAL_DROP_DOWN_MENU = new class IMenuCreator {
51 public void dispose() {
52 // do nothing
53 }
54
55 public Menu getMenu(Control parent) {
56 // do nothing
57 return null;
58 }
59
60 public Menu getMenu(Menu parent) {
61 // do nothing
62 return null;
63 }
64 };
65 VAL_RADIO_BTN_OFF = new ValueWrapperInt(0);
66 VAL_RADIO_BTN_ON = new ValueWrapperInt(1);
67 VAL_TOGGLE_BTN_OFF = new ValueWrapperBool(false);
68 VAL_TOGGLE_BTN_ON = new ValueWrapperBool(true);
69 }
70
71 /*
72 * The list of default values the action can have. These values will
73 * determine the style of the action.
74 */
75 private static const String VAL_PUSH_BTN = "PUSH_BTN"; //$NON-NLS-1$
76
77 private static const ValueWrapperInt VAL_RADIO_BTN_OFF;
78
79 private static const ValueWrapperInt VAL_RADIO_BTN_ON;
80
81 private static const ValueWrapperBool VAL_TOGGLE_BTN_OFF;
82
83 private static const ValueWrapperBool VAL_TOGGLE_BTN_ON;
84
85 /**
86 * Converts an accelerator key code to a string representation.
87 *
88 * @param keyCode
89 * the key code to be translated
90 * @return a string representation of the key code
91 */
92 public static String convertAccelerator(int keyCode) {
93 return LegacyActionTools.convertAccelerator(keyCode);
94 }
95
96 /**
97 * Parses the given accelerator text, and converts it to an accelerator key
98 * code.
99 *
100 * @param acceleratorText
101 * the accelerator text
102 * @return the SWT key code, or 0 if there is no accelerator
103 */
104 public static int convertAccelerator(String acceleratorText) {
105 return LegacyActionTools.convertAccelerator(acceleratorText);
106 }
107
108 /**
109 * Maps a standard keyboard key name to an SWT key code. Key names are
110 * converted to upper case before comparison. If the key name is a single
111 * letter, for example "S", its character code is returned.
112 * <p>
113 * The following key names are known (case is ignored):
114 * <ul>
115 * <li><code>"BACKSPACE"</code></li>
116 * <li><code>"TAB"</code></li>
117 * <li><code>"RETURN"</code></li>
118 * <li><code>"ENTER"</code></li>
119 * <li><code>"ESC"</code></li>
120 * <li><code>"ESCAPE"</code></li>
121 * <li><code>"DELETE"</code></li>
122 * <li><code>"SPACE"</code></li>
123 * <li><code>"ARROW_UP"</code>, <code>"ARROW_DOWN"</code>,
124 * <code>"ARROW_LEFT"</code>, and <code>"ARROW_RIGHT"</code></li>
125 * <li><code>"PAGE_UP"</code> and <code>"PAGE_DOWN"</code></li>
126 * <li><code>"HOME"</code></li>
127 * <li><code>"END"</code></li>
128 * <li><code>"INSERT"</code></li>
129 * <li><code>"F1"</code>, <code>"F2"</code> through <code>"F12"</code></li>
130 * </ul>
131 * </p>
132 *
133 * @param token
134 * the key name
135 * @return the SWT key code, <code>-1</code> if no match was found
136 * @see org.eclipse.swt.SWT
137 */
138 public static int findKeyCode(String token) {
139 return LegacyActionTools.findKeyCode(token);
140 }
141
142 /**
143 * Maps an SWT key code to a standard keyboard key name. The key code is
144 * stripped of modifiers (SWT.CTRL, SWT.ALT, SWT.SHIFT, and SWT.COMMAND). If
145 * the key code is not an SWT code (for example if it a key code for the key
146 * 'S'), a string containing a character representation of the key code is
147 * returned.
148 *
149 * @param keyCode
150 * the key code to be translated
151 * @return the string representation of the key code
152 * @see org.eclipse.swt.SWT
153 * @since 2.0
154 */
155 public static String findKeyString(int keyCode) {
156 return LegacyActionTools.findKeyString(keyCode);
157 }
158
159 /**
160 * Maps standard keyboard modifier key names to the corresponding SWT
161 * modifier bit. The following modifier key names are recognized (case is
162 * ignored): <code>"CTRL"</code>, <code>"SHIFT"</code>,
163 * <code>"ALT"</code>, and <code>"COMMAND"</code>. The given modifier
164 * key name is converted to upper case before comparison.
165 *
166 * @param token
167 * the modifier key name
168 * @return the SWT modifier bit, or <code>0</code> if no match was found
169 * @see org.eclipse.swt.SWT
170 */
171 public static int findModifier(String token) {
172 return LegacyActionTools.findModifier(token);
173 }
174
175 /**
176 * Returns a string representation of an SWT modifier bit (SWT.CTRL,
177 * SWT.ALT, SWT.SHIFT, and SWT.COMMAND). Returns <code>null</code> if the
178 * key code is not an SWT modifier bit.
179 *
180 * @param keyCode
181 * the SWT modifier bit to be translated
182 * @return the string representation of the SWT modifier bit, or
183 * <code>null</code> if the key code was not an SWT modifier bit
184 * @see org.eclipse.swt.SWT
185 * @since 2.0
186 */
187 public static String findModifierString(int keyCode) {
188 return LegacyActionTools.findModifierString(keyCode);
189 }
190
191 /**
192 * Convenience method for removing any optional accelerator text from the
193 * given string. The accelerator text appears at the end of the text, and is
194 * separated from the main part by a single tab character <code>'\t'</code>.
195 *
196 * @param text
197 * the text
198 * @return the text sans accelerator
199 */
200 public static String removeAcceleratorText(String text) {
201 return LegacyActionTools.removeAcceleratorText(text);
202 }
203
204 /**
205 * Convenience method for removing any mnemonics from the given string. For
206 * example, <code>removeMnemonics("&Open")</code> will return
207 * <code>"Open"</code>.
208 *
209 * @param text
210 * the text
211 * @return the text sans mnemonics
212 *
213 * @since 3.0
214 */
215 public static String removeMnemonics(String text) {
216 return LegacyActionTools.removeMnemonics(text);
217 }
218
219 /**
220 * This action's accelerator; <code>0</code> means none.
221 */
222 private int accelerator = 0;
223
224 /**
225 * This action's action definition id, or <code>null</code> if none.
226 */
227 private String actionDefinitionId;
228
229 /**
230 * This action's description, or <code>null</code> if none.
231 */
232 private String description;
233
234 /**
235 * This action's disabled image, or <code>null</code> if none.
236 */
237 private ImageDescriptor disabledImage;
238
239 /**
240 * Indicates this action is enabled.
241 */
242 private bool enabled = true;
243
244 /**
245 * An action's help listener, or <code>null</code> if none.
246 */
247 private HelpListener helpListener;
248
249 /**
250 * This action's hover image, or <code>null</code> if none.
251 */
252 private ImageDescriptor hoverImage;
253
254 /**
255 * This action's id, or <code>null</code> if none.
256 */
257 private String id;
258
259 /**
260 * This action's image, or <code>null</code> if none.
261 */
262 private ImageDescriptor image;
263
264 /**
265 * This action's text, or <code>null</code> if none.
266 */
267 private String text;
268
269 /**
270 * This action's tool tip text, or <code>null</code> if none.
271 */
272 private String toolTipText;
273
274 /**
275 * Holds the action's menu creator (an IMenuCreator) or checked state (a
276 * Boolean for toggle button, or an Integer for radio button), or
277 * <code>null</code> if neither have been set.
278 * <p>
279 * The value of this field affects the value of <code>getStyle()</code>.
280 * </p>
281 */
282 private Object value = null;
283
284 /**
285 * Creates a new action with no text and no image.
286 * <p>
287 * Configure the action later using the set methods.
288 * </p>
289 */
290 protected this() {
291 // do nothing
292 }
293
294 /**
295 * Creates a new action with the given text and no image. Calls the zero-arg
296 * constructor, then <code>setText</code>.
297 *
298 * @param text
299 * the string used as the text for the action, or
300 * <code>null</code> if there is no text
301 * @see #setText
302 */
303 protected this(String text) {
304 this();
305 setText(text);
306 }
307
308 /**
309 * Creates a new action with the given text and image. Calls the zero-arg
310 * constructor, then <code>setText</code> and
311 * <code>setImageDescriptor</code>.
312 *
313 * @param text
314 * the action's text, or <code>null</code> if there is no text
315 * @param image
316 * the action's image, or <code>null</code> if there is no
317 * image
318 * @see #setText
319 * @see #setImageDescriptor
320 */
321 protected this(String text, ImageDescriptor image) {
322 this(text);
323 setImageDescriptor(image);
324 }
325
326 /**
327 * Creates a new action with the given text and style.
328 *
329 * @param text
330 * the action's text, or <code>null</code> if there is no text
331 * @param style
332 * one of <code>AS_PUSH_BUTTON</code>,
333 * <code>AS_CHECK_BOX</code>, <code>AS_DROP_DOWN_MENU</code>,
334 * <code>AS_RADIO_BUTTON</code>, and
335 * <code>AS_UNSPECIFIED</code>.
336 */
337 protected this(String text, int style) {
338 this(text);
339 switch (style) {
340 case AS_PUSH_BUTTON:
341 value = stringcast(VAL_PUSH_BTN);
342 break;
343 case AS_CHECK_BOX:
344 value = VAL_TOGGLE_BTN_OFF;
345 break;
346 case AS_DROP_DOWN_MENU:
347 value = cast(Object)VAL_DROP_DOWN_MENU;
348 break;
349 case AS_RADIO_BUTTON:
350 value = VAL_RADIO_BTN_OFF;
351 break;
352 default:
353 }
354 }
355
356 /*
357 * (non-Javadoc) Method declared on IAction.
358 */
359 public int getAccelerator() {
360 return accelerator;
361 }
362
363 /*
364 * (non-Javadoc) Method declared on IAction.
365 *
366 */
367 public String getActionDefinitionId() {
368 return actionDefinitionId;
369 }
370
371 /*
372 * (non-Javadoc) Method declared on IAction.
373 */
374 public String getDescription() {
375 if (description !is null) {
376 return description;
377 }
378 return getToolTipText();
379 }
380
381 /*
382 * (non-Javadoc) Method declared on IAction.
383 */
384 public ImageDescriptor getDisabledImageDescriptor() {
385 return disabledImage;
386 }
387
388 /*
389 * (non-Javadoc) Method declared on IAction.
390 */
391 public HelpListener getHelpListener() {
392 return helpListener;
393 }
394
395 /*
396 * (non-Javadoc) Method declared on IAction.
397 */
398 public ImageDescriptor getHoverImageDescriptor() {
399 return hoverImage;
400 }
401
402 /*
403 * (non-Javadoc) Method declared on IAction.
404 */
405 public String getId() {
406 return id;
407 }
408
409 /*
410 * (non-Javadoc) Method declared on IAction.
411 */
412 public ImageDescriptor getImageDescriptor() {
413 return image;
414 }
415
416 /*
417 * (non-Javadoc) Method declared on IAction.
418 */
419 public IMenuCreator getMenuCreator() {
420 // The default drop down menu value is only used
421 // to mark this action requested style. So do not
422 // return it. For backward compatibility reasons.
423 if (value is cast(Object)VAL_DROP_DOWN_MENU) {
424 return null;
425 }
426 if ( auto v = cast(IMenuCreator) value ) {
427 return v;
428 }
429 return null;
430 }
431
432 /*
433 * (non-Javadoc) Method declared on IAction.
434 */
435 public int getStyle() {
436 // Infer the style from the value field.
437 if (value is stringcast(VAL_PUSH_BTN) || value is null) {
438 return AS_PUSH_BUTTON;
439 }
440 if (value is VAL_TOGGLE_BTN_ON || value is VAL_TOGGLE_BTN_OFF) {
441 return AS_CHECK_BOX;
442 }
443 if (value is VAL_RADIO_BTN_ON || value is VAL_RADIO_BTN_OFF) {
444 return AS_RADIO_BUTTON;
445 }
446 if (cast(IMenuCreator)value ) {
447 return AS_DROP_DOWN_MENU;
448 }
449
450 // We should never get to this line...
451 return AS_PUSH_BUTTON;
452 }
453
454 /*
455 * (non-Javadoc) Method declared on IAction.
456 */
457 public String getText() {
458 return text;
459 }
460
461 /*
462 * (non-Javadoc) Method declared on IAction.
463 */
464 public String getToolTipText() {
465 return toolTipText;
466 }
467
468 /*
469 * (non-Javadoc) Method declared on IAction.
470 */
471 public bool isChecked() {
472 return value is VAL_TOGGLE_BTN_ON || value is VAL_RADIO_BTN_ON;
473 }
474
475 /*
476 * (non-Javadoc) Method declared on IAction.
477 */
478 public bool isEnabled() {
479 return enabled;
480 }
481
482 /*
483 * (non-Javadoc) Method declared on IAction.
484 */
485 public bool isHandled() {
486 return true;
487 }
488
489 /**
490 * Reports the outcome of the running of this action via the
491 * {@link IAction#RESULT} property.
492 *
493 * @param success
494 * <code>true</code> if the action succeeded and
495 * <code>false</code> if the action failed or was not completed
496 * @see IAction#RESULT
497 * @since 3.0
498 */
499 public final void notifyResult(bool success) {
500 // avoid Boolean.valueOf(bool) to allow compilation against JCL
501 // Foundation (bug 80059)
502 firePropertyChange(RESULT, null, success ? Boolean.TRUE : Boolean.FALSE);
503 }
504
505 /**
506 * The default implementation of this <code>IAction</code> method does
507 * nothing. Subclasses should override this method if they do not need
508 * information from the triggering event, or override
509 * <code>runWithEvent(Event)</code> if they do.
510 */
511 public void run() {
512 // do nothing
513 }
514
515 /**
516 * The default implementation of this <code>IAction</code> method ignores
517 * the event argument, and simply calls <code>run()</code>. Subclasses
518 * should override this method if they need information from the triggering
519 * event, or override <code>run()</code> if not.
520 *
521 * @param event
522 * the SWT event which triggered this action being run
523 * @since 2.0
524 */
525 public void runWithEvent(Event event) {
526 run();
527 }
528
529 /*
530 * @see IAction#setAccelerator(int)
531 */
532 public void setAccelerator(int keycode) {
533 this.accelerator = keycode;
534 }
535
536 /*
537 * (non-Javadoc) Method declared on IAction.
538 */
539 public void setActionDefinitionId(String id) {
540 actionDefinitionId = id;
541 }
542
543 /*
544 * (non-Javadoc) Method declared on IAction.
545 */
546 public void setChecked(bool checked) {
547 Object newValue = null;
548
549 // For backward compatibility, if the style is not
550 // set yet, then convert it to a toggle button.
551 if (value is null || value is VAL_TOGGLE_BTN_ON
552 || value is VAL_TOGGLE_BTN_OFF) {
553 newValue = checked ? VAL_TOGGLE_BTN_ON : VAL_TOGGLE_BTN_OFF;
554 } else if (value is VAL_RADIO_BTN_ON || value is VAL_RADIO_BTN_OFF) {
555 newValue = checked ? VAL_RADIO_BTN_ON : VAL_RADIO_BTN_OFF;
556 } else {
557 // Some other style already, so do nothing.
558 return;
559 }
560
561 if (newValue !is value) {
562 value = newValue;
563 if (checked) {
564 firePropertyChange(CHECKED, Boolean.FALSE, Boolean.TRUE);
565 } else {
566 firePropertyChange(CHECKED, Boolean.TRUE, Boolean.FALSE);
567 }
568 }
569 }
570
571 /*
572 * (non-Javadoc) Method declared on IAction.
573 */
574 public void setDescription(String text) {
575
576 if ((description is null && text !is null)
577 || (description !is null && text is null)
578 || (description !is null && text !is null && !text
579 .equals(description))) {
580 String oldDescription = description;
581 description = text;
582 firePropertyChange(DESCRIPTION, stringcast(oldDescription), stringcast(description));
583 }
584 }
585
586 /*
587 * (non-Javadoc) Method declared on IAction.
588 */
589 public void setDisabledImageDescriptor(ImageDescriptor newImage) {
590 if (disabledImage !is newImage) {
591 ImageDescriptor oldImage = disabledImage;
592 disabledImage = newImage;
593 firePropertyChange(IMAGE, oldImage, newImage);
594 }
595 }
596
597 /*
598 * (non-Javadoc) Method declared on IAction.
599 */
600 public void setEnabled(bool enabled) {
601 if (enabled !is this.enabled) {
602 Boolean oldVal = this.enabled ? Boolean.TRUE : Boolean.FALSE;
603 Boolean newVal = enabled ? Boolean.TRUE : Boolean.FALSE;
604 this.enabled = enabled;
605 firePropertyChange(ENABLED, oldVal, newVal);
606 }
607 }
608
609 /*
610 * (non-Javadoc) Method declared on IAction.
611 */
612 public void setHelpListener(HelpListener listener) {
613 helpListener = listener;
614 }
615
616 /*
617 * (non-Javadoc) Method declared on IAction.
618 */
619 public void setHoverImageDescriptor(ImageDescriptor newImage) {
620 if (hoverImage !is newImage) {
621 ImageDescriptor oldImage = hoverImage;
622 hoverImage = newImage;
623 firePropertyChange(IMAGE, oldImage, newImage);
624 }
625 }
626
627 /*
628 * (non-Javadoc) Method declared on IAction.
629 */
630 public void setId(String id) {
631 this.id = id;
632 }
633
634 /*
635 * (non-Javadoc) Method declared on IAction.
636 */
637 public void setImageDescriptor(ImageDescriptor newImage) {
638 if (image !is newImage) {
639 ImageDescriptor oldImage = image;
640 image = newImage;
641 firePropertyChange(IMAGE, oldImage, newImage);
642 }
643 }
644
645 /**
646 * Sets the menu creator for this action.
647 * <p>
648 * Note that if this method is called, it overrides the check status.
649 * </p>
650 *
651 * @param creator
652 * the menu creator, or <code>null</code> if none
653 */
654 public void setMenuCreator(IMenuCreator creator) {
655 // For backward compatibility, if the style is not
656 // set yet, then convert it to a drop down menu.
657 if (value is null) {
658 value = cast(Object)creator;
659 return;
660 }
661
662 if ( cast(IMenuCreator)value ) {
663 value = creator is null ? cast(Object)VAL_DROP_DOWN_MENU : cast(Object)creator;
664 }
665 }
666
667 /**
668 * Sets the text for this action.
669 * <p>
670 * Fires a property change event for the <code>TEXT</code> property if the
671 * text actually changes as a consequence.
672 * </p>
673 * <p>
674 * The accelerator is identified by the last index of a tab character. If
675 * there are no tab characters, then it is identified by the last index of a
676 * '@' character. If neither, then there is no accelerator text. Note that
677 * if you want to insert a '@' character into the text (but no accelerator,
678 * you can simply insert a '@' or a tab at the end of the text.
679 * </p>
680 *
681 * @param text
682 * the text, or <code>null</code> if none
683 */
684 public void setText(String text) {
685 String oldText = this.text;
686 int oldAccel = this.accelerator;
687 this.text = text;
688 if (text !is null) {
689 String acceleratorText = LegacyActionTools
690 .extractAcceleratorText(text);
691 if (acceleratorText !is null) {
692 int newAccelerator = LegacyActionTools
693 .convertLocalizedAccelerator(acceleratorText);
694 // Be sure to not wipe out the accelerator if nothing found
695 if (newAccelerator > 0) {
696 setAccelerator(newAccelerator);
697 }
698 }
699 }
700 if (!(this.accelerator is oldAccel && (oldText is null ? this.text is null
701 : oldText.equals(this.text)))) {
702 firePropertyChange(TEXT, stringcast(oldText), stringcast(this.text));
703 }
704 }
705
706 /**
707 * Sets the tool tip text for this action.
708 * <p>
709 * Fires a property change event for the <code>TOOL_TIP_TEXT</code>
710 * property if the tool tip text actually changes as a consequence.
711 * </p>
712 *
713 * @param toolTipText
714 * the tool tip text, or <code>null</code> if none
715 */
716 public void setToolTipText(String toolTipText) {
717 String oldToolTipText = this.toolTipText;
718 if (!(oldToolTipText is null ? toolTipText is null : oldToolTipText
719 .equals(toolTipText))) {
720 this.toolTipText = toolTipText;
721 firePropertyChange(TOOL_TIP_TEXT, stringcast(oldToolTipText), stringcast(toolTipText));
722 }
723 }
724
725 }