• R/O
  • SSH
  • HTTPS

maskat: Commit


Commit MetaInfo

Révision114 (tree)
l'heure2009-03-03 12:16:14
Auteurkazatoh

Message de Log

マスカットIDE 2.0.0 のソースフォルダに合わせてリソースを移動

Change Summary

Modification

--- maskat-ide/trunk/org.maskat.ui/src/org/maskat/ui/wizards/application/NewMaskatApplicationWizard.java (nonexistent)
+++ maskat-ide/trunk/org.maskat.ui/src/org/maskat/ui/wizards/application/NewMaskatApplicationWizard.java (revision 114)
@@ -0,0 +1,213 @@
1+/*
2+ * Copyright 2006 Maskat Project.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+package org.maskat.ui.wizards.application;
17+
18+import java.io.BufferedReader;
19+import java.io.ByteArrayInputStream;
20+import java.io.IOException;
21+import java.io.InputStream;
22+import java.io.InputStreamReader;
23+import java.lang.reflect.InvocationTargetException;
24+
25+import org.eclipse.core.resources.IContainer;
26+import org.eclipse.core.resources.IFile;
27+import org.eclipse.core.resources.IResource;
28+import org.eclipse.core.resources.IWorkspaceRoot;
29+import org.eclipse.core.resources.ResourcesPlugin;
30+import org.eclipse.core.runtime.CoreException;
31+import org.eclipse.core.runtime.IProgressMonitor;
32+import org.eclipse.core.runtime.IStatus;
33+import org.eclipse.core.runtime.Path;
34+import org.eclipse.core.runtime.Status;
35+import org.eclipse.jface.dialogs.MessageDialog;
36+import org.eclipse.jface.operation.IRunnableWithProgress;
37+import org.eclipse.jface.viewers.ISelection;
38+import org.eclipse.jface.viewers.IStructuredSelection;
39+import org.eclipse.jface.wizard.Wizard;
40+import org.eclipse.ui.INewWizard;
41+import org.eclipse.ui.IWorkbench;
42+import org.eclipse.ui.IWorkbenchPage;
43+import org.eclipse.ui.IWorkbenchWizard;
44+import org.eclipse.ui.PartInitException;
45+import org.eclipse.ui.PlatformUI;
46+import org.eclipse.ui.ide.IDE;
47+import org.maskat.ui.MaskatUIPlugin;
48+
49+/**
50+ * This is a sample new wizard. Its role is to create a new file resource in the
51+ * provided container. If the container resource (a folder or a project) is
52+ * selected in the workspace when the wizard is opened, it will accept it as the
53+ * target container. The wizard creates one file with the extension "html". If a
54+ * sample multi-page editor (also available as a template) is registered for the
55+ * same extension, it will be able to open it.
56+ */
57+
58+public class NewMaskatApplicationWizard extends Wizard implements INewWizard {
59+ private NewMaskatApplicationWizardPage page;
60+
61+ private ISelection selection;
62+
63+ /**
64+ * Constructor for MaskatHtmlContainerNewWizard.
65+ */
66+ public NewMaskatApplicationWizard() {
67+ super();
68+ this.setWindowTitle("Maskat Container HTML Wizard");
69+ setNeedsProgressMonitor(true);
70+ }
71+
72+ /**
73+ * Adding the page to the wizard.
74+ */
75+
76+ public void addPages() {
77+ page = new NewMaskatApplicationWizardPage(selection);
78+ addPage(page);
79+ }
80+
81+ /**
82+ * This method is called when 'Finish' button is pressed in the wizard. We
83+ * will create an operation and run it using wizard as execution context.
84+ */
85+ public boolean performFinish() {
86+ final String containerName = page.getContainerName();
87+ final String fileName = page.getFileName();
88+ final HtmlContainerParam param = new HtmlContainerParam();
89+ param.setLayoutFileName(page.getLayoutFileName());
90+ param.setEventFileName(page.getEventFileName());
91+ param.setTitle(page.getHTMLTitle());
92+ param.setMaskatFWPath(page.getFWPath());
93+ param.setHeight(page.getDivHeight());
94+ param.setWidth(page.getDivWidth());
95+ IRunnableWithProgress op = new IRunnableWithProgress() {
96+ public void run(IProgressMonitor monitor) throws InvocationTargetException {
97+ try {
98+ doFinish(containerName, fileName, param, monitor);
99+ } catch (CoreException e) {
100+ throw new InvocationTargetException(e);
101+ } finally {
102+ monitor.done();
103+ }
104+ }
105+ };
106+ try {
107+ getContainer().run(true, false, op);
108+ } catch (InterruptedException e) {
109+ return false;
110+ } catch (InvocationTargetException e) {
111+ Throwable realException = e.getTargetException();
112+ MessageDialog.openError(getShell(), "Error", realException.getMessage());
113+ return false;
114+ }
115+ return true;
116+ }
117+
118+ /**
119+ * The worker method. It will find the container, create the file if missing
120+ * or just replace its contents, and open the editor on the newly created
121+ * file.
122+ */
123+
124+ private void doFinish(String containerName, String fileName,
125+ HtmlContainerParam param, IProgressMonitor monitor) throws CoreException {
126+
127+ monitor.beginTask("Creating " + fileName, 2);
128+ IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
129+ IResource resource = root.findMember(new Path(containerName));
130+ if (!resource.exists() || !(resource instanceof IContainer)) {
131+ throwCoreException("Container \"" + containerName + "\" does not exist.");
132+ }
133+ IContainer container = (IContainer) resource;
134+ final IFile file = container.getFile(new Path(fileName));
135+ try {
136+ InputStream stream = openContentStream(param);
137+ if (file.exists()) {
138+ throwCoreException("File already exists. Choose another name.");
139+ } else {
140+ file.create(stream, true, monitor);
141+ }
142+ stream.close();
143+ } catch (IOException e) {
144+ }
145+ monitor.worked(1);
146+ monitor.setTaskName("Opening file for editing...");
147+ getShell().getDisplay().asyncExec(new Runnable() {
148+ public void run() {
149+ IWorkbenchPage page = PlatformUI.getWorkbench()
150+ .getActiveWorkbenchWindow().getActivePage();
151+ try {
152+ IDE.openEditor(page, file, true);
153+ } catch (PartInitException e) {
154+ }
155+ }
156+ });
157+ monitor.worked(1);
158+ }
159+
160+ private static String containerTemplate = null;
161+
162+ private String getTemplate() throws IOException {
163+ if (containerTemplate == null) {
164+ containerTemplate = "";
165+ InputStream stream = null;
166+ try {
167+ stream = this.getClass().getResourceAsStream(
168+ "containerHtmlTemplate.template");
169+ BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
170+ String line = null;
171+ while ((line = reader.readLine()) != null) {
172+ containerTemplate += line + System.getProperty("line.separator");
173+ }
174+ } finally {
175+ if (stream != null)
176+ stream.close();
177+ }
178+ }
179+ return containerTemplate;
180+ }
181+
182+ /**
183+ * We will initialize file contents with a sample text.
184+ *
185+ * @throws IOException
186+ */
187+ private InputStream openContentStream(HtmlContainerParam param) throws IOException {
188+
189+ String contents = getTemplate().replaceAll("<\\$title\\$>", param.getTitle())
190+ .replaceAll("<\\$maskatFWPath\\$>", param.getMaskatFWPath()).replaceAll(
191+ "<\\$layoutFileName\\$>", param.getLayoutFileName()).replaceAll(
192+ "<\\$eventFileName\\$>", param.getEventFileName()).replaceAll(
193+ "<\\$containerWidth\\$>", String.valueOf(param.getWidth()))
194+ .replaceAll("<\\$containerHeight\\$>", String.valueOf(param.getHeight()));
195+ return new ByteArrayInputStream(contents.getBytes("UTF-8"));
196+ }
197+
198+ private void throwCoreException(String message) throws CoreException {
199+ IStatus status = new Status(IStatus.ERROR, MaskatUIPlugin.PLUGIN_ID, IStatus.OK,
200+ message, null);
201+ throw new CoreException(status);
202+ }
203+
204+ /**
205+ * We will accept the selection in the workbench to see if we can initialize
206+ * from it.
207+ *
208+ * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
209+ */
210+ public void init(IWorkbench workbench, IStructuredSelection selection) {
211+ this.selection = selection;
212+ }
213+}
\ No newline at end of file
--- maskat-ide/trunk/org.maskat.ui/src/org/maskat/ui/wizards/application/NewMaskatApplicationWizardPage.java (nonexistent)
+++ maskat-ide/trunk/org.maskat.ui/src/org/maskat/ui/wizards/application/NewMaskatApplicationWizardPage.java (revision 114)
@@ -0,0 +1,375 @@
1+/*
2+ * Copyright 2006 Maskat Project.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+package org.maskat.ui.wizards.application;
17+
18+import org.eclipse.core.resources.IContainer;
19+import org.eclipse.core.resources.IFile;
20+import org.eclipse.core.resources.IResource;
21+import org.eclipse.core.resources.ResourcesPlugin;
22+import org.eclipse.core.runtime.Path;
23+import org.eclipse.jface.dialogs.IDialogPage;
24+import org.eclipse.jface.viewers.ILabelProvider;
25+import org.eclipse.jface.viewers.ISelection;
26+import org.eclipse.jface.viewers.IStructuredSelection;
27+import org.eclipse.jface.viewers.ITreeContentProvider;
28+import org.eclipse.jface.wizard.WizardPage;
29+import org.eclipse.swt.SWT;
30+import org.eclipse.swt.events.ModifyEvent;
31+import org.eclipse.swt.events.ModifyListener;
32+import org.eclipse.swt.events.SelectionAdapter;
33+import org.eclipse.swt.events.SelectionEvent;
34+import org.eclipse.swt.layout.GridData;
35+import org.eclipse.swt.layout.GridLayout;
36+import org.eclipse.swt.widgets.Button;
37+import org.eclipse.swt.widgets.Composite;
38+import org.eclipse.swt.widgets.Label;
39+import org.eclipse.swt.widgets.Text;
40+import org.eclipse.ui.dialogs.ContainerSelectionDialog;
41+import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
42+import org.eclipse.ui.model.WorkbenchContentProvider;
43+import org.eclipse.ui.model.WorkbenchLabelProvider;
44+import org.eclipse.ui.views.navigator.ResourceSorter;
45+
46+/**
47+ * The "New" wizard page allows setting the container for the new file as well
48+ * as the file name. The page will only accept file name without the extension
49+ * OR with the extension that matches the expected one (html).
50+ */
51+
52+public class NewMaskatApplicationWizardPage extends WizardPage {
53+ private Text containerText;
54+
55+ private Text fileText;
56+
57+ private Text titleText, layoutFileText, eventFileText, maskatFWPathText;
58+
59+ private Text widthText, heightText;
60+
61+ private ISelection selection;
62+
63+ /**
64+ * Constructor for SampleNewWizardPage.
65+ *
66+ * @param pageName
67+ */
68+ public NewMaskatApplicationWizardPage(ISelection selection) {
69+ super("wizardPage");
70+ setTitle("Maskat Container HTML");
71+ setDescription("This wizard creates a container html file for maskat framework.");
72+ this.selection = selection;
73+ }
74+
75+ /**
76+ * @see IDialogPage#createControl(Composite)
77+ */
78+ public void createControl(Composite parent) {
79+ ModifyListener textModifyListener = new ModifyListener() {
80+ public void modifyText(ModifyEvent e) {
81+ dialogChanged();
82+ }
83+ };
84+ Composite container = new Composite(parent, SWT.NULL);
85+ GridLayout layout = new GridLayout();
86+ container.setLayout(layout);
87+ layout.numColumns = 5;
88+ layout.verticalSpacing = 9;
89+ Label label = new Label(container, SWT.NULL);
90+ label.setText("&Container:");
91+
92+ containerText = new Text(container, SWT.BORDER | SWT.SINGLE);
93+ GridData gd = new GridData(GridData.FILL_HORIZONTAL);
94+ gd.horizontalSpan = 3;
95+ containerText.setLayoutData(gd);
96+ containerText.addModifyListener(textModifyListener);
97+
98+ Button button = new Button(container, SWT.PUSH);
99+ button.setText("Browse...");
100+ button.addSelectionListener(new SelectionAdapter() {
101+ public void widgetSelected(SelectionEvent e) {
102+ handleBrowse();
103+ }
104+ });
105+ label = new Label(container, SWT.NULL);
106+ label.setText("&File name:");
107+
108+ fileText = new Text(container, SWT.BORDER | SWT.SINGLE);
109+ gd = new GridData(GridData.FILL_HORIZONTAL);
110+ gd.horizontalSpan = 4;
111+ fileText.setLayoutData(gd);
112+ fileText.addModifyListener(textModifyListener);
113+
114+ label = new Label(container, SWT.NULL);
115+ label.setText("&Title:");
116+
117+ titleText = new Text(container, SWT.BORDER | SWT.SINGLE);
118+ titleText.setText("Maskat Application");
119+ gd = new GridData(GridData.FILL_HORIZONTAL);
120+ gd.horizontalSpan = 4;
121+ titleText.setLayoutData(gd);
122+ titleText.addModifyListener(textModifyListener);
123+
124+ label = new Label(container, SWT.NULL);
125+ label.setText("&Maskat FW Path:");
126+
127+ maskatFWPathText = new Text(container, SWT.BORDER | SWT.SINGLE);
128+ maskatFWPathText.setText("../../maskatFW");
129+ gd = new GridData(GridData.FILL_HORIZONTAL);
130+ gd.horizontalSpan = 4;
131+ maskatFWPathText.setLayoutData(gd);
132+ maskatFWPathText.addModifyListener(textModifyListener);
133+
134+ label = new Label(container, SWT.NULL);
135+ label.setText("&Layout File:");
136+
137+ layoutFileText = new Text(container, SWT.BORDER | SWT.SINGLE);
138+ gd = new GridData(GridData.FILL_HORIZONTAL);
139+ gd.horizontalSpan = 3;
140+ layoutFileText.setLayoutData(gd);
141+ layoutFileText.addModifyListener(textModifyListener);
142+
143+ button = new Button(container, SWT.PUSH);
144+ button.setText("Browse...");
145+ button.addSelectionListener(new SelectionAdapter() {
146+ public void widgetSelected(SelectionEvent e) {
147+ handleBrowseLayout();
148+ }
149+ });
150+
151+ label = new Label(container, SWT.NULL);
152+ label.setText("&Event File:");
153+
154+ eventFileText = new Text(container, SWT.BORDER | SWT.SINGLE);
155+ gd = new GridData(GridData.FILL_HORIZONTAL);
156+ gd.horizontalSpan = 3;
157+ eventFileText.setLayoutData(gd);
158+ eventFileText.addModifyListener(textModifyListener);
159+
160+ button = new Button(container, SWT.PUSH);
161+ button.setText("Browse...");
162+ button.addSelectionListener(new SelectionAdapter() {
163+ public void widgetSelected(SelectionEvent e) {
164+ handleBrowseEvent();
165+ }
166+ });
167+
168+ label = new Label(container, SWT.NULL);
169+ label.setText("Div &width:");
170+
171+ widthText = new Text(container, SWT.BORDER | SWT.SINGLE);
172+ widthText.setText("800");
173+ gd = new GridData(GridData.FILL_HORIZONTAL);
174+ widthText.setLayoutData(gd);
175+ widthText.addModifyListener(textModifyListener);
176+
177+ label = new Label(container, SWT.NULL);
178+ label.setText("Div &height:");
179+
180+ heightText = new Text(container, SWT.BORDER | SWT.SINGLE);
181+ heightText.setText("600");
182+ gd = new GridData(GridData.FILL_HORIZONTAL);
183+ heightText.setLayoutData(gd);
184+ heightText.addModifyListener(textModifyListener);
185+
186+ initialize();
187+ dialogChanged();
188+ setControl(container);
189+ }
190+
191+ protected void handleBrowseLayout() {
192+ String value = handleBrowserResource();
193+ if (value != null)
194+ layoutFileText.setText(value);
195+ }
196+
197+ protected void handleBrowseEvent() {
198+ String value = handleBrowserResource();
199+ if (value != null)
200+ eventFileText.setText(value);
201+ }
202+
203+ protected String handleBrowserResource() {
204+ ILabelProvider lp = new WorkbenchLabelProvider();
205+ ITreeContentProvider cp = new WorkbenchContentProvider();
206+
207+ ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(this
208+ .getShell(), lp, cp);
209+ dialog.setTitle("Select file");
210+ dialog.setMessage("Select a file from the list:");
211+ dialog.addFilter(new FileExtensionFilter("xml"));
212+ if (container != null && container.getProject() != null)
213+ dialog.setInput(container.getProject());
214+ else
215+ dialog.setInput(ResourcesPlugin.getWorkspace().getRoot());
216+ dialog.setSorter(new ResourceSorter(ResourceSorter.NAME));
217+
218+ if (dialog.open() == ContainerSelectionDialog.OK) {
219+ Object[] result = dialog.getResult();
220+ if (result.length == 1) {
221+ this.getDialogSettings();
222+ return ((IFile) result[0]).getName();
223+ }
224+ }
225+ return null;
226+ }
227+
228+ private IContainer container = null;
229+
230+ /**
231+ * Tests if the current workbench selection is a suitable container to use.
232+ */
233+
234+ private void initialize() {
235+ if (selection != null && selection.isEmpty() == false
236+ && selection instanceof IStructuredSelection) {
237+ IStructuredSelection ssel = (IStructuredSelection) selection;
238+ if (ssel.size() > 1)
239+ return;
240+ Object obj = ssel.getFirstElement();
241+ if (obj instanceof IResource) {
242+ if (obj instanceof IContainer)
243+ container = (IContainer) obj;
244+ else
245+ container = ((IResource) obj).getParent();
246+ containerText.setText(container.getFullPath().toString());
247+ }
248+ }
249+ fileText.setText("container.html");
250+ }
251+
252+ /**
253+ * Uses the standard container selection dialog to choose the new value for
254+ * the container field.
255+ */
256+
257+ private void handleBrowse() {
258+ ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(),
259+ ResourcesPlugin.getWorkspace().getRoot(), true,
260+ "Select new file container");
261+ if (dialog.open() == ContainerSelectionDialog.OK) {
262+ Object[] result = dialog.getResult();
263+ if (result.length == 1) {
264+ containerText.setText(((Path) result[0]).toString());
265+ }
266+ }
267+ }
268+
269+ /**
270+ * Ensures that both text fields are set.
271+ */
272+
273+ private void dialogChanged() {
274+ IResource container = ResourcesPlugin.getWorkspace().getRoot().findMember(
275+ new Path(getContainerName()));
276+ String fileName = getFileName();
277+
278+ if (getContainerName().length() == 0) {
279+ updateStatus("File container must be specified");
280+ return;
281+ }
282+ if (container == null
283+ || (container.getType() & (IResource.PROJECT | IResource.FOLDER)) == 0) {
284+ updateStatus("File container must exist");
285+ return;
286+ }
287+ if (!container.isAccessible()) {
288+ updateStatus("Project must be writable");
289+ return;
290+ }
291+ if (fileName.length() == 0) {
292+ updateStatus("File name must be specified");
293+ return;
294+ }
295+ if (fileName.replace('\\', '/').indexOf('/', 1) > 0) {
296+ updateStatus("File name must be valid");
297+ return;
298+ }
299+ int dotLoc = fileName.lastIndexOf('.');
300+ if (dotLoc != -1) {
301+ String ext = fileName.substring(dotLoc + 1);
302+ if (ext.equalsIgnoreCase("html") == false) {
303+ updateStatus("File extension must be \"html\"");
304+ return;
305+ }
306+ }
307+ if (layoutFileText.getText().length() == 0) {
308+ updateStatus("Layout File Name must be specified");
309+ return;
310+ }
311+ if (eventFileText.getText().length() == 0) {
312+ updateStatus("Event File Name must be specified");
313+ return;
314+ }
315+ if (!isPositiveInteger(widthText.getText())) {
316+ updateStatus("Div width must be a positive integer.");
317+ return;
318+ }
319+ if (!isPositiveInteger(heightText.getText())) {
320+ updateStatus("Div height must be a positive integer.");
321+ return;
322+ }
323+
324+ updateStatus(null);
325+ }
326+
327+ private boolean isPositiveInteger(String value) {
328+ try {
329+ int intVal = Integer.parseInt(value);
330+ if (intVal > 0) {
331+ return true;
332+ }
333+ } catch (NumberFormatException e) {
334+ return false;
335+ }
336+ return false;
337+ }
338+
339+ private void updateStatus(String message) {
340+ setErrorMessage(message);
341+ setPageComplete(message == null);
342+ }
343+
344+ public String getContainerName() {
345+ return containerText.getText();
346+ }
347+
348+ public String getFileName() {
349+ return fileText.getText();
350+ }
351+
352+ public String getHTMLTitle() {
353+ return titleText.getText();
354+ }
355+
356+ public String getLayoutFileName() {
357+ return layoutFileText.getText();
358+ }
359+
360+ public String getEventFileName() {
361+ return eventFileText.getText();
362+ }
363+
364+ public String getFWPath() {
365+ return maskatFWPathText.getText();
366+ }
367+
368+ public int getDivWidth() {
369+ return Integer.parseInt(widthText.getText());
370+ }
371+
372+ public int getDivHeight() {
373+ return Integer.parseInt(heightText.getText());
374+ }
375+}
\ No newline at end of file
Afficher sur ancien navigateur de dépôt.