001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.commands;
011:
012: import org.eclipse.core.commands.Command;
013: import org.eclipse.core.commands.ParameterizedCommand;
014: import org.eclipse.core.expressions.IEvaluationContext;
015: import org.eclipse.jface.dialogs.IDialogConstants;
016: import org.eclipse.jface.viewers.ISelection;
017: import org.eclipse.jface.viewers.ISelectionChangedListener;
018: import org.eclipse.jface.viewers.IStructuredSelection;
019: import org.eclipse.jface.viewers.SelectionChangedEvent;
020: import org.eclipse.pde.internal.ui.PDEUIMessages;
021: import org.eclipse.swt.SWT;
022: import org.eclipse.swt.widgets.Button;
023: import org.eclipse.swt.widgets.Composite;
024: import org.eclipse.swt.widgets.Shell;
025: import org.eclipse.ui.forms.FormDialog;
026: import org.eclipse.ui.forms.IManagedForm;
027: import org.eclipse.ui.forms.widgets.ScrolledForm;
028:
029: public class CommandComposerDialog extends FormDialog {
030:
031: private CommandComposerPart fCCP;
032: private ParameterizedCommand fPC;
033: private Button fOKButton;
034:
035: public CommandComposerDialog(Shell parentShell, int filterType,
036: ParameterizedCommand preselectedCommand,
037: IEvaluationContext snapshot) {
038: super (parentShell);
039: setShellStyle(SWT.MODELESS | SWT.SHELL_TRIM | SWT.BORDER);
040: fCCP = new CommandComposerPart();
041: fCCP.setFilterType(filterType);
042: fCCP.setPresetCommand(preselectedCommand);
043: fCCP.setSnapshotContext(snapshot);
044: }
045:
046: protected void createFormContent(IManagedForm mform) {
047: ScrolledForm form = mform.getForm();
048: mform.getToolkit().decorateFormHeading(form.getForm());
049: initializeDialogUnits(form);
050: fCCP.createCC(form, mform.getToolkit(),
051: new ISelectionChangedListener() {
052: public void selectionChanged(
053: SelectionChangedEvent event) {
054: updateOkButtonEnablement(event.getSelection());
055: }
056: });
057: applyDialogFont(form);
058: }
059:
060: /* (non-Javadoc)
061: * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
062: */
063: protected void createButtonsForButtonBar(Composite parent) {
064: super .createButtonsForButtonBar(parent);
065: // Update the button enablement only after the button is created
066: fOKButton = getButton(IDialogConstants.OK_ID);
067:
068: CommandList list = fCCP.getCommandList();
069: // Ensure the tree viewer was created
070: if (list == null) {
071: updateOkButtonEnablement(false);
072: return;
073: }
074: // Retrieve the current selection
075: ISelection selection = list.getSelection();
076: // Update the OK button based on the current selection
077: updateOkButtonEnablement(selection);
078: }
079:
080: /**
081: * @param selection
082: */
083: private void updateOkButtonEnablement(Object selection) {
084: // Ensure there is a selection
085: if (selection == null) {
086: updateOkButtonEnablement(false);
087: return;
088: }
089: // Ensure the selection is structured
090: if ((selection instanceof IStructuredSelection) == false) {
091: updateOkButtonEnablement(false);
092: return;
093: }
094: IStructuredSelection sSelection = (IStructuredSelection) selection;
095: // Ensure the selection is a command
096: if (sSelection.getFirstElement() instanceof Command) {
097: // Enable button
098: updateOkButtonEnablement(true);
099: return;
100: }
101: // Disable button
102: updateOkButtonEnablement(false);
103: }
104:
105: /**
106: * @param enabled
107: */
108: private void updateOkButtonEnablement(boolean enabled) {
109: if (fOKButton != null) {
110: fOKButton.setEnabled(enabled);
111: }
112: }
113:
114: protected void configureShell(Shell newShell) {
115: newShell.setText(PDEUIMessages.CommandSerializerPart_name);
116: super .configureShell(newShell);
117: }
118:
119: public void okPressed() {
120: fPC = fCCP.getParameterizedCommand();
121: super .okPressed();
122: }
123:
124: public boolean close() {
125: fCCP.dispose();
126: return super .close();
127: }
128:
129: public ParameterizedCommand getCommand() {
130: return fPC;
131: }
132: }
|