Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e0940623ceafbe6695d7dfb53f17268a279de3a5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*******************************************************************************
 * Copyright (c) 2004, 2005 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.jst.jsf.facesconfig.ui.pageflow.command;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.jst.jsf.common.ui.internal.logging.Logger;
import org.eclipse.jst.jsf.facesconfig.ui.EditorPlugin;
import org.eclipse.jst.jsf.facesconfig.ui.FacesConfigEditor;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.PageflowMessages;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.model.PageflowPage;
import org.eclipse.jst.jsf.facesconfig.ui.pageflow.model.impl.PageflowNodeImpl;
import org.eclipse.jst.jsf.facesconfig.ui.util.WebrootUtil;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorDescriptor;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.part.FileEditorInput;

/**
 * 
 * This is the Command for opening a file in its default editor
 * 
 * @author Xiao-guang Zhang
 */
public class OpenEditorCommand extends Command {
	/** The selected object */
	private PageflowNodeImpl child = null;

	/** The edit part */
	private EditPart part;

	/** log instance */
	private static final Logger log = EditorPlugin
			.getLogger(FacesConfigEditor.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see Command#canExecute()
	 */
	public boolean canExecute() {
		return true;
	}

	/**
	 * The constructor
	 * 
	 * @param part -
	 *            the EditPart
	 */
	public OpenEditorCommand(EditPart part) {
		// Pageflow.Commands.OpenEditorCommand.Label = Open Editor
		super(PageflowMessages.Pageflow_Commands_OpenEditorCommand_Label);
		this.part = part;
	}

	/**
	 * Sets the selected object
	 * 
	 * @param child -
	 *            the PFPageImpl
	 */
	public void setChild(PageflowNodeImpl child) {
		this.child = child;
	}

	public PageflowNodeImpl getChild() {
		return child;
	}

	/**
	 * open existed JSF file or call the wizard to create a new one.
	 * 
	 */
	private void executeOpenPFPage() {
		IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
		String pagePath = ((PageflowPage) (part.getModel())).getPath();

		// Check the fileName is empty or not
		if (pagePath != null && pagePath.length() > 0) {
			String resourceName = WebrootUtil.getProjectPath((EObject) part
					.getModel(), pagePath);
			Path resourcePath = new Path(resourceName);
			if (resourcePath.getFileExtension() != null) {
				final IFile file = (IFile) workspaceRoot
						.findMember(resourcePath);
				openExistingJSFFile(file);
			} else {
				// Pageflow.PageflowEditor.Alert.errorOpenEditor4JSFTitle =
				// Open JSF File Error
				// Pageflow.PageflowEditor.Alert.errorOpenEditor4JSFFolderInfo =
				// Cannot open the page in the page editor.
				EditorPlugin
						.getAlerts()
						.error(
								"Pageflow.PageflowEditor.Alert.errorOpenEditor4JSFTitle",
								"Pageflow.PageflowEditor.Alert.errorOpenEditor4JSFFolderInfo");
			}
		} else
		// if the fileName is empty, a new jsf file should be created!
		{
			// Pageflow.PageflowEditor.Alert.errorOpenEditor4JSFTitle = Open JSF
			// File Error
			// Pageflow.PageflowEditor.Alert.confirmCreateNewJSFFile = No jsp
			// file is related with this page.
			EditorPlugin.getAlerts().error(
					"Pageflow.PageflowEditor.Alert.errorOpenEditor4JSFTitle",
					"Pageflow.PageflowEditor.Alert.confirmCreateNewJSFFile");// )
		}
	}

	/**
	 * open existing jsf file in a new editor.
	 * 
	 * @param file
	 */
	private void openExistingJSFFile(final IFile file) {
		// if the file is existed, open it.
		if (null != file && file.exists()) {
			Display display = PlatformUI.getWorkbench()
					.getActiveWorkbenchWindow().getShell().getDisplay();
			display.asyncExec(new Runnable() {
				public void run() {
					IDE.setDefaultEditor(file, null);
					try {
						IWorkbenchPage page = PlatformUI.getWorkbench()
								.getActiveWorkbenchWindow().getActivePage();
						IEditorDescriptor desc = IDE.getEditorDescriptor(file);
						page.openEditor(new FileEditorInput(file),
								desc.getId(), true, IWorkbenchPage.MATCH_INPUT
										| IWorkbenchPage.MATCH_ID);
					} catch (PartInitException e) {
						// Pageflow.PageflowEditor.Error.CanNotOpenEditor4JSF =
						// The jsf file can not be opened in the editor.
						log
								.error(
										"Pageflow.PageflowEditor.Error.canNotOpenEditor4JSF",
										e);
						// Pageflow.PageflowEditor.Alert.errorOpenEditor4JSFTitle
						// = Open JSF File Error
						EditorPlugin
								.getAlerts()
								.error(
										"Pageflow.PageflowEditor.Alert.errorOpenEditor4JSFTitle",
										"Pageflow.PageflowEditor.Error.CanNotOpenEditor4JSF");
					}
				}
			});
		} else
		// otherwise, pop-up a error message box
		{
			String pagePath = ((PageflowPage) (part.getModel())).getPath();
			// Pageflow.PageflowEditor.Alert.errorOpenEditor4JSFTitle = Open JSF
			// File Error
			// Pageflow.PageflowEditor.Alert.errorOpenEditor4JSFInfo = The JSF
			// file is not existed.
			EditorPlugin.getAlerts().error(
					"Pageflow.PageflowEditor.Alert.errorOpenEditor4JSFTitle",
					"Pageflow.PageflowEditor.Alert.errorOpenEditor4JSFInfo",
					pagePath);
		}
	}

	/**
	 * Executes the OpenEditorCommand and opens the editor
	 */
	public void execute() {
		if (part.getModel() instanceof PageflowPage) {
			executeOpenPFPage();
		}
	}
}

Back to the top