A lot of things changed in the expressions
[phpeclipse.git] / net.sourceforge.phpeclipse / src / com / xaraya / wizard / XarayaModuleContainerPage.java
1 package com.xaraya.wizard;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.InputStream;
5 import java.io.PipedInputStream;
6 import java.io.PipedOutputStream;
7 import java.io.PrintStream;
8 import java.util.ArrayList;
9 import java.util.Iterator;
10 import java.util.StringTokenizer;
11
12 import net.sourceforge.phpdt.internal.ui.util.StringUtil;
13
14 import org.eclipse.core.resources.IContainer;
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.core.resources.IFolder;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.resources.IResourceStatus;
19 import org.eclipse.core.resources.IWorkspace;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IAdaptable;
22 import org.eclipse.core.runtime.IPath;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.core.runtime.OperationCanceledException;
26 import org.eclipse.core.runtime.SubProgressMonitor;
27 import org.eclipse.jface.viewers.IStructuredSelection;
28 import org.eclipse.jface.wizard.WizardPage;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.layout.GridData;
31 import org.eclipse.swt.layout.GridLayout;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Event;
34 import org.eclipse.swt.widgets.Listener;
35 import org.eclipse.ui.actions.WorkspaceModifyOperation;
36 import org.eclipse.ui.dialogs.ContainerGenerator;
37 import org.eclipse.ui.help.WorkbenchHelp;
38 import org.eclipse.ui.internal.IHelpContextIds;
39 import org.eclipse.ui.internal.WorkbenchMessages;
40 import org.eclipse.ui.internal.WorkbenchPlugin;
41 import org.eclipse.ui.internal.misc.ResourceAndContainerGroup;
42
43 public class XarayaModuleContainerPage extends WizardPage implements Listener {
44         private static final int SIZING_CONTAINER_GROUP_HEIGHT = 250;
45         private IStructuredSelection currentSelection;
46         private IContainer currentParent;
47         NewXarayaResourceWizard wizard;
48         private IPath newModulePath;
49         private IFolder newModule;
50         
51         // widgets
52         private ResourceAndContainerGroup resourceGroup;
53
54 public XarayaModuleContainerPage(String pageName, IStructuredSelection selection) {
55         super(XarayaModuleMessages.getString("Xaraya.label.container"));
56         setTitle(pageName);
57         setDescription(XarayaModuleMessages.getString("Xaraya.label.container")); 
58         this.currentSelection = selection;
59 }
60
61 public void createControl(Composite parent) {
62         initializeDialogUnits(parent);
63         // top level group
64         Composite composite = new Composite(parent,SWT.NONE);
65         composite.setFont(parent.getFont());
66         composite.setLayout(new GridLayout());
67         composite.setLayoutData(new GridData(
68                 GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL));
69
70         WorkbenchHelp.setHelp(composite, IHelpContextIds.NEW_FOLDER_WIZARD_PAGE);
71
72         resourceGroup = new ResourceAndContainerGroup(composite,this,
73                 XarayaModuleMessages.getString("Xaraya.label.container"),
74                 XarayaModuleMessages.getString("Xaraya.label.modversionname"),
75                         false, SIZING_CONTAINER_GROUP_HEIGHT); //$NON-NLS-2$ //$NON-NLS-1$
76         resourceGroup.setAllowExistingResources(false);
77         initializePage();
78         validatePage();
79         // Show description on opening
80         setErrorMessage(null);
81         setMessage(null);
82         setControl(composite);
83 }
84
85 protected void createFolder(IFolder folderHandle, IProgressMonitor monitor) throws CoreException {
86     try {
87         // Create the folder resource in the workspace
88         // Update: Recursive to create any folders which do not exist already
89         if (!folderHandle.exists()) {
90             IContainer parent= folderHandle.getParent();
91             if (parent instanceof IFolder && (!((IFolder)parent).exists())) {
92                 createFolder((IFolder)parent, monitor);
93             }
94                         folderHandle.create(false, true, monitor);
95         }
96     }
97     catch (CoreException e) {
98         // If the folder already existed locally, just refresh to get contents
99         if (e.getStatus().getCode() == IResourceStatus.PATH_OCCUPIED)
100             folderHandle.refreshLocal(IResource.DEPTH_INFINITE, new SubProgressMonitor(monitor, 500));
101         else
102             throw e;
103     }
104
105     if (monitor.isCanceled())
106         throw new OperationCanceledException();
107 }
108
109 protected IFolder createFolderHandle(IPath folderPath) {
110         return WorkbenchPlugin.getPluginWorkspace().getRoot().getFolder(folderPath);
111 }
112
113 public IFolder createNewModuleFolder() {
114         if (newModule != null)
115                 return newModule;
116
117         // create the new folder and cache it if successful
118         final IPath containerPath = resourceGroup.getContainerFullPath();
119         newModulePath = containerPath.append(resourceGroup.getResource());
120         final IFolder newModuleHandle = createFolderHandle(newModulePath);
121
122         WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
123                 public void execute(IProgressMonitor monitor) throws CoreException {
124                         try {
125                                 monitor.beginTask(WorkbenchMessages.getString("WizardNewFolderCreationPage.progress"), 2000); //$NON-NLS-1$
126                                 ContainerGenerator generator = new ContainerGenerator(containerPath);
127                                 generator.generateContainer(new SubProgressMonitor(monitor, 1000));
128                                 createFolder(newModuleHandle, new SubProgressMonitor(monitor, 1000));
129                         } finally {
130                                 monitor.done();
131                         }
132                 }
133         };
134
135         try {
136                 getContainer().run(true, true, op);
137         } catch (Exception e) {
138                 return null;
139         }
140         newModule = newModuleHandle;
141         return newModule;
142 }
143
144 public boolean createNewGuiFolder() {
145         final IPath containerPath = newModulePath;
146         IPath newFolderPath = containerPath.append(XarayaModuleMessages.getString("Xaraya.folder.gui"));
147         final IFolder newFolderHandle = createFolderHandle(newFolderPath);
148
149         WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
150                 public void execute(IProgressMonitor monitor) throws CoreException {
151                         try {
152                                 monitor.beginTask(WorkbenchMessages.getString("WizardNewFolderCreationPage.progress"), 2000); //$NON-NLS-1$
153                                 ContainerGenerator generator = new ContainerGenerator(containerPath);
154                                 generator.generateContainer(new SubProgressMonitor(monitor, 1000));
155                                 createFolder(newFolderHandle, new SubProgressMonitor(monitor, 1000));
156                         } finally {
157                                 monitor.done();
158                         }
159                 }
160         };
161
162         try {
163                 getContainer().run(true, true, op);
164         } catch (Exception e) {
165                 return false;
166         } 
167         return true;
168 }
169
170 protected void createFile(IFile fileHandle, InputStream contents, IProgressMonitor monitor) throws CoreException {
171         if (contents == null)
172                 contents = new ByteArrayInputStream(new byte[0]);
173
174         try {
175                         fileHandle.create(contents, false, monitor);
176         }
177         catch (CoreException e) {
178                 // If the file already existed locally, just refresh to get contents
179                 if (e.getStatus().getCode() == IResourceStatus.PATH_OCCUPIED)
180                         fileHandle.refreshLocal(IResource.DEPTH_ZERO, null);
181                 else
182                         throw e;
183         }
184
185         if (monitor.isCanceled())
186                 throw new OperationCanceledException();
187 }
188
189 protected IFile createFileHandle(IPath filePath) {
190         return WorkbenchPlugin.getPluginWorkspace().getRoot().getFile(filePath);
191 }
192
193 public Object[] createNewModuleFiles(){
194         ArrayList fileAL = new ArrayList();
195         boolean isGuiDirCreated = false;
196         fileAL.add(createNewFile(XarayaModuleMessages.getString("Xaraya.files.init")));
197         fileAL.add(createNewFile(XarayaModuleMessages.getString("Xaraya.files.version")));
198         if (XarayaVersionModel.isAdminApi()) fileAL.add(createNewFile(XarayaModuleMessages.getString("Xaraya.files.adminApi"))); 
199         if (XarayaVersionModel.isAdminGui()) {
200                 fileAL.add(createNewFile(XarayaModuleMessages.getString("Xaraya.files.adminGui")));
201                 isGuiDirCreated=createNewGuiFolder();
202         }  
203         if (XarayaVersionModel.isUserApi()) fileAL.add(createNewFile(XarayaModuleMessages.getString("Xaraya.files.userApi")));  
204         if (XarayaVersionModel.isUserGui()) {
205                 fileAL.add(createNewFile(XarayaModuleMessages.getString("Xaraya.files.userGui")));
206                 if (!isGuiDirCreated) isGuiDirCreated=createNewGuiFolder();     
207         } 
208         return fileAL.toArray();
209 }
210
211 public IFile createNewFile(String fileName) {
212         final IPath containerPath = newModulePath;
213         IPath newFilePath = containerPath.append(fileName);
214         final IFile newFileHandle = createFileHandle(newFilePath);
215         final InputStream initialContents = getInitialContents(fileName);
216         WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
217                 protected void execute(IProgressMonitor monitor) throws CoreException,
218                         InterruptedException
219                 {
220                         try {
221                                 monitor.beginTask("Progress", 2000); //$NON-NLS-1$
222                                 ContainerGenerator generator = new ContainerGenerator(containerPath);
223                                 generator.generateContainer(new SubProgressMonitor(monitor, 1000));
224                                 createFile(newFileHandle,initialContents, new SubProgressMonitor(monitor, 1000));
225                         } finally {
226                                 monitor.done();
227                         }
228                 }
229         };
230
231         try {
232                 getContainer().run(true, true, op);
233         } catch (Exception e) {
234                 return null;
235         } 
236         return newFileHandle;
237 }
238
239         protected InputStream getInitialContents(String fileName) {
240                 StringBuffer inputString = new StringBuffer();
241                 if (fileName.equals(XarayaModuleMessages.getString("Xaraya.files.init"))) 
242                         inputString=XarayaModuleText.xarinit;
243                 if (fileName.equals(XarayaModuleMessages.getString("Xaraya.files.adminGui"))) 
244                         inputString=XarayaModuleText.xaradmin;
245                 if (fileName.equals(XarayaModuleMessages.getString("Xaraya.files.adminApi"))) 
246                         inputString=XarayaModuleText.xaradminapi;
247                 if (fileName.equals(XarayaModuleMessages.getString("Xaraya.files.userGui"))) 
248                         inputString=XarayaModuleText.xaruser;
249                 if (fileName.equals(XarayaModuleMessages.getString("Xaraya.files.userApi"))) 
250                         inputString=XarayaModuleText.xaruserapi;
251                 if (fileName.equals(XarayaModuleMessages.getString("Xaraya.files.version"))) 
252                         inputString=XarayaModuleText.xarversion;
253                 
254                 ArrayList inserts = new ArrayList();
255                 //makes things simpler to just refer to the arraylist...
256                 inserts.add(XarayaModuleMessages.getString("Xaraya.insert.name"));
257                 inserts.add(XarayaModuleMessages.getString("Xaraya.insert.id"));
258                 inserts.add(XarayaModuleMessages.getString("Xaraya.insert.version"));
259                 inserts.add(XarayaModuleMessages.getString("Xaraya.insert.description"));
260                 inserts.add(XarayaModuleMessages.getString("Xaraya.insert.official"));
261                 inserts.add(XarayaModuleMessages.getString("Xaraya.insert.author"));
262                 inserts.add(XarayaModuleMessages.getString("Xaraya.insert.contact"));
263                 inserts.add(XarayaModuleMessages.getString("Xaraya.insert.admin"));
264                 inserts.add(XarayaModuleMessages.getString("Xaraya.insert.user"));
265                 inserts.add(XarayaModuleMessages.getString("Xaraya.insert.securityschema"));
266                 inserts.add(XarayaModuleMessages.getString("Xaraya.insert.class"));
267                 inserts.add(XarayaModuleMessages.getString("Xaraya.insert.category"));
268         
269                 ArrayList names = new ArrayList();
270                 names.add(XarayaVersionModel.getModversionname());
271                 names.add(XarayaVersionModel.getModversionid());
272                 names.add(XarayaVersionModel.getModversionversion());
273                 names.add(XarayaVersionModel.getModversiondescription());
274                 names.add(XarayaVersionModel.getModversionofficial());
275                 names.add(XarayaVersionModel.getModversionauthor());
276                 names.add(XarayaVersionModel.getModversioncontact());
277                 names.add(XarayaVersionModel.getModversionadmin());
278                 names.add(XarayaVersionModel.getModversionuser());
279                 names.add(XarayaVersionModel.getModversionsecurityschema());
280                 names.add(XarayaVersionModel.getModversionclass());
281                 names.add(XarayaVersionModel.getModversioncategory());
282                 
283                 PipedOutputStream ps = null;
284                 PipedInputStream is = null;
285                 String buffer = inputString.toString();         
286                 for (int i=0; i<inserts.size(); i++)
287                 {
288                         String insert = (String)inserts.get(i);
289                         String replace = (String)names.get(i);
290         
291       buffer = StringUtil.replaceAll(buffer, insert, replace.toLowerCase());
292                         try {
293                                 ps = new PipedOutputStream();
294                                 is = new PipedInputStream(ps);
295                                 PrintStream os = new PrintStream(ps);
296                                 os.println(buffer);
297                                 os.close();
298                         } catch (Exception e) {
299                                 System.out.println("writing to file:"+fileName +"failed with:"+ e);
300                         }
301                 }
302                 return is;
303         }
304         
305
306 public void handleEvent(Event ev) {
307         setPageComplete(validatePage());
308 }
309
310 public boolean canFlipToNextPage() {
311         return isPageComplete();
312 }
313
314 protected void initializePage() {
315         Iterator enum = currentSelection.iterator();
316         if (enum.hasNext()) {
317                 Object next = enum.next();
318                 IResource selectedResource = null;
319                 if (next instanceof IResource) {
320                         selectedResource = (IResource)next;
321                 } else if (next instanceof IAdaptable) {
322                         selectedResource = (IResource)((IAdaptable)next).getAdapter(IResource.class);
323                 }
324                 if (selectedResource != null) {
325                         if (selectedResource.getType() == IResource.FILE)
326                                 selectedResource = selectedResource.getParent();
327                         if (selectedResource.isAccessible())
328                                 resourceGroup.setContainerFullPath(selectedResource.getFullPath());
329                 }
330         }
331         setPageComplete(false);
332 }
333
334 public void setVisible(boolean visible) {
335         super.setVisible(visible);
336         if(visible)
337                 resourceGroup.setFocus();
338 }
339
340 protected boolean validatePage() {
341         boolean valid = true;
342         String moduleName = new String();
343         IWorkspace workspace = WorkbenchPlugin.getPluginWorkspace();
344     IStatus nameStatus = null;
345     String folderName = resourceGroup.getResource();
346     moduleName = folderName;
347     if (folderName.indexOf(IPath.SEPARATOR) != -1) {
348         StringTokenizer tok = new StringTokenizer(folderName, String.valueOf(IPath.SEPARATOR));
349         while (tok.hasMoreTokens()) {
350             String pathFragment = tok.nextToken();
351             nameStatus = workspace.validateName(pathFragment, IResource.FOLDER);
352             if (!nameStatus.isOK()) {
353                 break;
354             }
355                 }
356     }
357         //If the name status was not set validate using the name
358         if(nameStatus == null && folderName.length() > 0)
359         nameStatus = workspace.validateName(folderName, IResource.FOLDER);
360     if (nameStatus != null && !nameStatus.isOK()) {
361         setErrorMessage(nameStatus.getMessage());
362         return false;
363     }
364
365         if (!resourceGroup.areAllValuesValid()) {
366                 // if blank name then fail silently
367                 if (resourceGroup.getProblemType() == ResourceAndContainerGroup.PROBLEM_RESOURCE_EMPTY
368                         || resourceGroup.getProblemType() == ResourceAndContainerGroup.PROBLEM_CONTAINER_EMPTY) {
369                         setMessage(resourceGroup.getProblemMessage());
370                         setErrorMessage(null);
371                 } else {
372                         setErrorMessage(resourceGroup.getProblemMessage());
373                 }
374                 valid = false;
375         }
376         if (valid) {
377                 XarayaVersionModel.setModversionname(moduleName);
378                 setErrorMessage(null);
379         } 
380         return valid;
381 }
382
383 }
384