KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > feature > FeatureEditor


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.editor.feature;
12
13 import java.io.File JavaDoc;
14 import java.util.Locale JavaDoc;
15
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.jface.preference.IPreferenceStore;
20 import org.eclipse.pde.core.IBaseModel;
21 import org.eclipse.pde.core.IIdentifiable;
22 import org.eclipse.pde.internal.core.build.IBuildObject;
23 import org.eclipse.pde.internal.core.ifeature.IFeature;
24 import org.eclipse.pde.internal.core.ifeature.IFeatureImport;
25 import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
26 import org.eclipse.pde.internal.core.ifeature.IFeatureObject;
27 import org.eclipse.pde.internal.ui.IPDEUIConstants;
28 import org.eclipse.pde.internal.ui.IPreferenceConstants;
29 import org.eclipse.pde.internal.ui.PDEPlugin;
30 import org.eclipse.pde.internal.ui.PDEUIMessages;
31 import org.eclipse.pde.internal.ui.editor.ISortableContentOutlinePage;
32 import org.eclipse.pde.internal.ui.editor.MultiSourceEditor;
33 import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
34 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
35 import org.eclipse.pde.internal.ui.editor.PDESourcePage;
36 import org.eclipse.pde.internal.ui.editor.SystemFileEditorInput;
37 import org.eclipse.pde.internal.ui.editor.build.BuildInputContext;
38 import org.eclipse.pde.internal.ui.editor.build.BuildPage;
39 import org.eclipse.pde.internal.ui.editor.build.BuildSourcePage;
40 import org.eclipse.pde.internal.ui.editor.context.InputContext;
41 import org.eclipse.pde.internal.ui.editor.context.InputContextManager;
42 import org.eclipse.swt.SWTError;
43 import org.eclipse.swt.dnd.RTFTransfer;
44 import org.eclipse.swt.dnd.TextTransfer;
45 import org.eclipse.swt.dnd.Transfer;
46 import org.eclipse.swt.dnd.TransferData;
47 import org.eclipse.swt.widgets.Display;
48 import org.eclipse.ui.IEditorInput;
49 import org.eclipse.ui.IFileEditorInput;
50 import org.eclipse.ui.IShowEditorInput;
51 import org.eclipse.ui.IStorageEditorInput;
52 import org.eclipse.ui.PartInitException;
53 import org.eclipse.ui.forms.editor.IFormPage;
54 import org.eclipse.ui.ide.IDE;
55 import org.eclipse.ui.part.FileEditorInput;
56 import org.eclipse.ui.views.properties.IPropertySheetPage;
57
58 public class FeatureEditor extends MultiSourceEditor implements IShowEditorInput {
59
60     /* (non-Javadoc)
61      * @see org.eclipse.pde.internal.ui.editor.PDEFormEditor#getEditorID()
62      */

63     protected String JavaDoc getEditorID() {
64         return IPDEUIConstants.FEATURE_EDITOR_ID;
65     }
66     
67     public static void openFeatureEditor(IFeature feature) {
68         if(feature!=null){
69             IFeatureModel model = feature.getModel();
70             openFeatureEditor(model);
71         } else {
72             Display.getCurrent().beep();
73         }
74     }
75
76     public static void openFeatureEditor(IFeatureModel model) {
77         if (model != null) {
78             IResource resource = model.getUnderlyingResource();
79             try {
80                 IEditorInput input = null;
81                 if (resource != null)
82                     input = new FileEditorInput((IFile) resource);
83                 else
84                     input = new SystemFileEditorInput(new File JavaDoc(model
85                             .getInstallLocation(), "feature.xml")); //$NON-NLS-1$
86
IDE.openEditor(PDEPlugin.getActivePage(), input,
87                         IPDEUIConstants.FEATURE_EDITOR_ID, true);
88             } catch (PartInitException e) {
89             }
90         } else {
91             Display.getCurrent().beep();
92         }
93
94     }
95
96     public FeatureEditor() {
97     }
98
99     protected void createResourceContexts(InputContextManager manager,
100             IFileEditorInput input) {
101         IFile file = input.getFile();
102         IProject project = file.getProject();
103         IFile buildFile = null;
104         IFile featureFile = null;
105
106         String JavaDoc name = file.getName().toLowerCase(Locale.ENGLISH);
107         if (name.equals("feature.xml")) { //$NON-NLS-1$
108
featureFile = file;
109             buildFile = project.getFile("build.properties"); //$NON-NLS-1$
110
} else if (name.equals("build.properties")) { //$NON-NLS-1$
111
buildFile = file;
112             featureFile = createFeatureFile(project);
113         }
114         if (featureFile.exists()) {
115             FileEditorInput in = new FileEditorInput(featureFile);
116             manager.putContext(in, new FeatureInputContext(this, in,
117                     file == featureFile));
118         }
119         if (buildFile.exists()) {
120             FileEditorInput in = new FileEditorInput(buildFile);
121             manager.putContext(in, new BuildInputContext(this, in,
122                     file == buildFile));
123         }
124         manager.monitorFile(featureFile);
125         manager.monitorFile(buildFile);
126     }
127
128     protected InputContextManager createInputContextManager() {
129         FeatureInputContextManager manager = new FeatureInputContextManager(
130                 this);
131         manager.setUndoManager(new FeatureUndoManager(this));
132         return manager;
133     }
134
135     public void monitoredFileAdded(IFile file) {
136         String JavaDoc name = file.getName();
137         if (name.equalsIgnoreCase("feature.xml")) { //$NON-NLS-1$
138
/*
139              * IEditorInput in = new FileEditorInput(file);
140              * inputContextManager.putContext(in, new FeatureInputContext(this,
141              * in, false));
142              */

143         } else if (name.equalsIgnoreCase("build.properties")) { //$NON-NLS-1$
144
if (!fInputContextManager.hasContext(BuildInputContext.CONTEXT_ID)) {
145                 IEditorInput in = new FileEditorInput(file);
146                 fInputContextManager.putContext(in, new BuildInputContext(this,
147                         in, false));
148             }
149         }
150     }
151
152     public boolean monitoredFileRemoved(IFile file) {
153         // TODO may need to check with the user if there
154
// are unsaved changes in the model for the
155
// file that just got removed under us.
156
return true;
157     }
158
159     public void editorContextAdded(InputContext context) {
160         addSourcePage(context.getId());
161     }
162
163     public void contextRemoved(InputContext context) {
164         if (context.isPrimary()) {
165             close(true);
166             return;
167         }
168         IFormPage page = findPage(context.getId());
169         if (page != null)
170             removePage(context.getId());
171     }
172
173     protected void createSystemFileContexts(InputContextManager manager,
174             SystemFileEditorInput input) {
175         File JavaDoc file = (File JavaDoc) input.getAdapter(File JavaDoc.class);
176         File JavaDoc buildFile = null;
177         File JavaDoc featureFile = null;
178         String JavaDoc name = file.getName().toLowerCase(Locale.ENGLISH);
179         if (name.equals("feature.xml")) { //$NON-NLS-1$
180
featureFile = file;
181             File JavaDoc dir = file.getParentFile();
182             buildFile = new File JavaDoc(dir, "build.properties"); //$NON-NLS-1$
183
} else if (name.equals("build.properties")) { //$NON-NLS-1$
184
buildFile = file;
185             File JavaDoc dir = file.getParentFile();
186             featureFile = createFeatureFile(dir);
187         }
188         if (featureFile.exists()) {
189             SystemFileEditorInput in = new SystemFileEditorInput(featureFile);
190             manager.putContext(in, new FeatureInputContext(this, in,
191                     file == featureFile));
192         }
193         if (buildFile.exists()) {
194             SystemFileEditorInput in = new SystemFileEditorInput(buildFile);
195             manager.putContext(in, new BuildInputContext(this, in,
196                     file == buildFile));
197         }
198     }
199
200     private File JavaDoc createFeatureFile(File JavaDoc dir) {
201         File JavaDoc pluginFile = new File JavaDoc(dir, "plugin.xml"); //$NON-NLS-1$
202
return pluginFile;
203     }
204
205     private IFile createFeatureFile(IProject project) {
206         IFile featureFile = project.getFile("feature.xml"); //$NON-NLS-1$
207
return featureFile;
208     }
209
210     protected void createStorageContexts(InputContextManager manager,
211             IStorageEditorInput input) {
212         String JavaDoc name = input.getName().toLowerCase(Locale.ENGLISH);
213         if (name.equals("build.properties")) { //$NON-NLS-1$
214
manager.putContext(input, new BuildInputContext(this, input, true));
215         } else if (name.startsWith("feature.xml")) { //$NON-NLS-1$
216
manager.putContext(input,
217                     new FeatureInputContext(this, input, true));
218         }
219     }
220
221     protected void addEditorPages() {
222         try {
223             addPage(new FeatureFormPage(this, PDEUIMessages.FeatureEditor_FeaturePage_title));
224             addPage(new InfoFormPage(this, PDEUIMessages.FeatureEditor_InfoPage_title));
225             addPage(new FeatureReferencePage(this, PDEUIMessages.FeatureEditor_ReferencePage_title));
226             addPage(new FeatureIncludesPage(this, PDEUIMessages.FeatureEditor_IncludesPage_title));
227             addPage(new FeatureDependenciesPage(this, PDEUIMessages.FeatureEditor_DependenciesPage_title));
228             addPage(new FeatureAdvancedPage(this, PDEUIMessages.FeatureEditor_AdvancedPage_title));
229             if (fInputContextManager.hasContext(BuildInputContext.CONTEXT_ID))
230                 addPage(new BuildPage(this));
231         } catch (PartInitException e) {
232             PDEPlugin.logException(e);
233         }
234         addSourcePage(FeatureInputContext.CONTEXT_ID);
235         addSourcePage(BuildInputContext.CONTEXT_ID);
236     }
237
238     protected String JavaDoc computeInitialPageId() {
239         String JavaDoc firstPageId = super.computeInitialPageId();
240         if (firstPageId == null) {
241             InputContext primary = fInputContextManager.getPrimaryContext();
242             if (primary != null && FeatureInputContext.CONTEXT_ID.equals(primary.getId()))
243                 firstPageId = FeatureFormPage.PAGE_ID;
244             if (firstPageId == null)
245                 firstPageId = FeatureFormPage.PAGE_ID;
246         }
247         return firstPageId;
248     }
249
250     /*
251      * (non-Javadoc)
252      *
253      * @see org.eclipse.pde.internal.ui.neweditor.MultiSourceEditor#createXMLSourcePage(org.eclipse.pde.internal.ui.neweditor.PDEFormEditor,
254      * java.lang.String, java.lang.String)
255      */

256     protected PDESourcePage createSourcePage(PDEFormEditor editor,
257             String JavaDoc title, String JavaDoc name, String JavaDoc contextId) {
258         if (contextId.equals(FeatureInputContext.CONTEXT_ID))
259             return new FeatureSourcePage(editor, title, name);
260         if (contextId.equals(BuildInputContext.CONTEXT_ID))
261             return new BuildSourcePage(editor, title, name);
262         return super.createSourcePage(editor, title, name, contextId);
263     }
264
265     protected ISortableContentOutlinePage createContentOutline() {
266         return new FeatureOutlinePage(this);
267     }
268
269     protected IPropertySheetPage getPropertySheet(PDEFormPage page) {
270         return null;
271     }
272
273     public String JavaDoc getTitle() {
274         if (!isModelCorrect(getAggregateModel()))
275             return super.getTitle();
276         IFeatureModel model = (IFeatureModel) getAggregateModel();
277         String JavaDoc name = getTitleText(model.getFeature());
278         if (name == null)
279             return super.getTitle();
280         return model.getResourceString(name);
281     }
282
283     public String JavaDoc getTitleProperty() {
284         IPreferenceStore store = PDEPlugin.getDefault().getPreferenceStore();
285         String JavaDoc pref = store.getString(IPreferenceConstants.PROP_SHOW_OBJECTS);
286         if (pref != null && pref.equals(IPreferenceConstants.VALUE_USE_NAMES))
287             return IFeatureObject.P_LABEL;
288         return IIdentifiable.P_ID;
289     }
290
291     private String JavaDoc getTitleText(IFeature feature) {
292         IPreferenceStore store = PDEPlugin.getDefault().getPreferenceStore();
293         String JavaDoc pref = store.getString(IPreferenceConstants.PROP_SHOW_OBJECTS);
294         if (pref != null && pref.equals(IPreferenceConstants.VALUE_USE_NAMES))
295             return feature.getTranslatableLabel();
296         return feature.getId();
297     }
298
299     protected boolean isModelCorrect(Object JavaDoc model) {
300         return model != null ? ((IFeatureModel) model).isValid() : false;
301     }
302
303     protected boolean hasKnownTypes() {
304         try {
305             TransferData[] types = getClipboard().getAvailableTypes();
306             Transfer[] transfers = new Transfer[] { TextTransfer.getInstance(),
307                     RTFTransfer.getInstance() };
308             for (int i = 0; i < types.length; i++) {
309                 for (int j = 0; j < transfers.length; j++) {
310                     if (transfers[j].isSupportedType(types[i]))
311                         return true;
312                 }
313             }
314         } catch (SWTError e) {
315         }
316         return false;
317     }
318
319     public Object JavaDoc getAdapter(Class JavaDoc key) {
320         // No property sheet needed - block super
321
if (key.equals(IPropertySheetPage.class)) {
322             return null;
323         }
324         return super.getAdapter(key);
325     }
326
327     /*
328      * (non-Javadoc)
329      *
330      * @see org.eclipse.pde.internal.ui.editor.PDEFormEditor#getInputContext(java.lang.Object)
331      */

332     protected InputContext getInputContext(Object JavaDoc object) {
333         InputContext context = null;
334         if (object instanceof IBuildObject) {
335             context = fInputContextManager
336                     .findContext(BuildInputContext.CONTEXT_ID);
337         } else if (object instanceof IFeatureObject) {
338             context = fInputContextManager
339                     .findContext(FeatureInputContext.CONTEXT_ID);
340         }
341         return context;
342     }
343
344     protected boolean isPatchEditor() {
345         IBaseModel model = getAggregateModel();
346         if(model==null || !(model instanceof IFeatureModel)){
347             return false;
348         }
349         IFeature feature = ((IFeatureModel)model).getFeature();
350         IFeatureImport[] imports = feature.getImports();
351         for (int i = 0; i < imports.length; i++) {
352             if (imports[i].isPatch()) {
353                 return true;
354             }
355         }
356         return false;
357     }
358
359     public void showEditorInput(IEditorInput editorInput) {
360         String JavaDoc name = editorInput.getName();
361         if (name.equals("feature.xml")) { //$NON-NLS-1$
362
setActivePage(0);
363         } else {
364             setActivePage(getPageCount() - 3);
365         }
366     }
367 }
368
Popular Tags