View Javadoc

1   /*
2    WSMO Studio - a Semantic Web Service Editor
3    Copyright (c) 2004-2006, OntoText Lab. / SIRMA Group
4    
5    This library is free software; you can redistribute it and/or modify it under
6    the terms of the GNU Lesser General Public License as published by the Free
7    Software Foundation; either version 2.1 of the License, or (at your option)
8    any later version.
9    This library is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   details.
13   You should have received a copy of the GNU Lesser General Public License along
14   with this library; if not, write to the Free Software Foundation, Inc.,
15   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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  * $Log$
149  * Revision 1.5  2007/07/30 15:49:03  alex_simov
150  * no message
151  *
152  * Revision 1.4  2007/06/27 10:49:01  alex_simov
153  * no message
154  *
155  * Revision 1.3  2006/05/22 11:39:13  alex_simov
156  * bugfix: new entities not registered in the internal topEntity -> file map and
157  * thus producing "Save as" dialogs on each save operation
158  *
159  * Revision 1.2  2006/01/09 12:51:12  alex_simov
160  * Copyright message in header updated
161  *
162  * Revision 1.1  2005/12/06 12:23:15  alex_simov
163  * A 'Save as' functionality added for resources for which the link
164  * to the workspace is missing (not a normal case)
165  *
166  */