1 11 12 13 package org.eclipse.ui.texteditor; 14 15 16 import java.util.ResourceBundle ; 17 18 import org.eclipse.swt.events.HelpEvent; 19 import org.eclipse.swt.events.HelpListener; 20 21 import org.eclipse.jface.action.IAction; 22 import org.eclipse.jface.util.IPropertyChangeListener; 23 import org.eclipse.jface.util.PropertyChangeEvent; 24 25 26 34 public final class RetargetTextEditorAction extends ResourceAction { 35 36 37 private IAction fAction; 38 39 private String fDefaultText; 40 44 private HelpListener fLocalHelpListener; 45 46 private IPropertyChangeListener fListener= new IPropertyChangeListener() { 47 public void propertyChange(PropertyChangeEvent event) { 48 update(event); 49 } 50 }; 51 52 72 public RetargetTextEditorAction(ResourceBundle bundle, String prefix, int style) { 73 super(bundle, prefix, style); 74 fDefaultText= getText(); 75 installHelpListener(); 76 } 77 78 90 public RetargetTextEditorAction(ResourceBundle bundle, String prefix) { 91 super(bundle, prefix); 92 fDefaultText= getText(); 93 installHelpListener(); 94 } 95 96 117 public RetargetTextEditorAction(ResourceBundle bundle, String prefix, String actionId, int style) { 118 super(bundle, prefix, style); 119 fDefaultText= getText(); 120 setId(actionId); 121 installHelpListener(); 122 } 123 124 138 public RetargetTextEditorAction(ResourceBundle bundle, String prefix, String actionId) { 139 super(bundle, prefix); 140 fDefaultText= getText(); 141 setId(actionId); 142 installHelpListener(); 143 } 144 145 150 private void update(PropertyChangeEvent event) { 151 if (ENABLED.equals(event.getProperty())) { 152 Boolean bool= (Boolean ) event.getNewValue(); 153 setEnabled(bool.booleanValue()); 154 } else if (TEXT.equals(event.getProperty())) 155 setText((String ) event.getNewValue()); 156 else if (TOOL_TIP_TEXT.equals(event.getProperty())) 157 setToolTipText((String ) event.getNewValue()); 158 else if (CHECKED.equals(event.getProperty())) { 159 Boolean bool= (Boolean ) event.getNewValue(); 160 setChecked(bool.booleanValue()); 161 } 162 } 163 164 169 public void setAction(IAction action) { 170 171 if (fAction != null) { 172 fAction.removePropertyChangeListener(fListener); 173 fAction= null; 174 } 175 176 fAction= action; 177 178 if (fAction == null) { 179 180 setEnabled(false); 181 if (getStyle() == AS_CHECK_BOX || getStyle() == AS_RADIO_BUTTON) 182 setChecked(false); 183 setText(fDefaultText); 184 setToolTipText(""); 186 } else { 187 188 setEnabled(fAction.isEnabled()); 189 if (fAction.getStyle() == AS_CHECK_BOX || fAction.getStyle() == AS_RADIO_BUTTON) 190 super.setChecked(fAction.isChecked()); 191 setText(fAction.getText()); 192 setToolTipText(fAction.getToolTipText()); 193 fAction.addPropertyChangeListener(fListener); 194 } 195 } 196 197 202 private void installHelpListener() { 203 super.setHelpListener(new HelpListener() { 204 public void helpRequested(HelpEvent e) { 205 HelpListener listener= null; 206 if (fAction != null) { 207 listener= fAction.getHelpListener(); 209 if (listener == null) 210 listener= fLocalHelpListener; 212 } 213 if (listener != null) 214 listener.helpRequested(e); 216 } 217 }); 218 } 219 220 228 public void setHelpListener(HelpListener listener) { 229 fLocalHelpListener= listener; 230 } 231 232 235 public void run() { 236 if (fAction != null) 237 fAction.run(); 238 } 239 } 240 | Popular Tags |