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 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;
 
  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;
 
  50         private ResourceAndContainerGroup resourceGroup;
 
  52 public XarayaModuleContainerPage(String pageName, IStructuredSelection selection) {
 
  53         super(XarayaModuleMessages.getString("Xaraya.label.container"));
 
  55         setDescription(XarayaModuleMessages.getString("Xaraya.label.container")); 
 
  56         this.currentSelection = selection;
 
  59 public void createControl(Composite parent) {
 
  60         initializeDialogUnits(parent);
 
  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));
 
  68         WorkbenchHelp.setHelp(composite, IHelpContextIds.NEW_FOLDER_WIZARD_PAGE);
 
  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);
 
  77         // Show description on opening
 
  78         setErrorMessage(null);
 
  80         setControl(composite);
 
  83 protected void createFolder(IFolder folderHandle, IProgressMonitor monitor) throws CoreException {
 
  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);
 
  92                         folderHandle.create(false, true, monitor);
 
  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));
 
 103     if (monitor.isCanceled())
 
 104         throw new OperationCanceledException();
 
 107 protected IFolder createFolderHandle(IPath folderPath) {
 
 108         return WorkbenchPlugin.getPluginWorkspace().getRoot().getFolder(folderPath);
 
 111 public IFolder createNewModuleFolder() {
 
 112         if (newModule != null)
 
 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);
 
 120         WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
 
 121                 public void execute(IProgressMonitor monitor) throws CoreException {
 
 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));
 
 134                 getContainer().run(true, true, op);
 
 135         } catch (Exception e) {
 
 138         newModule = newModuleHandle;
 
 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);
 
 147         WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
 
 148                 public void execute(IProgressMonitor monitor) throws CoreException {
 
 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));
 
 161                 getContainer().run(true, true, op);
 
 162         } catch (Exception e) {
 
 168 protected void createFile(IFile fileHandle, InputStream contents, IProgressMonitor monitor) throws CoreException {
 
 169         if (contents == null)
 
 170                 contents = new ByteArrayInputStream(new byte[0]);
 
 173                         fileHandle.create(contents, false, monitor);
 
 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);
 
 183         if (monitor.isCanceled())
 
 184                 throw new OperationCanceledException();
 
 187 protected IFile createFileHandle(IPath filePath) {
 
 188         return WorkbenchPlugin.getPluginWorkspace().getRoot().getFile(filePath);
 
 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();
 
 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();     
 
 206         return fileAL.toArray();
 
 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,
 
 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));
 
 230                 getContainer().run(true, true, op);
 
 231         } catch (Exception e) {
 
 234         return newFileHandle;
 
 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;
 
 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"));
 
 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());
 
 281                 PipedOutputStream ps = null;
 
 282                 PipedInputStream is = null;
 
 283                 String buffer = inputString.toString();         
 
 284                 for (int i=0; i<inserts.size(); i++)
 
 286                         String insert = (String)inserts.get(i);
 
 287                         String replace = (String)names.get(i);
 
 289                         buffer = buffer.replaceAll(insert, replace.toLowerCase());              
 
 291                                 ps = new PipedOutputStream();
 
 292                                 is = new PipedInputStream(ps);
 
 293                                 PrintStream os = new PrintStream(ps);
 
 296                         } catch (Exception e) {
 
 297                                 System.out.println("writing to file:"+fileName +"failed with:"+ e);
 
 304 public void handleEvent(Event ev) {
 
 305         setPageComplete(validatePage());
 
 308 public boolean canFlipToNextPage() {
 
 309         return isPageComplete();
 
 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);
 
 322                 if (selectedResource != null) {
 
 323                         if (selectedResource.getType() == IResource.FILE)
 
 324                                 selectedResource = selectedResource.getParent();
 
 325                         if (selectedResource.isAccessible())
 
 326                                 resourceGroup.setContainerFullPath(selectedResource.getFullPath());
 
 329         setPageComplete(false);
 
 332 public void setVisible(boolean visible) {
 
 333         super.setVisible(visible);
 
 335                 resourceGroup.setFocus();
 
 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()) {
 
 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());
 
 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);
 
 370                         setErrorMessage(resourceGroup.getProblemMessage());
 
 375                 XarayaVersionModel.setModversionname(moduleName);
 
 376                 setErrorMessage(null);