1 11 package org.eclipse.ui.actions; 12 13 import org.eclipse.jface.action.IAction; 14 import org.eclipse.jface.action.LegacyActionTools; 15 import org.eclipse.jface.resource.ImageDescriptor; 16 import org.eclipse.jface.util.PropertyChangeEvent; 17 18 28 public class LabelRetargetAction extends RetargetAction { 29 private String defaultText; 30 31 private String defaultToolTipText; 32 33 private ImageDescriptor defaultHoverImage; 34 35 private ImageDescriptor defaultImage; 36 37 private ImageDescriptor defaultDisabledImage; 38 39 private String acceleratorText; 40 41 47 public LabelRetargetAction(String actionID, String text) { 48 this(actionID, text, IAction.AS_UNSPECIFIED); 49 } 50 51 61 public LabelRetargetAction(String actionID, String text, int style) { 62 super(actionID, text, style); 63 this.defaultText = text; 64 this.defaultToolTipText = text; 65 acceleratorText = LegacyActionTools.extractAcceleratorText(text); 66 } 67 68 71 protected void propagateChange(PropertyChangeEvent event) { 72 super.propagateChange(event); 73 String prop = event.getProperty(); 74 if (prop.equals(IAction.TEXT)) { 75 String str = (String ) event.getNewValue(); 76 super.setText(appendAccelerator(str)); 77 } else if (prop.equals(IAction.TOOL_TIP_TEXT)) { 78 String str = (String ) event.getNewValue(); 79 super.setToolTipText(str); 80 } else if (prop.equals(IAction.IMAGE)) { 81 updateImages(getActionHandler()); 82 } 83 } 84 85 88 protected void setActionHandler(IAction handler) { 89 super.setActionHandler(handler); 91 92 if (handler == null) { 94 super.setText(defaultText); 95 super.setToolTipText(defaultToolTipText); 96 } else { 97 String handlerText = handler.getText(); 99 if (handlerText == null || handlerText.length() == 0) { 100 handlerText = defaultText; 101 } 102 super.setText(appendAccelerator(handlerText)); 103 super.setToolTipText(handler.getToolTipText()); 104 } 105 updateImages(handler); 106 } 107 108 111 public void setDisabledImageDescriptor(ImageDescriptor image) { 112 super.setDisabledImageDescriptor(image); 113 defaultDisabledImage = image; 114 } 115 116 119 public void setHoverImageDescriptor(ImageDescriptor image) { 120 super.setHoverImageDescriptor(image); 121 defaultHoverImage = image; 122 } 123 124 127 public void setImageDescriptor(ImageDescriptor image) { 128 super.setImageDescriptor(image); 129 defaultImage = image; 130 } 131 132 135 public void setText(String text) { 136 super.setText(text); 137 acceleratorText = LegacyActionTools.extractAcceleratorText(text); 138 defaultText = text; 139 } 140 141 145 public void setToolTipText(String text) { 146 super.setToolTipText(text); 147 defaultToolTipText = text; 148 } 149 150 154 private String appendAccelerator(String newText) { 155 if (newText == null) { 156 return null; 157 } 158 159 String str = removeAcceleratorText(newText); 161 if (acceleratorText != null) { 163 str = str + acceleratorText; 164 } 165 return str; 166 } 167 168 171 private void updateImages(IAction handler) { 172 if (handler == null) { 173 super.setHoverImageDescriptor(defaultHoverImage); 174 super.setImageDescriptor(defaultImage); 175 super.setDisabledImageDescriptor(defaultDisabledImage); 176 } else { 177 ImageDescriptor hoverImage = handler.getHoverImageDescriptor(); 179 ImageDescriptor image = handler.getImageDescriptor(); 180 ImageDescriptor disabledImage = handler 181 .getDisabledImageDescriptor(); 182 if (hoverImage != null || image != null || disabledImage != null) { 183 super.setHoverImageDescriptor(hoverImage); 184 super.setImageDescriptor(image); 185 super.setDisabledImageDescriptor(disabledImage); 186 } else { 187 super.setHoverImageDescriptor(defaultHoverImage); 188 super.setImageDescriptor(defaultImage); 189 super.setDisabledImageDescriptor(defaultDisabledImage); 190 } 191 } 192 } 193 194 } 195 | Popular Tags |