KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > commands > CommandComposerDialog


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.commands;
12
13 import org.eclipse.core.commands.Command;
14 import org.eclipse.core.commands.ParameterizedCommand;
15 import org.eclipse.core.expressions.IEvaluationContext;
16 import org.eclipse.jface.dialogs.IDialogConstants;
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.jface.viewers.ISelectionChangedListener;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.jface.viewers.SelectionChangedEvent;
21 import org.eclipse.pde.internal.ui.PDEUIMessages;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Shell;
26 import org.eclipse.ui.forms.FormDialog;
27 import org.eclipse.ui.forms.IManagedForm;
28 import org.eclipse.ui.forms.widgets.ScrolledForm;
29
30 public class CommandComposerDialog extends FormDialog {
31     
32     private CommandComposerPart fCCP;
33     private ParameterizedCommand fPC;
34     private Button fOKButton;
35     
36     public CommandComposerDialog(Shell parentShell, int filterType, ParameterizedCommand preselectedCommand,
37             IEvaluationContext snapshot) {
38         super(parentShell);
39         setShellStyle(SWT.MODELESS | SWT.SHELL_TRIM | SWT.BORDER);
40         fCCP = new CommandComposerPart();
41         fCCP.setFilterType(filterType);
42         fCCP.setPresetCommand(preselectedCommand);
43         fCCP.setSnapshotContext(snapshot);
44     }
45     
46     protected void createFormContent(IManagedForm mform) {
47         ScrolledForm form = mform.getForm();
48         mform.getToolkit().decorateFormHeading(form.getForm());
49         initializeDialogUnits(form);
50         fCCP.createCC(form, mform.getToolkit(), new ISelectionChangedListener() {
51             public void selectionChanged(SelectionChangedEvent event) {
52                 updateOkButtonEnablement(event.getSelection());
53             }
54         });
55         applyDialogFont(form);
56     }
57     
58     /* (non-Javadoc)
59      * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
60      */

61     protected void createButtonsForButtonBar(Composite parent) {
62         super.createButtonsForButtonBar(parent);
63         // Update the button enablement only after the button is created
64
fOKButton = getButton(IDialogConstants.OK_ID);
65         
66         CommandList list = fCCP.getCommandList();
67         // Ensure the tree viewer was created
68
if (list == null) {
69             updateOkButtonEnablement(false);
70             return;
71         }
72         // Retrieve the current selection
73
ISelection selection = list.getSelection();
74         // Update the OK button based on the current selection
75
updateOkButtonEnablement(selection);
76     }
77     
78     /**
79      * @param selection
80      */

81     private void updateOkButtonEnablement(Object JavaDoc selection) {
82         // Ensure there is a selection
83
if (selection == null) {
84             updateOkButtonEnablement(false);
85             return;
86         }
87         // Ensure the selection is structured
88
if ((selection instanceof IStructuredSelection) == false) {
89             updateOkButtonEnablement(false);
90             return;
91         }
92         IStructuredSelection sSelection = (IStructuredSelection)selection;
93         // Ensure the selection is a command
94
if (sSelection.getFirstElement() instanceof Command) {
95             // Enable button
96
updateOkButtonEnablement(true);
97             return;
98         }
99         // Disable button
100
updateOkButtonEnablement(false);
101     }
102     
103     /**
104      * @param enabled
105      */

106     private void updateOkButtonEnablement(boolean enabled) {
107         if (fOKButton != null) {
108             fOKButton.setEnabled(enabled);
109         }
110     }
111     
112     protected void configureShell(Shell newShell) {
113         newShell.setText(PDEUIMessages.CommandSerializerPart_name);
114         super.configureShell(newShell);
115     }
116     
117     public void okPressed() {
118         fPC = fCCP.getParameterizedCommand();
119         super.okPressed();
120     }
121     
122     public boolean close() {
123         fCCP.dispose();
124         return super.close();
125     }
126     
127     public ParameterizedCommand getCommand() {
128         return fPC;
129     }
130 }
131
Popular Tags