-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-/*\r
- * Created on 5.10.2005\r
- * @author Toni Kalajainen \r
- */\r
-package org.simantics.utils.ui.jface;\r
-\r
-import org.eclipse.jface.action.Action;\r
-import org.eclipse.jface.util.IPropertyChangeListener;\r
-import org.eclipse.jface.util.PropertyChangeEvent;\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.events.DisposeEvent;\r
-import org.eclipse.swt.events.DisposeListener;\r
-import org.eclipse.swt.events.SelectionEvent;\r
-import org.eclipse.swt.events.SelectionListener;\r
-import org.eclipse.swt.widgets.Menu;\r
-import org.eclipse.swt.widgets.MenuItem;\r
-\r
-/**\r
- * JFace Action -> SWT's MenuItem adapter.\r
- * <p>\r
- * So you have a SWT's MainMenu and you want to add Actions and \r
- * for some reason you can't use JFace MenuManager. \r
- * This adapter "converts" Actions into MenuItems.\r
- */\r
-public class ActionMenuItemAdapter implements SelectionListener, IPropertyChangeListener {\r
-\r
- private final Action action;\r
- private final MenuItem menuitem;\r
- \r
- public static MenuItem adapt(Action action, Menu parent, int style, int index) {\r
- int _style = _getActionStyle(action) | style;\r
- MenuItem mi = new MenuItem(parent, _style, index);\r
- new ActionMenuItemAdapter(action, mi);\r
- return mi;\r
- }\r
- \r
- public static MenuItem adapt(Action action, Menu parent, int style) {\r
- int _style = _getActionStyle(action) | style;\r
- MenuItem mi = new MenuItem(parent, _style);\r
- new ActionMenuItemAdapter(action, mi);\r
- return mi;\r
- }\r
- \r
- private ActionMenuItemAdapter(Action action, MenuItem menuitem) {\r
- this.menuitem = menuitem;\r
- this.action = action;\r
- \r
- menuitem.setText(action.getText());\r
- if (action.getImageDescriptor() != null)\r
- menuitem.setImage(action.getImageDescriptor().createImage());\r
- menuitem.setSelection(action.isChecked());\r
- menuitem.addSelectionListener(this);\r
- action.addPropertyChangeListener(this);\r
- menuitem.addDisposeListener(new DisposeListener() {\r
- public void widgetDisposed(DisposeEvent e) {\r
- ActionMenuItemAdapter.this.action.removePropertyChangeListener(ActionMenuItemAdapter.this);\r
- ActionMenuItemAdapter.this.menuitem.removeDisposeListener(this);\r
- }});\r
- }\r
- \r
-\r
- private static int _getActionStyle(Action action) {\r
- int style = SWT.PUSH;\r
- if ((action.getStyle() & Action.AS_CHECK_BOX) > 0)\r
- style = SWT.CHECK;\r
- if ((action.getStyle() & Action.AS_RADIO_BUTTON) > 0)\r
- style = SWT.RADIO;\r
- return style;\r
- }\r
-\r
- public void widgetSelected(SelectionEvent e) { \r
- action.run();\r
- }\r
-\r
- public void widgetDefaultSelected(SelectionEvent e) {\r
- }\r
-\r
- public Action getAction() {\r
- return action;\r
- }\r
-\r
- public void propertyChange(PropertyChangeEvent event) {\r
- menuitem.setSelection(action.isChecked());\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * 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.
+ * <p>
+ * 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());
+ }
+
+}