Project

General

Profile

Download (3.56 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.taxeditor.ui.element;
2

    
3
import java.util.Observable;
4
import java.util.Observer;
5

    
6
import org.eclipse.core.commands.Command;
7
import org.eclipse.core.commands.ExecutionException;
8
import org.eclipse.core.commands.NotEnabledException;
9
import org.eclipse.core.commands.NotHandledException;
10
import org.eclipse.core.commands.common.NotDefinedException;
11
import org.eclipse.e4.core.commands.ECommandService;
12
import org.eclipse.swt.SWT;
13
import org.eclipse.swt.events.DisposeEvent;
14
import org.eclipse.swt.events.DisposeListener;
15
import org.eclipse.swt.events.SelectionAdapter;
16
import org.eclipse.swt.events.SelectionEvent;
17
import org.eclipse.swt.layout.GridData;
18
import org.eclipse.swt.layout.GridLayout;
19
import org.eclipse.swt.widgets.Button;
20
import org.eclipse.swt.widgets.Composite;
21
import org.eclipse.ui.handlers.IHandlerService;
22

    
23
import eu.etaxonomy.taxeditor.event.EventUtility;
24
import eu.etaxonomy.taxeditor.store.CdmStore;
25
import eu.etaxonomy.taxeditor.store.LoginManager;
26
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
27

    
28
/**
29
 * A Button which executes a <code>IHandler</code> managed through a
30
 * <code>org.eclipse.ui.handlers</code> extension point
31
 *
32
 * see http://wiki.eclipse.org/Platform_Command_Framework#Commands
33
 *
34
 * @author a.kohlbecker
35
 *
36
 */
37
public class CommandHandlerButton extends Composite implements Observer {
38

    
39
	String commandId;
40
	Button button;
41

    
42
	/**
43
	 * @param parent
44
	 * @param style
45
	 * @param commandId the id of the command handler as defined in a <code>org.eclipse.ui.handlers</code> extension point
46
	 */
47
	public CommandHandlerButton(Composite parent, int style, String commandId)  {
48
		super(parent, SWT.NONE);
49
		this.commandId = commandId;
50

    
51
		setLayout(new GridLayout());
52
		setLayoutData(new GridData());
53

    
54
		button = new Button(this, style);
55
		button.addSelectionListener(new SelectionAdapter() {
56
			@Override
57
			public void widgetSelected(SelectionEvent se) {
58
				// you can execute a command directly ... but to get the proper environment it's better to execute it through the IHandlerService:
59
				IHandlerService handlerService =  TaxeditorStorePlugin.getDefault().getWorkbench().getService(IHandlerService.class);
60
				try {
61
					handlerService.executeCommand(CommandHandlerButton.this.commandId, null);
62
				} catch (ExecutionException e) {
63
					// TODO Auto-generated catch block
64
					e.printStackTrace();
65
				} catch (NotDefinedException e) {
66
					// TODO Auto-generated catch block
67
					e.printStackTrace();
68
				} catch (NotEnabledException e) {
69
					// TODO Auto-generated catch block
70
					e.printStackTrace();
71
				} catch (NotHandledException e) {
72
					// TODO Auto-generated catch block
73
					e.printStackTrace();
74
				}
75
			}
76

    
77
		});
78
		updateButtonEnabledState();
79

    
80
		CdmStore.getLoginManager().addObserver(this);
81
        addDisposeListener(new DisposeListener() {
82
			@Override
83
			public void widgetDisposed(DisposeEvent e) {
84
				CdmStore.getLoginManager().deleteObserver(CommandHandlerButton.this);
85
			}
86
        });
87
	}
88

    
89

    
90
	/* (non-Javadoc)
91
	 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
92
	 */
93
	@Override
94
	public void update(Observable o, Object arg) {
95
		if(o instanceof LoginManager){
96
			updateButtonEnabledState();
97
		}
98

    
99
	}
100

    
101
	private void updateButtonEnabledState() {
102
		ECommandService commandService = EventUtility.getCommandService();
103
		Command command = commandService.getCommand(commandId);
104
		if(!button.isDisposed()){
105
			button.setEnabled(command != null && command.isEnabled());
106
		}
107
	}
108

    
109
	public void setText(String string) {
110
		button.setText(string);
111
	}
112

    
113
	public void setButtonEnabled(boolean enabled){
114
	    button.setEnabled(enabled);
115
	}
116
}
(11-11/53)