• R/O
  • SSH
  • HTTPS

maskat: Commit


Commit MetaInfo

Révision112 (tree)
l'heure2009-03-03 12:04:48
Auteurkazatoh

Message de Log

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

Change Summary

Modification

--- maskat-ide/trunk/org.maskat.ui/src/org/maskat/ui/wizards/layout/NewLayoutFileWizardPage.java (nonexistent)
+++ maskat-ide/trunk/org.maskat.ui/src/org/maskat/ui/wizards/layout/NewLayoutFileWizardPage.java (revision 112)
@@ -0,0 +1,228 @@
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.layout;
17+
18+import org.eclipse.core.resources.IContainer;
19+import org.eclipse.core.resources.IResource;
20+import org.eclipse.core.resources.ResourcesPlugin;
21+import org.eclipse.core.runtime.Path;
22+import org.eclipse.jface.viewers.ISelection;
23+import org.eclipse.jface.viewers.IStructuredSelection;
24+import org.eclipse.jface.wizard.WizardPage;
25+import org.eclipse.swt.SWT;
26+import org.eclipse.swt.events.ModifyEvent;
27+import org.eclipse.swt.events.ModifyListener;
28+import org.eclipse.swt.events.SelectionAdapter;
29+import org.eclipse.swt.events.SelectionEvent;
30+import org.eclipse.swt.layout.GridData;
31+import org.eclipse.swt.layout.GridLayout;
32+import org.eclipse.swt.widgets.Button;
33+import org.eclipse.swt.widgets.Composite;
34+import org.eclipse.swt.widgets.Label;
35+import org.eclipse.swt.widgets.Text;
36+import org.eclipse.ui.dialogs.ContainerSelectionDialog;
37+
38+/**
39+ * The "New" wizard page allows setting the container for the new file as well
40+ * as the file name. The page will only accept file name without the extension
41+ * OR with the extension that matches the expected one (xml).
42+ */
43+
44+public class NewLayoutFileWizardPage extends WizardPage {
45+ private Text containerText;
46+
47+ private Text fileText;
48+
49+ private Text layoutNameText;
50+
51+ private ISelection selection;
52+
53+ /**
54+ * Constructor for SampleNewWizardPage.
55+ *
56+ * @param pageName
57+ */
58+ public NewLayoutFileWizardPage(ISelection selection) {
59+ super("wizardPage");
60+ setTitle("Maskat Layout File");
61+ setDescription("This wizard creates a Maskat layout file.");
62+ this.selection = selection;
63+ }
64+
65+ /**
66+ * @see IDialogPage#createControl(Composite)
67+ */
68+ public void createControl(Composite parent) {
69+ Composite container = new Composite(parent, SWT.NULL);
70+ GridLayout layout = new GridLayout();
71+ container.setLayout(layout);
72+ layout.numColumns = 3;
73+ layout.verticalSpacing = 9;
74+ Label label = new Label(container, SWT.NULL);
75+ label.setText("&Container:");
76+
77+ containerText = new Text(container, SWT.BORDER | SWT.SINGLE);
78+ GridData gd = new GridData(GridData.FILL_HORIZONTAL);
79+ containerText.setLayoutData(gd);
80+ containerText.addModifyListener(new ModifyListener() {
81+ public void modifyText(ModifyEvent e) {
82+ dialogChanged();
83+ }
84+ });
85+
86+ Button button = new Button(container, SWT.PUSH);
87+ button.setText("Browse...");
88+ button.addSelectionListener(new SelectionAdapter() {
89+ public void widgetSelected(SelectionEvent e) {
90+ handleBrowse();
91+ }
92+ });
93+ label = new Label(container, SWT.NULL);
94+ label.setText("&File name:");
95+
96+ fileText = new Text(container, SWT.BORDER | SWT.SINGLE);
97+ gd = new GridData(GridData.FILL_HORIZONTAL);
98+ gd.horizontalSpan = 2;
99+ fileText.setLayoutData(gd);
100+ fileText.addModifyListener(new ModifyListener() {
101+ public void modifyText(ModifyEvent e) {
102+ dialogChanged();
103+ }
104+ });
105+
106+ label = new Label(container, SWT.NULL);
107+ label.setText("&Layout name:");
108+
109+ layoutNameText = new Text(container, SWT.BORDER | SWT.SINGLE);
110+ layoutNameText.setText("newLayout");
111+ gd = new GridData(GridData.FILL_HORIZONTAL);
112+ gd.horizontalSpan = 2;
113+ layoutNameText.setLayoutData(gd);
114+ layoutNameText.addModifyListener(new ModifyListener() {
115+ public void modifyText(ModifyEvent e) {
116+ dialogChanged();
117+ }
118+ });
119+
120+ initialize();
121+ dialogChanged();
122+ setControl(container);
123+ }
124+
125+ /**
126+ * Tests if the current workbench selection is a suitable container to use.
127+ */
128+
129+ private void initialize() {
130+ if (selection != null && selection.isEmpty() == false
131+ && selection instanceof IStructuredSelection) {
132+ IStructuredSelection ssel = (IStructuredSelection) selection;
133+ if (ssel.size() > 1)
134+ return;
135+ Object obj = ssel.getFirstElement();
136+ if (obj instanceof IResource) {
137+ IContainer container;
138+ if (obj instanceof IContainer)
139+ container = (IContainer) obj;
140+ else
141+ container = ((IResource) obj).getParent();
142+ containerText.setText(container.getFullPath().toString());
143+ }
144+ }
145+ fileText.setText("newLayout.xml");
146+ }
147+
148+ /**
149+ * Uses the standard container selection dialog to choose the new value for
150+ * the container field.
151+ */
152+
153+ private void handleBrowse() {
154+ ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(),
155+ ResourcesPlugin.getWorkspace().getRoot(), false,
156+ "Select new file container");
157+ if (dialog.open() == ContainerSelectionDialog.OK) {
158+ Object[] result = dialog.getResult();
159+ if (result.length == 1) {
160+ containerText.setText(((Path) result[0]).toString());
161+ }
162+ }
163+ }
164+
165+ /**
166+ * Ensures that both text fields are set.
167+ */
168+
169+ private void dialogChanged() {
170+ IResource container = ResourcesPlugin.getWorkspace().getRoot().findMember(
171+ new Path(getContainerName()));
172+ String fileName = getFileName();
173+
174+ if (getContainerName().length() == 0) {
175+ updateStatus("File container must be specified");
176+ return;
177+ }
178+ if (container == null
179+ || (container.getType() & (IResource.PROJECT | IResource.FOLDER)) == 0) {
180+ updateStatus("File container must exist");
181+ return;
182+ }
183+ if (!container.isAccessible()) {
184+ updateStatus("Project must be writable");
185+ return;
186+ }
187+ if (fileName.length() == 0) {
188+ updateStatus("File name must be specified");
189+ return;
190+ }
191+ if (fileName.replace('\\', '/').indexOf('/', 1) > 0) {
192+ updateStatus("File name must be valid");
193+ return;
194+ }
195+ int dotLoc = fileName.lastIndexOf('.');
196+ if (dotLoc != -1) {
197+ String ext = fileName.substring(dotLoc + 1);
198+ if (ext.equalsIgnoreCase("xml") == false) {
199+ updateStatus("File extension must be \"xml\"");
200+ return;
201+ }
202+ }
203+ String layoutName = getLayoutName();
204+ if (layoutName.length() == 0) {
205+ updateStatus("The layout name must be specified");
206+ return;
207+ }
208+
209+ updateStatus(null);
210+ }
211+
212+ private void updateStatus(String message) {
213+ setErrorMessage(message);
214+ setPageComplete(message == null);
215+ }
216+
217+ public String getContainerName() {
218+ return containerText.getText();
219+ }
220+
221+ public String getFileName() {
222+ return fileText.getText();
223+ }
224+
225+ public String getLayoutName() {
226+ return layoutNameText.getText();
227+ }
228+}
\ No newline at end of file
--- maskat-ide/trunk/org.maskat.ui/src/org/maskat/ui/wizards/layout/NewLayoutFileWizard.java (nonexistent)
+++ maskat-ide/trunk/org.maskat.ui/src/org/maskat/ui/wizards/layout/NewLayoutFileWizard.java (revision 112)
@@ -0,0 +1,184 @@
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.layout;
17+
18+import java.io.ByteArrayInputStream;
19+import java.io.IOException;
20+import java.io.InputStream;
21+import java.io.UnsupportedEncodingException;
22+import java.lang.reflect.InvocationTargetException;
23+
24+import org.eclipse.core.resources.IContainer;
25+import org.eclipse.core.resources.IFile;
26+import org.eclipse.core.resources.IResource;
27+import org.eclipse.core.resources.IWorkspaceRoot;
28+import org.eclipse.core.resources.ResourcesPlugin;
29+import org.eclipse.core.runtime.CoreException;
30+import org.eclipse.core.runtime.IProgressMonitor;
31+import org.eclipse.core.runtime.IStatus;
32+import org.eclipse.core.runtime.Path;
33+import org.eclipse.core.runtime.Status;
34+import org.eclipse.jface.dialogs.MessageDialog;
35+import org.eclipse.jface.operation.IRunnableWithProgress;
36+import org.eclipse.jface.viewers.ISelection;
37+import org.eclipse.jface.viewers.IStructuredSelection;
38+import org.eclipse.jface.wizard.Wizard;
39+import org.eclipse.ui.INewWizard;
40+import org.eclipse.ui.IWorkbench;
41+import org.eclipse.ui.IWorkbenchPage;
42+import org.eclipse.ui.IWorkbenchWizard;
43+import org.eclipse.ui.PartInitException;
44+import org.eclipse.ui.PlatformUI;
45+import org.eclipse.ui.ide.IDE;
46+import org.maskat.ui.editors.layout.LayoutGraphicalEditor;
47+
48+/**
49+ * This is a sample new wizard. Its role is to create a new file resource in the
50+ * provided container. If the container resource (a folder or a project) is
51+ * selected in the workspace when the wizard is opened, it will accept it as the
52+ * target container. The wizard creates one
53+ */
54+public class NewLayoutFileWizard extends Wizard implements INewWizard {
55+ private NewLayoutFileWizardPage page;
56+
57+ private ISelection selection;
58+
59+ /**
60+ * Constructor for NewLayoutFileWizard.
61+ */
62+ public NewLayoutFileWizard() {
63+ super();
64+ this.setWindowTitle("Maskat Layout File Wizard");
65+ setNeedsProgressMonitor(true);
66+ }
67+
68+ /**
69+ * Adding the page to the wizard.
70+ */
71+
72+ public void addPages() {
73+ page = new NewLayoutFileWizardPage(selection);
74+ addPage(page);
75+ }
76+
77+ /**
78+ * This method is called when 'Finish' button is pressed in the wizard. We
79+ * will create an operation and run it using wizard as execution context.
80+ */
81+ public boolean performFinish() {
82+ final String containerName = page.getContainerName();
83+ final String fileName = page.getFileName();
84+ final String layoutName = page.getLayoutName();
85+ IRunnableWithProgress op = new IRunnableWithProgress() {
86+ public void run(IProgressMonitor monitor) throws InvocationTargetException {
87+ try {
88+ doFinish(containerName, fileName, layoutName, monitor);
89+ } catch (CoreException e) {
90+ throw new InvocationTargetException(e);
91+ } finally {
92+ monitor.done();
93+ }
94+ }
95+ };
96+ try {
97+ getContainer().run(true, false, op);
98+ } catch (InterruptedException e) {
99+ return false;
100+ } catch (InvocationTargetException e) {
101+ Throwable realException = e.getTargetException();
102+ MessageDialog.openError(getShell(), "Error", realException.getMessage());
103+ return false;
104+ }
105+ return true;
106+ }
107+
108+ /**
109+ * The worker method. It will find the container, create the file if missing
110+ * or just replace its contents, and open the editor on the newly created
111+ * file.
112+ */
113+
114+ private void doFinish(String containerName, String fileName, String layoutName,
115+ IProgressMonitor monitor) throws CoreException {
116+ // create a sample file
117+ monitor.beginTask("Creating " + fileName, 2);
118+ IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
119+ IResource resource = root.findMember(new Path(containerName));
120+ if (!resource.exists() || !(resource instanceof IContainer)) {
121+ throwCoreException("Container \"" + containerName + "\" does not exist.");
122+ }
123+ IContainer container = (IContainer) resource;
124+ final IFile file = container.getFile(new Path(fileName));
125+ try {
126+ InputStream stream = openContentStream(layoutName);
127+ if (file.exists()) {
128+ throwCoreException("File already exists. Choose another name.");
129+ } else {
130+ file.create(stream, true, monitor);
131+ }
132+ stream.close();
133+ } catch (IOException e) {
134+ }
135+ monitor.worked(1);
136+ monitor.setTaskName("Opening file for editing...");
137+ getShell().getDisplay().asyncExec(new Runnable() {
138+ public void run() {
139+ IWorkbenchPage page = PlatformUI.getWorkbench()
140+ .getActiveWorkbenchWindow().getActivePage();
141+ try {
142+ IDE.openEditor(page, file, LayoutGraphicalEditor.ID, true);
143+ } catch (PartInitException e) {
144+ }
145+ }
146+ });
147+ monitor.worked(1);
148+ }
149+
150+ /**
151+ * We will initialize file contents with a sample text.
152+ *
153+ * @throws UnsupportedEncodingException
154+ */
155+
156+ private InputStream openContentStream(String layoutName)
157+ throws UnsupportedEncodingException {
158+ layoutName = layoutName.replaceAll("<", "&lt;").replaceAll(">", "&gt;")
159+ .replaceAll("\"", "&quot;");
160+ String lineSeparator = System.getProperty("line.separator");
161+ String contents = "<?xml version='1.0' encoding='UTF-8'?>" + lineSeparator
162+ + "<!DOCTYPE layoutDef SYSTEM \"layoutDef.dtd\">" + lineSeparator
163+ + "<layoutDef>" + lineSeparator + " <layout name=\"" + layoutName
164+ + "\" >" + lineSeparator + " </layout>" + lineSeparator
165+ + "</layoutDef>";
166+ return new ByteArrayInputStream(contents.getBytes("UTF-8"));
167+ }
168+
169+ private void throwCoreException(String message) throws CoreException {
170+ IStatus status = new Status(IStatus.ERROR, "org.maskat.ide.gef", IStatus.OK,
171+ message, null);
172+ throw new CoreException(status);
173+ }
174+
175+ /**
176+ * We will accept the selection in the workbench to see if we can initialize
177+ * from it.
178+ *
179+ * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
180+ */
181+ public void init(IWorkbench workbench, IStructuredSelection selection) {
182+ this.selection = selection;
183+ }
184+}
\ No newline at end of file
Afficher sur ancien navigateur de dépôt.