01: /*******************************************************************************
02: * Copyright (c) 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.schema;
11:
12: import org.eclipse.core.resources.IFile;
13: import org.eclipse.core.resources.ResourcesPlugin;
14: import org.eclipse.core.runtime.IPath;
15: import org.eclipse.core.runtime.NullProgressMonitor;
16: import org.eclipse.jface.action.IAction;
17: import org.eclipse.jface.dialogs.MessageDialog;
18: import org.eclipse.jface.viewers.StructuredSelection;
19: import org.eclipse.pde.internal.ui.PDEUIMessages;
20: import org.eclipse.pde.internal.ui.search.PreviewReferenceAction;
21: import org.eclipse.pde.internal.ui.util.PDEModelUtility;
22: import org.eclipse.swt.widgets.Display;
23: import org.eclipse.ui.IEditorLauncher;
24:
25: /**
26: * SchemaPreviewLauncher
27: *
28: */
29: public class SchemaPreviewLauncher implements IEditorLauncher {
30:
31: /* (non-Javadoc)
32: * @see org.eclipse.ui.IEditorLauncher#open(org.eclipse.core.runtime.IPath)
33: */
34: public void open(IPath filePath) {
35: // Create the preview action
36: PreviewReferenceAction action = new PreviewReferenceAction();
37: // Get the file in the workspace which the user right-clicked on and
38: // selected "Open With"
39: IFile file = ResourcesPlugin.getWorkspace().getRoot()
40: .getFileForLocation(filePath);
41: // Ensure the file is defined
42: if (file == null) {
43: // This should never happen
44: // Probably workspace out of sync with the file system
45: Display.getDefault().beep();
46: } else {
47: // If an associated schema editor is open and contains unsaved
48: // changes, prompt the user first asking whether to save these
49: // changes before launching the schema preview
50: handleUnsavedOpenSchemaEditor(file);
51: // Perform the preview schema action
52: // Action parameter not used (unnecessary)
53: IAction emptyAction = null;
54: // Set data
55: action.selectionChanged(emptyAction,
56: new StructuredSelection(file));
57: // Run action
58: action.run(emptyAction);
59: }
60: }
61:
62: /**
63: * @param file
64: */
65: private void handleUnsavedOpenSchemaEditor(IFile file) {
66: // Get the open schema editor with the specified underlying file
67: // (if there is any)
68: SchemaEditor editor = PDEModelUtility.getOpenSchemaEditor(file);
69: // Ensure we have a dirty editor
70: if (editor == null) {
71: // No matching open editor found
72: return;
73: } else if (editor.isDirty() == false) {
74: // Matching open editor found that is NOT dirty
75: return;
76: }
77: // Matching open editor found that IS dirty
78: // Open a dialog asking the user whether they would like to save the
79: // editor
80: boolean doSave = MessageDialog
81: .openQuestion(
82: Display.getDefault().getActiveShell(),
83: PDEUIMessages.SchemaPreviewLauncher_msgEditorHasUnsavedChanges,
84: PDEUIMessages.SchemaPreviewLauncher_msgSaveChanges);
85: // Save the editor if the user indicated so
86: if (doSave) {
87: editor.doSave(new NullProgressMonitor());
88: }
89: }
90:
91: }
|