X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.utils.ui%2Fsrc%2Forg%2Fsimantics%2Futils%2Fui%2Fjface%2FActionMenuItemAdapter.java;h=08d589779bca22175a98fba2b5dadf1a5b13a8a2;hb=2161d658d9d7f6a5bd68bd2d629dbfaa14fc3be0;hp=f53df946d3e6fa7177089463ba4fc84ae25e5b15;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/jface/ActionMenuItemAdapter.java b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/jface/ActionMenuItemAdapter.java index f53df946d..08d589779 100644 --- a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/jface/ActionMenuItemAdapter.java +++ b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/jface/ActionMenuItemAdapter.java @@ -1,97 +1,97 @@ -/******************************************************************************* - * Copyright (c) 2007, 2010 Association for Decentralized Information Management - * in Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -/* - * Created on 5.10.2005 - * @author Toni Kalajainen - */ -package org.simantics.utils.ui.jface; - -import org.eclipse.jface.action.Action; -import org.eclipse.jface.util.IPropertyChangeListener; -import org.eclipse.jface.util.PropertyChangeEvent; -import org.eclipse.swt.SWT; -import org.eclipse.swt.events.DisposeEvent; -import org.eclipse.swt.events.DisposeListener; -import org.eclipse.swt.events.SelectionEvent; -import org.eclipse.swt.events.SelectionListener; -import org.eclipse.swt.widgets.Menu; -import org.eclipse.swt.widgets.MenuItem; - -/** - * JFace Action -> SWT's MenuItem adapter. - *
- * So you have a SWT's MainMenu and you want to add Actions and - * for some reason you can't use JFace MenuManager. - * This adapter "converts" Actions into MenuItems. - */ -public class ActionMenuItemAdapter implements SelectionListener, IPropertyChangeListener { - - private final Action action; - private final MenuItem menuitem; - - public static MenuItem adapt(Action action, Menu parent, int style, int index) { - int _style = _getActionStyle(action) | style; - MenuItem mi = new MenuItem(parent, _style, index); - new ActionMenuItemAdapter(action, mi); - return mi; - } - - public static MenuItem adapt(Action action, Menu parent, int style) { - int _style = _getActionStyle(action) | style; - MenuItem mi = new MenuItem(parent, _style); - new ActionMenuItemAdapter(action, mi); - return mi; - } - - private ActionMenuItemAdapter(Action action, MenuItem menuitem) { - this.menuitem = menuitem; - this.action = action; - - menuitem.setText(action.getText()); - if (action.getImageDescriptor() != null) - menuitem.setImage(action.getImageDescriptor().createImage()); - menuitem.setSelection(action.isChecked()); - menuitem.addSelectionListener(this); - action.addPropertyChangeListener(this); - menuitem.addDisposeListener(new DisposeListener() { - public void widgetDisposed(DisposeEvent e) { - ActionMenuItemAdapter.this.action.removePropertyChangeListener(ActionMenuItemAdapter.this); - ActionMenuItemAdapter.this.menuitem.removeDisposeListener(this); - }}); - } - - - private static int _getActionStyle(Action action) { - int style = SWT.PUSH; - if ((action.getStyle() & Action.AS_CHECK_BOX) > 0) - style = SWT.CHECK; - if ((action.getStyle() & Action.AS_RADIO_BUTTON) > 0) - style = SWT.RADIO; - return style; - } - - public void widgetSelected(SelectionEvent e) { - action.run(); - } - - public void widgetDefaultSelected(SelectionEvent e) { - } - - public Action getAction() { - return action; - } - - public void propertyChange(PropertyChangeEvent event) { - menuitem.setSelection(action.isChecked()); - } - -} +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +/* + * Created on 5.10.2005 + * @author Toni Kalajainen + */ +package org.simantics.utils.ui.jface; + +import org.eclipse.jface.action.Action; +import org.eclipse.jface.util.IPropertyChangeListener; +import org.eclipse.jface.util.PropertyChangeEvent; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.DisposeEvent; +import org.eclipse.swt.events.DisposeListener; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.events.SelectionListener; +import org.eclipse.swt.widgets.Menu; +import org.eclipse.swt.widgets.MenuItem; + +/** + * JFace Action -> SWT's MenuItem adapter. + *
+ * So you have a SWT's MainMenu and you want to add Actions and + * for some reason you can't use JFace MenuManager. + * This adapter "converts" Actions into MenuItems. + */ +public class ActionMenuItemAdapter implements SelectionListener, IPropertyChangeListener { + + private final Action action; + private final MenuItem menuitem; + + public static MenuItem adapt(Action action, Menu parent, int style, int index) { + int _style = _getActionStyle(action) | style; + MenuItem mi = new MenuItem(parent, _style, index); + new ActionMenuItemAdapter(action, mi); + return mi; + } + + public static MenuItem adapt(Action action, Menu parent, int style) { + int _style = _getActionStyle(action) | style; + MenuItem mi = new MenuItem(parent, _style); + new ActionMenuItemAdapter(action, mi); + return mi; + } + + private ActionMenuItemAdapter(Action action, MenuItem menuitem) { + this.menuitem = menuitem; + this.action = action; + + menuitem.setText(action.getText()); + if (action.getImageDescriptor() != null) + menuitem.setImage(action.getImageDescriptor().createImage()); + menuitem.setSelection(action.isChecked()); + menuitem.addSelectionListener(this); + action.addPropertyChangeListener(this); + menuitem.addDisposeListener(new DisposeListener() { + public void widgetDisposed(DisposeEvent e) { + ActionMenuItemAdapter.this.action.removePropertyChangeListener(ActionMenuItemAdapter.this); + ActionMenuItemAdapter.this.menuitem.removeDisposeListener(this); + }}); + } + + + private static int _getActionStyle(Action action) { + int style = SWT.PUSH; + if ((action.getStyle() & Action.AS_CHECK_BOX) > 0) + style = SWT.CHECK; + if ((action.getStyle() & Action.AS_RADIO_BUTTON) > 0) + style = SWT.RADIO; + return style; + } + + public void widgetSelected(SelectionEvent e) { + action.run(); + } + + public void widgetDefaultSelected(SelectionEvent e) { + } + + public Action getAction() { + return action; + } + + public void propertyChange(PropertyChangeEvent event) { + menuitem.setSelection(action.isChecked()); + } + +}