1 package eu
.etaxonomy
.taxeditor
.ui
.element
;
3 import java
.util
.Observable
;
4 import java
.util
.Observer
;
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
;
23 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
24 import eu
.etaxonomy
.taxeditor
.store
.LoginManager
;
25 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
28 * A Button which executes a <code>IHandler</code> managed through a
29 * <code>org.eclipse.ui.handlers</code> extension point
31 * see http://wiki.eclipse.org/Platform_Command_Framework#Commands
33 * @author a.kohlbecker
36 public class CommandHandlerButton
extends Composite
implements Observer
{
44 * @param commandId the id of the command handler as defined in a <code>org.eclipse.ui.handlers</code> extension point
46 public CommandHandlerButton(Composite parent
, int style
, String commandId
) {
47 super(parent
, SWT
.NONE
);
48 this.commandId
= commandId
;
50 setLayout(new GridLayout());
51 setLayoutData(new GridData());
53 button
= new Button(this, style
);
54 button
.addSelectionListener(new SelectionAdapter() {
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);
60 handlerService
.executeCommand(CommandHandlerButton
.this.commandId
, null);
61 } catch (ExecutionException e
) {
62 // TODO Auto-generated catch block
64 } catch (NotDefinedException e
) {
65 // TODO Auto-generated catch block
67 } catch (NotEnabledException e
) {
68 // TODO Auto-generated catch block
70 } catch (NotHandledException e
) {
71 // TODO Auto-generated catch block
77 updateButtonEnabledState();
79 CdmStore
.getLoginManager().addObserver(this);
80 addDisposeListener(new DisposeListener() {
82 public void widgetDisposed(DisposeEvent e
) {
83 CdmStore
.getLoginManager().deleteObserver(CommandHandlerButton
.this);
90 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
93 public void update(Observable o
, Object arg
) {
94 if(o
instanceof LoginManager
){
95 updateButtonEnabledState();
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());
108 public void setText(String string
) {
109 button
.setText(string
);