001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.ui.internal.ide.actions;
011:
012: import org.eclipse.core.resources.IProject;
013: import org.eclipse.core.resources.IResource;
014: import org.eclipse.core.runtime.IAdaptable;
015: import org.eclipse.jface.viewers.ISelection;
016: import org.eclipse.jface.viewers.ISelectionChangedListener;
017: import org.eclipse.jface.viewers.ISelectionProvider;
018: import org.eclipse.jface.viewers.IStructuredSelection;
019: import org.eclipse.jface.viewers.StructuredSelection;
020: import org.eclipse.ui.IEditorPart;
021: import org.eclipse.ui.INullSelectionListener;
022: import org.eclipse.ui.IWorkbenchPart;
023: import org.eclipse.ui.IWorkbenchWindow;
024: import org.eclipse.ui.PlatformUI;
025: import org.eclipse.ui.actions.ActionFactory;
026: import org.eclipse.ui.actions.PartEventAction;
027: import org.eclipse.ui.dialogs.PropertyDialogAction;
028: import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
029: import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
030:
031: /**
032: * Implementation for the action Property on the Project menu.
033: */
034: public class ProjectPropertyDialogAction extends PartEventAction
035: implements INullSelectionListener,
036: ActionFactory.IWorkbenchAction {
037:
038: /**
039: * The workbench window; or <code>null</code> if this
040: * action has been <code>dispose</code>d.
041: */
042: private IWorkbenchWindow workbenchWindow;
043:
044: /**
045: * Create a new dialog.
046: *
047: * @param window the window
048: */
049: public ProjectPropertyDialogAction(IWorkbenchWindow window) {
050: super (new String());
051: if (window == null) {
052: throw new IllegalArgumentException();
053: }
054: this .workbenchWindow = window;
055: setText(IDEWorkbenchMessages.Workbench_projectProperties);
056: setToolTipText(IDEWorkbenchMessages.Workbench_projectPropertiesToolTip);
057: PlatformUI.getWorkbench().getHelpSystem().setHelp(this ,
058: IIDEHelpContextIds.PROJECT_PROPERTY_DIALOG_ACTION);
059: workbenchWindow.getSelectionService()
060: .addSelectionListener(this );
061: workbenchWindow.getPartService().addPartListener(this );
062: setActionDefinitionId("org.eclipse.ui.project.properties"); //$NON-NLS-1$
063: }
064:
065: /**
066: * Opens the project properties dialog.
067: */
068: public void run() {
069: IProject project = getProject();
070: if (project == null) {
071: return;
072: }
073:
074: SelProvider selProvider = new SelProvider();
075: selProvider.projectSelection = new StructuredSelection(project);
076: PropertyDialogAction propAction = new PropertyDialogAction(
077: workbenchWindow.getShell(), selProvider);
078: propAction.run();
079: }
080:
081: /**
082: * Update the enablement state when a the selection changes.
083: */
084: public void selectionChanged(IWorkbenchPart part, ISelection sel) {
085: setEnabled(getProject() != null);
086: }
087:
088: /**
089: * Update the enablement state when a new part is activated.
090: */
091: public void partActivated(IWorkbenchPart part) {
092: super .partActivated(part);
093: setEnabled(getProject() != null);
094: }
095:
096: /**
097: * Returns a project from the selection of the active part.
098: */
099: private IProject getProject() {
100: IWorkbenchPart part = getActivePart();
101: Object selection = null;
102: if (part instanceof IEditorPart) {
103: selection = ((IEditorPart) part).getEditorInput();
104: } else {
105: ISelection sel = workbenchWindow.getSelectionService()
106: .getSelection();
107: if ((sel != null) && (sel instanceof IStructuredSelection)) {
108: selection = ((IStructuredSelection) sel)
109: .getFirstElement();
110: }
111: }
112: if (selection == null) {
113: return null;
114: }
115: if (!(selection instanceof IAdaptable)) {
116: return null;
117: }
118: IResource resource = (IResource) ((IAdaptable) selection)
119: .getAdapter(IResource.class);
120: if (resource == null) {
121: return null;
122: }
123: return resource.getProject();
124: }
125:
126: /* (non-javadoc)
127: * Method declared on ActionFactory.IWorkbenchAction
128: */
129: public void dispose() {
130: if (workbenchWindow == null) {
131: // action has already been disposed
132: return;
133: }
134: workbenchWindow.getSelectionService().removeSelectionListener(
135: this );
136: workbenchWindow.getPartService().removePartListener(this );
137: workbenchWindow = null;
138: }
139:
140: /*
141: * Helper class to simulate a selection provider
142: */
143: private static final class SelProvider implements
144: ISelectionProvider {
145: protected IStructuredSelection projectSelection = StructuredSelection.EMPTY;
146:
147: public void addSelectionChangedListener(
148: ISelectionChangedListener listener) {
149: // do nothing
150: }
151:
152: public ISelection getSelection() {
153: return projectSelection;
154: }
155:
156: public void removeSelectionChangedListener(
157: ISelectionChangedListener listener) {
158: // do nothing
159: }
160:
161: public void setSelection(ISelection selection) {
162: // do nothing
163: }
164: }
165: }
|