KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > schema > SchemaPreviewLauncher


1 /*******************************************************************************
2  * Copyright (c) 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
12 package org.eclipse.pde.internal.ui.editor.schema;
13
14 import org.eclipse.core.resources.IFile;
15 import org.eclipse.core.resources.ResourcesPlugin;
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.runtime.NullProgressMonitor;
18 import org.eclipse.jface.action.IAction;
19 import org.eclipse.jface.dialogs.MessageDialog;
20 import org.eclipse.jface.viewers.StructuredSelection;
21 import org.eclipse.pde.internal.ui.PDEUIMessages;
22 import org.eclipse.pde.internal.ui.search.PreviewReferenceAction;
23 import org.eclipse.pde.internal.ui.util.PDEModelUtility;
24 import org.eclipse.swt.widgets.Display;
25 import org.eclipse.ui.IEditorLauncher;
26
27 /**
28  * SchemaPreviewLauncher
29  *
30  */

31 public class SchemaPreviewLauncher implements IEditorLauncher {
32
33     /* (non-Javadoc)
34      * @see org.eclipse.ui.IEditorLauncher#open(org.eclipse.core.runtime.IPath)
35      */

36     public void open(IPath filePath) {
37         // Create the preview action
38
PreviewReferenceAction action = new PreviewReferenceAction();
39         // Get the file in the workspace which the user right-clicked on and
40
// selected "Open With"
41
IFile file =
42             ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(filePath);
43         // Ensure the file is defined
44
if (file == null) {
45             // This should never happen
46
// Probably workspace out of sync with the file system
47
Display.getDefault().beep();
48         } else {
49             // If an associated schema editor is open and contains unsaved
50
// changes, prompt the user first asking whether to save these
51
// changes before launching the schema preview
52
handleUnsavedOpenSchemaEditor(file);
53             // Perform the preview schema action
54
// Action parameter not used (unnecessary)
55
IAction emptyAction = null;
56             // Set data
57
action.selectionChanged(emptyAction, new StructuredSelection(file));
58             // Run action
59
action.run(emptyAction);
60         }
61     }
62
63     /**
64      * @param file
65      */

66     private void handleUnsavedOpenSchemaEditor(IFile file) {
67         // Get the open schema editor with the specified underlying file
68
// (if there is any)
69
SchemaEditor editor = PDEModelUtility.getOpenSchemaEditor(file);
70         // Ensure we have a dirty editor
71
if (editor == null) {
72             // No matching open editor found
73
return;
74         } else if (editor.isDirty() == false) {
75             // Matching open editor found that is NOT dirty
76
return;
77         }
78         // Matching open editor found that IS dirty
79
// Open a dialog asking the user whether they would like to save the
80
// editor
81
boolean doSave =
82             MessageDialog.openQuestion(
83                     Display.getDefault().getActiveShell(),
84                     PDEUIMessages.SchemaPreviewLauncher_msgEditorHasUnsavedChanges,
85                     PDEUIMessages.SchemaPreviewLauncher_msgSaveChanges);
86         // Save the editor if the user indicated so
87
if (doSave) {
88             editor.doSave(new NullProgressMonitor());
89         }
90     }
91
92 }
93
Popular Tags