]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/jface/ActionMenuItemAdapter.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / jface / ActionMenuItemAdapter.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 /*
13  * Created on 5.10.2005
14  * @author Toni Kalajainen 
15  */
16 package org.simantics.utils.ui.jface;
17
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;
28
29 /**
30  * JFace Action -> SWT's MenuItem adapter.
31  * <p>
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.
35  */
36 public class ActionMenuItemAdapter implements SelectionListener, IPropertyChangeListener {
37
38     private final Action action;
39     private final MenuItem menuitem;
40     
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);
45         return mi;
46     }
47     
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);
52         return mi;
53     }
54     
55     private ActionMenuItemAdapter(Action action, MenuItem menuitem) {
56         this.menuitem = menuitem;
57         this.action = action;
58         
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);
69             }});
70     }
71         
72
73     private static int _getActionStyle(Action action) {
74         int style = SWT.PUSH;
75         if ((action.getStyle() & Action.AS_CHECK_BOX) > 0)
76             style = SWT.CHECK;
77         if ((action.getStyle() & Action.AS_RADIO_BUTTON) > 0)
78             style = SWT.RADIO;
79         return style;
80     }
81
82     public void widgetSelected(SelectionEvent e) {        
83         action.run();
84     }
85
86     public void widgetDefaultSelected(SelectionEvent e) {
87     }
88
89     public Action getAction() {
90         return action;
91     }
92
93     public void propertyChange(PropertyChangeEvent event) {
94         menuitem.setSelection(action.isChecked());
95     }
96
97 }