1 package com.xaraya.wizard;
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;
12 import net.sourceforge.phpdt.internal.ui.util.StringUtil;
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;
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;
52 private ResourceAndContainerGroup resourceGroup;
54 public XarayaModuleContainerPage(String pageName, IStructuredSelection selection) {
55 super(XarayaModuleMessages.getString("Xaraya.label.container"));
57 setDescription(XarayaModuleMessages.getString("Xaraya.label.container"));
58 this.currentSelection = selection;
61 public void createControl(Composite parent) {
62 initializeDialogUnits(parent);
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));
70 WorkbenchHelp.setHelp(composite, IHelpContextIds.NEW_FOLDER_WIZARD_PAGE);
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);
79 // Show description on opening
80 setErrorMessage(null);
82 setControl(composite);
85 protected void createFolder(IFolder folderHandle, IProgressMonitor monitor) throws CoreException {
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);
94 folderHandle.create(false, true, monitor);
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));
105 if (monitor.isCanceled())
106 throw new OperationCanceledException();
109 protected IFolder createFolderHandle(IPath folderPath) {
110 return WorkbenchPlugin.getPluginWorkspace().getRoot().getFolder(folderPath);
113 public IFolder createNewModuleFolder() {
114 if (newModule != null)
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);
122 WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
123 public void execute(IProgressMonitor monitor) throws CoreException {
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));
136 getContainer().run(true, true, op);
137 } catch (Exception e) {
140 newModule = newModuleHandle;
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);
149 WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
150 public void execute(IProgressMonitor monitor) throws CoreException {
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));
163 getContainer().run(true, true, op);
164 } catch (Exception e) {
170 protected void createFile(IFile fileHandle, InputStream contents, IProgressMonitor monitor) throws CoreException {
171 if (contents == null)
172 contents = new ByteArrayInputStream(new byte[0]);
175 fileHandle.create(contents, false, monitor);
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);
185 if (monitor.isCanceled())
186 throw new OperationCanceledException();
189 protected IFile createFileHandle(IPath filePath) {
190 return WorkbenchPlugin.getPluginWorkspace().getRoot().getFile(filePath);
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();
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();
208 return fileAL.toArray();
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,
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));
232 getContainer().run(true, true, op);
233 } catch (Exception e) {
236 return newFileHandle;
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;
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"));
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());
283 PipedOutputStream ps = null;
284 PipedInputStream is = null;
285 String buffer = inputString.toString();
286 for (int i=0; i<inserts.size(); i++)
288 String insert = (String)inserts.get(i);
289 String replace = (String)names.get(i);
291 buffer = StringUtil.replaceAll(buffer, insert, replace.toLowerCase());
293 ps = new PipedOutputStream();
294 is = new PipedInputStream(ps);
295 PrintStream os = new PrintStream(ps);
298 } catch (Exception e) {
299 System.out.println("writing to file:"+fileName +"failed with:"+ e);
306 public void handleEvent(Event ev) {
307 setPageComplete(validatePage());
310 public boolean canFlipToNextPage() {
311 return isPageComplete();
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);
324 if (selectedResource != null) {
325 if (selectedResource.getType() == IResource.FILE)
326 selectedResource = selectedResource.getParent();
327 if (selectedResource.isAccessible())
328 resourceGroup.setContainerFullPath(selectedResource.getFullPath());
331 setPageComplete(false);
334 public void setVisible(boolean visible) {
335 super.setVisible(visible);
337 resourceGroup.setFocus();
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()) {
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());
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);
372 setErrorMessage(resourceGroup.getProblemMessage());
377 XarayaVersionModel.setModversionname(moduleName);
378 setErrorMessage(null);