1 11 package org.eclipse.ui.internal.menus; 12 13 import org.eclipse.core.commands.common.NotDefinedException; 14 import org.eclipse.jface.menus.IWidget; 15 import org.eclipse.jface.util.PropertyChangeEvent; 16 17 35 public final class SWidget extends MenuElement { 36 37 41 public static final String PROPERTY_WIDGET = "WIDGET"; 43 47 private SLayout layout; 48 49 53 private IWidget thirdPartyCode; 54 55 62 SWidget(final String id) { 63 super(id); 64 } 65 66 80 public final void define(final IWidget widget, final SLocation location) { 81 final SLocation[] locations; 82 if (location == null) { 83 locations = null; 84 } else { 85 locations = new SLocation[] { location }; 86 } 87 define(widget, locations); 88 } 89 90 104 public final void define(final IWidget widget, final SLocation[] locations) { 105 define(widget, locations, new SLayout()); 106 } 107 108 124 public final void define(final IWidget widget, final SLocation[] locations, 125 SLayout layout) { 126 if (widget == null) { 127 throw new NullPointerException ( 128 "A widget needs a class to contribute the widgets"); } 130 131 setDefined(true); 132 setLocations(locations); 133 setLayout(layout); 134 setWidget(widget); 135 } 136 137 143 public final SLayout getLayout() throws NotDefinedException { 144 if (!isDefined()) { 145 throw new NotDefinedException( 146 "Cannot get the layout from an undefined widget"); } 148 149 return layout; 150 } 151 152 159 public final IWidget getWidget() throws NotDefinedException { 160 if (!isDefined()) { 161 throw new NotDefinedException( 162 "Cannot get the widget class from an undefined widget"); } 164 165 return thirdPartyCode; 166 } 167 168 176 private final void setLayout(final SLayout layout) { 177 this.layout = layout; 178 } 179 180 187 protected final void setWidget(final IWidget widget) { 188 PropertyChangeEvent event = null; 189 if (isListenerAttached()) { 190 event = new PropertyChangeEvent(this, PROPERTY_WIDGET, 191 this.thirdPartyCode, widget); 192 } 193 this.thirdPartyCode = widget; 194 firePropertyChangeEvent(event); 195 } 196 197 203 public final String toString() { 204 if (string == null) { 205 final StringBuffer stringBuffer = new StringBuffer (); 206 stringBuffer.append("SWidget("); stringBuffer.append(id); 208 stringBuffer.append(','); 209 stringBuffer.append(locations); 210 stringBuffer.append(','); 211 try { 212 stringBuffer.append(thirdPartyCode); 213 } catch (final Exception e) { 214 stringBuffer.append(e.getClass().getName()); 216 } 217 stringBuffer.append(','); 218 stringBuffer.append(defined); 219 stringBuffer.append(')'); 220 string = stringBuffer.toString(); 221 } 222 return string; 223 } 224 225 230 public final void undefine() { 231 string = null; 232 233 setWidget(null); 234 setLocations(null); 235 setLayout(null); 236 setDefined(false); 237 } 238 } 239 | Popular Tags |