comparison dwtx/jface/action/ActionContributionItem.d @ 71:4878bef4a38e

Some fixing
author Frank Benoit <benoit@tionex.de>
date Thu, 22 May 2008 04:03:58 +0200
parents 46a6e0e6ccd4
children 5df4896124c7
comparison
equal deleted inserted replaced
70:46a6e0e6ccd4 71:4878bef4a38e
60 * This class may be instantiated; it is not intended to be subclassed. 60 * This class may be instantiated; it is not intended to be subclassed.
61 * </p> 61 * </p>
62 */ 62 */
63 public class ActionContributionItem : ContributionItem { 63 public class ActionContributionItem : ContributionItem {
64 alias ContributionItem.fill fill; 64 alias ContributionItem.fill fill;
65 65
66 /** 66 /**
67 * Mode bit: Show text on tool items or buttons, even if an image is 67 * Mode bit: Show text on tool items or buttons, even if an image is
68 * present. If this mode bit is not set, text is only shown on tool items if 68 * present. If this mode bit is not set, text is only shown on tool items if
69 * there is no image present. 69 * there is no image present.
70 * 70 *
296 if (action.getHelpListener() !is null) { 296 if (action.getHelpListener() !is null) {
297 mi.addHelpListener(action.getHelpListener()); 297 mi.addHelpListener(action.getHelpListener());
298 } 298 }
299 299
300 if (flags is DWT.CASCADE) { 300 if (flags is DWT.CASCADE) {
301 // just create a proxy for now, if the user shows it then 301 // just create a proxy for now, if the user shows it then
302 // fill it in 302 // fill it in
303 Menu subMenu = new Menu(parent); 303 Menu subMenu = new Menu(parent);
304 subMenu.addListener(DWT.Show, getMenuCreatorListener()); 304 subMenu.addListener(DWT.Show, getMenuCreatorListener());
305 subMenu.addListener(DWT.Hide, getMenuCreatorListener()); 305 subMenu.addListener(DWT.Hide, getMenuCreatorListener());
306 mi.setMenu(subMenu); 306 mi.setMenu(subMenu);
564 ExternalActionManager.IExecuteCallback callback = null; 564 ExternalActionManager.IExecuteCallback callback = null;
565 String actionDefinitionId = action.getActionDefinitionId(); 565 String actionDefinitionId = action.getActionDefinitionId();
566 if (actionDefinitionId !is null) { 566 if (actionDefinitionId !is null) {
567 Object obj = ExternalActionManager.getInstance() 567 Object obj = ExternalActionManager.getInstance()
568 .getCallback(); 568 .getCallback();
569 if (obj instanceof ExternalActionManager.IExecuteCallback) { 569 if (null !is cast(ExternalActionManager.IExecuteCallback)obj ) {
570 callback = (ExternalActionManager.IExecuteCallback) obj; 570 callback = cast(ExternalActionManager.IExecuteCallback) obj;
571 } 571 }
572 } 572 }
573 573
574 // Ensure action is enabled first. 574 // Ensure action is enabled first.
575 // See 1GAN3M6: ITPUI:WINNT - Any IAction in the workbench can be 575 // See 1GAN3M6: ITPUI:WINNT - Any IAction in the workbench can be
579 579
580 long ms = 0L; 580 long ms = 0L;
581 if (trace) { 581 if (trace) {
582 ms = System.currentTimeMillis(); 582 ms = System.currentTimeMillis();
583 Stdout.formatln("Running action: {}", action.getText()); //$NON-NLS-1$ 583 Stdout.formatln("Running action: {}", action.getText()); //$NON-NLS-1$
584 } 584 }
585 585
586 IPropertyChangeListener resultListener = null; 586 IPropertyChangeListener resultListener = null;
587 if (callback !is null) { 587 if (callback !is null) {
588 resultListener = new IPropertyChangeListener() { 588 resultListener = new class IPropertyChangeListener {
589 public void propertyChange(PropertyChangeEvent event) { 589 public void propertyChange(PropertyChangeEvent event) {
590 // Check on result 590 // Check on result
591 if (event.getProperty().equals(IAction.RESULT)) { 591 if (event.getProperty().equals(IAction.RESULT)) {
592 if (event.getNewValue() instanceof Boolean) { 592 if (null !is cast(Boolean)event.getNewValue() ) {
593 result = (Boolean) event.getNewValue(); 593 result = cast(Boolean) event.getNewValue();
594 } 594 }
595 } 595 }
596 } 596 }
597 }; 597 };
598 action.addPropertyChangeListener(resultListener); 598 action.addPropertyChangeListener(resultListener);
609 new ExecutionException(action.getText() 609 new ExecutionException(action.getText()
610 + " returned failure.")); //$NON-NLS-1$ 610 + " returned failure.")); //$NON-NLS-1$
611 } 611 }
612 } 612 }
613 613
614 if (resultListener!isnull) { 614 if (resultListener !is null) {
615 result = null; 615 result = null;
616 action.removePropertyChangeListener(resultListener); 616 action.removePropertyChangeListener(resultListener);
617 } 617 }
618 if (trace) { 618 if (trace) {
619 Stdout.formatln("{} ms to run action: {}",(System.currentTimeMillis() - ms), action.getText()); //$NON-NLS-1$ 619 Stdout.formatln("{} ms to run action: {}",(System.currentTimeMillis() - ms), action.getText()); //$NON-NLS-1$
1198 return textValue; 1198 return textValue;
1199 } 1199 }
1200 1200
1201 /* 1201 /*
1202 * (non-Javadoc) 1202 * (non-Javadoc)
1203 * 1203 *
1204 * @see dwtx.jface.action.ContributionItem#dispose() 1204 * @see dwtx.jface.action.ContributionItem#dispose()
1205 */ 1205 */
1206 public void dispose() { 1206 public void dispose() {
1207 if (widget !is null) { 1207 if (widget !is null) {
1208 widget.dispose(); 1208 widget.dispose();
1209 widget = null; 1209 widget = null;
1210 } 1210 }
1211 holdMenu = null; 1211 holdMenu = null;
1212 } 1212 }
1213 1213
1214 /** 1214 /**
1215 * Handle show and hide on the proxy menu for IAction.AS_DROP_DOWN_MENU 1215 * Handle show and hide on the proxy menu for IAction.AS_DROP_DOWN_MENU
1216 * actions. 1216 * actions.
1217 * 1217 *
1218 * @return the appropriate listener 1218 * @return the appropriate listener
1219 * @since 3.4 1219 * @since 3.4
1220 */ 1220 */
1221 private Listener getMenuCreatorListener() { 1221 private Listener getMenuCreatorListener() {
1222 if (menuCreatorListener is null) { 1222 if (menuCreatorListener is null) {
1223 menuCreatorListener = new Listener() { 1223 menuCreatorListener = new class Listener {
1224 public void handleEvent(Event event) { 1224 public void handleEvent(Event event) {
1225 switch (event.type) { 1225 switch (event.type) {
1226 case DWT.Show: 1226 case DWT.Show:
1227 handleShowProxy((Menu) event.widget); 1227 handleShowProxy(cast(Menu) event.widget);
1228 break; 1228 break;
1229 case DWT.Hide: 1229 case DWT.Hide:
1230 handleHideProxy((Menu) event.widget); 1230 handleHideProxy(cast(Menu) event.widget);
1231 break; 1231 break;
1232 } 1232 }
1233 } 1233 }
1234 }; 1234 };
1235 } 1235 }
1236 return menuCreatorListener; 1236 return menuCreatorListener;
1237 } 1237 }
1238 1238
1239 /** 1239 /**
1240 * This is the easiest way to hold the menu until we can swap it in to the 1240 * This is the easiest way to hold the menu until we can swap it in to the
1241 * proxy. 1241 * proxy.
1242 */ 1242 */
1243 private Menu holdMenu = null; 1243 private Menu holdMenu = null;
1244 1244
1245 private bool menuCreatorCalled = false; 1245 private bool menuCreatorCalled = false;
1246 1246
1247 /** 1247 /**
1248 * The proxy menu is being shown, we better get the real menu. 1248 * The proxy menu is being shown, we better get the real menu.
1249 * 1249 *
1250 * @param proxy 1250 * @param proxy
1251 * the proxy menu 1251 * the proxy menu
1252 * @since 3.4 1252 * @since 3.4
1253 */ 1253 */
1254 private void handleShowProxy(Menu proxy) { 1254 private void handleShowProxy(Menu proxy) {
1266 } 1266 }
1267 1267
1268 /** 1268 /**
1269 * Create MenuItems in the proxy menu that can execute the real menu items 1269 * Create MenuItems in the proxy menu that can execute the real menu items
1270 * if selected. Create proxy menus for any real item submenus. 1270 * if selected. Create proxy menus for any real item submenus.
1271 * 1271 *
1272 * @param realMenu 1272 * @param realMenu
1273 * the real menu to copy from 1273 * the real menu to copy from
1274 * @param proxy 1274 * @param proxy
1275 * the proxy menu to populate 1275 * the proxy menu to populate
1276 * @since 3.4 1276 * @since 3.4
1277 */ 1277 */
1278 private void copyMenu(Menu realMenu, Menu proxy) { 1278 private void copyMenu(Menu realMenu, Menu proxy) {
1279 if (realMenu.isDisposed() || proxy.isDisposed()) { 1279 if (realMenu.isDisposed() || proxy.isDisposed()) {
1280 return; 1280 return;
1281 } 1281 }
1282 1282
1283 // we notify the real menu so it can populate itself if it was 1283 // we notify the real menu so it can populate itself if it was
1284 // listening for DWT.Show 1284 // listening for DWT.Show
1285 realMenu.notifyListeners(DWT.Show, null); 1285 realMenu.notifyListeners(DWT.Show, null);
1286 1286
1287 final Listener passThrough = new Listener() { 1287 final Listener passThrough = new class Listener {
1288 public void handleEvent(Event event) { 1288 public void handleEvent(Event event) {
1289 if (!event.widget.isDisposed()) { 1289 if (!event.widget.isDisposed()) {
1290 Widget realItem = (Widget) event.widget.getData(); 1290 Widget realItem = cast(Widget) event.widget.getData();
1291 if (!realItem.isDisposed()) { 1291 if (!realItem.isDisposed()) {
1292 int style = event.widget.getStyle(); 1292 int style = event.widget.getStyle();
1293 if (event.type is DWT.Selection 1293 if (event.type is DWT.Selection
1294 && ((style & (DWT.TOGGLE | DWT.CHECK)) !is 0) 1294 && ((style & (DWT.TOGGLE | DWT.CHECK)) !is 0)
1295 && realItem instanceof MenuItem) { 1295 && (null !is cast(MenuItem)realItem )) {
1296 ((MenuItem) realItem) 1296 (cast(MenuItem) realItem)
1297 .setSelection(((MenuItem) event.widget) 1297 .setSelection((cast(MenuItem) event.widget)
1298 .getSelection()); 1298 .getSelection());
1299 } 1299 }
1300 event.widget = realItem; 1300 event.widget = realItem;
1301 realItem.notifyListeners(event.type, event); 1301 realItem.notifyListeners(event.type, event);
1302 } 1302 }
1324 if (itemMenu !is null) { 1324 if (itemMenu !is null) {
1325 // create a proxy for any sub menu items 1325 // create a proxy for any sub menu items
1326 final Menu subMenu = new Menu(proxy); 1326 final Menu subMenu = new Menu(proxy);
1327 subMenu.setData(itemMenu); 1327 subMenu.setData(itemMenu);
1328 proxyItem.setMenu(subMenu); 1328 proxyItem.setMenu(subMenu);
1329 subMenu.addListener(DWT.Show, new Listener() { 1329 subMenu.addListener(DWT.Show, dgListener( (Event event,Menu subMenu_,Menu itemMenu_) {
1330 public void handleEvent(Event event) { 1330 event.widget.removeListener(DWT.Show, this);
1331 event.widget.removeListener(DWT.Show, this); 1331 if (event.type is DWT.Show) {
1332 if (event.type is DWT.Show) { 1332 copyMenu(itemMenu_, subMenu_);
1333 copyMenu(itemMenu, subMenu); 1333 }
1334 } 1334 }, subMenu, itemMenu));
1335 }
1336 });
1337 subMenu.addListener(DWT.Help, passThrough); 1335 subMenu.addListener(DWT.Help, passThrough);
1338 subMenu.addListener(DWT.Hide, passThrough); 1336 subMenu.addListener(DWT.Hide, passThrough);
1339 } 1337 }
1340 } 1338 }
1341 } 1339 }
1342 1340
1343 /** 1341 /**
1344 * The proxy menu is being hidden, so we need to make it go away. 1342 * The proxy menu is being hidden, so we need to make it go away.
1345 * 1343 *
1346 * @param proxy 1344 * @param proxy
1347 * the proxy menu 1345 * the proxy menu
1348 * @since 3.4 1346 * @since 3.4
1349 */ 1347 */
1350 private void handleHideProxy(final Menu proxy) { 1348 private void handleHideProxy(Menu proxy) {
1351 proxy.removeListener(DWT.Hide, getMenuCreatorListener()); 1349 proxy.removeListener(DWT.Hide, getMenuCreatorListener());
1352 proxy.getDisplay().asyncExec(new Runnable() { 1350 proxy.getDisplay().asyncExec(dgRunnable( (Menu proxy_) {
1353 public void run() { 1351 if (!proxy_.isDisposed()) {
1354 if (!proxy.isDisposed()) { 1352 MenuItem parentItem = proxy_.getParentItem();
1355 MenuItem parentItem = proxy.getParentItem(); 1353 proxy_.dispose();
1356 proxy.dispose();
1357 parentItem.setMenu(holdMenu); 1354 parentItem.setMenu(holdMenu);
1358 } 1355 }
1359 if (holdMenu !is null && !holdMenu.isDisposed()) { 1356 if (holdMenu !is null && !holdMenu.isDisposed()) {
1360 holdMenu.notifyListeners(DWT.Hide, null); 1357 holdMenu.notifyListeners(DWT.Hide, null);
1361 } 1358 }
1362 holdMenu = null; 1359 holdMenu = null;
1363 } 1360 }, proxy ));
1364 }); 1361 }
1365 } 1362
1366
1367 /** 1363 /**
1368 * Return the widget associated with this contribution item. It should not 1364 * Return the widget associated with this contribution item. It should not
1369 * be cached, as it can be disposed and re-created by its containing 1365 * be cached, as it can be disposed and re-created by its containing
1370 * ContributionManager, which controls all of the widgets lifecycle methods. 1366 * ContributionManager, which controls all of the widgets lifecycle methods.
1371 * <p> 1367 * <p>
1372 * This can be used to set layout data on the widget if appropriate. The 1368 * This can be used to set layout data on the widget if appropriate. The
1373 * actual type of the widget can be any valid control for this 1369 * actual type of the widget can be any valid control for this
1374 * ContributionItem's current ContributionManager. 1370 * ContributionItem's current ContributionManager.
1375 * </p> 1371 * </p>
1376 * 1372 *
1377 * @return the widget, or <code>null</code> depending on the lifecycle. 1373 * @return the widget, or <code>null</code> depending on the lifecycle.
1378 * @since 3.4 1374 * @since 3.4
1379 */ 1375 */
1380 public Widget getWidget() { 1376 public Widget getWidget() {
1381 return widget; 1377 return widget;