1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
13 * Created on 5.10.2005
14 * @author Toni Kalajainen
16 package org.simantics.utils.ui.jface;
18 import org.eclipse.jface.action.Action;
19 import org.eclipse.jface.util.IPropertyChangeListener;
20 import org.eclipse.jface.util.PropertyChangeEvent;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.DisposeEvent;
23 import org.eclipse.swt.events.DisposeListener;
24 import org.eclipse.swt.events.SelectionEvent;
25 import org.eclipse.swt.events.SelectionListener;
26 import org.eclipse.swt.widgets.Menu;
27 import org.eclipse.swt.widgets.MenuItem;
30 * JFace Action -> SWT's MenuItem adapter.
32 * So you have a SWT's MainMenu and you want to add Actions and
33 * for some reason you can't use JFace MenuManager.
34 * This adapter "converts" Actions into MenuItems.
36 public class ActionMenuItemAdapter implements SelectionListener, IPropertyChangeListener {
38 private final Action action;
39 private final MenuItem menuitem;
41 public static MenuItem adapt(Action action, Menu parent, int style, int index) {
42 int _style = _getActionStyle(action) | style;
43 MenuItem mi = new MenuItem(parent, _style, index);
44 new ActionMenuItemAdapter(action, mi);
48 public static MenuItem adapt(Action action, Menu parent, int style) {
49 int _style = _getActionStyle(action) | style;
50 MenuItem mi = new MenuItem(parent, _style);
51 new ActionMenuItemAdapter(action, mi);
55 private ActionMenuItemAdapter(Action action, MenuItem menuitem) {
56 this.menuitem = menuitem;
59 menuitem.setText(action.getText());
60 if (action.getImageDescriptor() != null)
61 menuitem.setImage(action.getImageDescriptor().createImage());
62 menuitem.setSelection(action.isChecked());
63 menuitem.addSelectionListener(this);
64 action.addPropertyChangeListener(this);
65 menuitem.addDisposeListener(new DisposeListener() {
66 public void widgetDisposed(DisposeEvent e) {
67 ActionMenuItemAdapter.this.action.removePropertyChangeListener(ActionMenuItemAdapter.this);
68 ActionMenuItemAdapter.this.menuitem.removeDisposeListener(this);
73 private static int _getActionStyle(Action action) {
75 if ((action.getStyle() & Action.AS_CHECK_BOX) > 0)
77 if ((action.getStyle() & Action.AS_RADIO_BUTTON) > 0)
82 public void widgetSelected(SelectionEvent e) {
86 public void widgetDefaultSelected(SelectionEvent e) {
89 public Action getAction() {
93 public void propertyChange(PropertyChangeEvent event) {
94 menuitem.setSelection(action.isChecked());