001: /*******************************************************************************
002: * Copyright (c) 2000, 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.editor.feature;
011:
012: import java.io.File;
013: import java.util.Locale;
014:
015: import org.eclipse.core.resources.IFile;
016: import org.eclipse.core.resources.IProject;
017: import org.eclipse.core.resources.IResource;
018: import org.eclipse.jface.action.Action;
019: import org.eclipse.jface.action.IToolBarManager;
020: import org.eclipse.jface.preference.IPreferenceStore;
021: import org.eclipse.pde.core.IBaseModel;
022: import org.eclipse.pde.core.IIdentifiable;
023: import org.eclipse.pde.internal.core.build.IBuildObject;
024: import org.eclipse.pde.internal.core.ifeature.IFeature;
025: import org.eclipse.pde.internal.core.ifeature.IFeatureImport;
026: import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
027: import org.eclipse.pde.internal.core.ifeature.IFeatureObject;
028: import org.eclipse.pde.internal.ui.IPDEUIConstants;
029: import org.eclipse.pde.internal.ui.IPreferenceConstants;
030: import org.eclipse.pde.internal.ui.PDEPlugin;
031: import org.eclipse.pde.internal.ui.PDEPluginImages;
032: import org.eclipse.pde.internal.ui.PDEUIMessages;
033: import org.eclipse.pde.internal.ui.editor.ISortableContentOutlinePage;
034: import org.eclipse.pde.internal.ui.editor.MultiSourceEditor;
035: import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
036: import org.eclipse.pde.internal.ui.editor.PDEFormPage;
037: import org.eclipse.pde.internal.ui.editor.PDESourcePage;
038: import org.eclipse.pde.internal.ui.editor.SystemFileEditorInput;
039: import org.eclipse.pde.internal.ui.editor.build.BuildInputContext;
040: import org.eclipse.pde.internal.ui.editor.build.BuildPage;
041: import org.eclipse.pde.internal.ui.editor.build.BuildSourcePage;
042: import org.eclipse.pde.internal.ui.editor.context.InputContext;
043: import org.eclipse.pde.internal.ui.editor.context.InputContextManager;
044: import org.eclipse.swt.SWTError;
045: import org.eclipse.swt.dnd.RTFTransfer;
046: import org.eclipse.swt.dnd.TextTransfer;
047: import org.eclipse.swt.dnd.Transfer;
048: import org.eclipse.swt.dnd.TransferData;
049: import org.eclipse.swt.widgets.Display;
050: import org.eclipse.ui.IEditorInput;
051: import org.eclipse.ui.IFileEditorInput;
052: import org.eclipse.ui.IShowEditorInput;
053: import org.eclipse.ui.IStorageEditorInput;
054: import org.eclipse.ui.PartInitException;
055: import org.eclipse.ui.forms.editor.IFormPage;
056: import org.eclipse.ui.ide.IDE;
057: import org.eclipse.ui.part.FileEditorInput;
058: import org.eclipse.ui.views.properties.IPropertySheetPage;
059:
060: public class FeatureEditor extends MultiSourceEditor implements
061: IShowEditorInput {
062:
063: private Action fExportAction;
064:
065: /* (non-Javadoc)
066: * @see org.eclipse.pde.internal.ui.editor.PDEFormEditor#getEditorID()
067: */
068: protected String getEditorID() {
069: return IPDEUIConstants.FEATURE_EDITOR_ID;
070: }
071:
072: public static void openFeatureEditor(IFeature feature) {
073: if (feature != null) {
074: IFeatureModel model = feature.getModel();
075: openFeatureEditor(model);
076: } else {
077: Display.getCurrent().beep();
078: }
079: }
080:
081: public static void openFeatureEditor(IFeatureModel model) {
082: if (model != null) {
083: IResource resource = model.getUnderlyingResource();
084: try {
085: IEditorInput input = null;
086: if (resource != null)
087: input = new FileEditorInput((IFile) resource);
088: else
089: input = new SystemFileEditorInput(new File(model
090: .getInstallLocation(), "feature.xml")); //$NON-NLS-1$
091: IDE.openEditor(PDEPlugin.getActivePage(), input,
092: IPDEUIConstants.FEATURE_EDITOR_ID, true);
093: } catch (PartInitException e) {
094: }
095: } else {
096: Display.getCurrent().beep();
097: }
098:
099: }
100:
101: public FeatureEditor() {
102: }
103:
104: protected void createResourceContexts(InputContextManager manager,
105: IFileEditorInput input) {
106: IFile file = input.getFile();
107: IProject project = file.getProject();
108: IFile buildFile = null;
109: IFile featureFile = null;
110:
111: String name = file.getName().toLowerCase(Locale.ENGLISH);
112: if (name.equals("feature.xml")) { //$NON-NLS-1$
113: featureFile = file;
114: buildFile = project.getFile("build.properties"); //$NON-NLS-1$
115: } else if (name.equals("build.properties")) { //$NON-NLS-1$
116: buildFile = file;
117: featureFile = createFeatureFile(project);
118: }
119: if (featureFile.exists()) {
120: FileEditorInput in = new FileEditorInput(featureFile);
121: manager.putContext(in, new FeatureInputContext(this , in,
122: file == featureFile));
123: }
124: if (buildFile.exists()) {
125: FileEditorInput in = new FileEditorInput(buildFile);
126: manager.putContext(in, new BuildInputContext(this , in,
127: file == buildFile));
128: }
129: manager.monitorFile(featureFile);
130: manager.monitorFile(buildFile);
131: }
132:
133: protected InputContextManager createInputContextManager() {
134: FeatureInputContextManager manager = new FeatureInputContextManager(
135: this );
136: manager.setUndoManager(new FeatureUndoManager(this ));
137: return manager;
138: }
139:
140: public void monitoredFileAdded(IFile file) {
141: String name = file.getName();
142: if (name.equalsIgnoreCase("feature.xml")) { //$NON-NLS-1$
143: /*
144: * IEditorInput in = new FileEditorInput(file);
145: * inputContextManager.putContext(in, new FeatureInputContext(this,
146: * in, false));
147: */
148: } else if (name.equalsIgnoreCase("build.properties")) { //$NON-NLS-1$
149: if (!fInputContextManager
150: .hasContext(BuildInputContext.CONTEXT_ID)) {
151: IEditorInput in = new FileEditorInput(file);
152: fInputContextManager.putContext(in,
153: new BuildInputContext(this , in, false));
154: }
155: }
156: }
157:
158: public boolean monitoredFileRemoved(IFile file) {
159: // TODO may need to check with the user if there
160: // are unsaved changes in the model for the
161: // file that just got removed under us.
162: return true;
163: }
164:
165: public void editorContextAdded(InputContext context) {
166: addSourcePage(context.getId());
167: }
168:
169: public void contextRemoved(InputContext context) {
170: if (context.isPrimary()) {
171: close(true);
172: return;
173: }
174: IFormPage page = findPage(context.getId());
175: if (page != null)
176: removePage(context.getId());
177: }
178:
179: protected void createSystemFileContexts(
180: InputContextManager manager, SystemFileEditorInput input) {
181: File file = (File) input.getAdapter(File.class);
182: File buildFile = null;
183: File featureFile = null;
184: String name = file.getName().toLowerCase(Locale.ENGLISH);
185: if (name.equals("feature.xml")) { //$NON-NLS-1$
186: featureFile = file;
187: File dir = file.getParentFile();
188: buildFile = new File(dir, "build.properties"); //$NON-NLS-1$
189: } else if (name.equals("build.properties")) { //$NON-NLS-1$
190: buildFile = file;
191: File dir = file.getParentFile();
192: featureFile = createFeatureFile(dir);
193: }
194: if (featureFile.exists()) {
195: SystemFileEditorInput in = new SystemFileEditorInput(
196: featureFile);
197: manager.putContext(in, new FeatureInputContext(this , in,
198: file == featureFile));
199: }
200: if (buildFile.exists()) {
201: SystemFileEditorInput in = new SystemFileEditorInput(
202: buildFile);
203: manager.putContext(in, new BuildInputContext(this , in,
204: file == buildFile));
205: }
206: }
207:
208: private File createFeatureFile(File dir) {
209: File pluginFile = new File(dir, "plugin.xml"); //$NON-NLS-1$
210: return pluginFile;
211: }
212:
213: private IFile createFeatureFile(IProject project) {
214: IFile featureFile = project.getFile("feature.xml"); //$NON-NLS-1$
215: return featureFile;
216: }
217:
218: protected void createStorageContexts(InputContextManager manager,
219: IStorageEditorInput input) {
220: String name = input.getName().toLowerCase(Locale.ENGLISH);
221: if (name.equals("build.properties")) { //$NON-NLS-1$
222: manager.putContext(input, new BuildInputContext(this ,
223: input, true));
224: } else if (name.startsWith("feature.xml")) { //$NON-NLS-1$
225: manager.putContext(input, new FeatureInputContext(this ,
226: input, true));
227: }
228: }
229:
230: protected void addEditorPages() {
231: try {
232: addPage(new FeatureFormPage(this ,
233: PDEUIMessages.FeatureEditor_FeaturePage_title));
234: addPage(new InfoFormPage(this ,
235: PDEUIMessages.FeatureEditor_InfoPage_title));
236: addPage(new FeatureReferencePage(this ,
237: PDEUIMessages.FeatureEditor_ReferencePage_title));
238: addPage(new FeatureIncludesPage(this ,
239: PDEUIMessages.FeatureEditor_IncludesPage_title));
240: addPage(new FeatureDependenciesPage(this ,
241: PDEUIMessages.FeatureEditor_DependenciesPage_title));
242: addPage(new FeatureAdvancedPage(this ,
243: PDEUIMessages.FeatureEditor_AdvancedPage_title));
244: if (fInputContextManager
245: .hasContext(BuildInputContext.CONTEXT_ID))
246: addPage(new BuildPage(this ));
247: } catch (PartInitException e) {
248: PDEPlugin.logException(e);
249: }
250: addSourcePage(FeatureInputContext.CONTEXT_ID);
251: addSourcePage(BuildInputContext.CONTEXT_ID);
252: }
253:
254: protected String computeInitialPageId() {
255: String firstPageId = super .computeInitialPageId();
256: if (firstPageId == null) {
257: InputContext primary = fInputContextManager
258: .getPrimaryContext();
259: if (primary != null
260: && FeatureInputContext.CONTEXT_ID.equals(primary
261: .getId()))
262: firstPageId = FeatureFormPage.PAGE_ID;
263: if (firstPageId == null)
264: firstPageId = FeatureFormPage.PAGE_ID;
265: }
266: return firstPageId;
267: }
268:
269: /*
270: * (non-Javadoc)
271: *
272: * @see org.eclipse.pde.internal.ui.neweditor.MultiSourceEditor#createXMLSourcePage(org.eclipse.pde.internal.ui.neweditor.PDEFormEditor,
273: * java.lang.String, java.lang.String)
274: */
275: protected PDESourcePage createSourcePage(PDEFormEditor editor,
276: String title, String name, String contextId) {
277: if (contextId.equals(FeatureInputContext.CONTEXT_ID))
278: return new FeatureSourcePage(editor, title, name);
279: if (contextId.equals(BuildInputContext.CONTEXT_ID))
280: return new BuildSourcePage(editor, title, name);
281: return super .createSourcePage(editor, title, name, contextId);
282: }
283:
284: protected ISortableContentOutlinePage createContentOutline() {
285: return new FeatureOutlinePage(this );
286: }
287:
288: protected IPropertySheetPage getPropertySheet(PDEFormPage page) {
289: return null;
290: }
291:
292: public String getTitle() {
293: if (!isModelCorrect(getAggregateModel()))
294: return super .getTitle();
295: IFeatureModel model = (IFeatureModel) getAggregateModel();
296: String name = getTitleText(model.getFeature());
297: if (name == null)
298: return super .getTitle();
299: return model.getResourceString(name);
300: }
301:
302: public String getTitleProperty() {
303: IPreferenceStore store = PDEPlugin.getDefault()
304: .getPreferenceStore();
305: String pref = store
306: .getString(IPreferenceConstants.PROP_SHOW_OBJECTS);
307: if (pref != null
308: && pref.equals(IPreferenceConstants.VALUE_USE_NAMES))
309: return IFeatureObject.P_LABEL;
310: return IIdentifiable.P_ID;
311: }
312:
313: private String getTitleText(IFeature feature) {
314: IPreferenceStore store = PDEPlugin.getDefault()
315: .getPreferenceStore();
316: String pref = store
317: .getString(IPreferenceConstants.PROP_SHOW_OBJECTS);
318: if (pref != null
319: && pref.equals(IPreferenceConstants.VALUE_USE_NAMES))
320: return feature.getTranslatableLabel();
321: return feature.getId();
322: }
323:
324: protected boolean isModelCorrect(Object model) {
325: return model != null ? ((IFeatureModel) model).isValid()
326: : false;
327: }
328:
329: protected boolean hasKnownTypes() {
330: try {
331: TransferData[] types = getClipboard().getAvailableTypes();
332: Transfer[] transfers = new Transfer[] {
333: TextTransfer.getInstance(),
334: RTFTransfer.getInstance() };
335: for (int i = 0; i < types.length; i++) {
336: for (int j = 0; j < transfers.length; j++) {
337: if (transfers[j].isSupportedType(types[i]))
338: return true;
339: }
340: }
341: } catch (SWTError e) {
342: }
343: return false;
344: }
345:
346: public Object getAdapter(Class key) {
347: // No property sheet needed - block super
348: if (key.equals(IPropertySheetPage.class)) {
349: return null;
350: }
351: return super .getAdapter(key);
352: }
353:
354: /*
355: * (non-Javadoc)
356: *
357: * @see org.eclipse.pde.internal.ui.editor.PDEFormEditor#getInputContext(java.lang.Object)
358: */
359: protected InputContext getInputContext(Object object) {
360: InputContext context = null;
361: if (object instanceof IBuildObject) {
362: context = fInputContextManager
363: .findContext(BuildInputContext.CONTEXT_ID);
364: } else if (object instanceof IFeatureObject) {
365: context = fInputContextManager
366: .findContext(FeatureInputContext.CONTEXT_ID);
367: }
368: return context;
369: }
370:
371: protected boolean isPatchEditor() {
372: IBaseModel model = getAggregateModel();
373: if (model == null || !(model instanceof IFeatureModel)) {
374: return false;
375: }
376: IFeature feature = ((IFeatureModel) model).getFeature();
377: IFeatureImport[] imports = feature.getImports();
378: for (int i = 0; i < imports.length; i++) {
379: if (imports[i].isPatch()) {
380: return true;
381: }
382: }
383: return false;
384: }
385:
386: public void showEditorInput(IEditorInput editorInput) {
387: String name = editorInput.getName();
388: if (name.equals("feature.xml")) { //$NON-NLS-1$
389: setActivePage(0);
390: } else {
391: setActivePage(getPageCount() - 3);
392: }
393: }
394:
395: protected Action getFeatureExportAction() {
396: if (fExportAction == null) {
397: fExportAction = new Action() {
398: public void run() {
399: doSave(null);
400: FeatureEditorContributor contributor = (FeatureEditorContributor) getContributor();
401: contributor.getBuildAction().run();
402: }
403: };
404: fExportAction
405: .setToolTipText(PDEUIMessages.FeatureEditor_exportTooltip);
406: fExportAction
407: .setImageDescriptor(PDEPluginImages.DESC_EXPORT_FEATURE_TOOL);
408: }
409: return fExportAction;
410: }
411:
412: public void contributeToToolbar(IToolBarManager manager) {
413: manager.add(getFeatureExportAction());
414: }
415: }
|