KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > actions > ProjectPropertyDialogAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ui.internal.ide.actions;
12
13 import org.eclipse.core.resources.IProject;
14 import org.eclipse.core.resources.IResource;
15 import org.eclipse.core.runtime.IAdaptable;
16 import org.eclipse.jface.viewers.ISelection;
17 import org.eclipse.jface.viewers.ISelectionChangedListener;
18 import org.eclipse.jface.viewers.ISelectionProvider;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.jface.viewers.StructuredSelection;
21 import org.eclipse.ui.IEditorPart;
22 import org.eclipse.ui.INullSelectionListener;
23 import org.eclipse.ui.IWorkbenchPart;
24 import org.eclipse.ui.IWorkbenchWindow;
25 import org.eclipse.ui.PlatformUI;
26 import org.eclipse.ui.actions.ActionFactory;
27 import org.eclipse.ui.actions.PartEventAction;
28 import org.eclipse.ui.dialogs.PropertyDialogAction;
29 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
30 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
31
32 /**
33  * Implementation for the action Property on the Project menu.
34  */

35 public class ProjectPropertyDialogAction extends PartEventAction implements
36         INullSelectionListener, ActionFactory.IWorkbenchAction {
37
38     /**
39      * The workbench window; or <code>null</code> if this
40      * action has been <code>dispose</code>d.
41      */

42     private IWorkbenchWindow workbenchWindow;
43
44     /**
45      * Create a new dialog.
46      *
47      * @param window the window
48      */

49     public ProjectPropertyDialogAction(IWorkbenchWindow window) {
50         super(new String JavaDoc());
51         if (window == null) {
52             throw new IllegalArgumentException JavaDoc();
53         }
54         this.workbenchWindow = window;
55         setText(IDEWorkbenchMessages.Workbench_projectProperties);
56         setToolTipText(IDEWorkbenchMessages.Workbench_projectPropertiesToolTip);
57         PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
58                 IIDEHelpContextIds.PROJECT_PROPERTY_DIALOG_ACTION);
59         workbenchWindow.getSelectionService().addSelectionListener(this);
60         workbenchWindow.getPartService().addPartListener(this);
61         setActionDefinitionId("org.eclipse.ui.project.properties"); //$NON-NLS-1$
62
}
63
64     /**
65      * Opens the project properties dialog.
66      */

67     public void run() {
68         IProject project = getProject();
69         if (project == null) {
70             return;
71         }
72
73         SelProvider selProvider = new SelProvider();
74         selProvider.projectSelection = new StructuredSelection(project);
75         PropertyDialogAction propAction = new PropertyDialogAction(
76                 workbenchWindow.getShell(), selProvider);
77         propAction.run();
78     }
79
80     /**
81      * Update the enablement state when a the selection changes.
82      */

83     public void selectionChanged(IWorkbenchPart part, ISelection sel) {
84         setEnabled(getProject() != null);
85     }
86
87     /**
88      * Update the enablement state when a new part is activated.
89      */

90     public void partActivated(IWorkbenchPart part) {
91         super.partActivated(part);
92         setEnabled(getProject() != null);
93     }
94
95     /**
96      * Returns a project from the selection of the active part.
97      */

98     private IProject getProject() {
99         IWorkbenchPart part = getActivePart();
100         Object JavaDoc selection = null;
101         if (part instanceof IEditorPart) {
102             selection = ((IEditorPart) part).getEditorInput();
103         } else {
104             ISelection sel = workbenchWindow.getSelectionService()
105                     .getSelection();
106             if ((sel != null) && (sel instanceof IStructuredSelection)) {
107                 selection = ((IStructuredSelection) sel).getFirstElement();
108             }
109         }
110         if (selection == null) {
111             return null;
112         }
113         if (!(selection instanceof IAdaptable)) {
114             return null;
115         }
116         IResource resource = (IResource) ((IAdaptable) selection)
117                 .getAdapter(IResource.class);
118         if (resource == null) {
119             return null;
120         }
121         return resource.getProject();
122     }
123
124     /* (non-javadoc)
125      * Method declared on ActionFactory.IWorkbenchAction
126      */

127     public void dispose() {
128         if (workbenchWindow == null) {
129             // action has already been disposed
130
return;
131         }
132         workbenchWindow.getSelectionService().removeSelectionListener(this);
133         workbenchWindow.getPartService().removePartListener(this);
134         workbenchWindow = null;
135     }
136
137     /*
138      * Helper class to simulate a selection provider
139      */

140     private static final class SelProvider implements ISelectionProvider {
141         protected IStructuredSelection projectSelection = StructuredSelection.EMPTY;
142
143         public void addSelectionChangedListener(
144                 ISelectionChangedListener listener) {
145             // do nothing
146
}
147
148         public ISelection getSelection() {
149             return projectSelection;
150         }
151
152         public void removeSelectionChangedListener(
153                 ISelectionChangedListener listener) {
154             // do nothing
155
}
156
157         public void setSelection(ISelection selection) {
158             // do nothing
159
}
160     }
161 }
162
Popular Tags