Show URI parsing exceptions below URI text field (fixes #5055, #5003, #4587)
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / element / CommandHandlerButton.java
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.swt.SWT;
12 import org.eclipse.swt.events.DisposeEvent;
13 import org.eclipse.swt.events.DisposeListener;
14 import org.eclipse.swt.events.SelectionAdapter;
15 import org.eclipse.swt.events.SelectionEvent;
16 import org.eclipse.swt.layout.GridData;
17 import org.eclipse.swt.layout.GridLayout;
18 import org.eclipse.swt.widgets.Button;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.ui.commands.ICommandService;
21 import org.eclipse.ui.handlers.IHandlerService;
22
23 import eu.etaxonomy.taxeditor.store.CdmStore;
24 import eu.etaxonomy.taxeditor.store.LoginManager;
25 import eu.etaxonomy.taxeditor.store.StoreUtil;
26
27 /**
28 * A Button which executes a <code>IHandler</code> managed through a
29 * <code>org.eclipse.ui.handlers</code> extension point
30 *
31 * see http://wiki.eclipse.org/Platform_Command_Framework#Commands
32 *
33 * @author a.kohlbecker
34 *
35 */
36 public class CommandHandlerButton extends Composite implements Observer {
37
38 String commandId;
39 Button button;
40
41 /**
42 * @param parent
43 * @param style
44 * @param commandId the id of the command handler as defined in a <code>org.eclipse.ui.handlers</code> extension point
45 */
46 public CommandHandlerButton(Composite parent, int style, String commandId) {
47 super(parent, SWT.NONE);
48 this.commandId = commandId;
49
50 setLayout(new GridLayout());
51 setLayoutData(new GridData());
52
53 button = new Button(this, style);
54 button.addSelectionListener(new SelectionAdapter() {
55 @Override
56 public void widgetSelected(SelectionEvent se) {
57 // you can execute a command directly ... but to get the proper environment it's better to execute it through the IHandlerService:
58 IHandlerService handlerService = (IHandlerService) StoreUtil.getActivePart().getSite().getService(IHandlerService.class);
59 try {
60 handlerService.executeCommand(CommandHandlerButton.this.commandId, null);
61 } catch (ExecutionException e) {
62 // TODO Auto-generated catch block
63 e.printStackTrace();
64 } catch (NotDefinedException e) {
65 // TODO Auto-generated catch block
66 e.printStackTrace();
67 } catch (NotEnabledException e) {
68 // TODO Auto-generated catch block
69 e.printStackTrace();
70 } catch (NotHandledException e) {
71 // TODO Auto-generated catch block
72 e.printStackTrace();
73 }
74 }
75
76 });
77 updateButtonEnabledState();
78
79 CdmStore.getLoginManager().addObserver(this);
80 addDisposeListener(new DisposeListener() {
81 @Override
82 public void widgetDisposed(DisposeEvent e) {
83 CdmStore.getLoginManager().deleteObserver(CommandHandlerButton.this);
84 }
85 });
86 }
87
88
89 /* (non-Javadoc)
90 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
91 */
92 @Override
93 public void update(Observable o, Object arg) {
94 if(o instanceof LoginManager){
95 updateButtonEnabledState();
96 }
97
98 }
99
100 private void updateButtonEnabledState() {
101 ICommandService commandService = (ICommandService) StoreUtil.getActivePart().getSite().getService(ICommandService.class);
102 Command command = commandService.getCommand(commandId);
103 if(!button.isDisposed()){
104 button.setEnabled(command != null && command.isEnabled());
105 }
106 }
107
108 public void setText(String string) {
109 button.setText(string);
110 }
111 }