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