1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 /***
19 * <p>Title: WSMO Studio</p>
20 * <p>Description: Semantic Web Service Editor</p>
21 * <p>Copyright: Copyright (c) 2004-2006</p>
22 * <p>Company: OntoText Lab. / SIRMA </p>
23 */
24
25 package org.wsmostudio.runtime;
26
27 import java.io.ByteArrayInputStream;
28
29 import org.eclipse.core.resources.IFile;
30 import org.eclipse.core.resources.ResourcesPlugin;
31 import org.eclipse.core.runtime.IPath;
32 import org.eclipse.jface.resource.JFaceResources;
33 import org.eclipse.jface.viewers.*;
34 import org.eclipse.jface.wizard.Wizard;
35 import org.eclipse.ui.*;
36 import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
37 import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard;
38 import org.wsmo.common.*;
39 import org.wsmo.wsml.Serializer;
40
41 public class SaveResourceWizard extends Wizard implements INewWizard {
42
43 protected WizardNewFileCreationPage filePage;
44 private TopEntity unsavedEntity;
45 private IFile resultFile = null;
46
47 public SaveResourceWizard(TopEntity unsaved) {
48 super();
49 this.unsavedEntity = unsaved;
50 }
51
52 public void addPages() {
53 addPage(filePage);
54 filePage.setImageDescriptor(
55 JFaceResources.getImageRegistry().getDescriptor(
56 WsmoImageRegistry.WSMO_LOGO_SMALL));
57 filePage.setTitle("Save Entity As");
58 filePage.setMessage("Save in Workspace");
59 filePage.setDescription("Select a storage location for entity: \n"
60 + unsavedEntity.getIdentifier().toString());
61 this.setWindowTitle("Save Entity As");
62 }
63
64 public void init(IWorkbench workbench, IStructuredSelection selection) {
65 filePage = new WizardNewFileCreationPage("SaveAsPage", selection) {
66 protected boolean validatePage() {
67 if (false == super.validatePage()) {
68 return false;
69 }
70 String testName = getFileName();
71 if (testName.length() == 0) {
72 return true;
73 }
74 if (false == testName.toLowerCase().endsWith(".wsml")) {
75 testName += ".wsml";
76 }
77
78 IPath path = super.getContainerFullPath().append(testName);
79 if (ResourcesPlugin.getWorkspace().getRoot().getFile(path).exists()) {
80 setErrorMessage("The same name (" + testName + ") already exists!");
81 return false;
82 }
83 return true;
84 }
85 };
86 }
87
88 public boolean canFinish() {
89 return filePage.isPageComplete();
90 }
91
92 public boolean isHelpAvailable() {
93 return false;
94 }
95
96 public boolean needsPreviousAndNextButtons() {
97 return false;
98 }
99
100 public boolean needsProgressMonitor() {
101 return false;
102 }
103
104 public boolean performCancel() {
105 return true;
106 }
107
108 public boolean performFinish() {
109 IFile newFile = createWSMOFile(unsavedEntity);
110 WSMORuntime.getRuntime().registerEntity(unsavedEntity, newFile.getLocation());
111 BasicNewResourceWizard.selectAndReveal(newFile,
112 PlatformUI.getWorkbench().getActiveWorkbenchWindow());
113 this.resultFile = newFile;
114 return true;
115 }
116
117 protected IFile createWSMOFile(TopEntity entity) {
118
119 String newFileName = filePage.getFileName();
120 if (false == newFileName.toLowerCase().endsWith(".wsml")) {
121 filePage.setFileName(newFileName+".wsml");
122 }
123 IFile file = filePage.createNewFile();
124 Serializer wsmlSerializar = WSMORuntime.getRuntime().getWsmlSerializer();
125 StringBuffer str = new StringBuffer();
126 wsmlSerializar.serialize( new TopEntity[] { entity }, str);
127 try {
128 file.setContents(new ByteArrayInputStream(str.toString().getBytes()), true, false, null);
129 }
130 catch(Exception e) {
131 LogManager.logError(e);
132 }
133 return file;
134 }
135
136 public void dispose() {
137 filePage.dispose();
138 super.dispose();
139 }
140
141 public IFile getSelectionFile() {
142 return this.resultFile;
143 }
144
145 }
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166