Fixed Bug<a href="http://sourceforge.net/tracker/?func=detail&atid=484801&aid=1145683...
authoraxelcl <axelcl>
Thu, 12 Jan 2006 19:04:28 +0000 (19:04 +0000)
committeraxelcl <axelcl>
Thu, 12 Jan 2006 19:04:28 +0000 (19:04 +0000)
net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/corext/template/php/CodeTemplateContext.java
net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/wizards/HTMLFileWizard.java
net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/wizards/PHPFileWizard.java
net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/wizards/PHPFileWizardPage.java

index 6d3d3a6..196da16 100644 (file)
@@ -1,10 +1,10 @@
 /*******************************************************************************
  * Copyright (c) 2000, 2004 IBM Corporation and others.
- * All rights reserved. This program and the accompanying materials 
+ * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Common Public License v1.0
  * which accompanies this distribution, and is available at
  * http://www.eclipse.org/legal/cpl-v10.html
- * 
+ *
  * Contributors:
  *     IBM Corporation - initial API and implementation
  *******************************************************************************/
@@ -28,7 +28,7 @@ import org.eclipse.jface.text.templates.TemplateTranslator;
 import org.eclipse.jface.text.templates.TemplateVariableResolver;
 
 public class CodeTemplateContext extends TemplateContext {
-       
+
        private String fLineDelimiter;
        private IJavaProject fProject;
 
@@ -57,16 +57,16 @@ public class CodeTemplateContext extends TemplateContext {
 
                if (!canEvaluate(template))
                        return null;
-                       
+
                String pattern= changeLineDelimiter(template.getPattern(), fLineDelimiter);
-               
+
                TemplateTranslator translator= new TemplateTranslator();
                TemplateBuffer buffer= translator.translate(pattern);
                getContextType().resolve(buffer, this);
 
                return buffer;
        }
-       
+
        private static String changeLineDelimiter(String code, String lineDelim) {
                try {
                        ILineTracker tracker= new DefaultLineTracker();
@@ -75,7 +75,7 @@ public class CodeTemplateContext extends TemplateContext {
                        if (nLines == 1) {
                                return code;
                        }
-                       
+
                        StringBuffer buf= new StringBuffer();
                        for (int i= 0; i < nLines; i++) {
                                if (i != 0) {
@@ -90,7 +90,7 @@ public class CodeTemplateContext extends TemplateContext {
                        // can not happen
                        return code;
                }
-       }               
+       }
 
        /* (non-Javadoc)
         * @see net.sourceforge.phpdt.internal.corext.template.TemplateContext#canEvaluate(net.sourceforge.phpdt.internal.corext.template.Template)
@@ -98,14 +98,15 @@ public class CodeTemplateContext extends TemplateContext {
        public boolean canEvaluate(Template template) {
                return true;
        }
-       
+
        public void setCompilationUnitVariables(ICompilationUnit cu) {
                setVariable(CodeTemplateContextType.FILENAME, cu.getElementName());
                setVariable(CodeTemplateContextType.PACKAGENAME, cu.getParent().getElementName());
                setVariable(CodeTemplateContextType.PROJECTNAME, cu.getJavaProject().getElementName());
        }
 
-       public void setFileNameVariable(String filename) {
+       public void setFileNameVariable(String filename, String projectname) {
                setVariable(CodeTemplateContextType.FILENAME, filename);
+               setVariable(CodeTemplateContextType.PROJECTNAME, projectname);
        }
 }
index c699b5e..f67dbfb 100644 (file)
@@ -24,6 +24,7 @@ import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 
 import org.eclipse.core.resources.IContainer;
 import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.resources.IWorkspaceRoot;
 import org.eclipse.core.resources.ResourcesPlugin;
@@ -115,9 +116,11 @@ public class HTMLFileWizard extends Wizard implements INewWizard {
     }
     IContainer container = (IContainer) resource;
     final IFile file = container.getFile(new Path(fileName));
+    IProject project = file.getProject();
+    String projectName = project.getName();
     try {
       InputStream stream;
-      stream = openContentStream(fileName);
+      stream = openContentStream(fileName, projectName);
       if (file.exists()) {
         file.setContents(stream, true, true, monitor);
       } else {
@@ -143,7 +146,7 @@ public class HTMLFileWizard extends Wizard implements INewWizard {
   /**
    * We will initialize file contents with a sample text.
    */
-  private InputStream openContentStream(String fileName) {
+  private InputStream openContentStream(String fileName, String projectname) {
     try {
       Template template = PHPeclipsePlugin.getDefault().getCodeTemplateStore().findTemplate(CodeTemplateContextType.NEWHTML);
       if (template == null) {
@@ -151,7 +154,7 @@ public class HTMLFileWizard extends Wizard implements INewWizard {
       }
       String lineDelimiter = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
       CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), null, lineDelimiter);
-      context.setFileNameVariable(fileName);
+      context.setFileNameVariable(fileName, projectname);
       return new ByteArrayInputStream(StubUtility.evaluateTemplate(context, template).getBytes());
     } catch (CoreException e) {
       e.printStackTrace();
@@ -166,7 +169,7 @@ public class HTMLFileWizard extends Wizard implements INewWizard {
 
   /**
    * We will accept the selection in the workbench to see if we can initialize from it.
-   * 
+   *
    * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
    */
   public void init(IWorkbench workbench, IStructuredSelection selection) {
index 89674df..e04bf7e 100644 (file)
@@ -24,6 +24,7 @@ import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 
 import org.eclipse.core.resources.IContainer;
 import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.resources.IWorkspaceRoot;
 import org.eclipse.core.resources.ResourcesPlugin;
@@ -115,12 +116,14 @@ public class PHPFileWizard extends Wizard implements INewWizard {
     }
     IContainer container = (IContainer) resource;
     final IFile file = container.getFile(new Path(fileName));
+    IProject project = file.getProject();
+    String projectName = project.getName();
     String className = getClassName(fileName);
 
     try {
       InputStream stream;
       if (className == null) {
-        stream = openContentStream(fileName);
+        stream = openContentStream(fileName, projectName);
       } else {
         stream = openContentStreamClass(className);
       }
@@ -148,7 +151,7 @@ public class PHPFileWizard extends Wizard implements INewWizard {
 
   /**
    * Check if the filename is like this anyname.class.php
-   * 
+   *
    * @param fFileName
    *          the filename
    * @return the anyname or null
@@ -167,7 +170,7 @@ public class PHPFileWizard extends Wizard implements INewWizard {
 
   /**
    * We will initialize file contents for a class
-   * 
+   *
    * @param className
    *          the classname
    */
@@ -186,7 +189,7 @@ public class PHPFileWizard extends Wizard implements INewWizard {
   /**
    * We will initialize file contents with a sample text.
    */
-  private InputStream openContentStream(String fileName) {
+  private InputStream openContentStream(String fileName, String projectname) {
     try {
       Template template = PHPeclipsePlugin.getDefault().getCodeTemplateStore().findTemplate(CodeTemplateContextType.NEWTYPE);
       if (template == null) {
@@ -194,7 +197,7 @@ public class PHPFileWizard extends Wizard implements INewWizard {
       }
       String lineDelimiter = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
       CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), null, lineDelimiter);
-      context.setFileNameVariable(fileName);
+      context.setFileNameVariable(fileName, projectname);
       String content=StubUtility.evaluateTemplate(context, template);
       if (content==null) {
          content="";
@@ -214,7 +217,7 @@ public class PHPFileWizard extends Wizard implements INewWizard {
 
   /**
    * We will accept the selection in the workbench to see if we can initialize from it.
-   * 
+   *
    * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
    */
   public void init(IWorkbench workbench, IStructuredSelection selection) {
index cb4cb35..8fffaec 100644 (file)
@@ -12,9 +12,13 @@ package net.sourceforge.phpeclipse.wizards;
  www.phpeclipse.de
  **********************************************************************/
 
+import net.sourceforge.phpdt.core.ICompilationUnit;
+import net.sourceforge.phpdt.internal.corext.codemanipulation.StubUtility;
+
 import org.eclipse.core.resources.IContainer;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.Path;
 import org.eclipse.jface.dialogs.IDialogPage;
@@ -41,186 +45,180 @@ import org.eclipse.ui.dialogs.ContainerSelectionDialog;
  */
 
 public class PHPFileWizardPage extends WizardPage {
-  private static final String INITIAL_FILENAME = "file.php";
-
-private Text containerText;
-
-  private Text fileText;
-
-  private ISelection selection;
-
-  /**
-   * Constructor for SampleNewWizardPage.
-   * 
-   * @param pageName
-   */
-  public PHPFileWizardPage(ISelection selection) {
-    super("wizardPage");
-    setTitle(PHPWizardMessages.getString("WizardPage.title"));
-    setDescription(PHPWizardMessages.getString("WizardPage.description"));
-    this.selection = selection;
-  }
-
-  /**
-   * @see IDialogPage#createControl(Composite)
-   */
-  public void createControl(Composite parent) {
-    Composite container = new Composite(parent, SWT.NULL);
-    GridLayout layout = new GridLayout();
-    container.setLayout(layout);
-    layout.numColumns = 3;
-    layout.verticalSpacing = 9;
-    Label label = new Label(container, SWT.NULL);
-    label.setText(PHPWizardMessages.getString("WizardPage.containerLabel"));
-
-    containerText = new Text(container, SWT.BORDER | SWT.SINGLE);
-    GridData gd = new GridData(GridData.FILL_HORIZONTAL);
-    containerText.setLayoutData(gd);
-    containerText.addModifyListener(new ModifyListener() {
-      public void modifyText(ModifyEvent e) {
-        dialogChanged();
-      }
-    });
-
-    Button button = new Button(container, SWT.PUSH);
-    button.setText(PHPWizardMessages.getString("WizardPage.browseButtonText"));
-    button.addSelectionListener(new SelectionAdapter() {
-      public void widgetSelected(SelectionEvent e) {
-        handleBrowse();
-      }
-    });
-    label = new Label(container, SWT.NULL);
-    label.setText(PHPWizardMessages.getString("WizardPage.fileLabel"));
-
-    fileText = new Text(container, SWT.BORDER | SWT.SINGLE);
-    gd = new GridData(GridData.FILL_HORIZONTAL);
-    fileText.setLayoutData(gd);
-    fileText.addModifyListener(new ModifyListener() {
-      public void modifyText(ModifyEvent e) {
-        dialogChanged();
-      }
-    });
-    initialize();
-    dialogChanged();
-    setControl(container);
-  }
-
-  /**
-   * Tests if the current workbench selection is a suitable container to use.
-   */
-
-  private void initialize() {
-    if (selection != null && selection.isEmpty() == false
-        && selection instanceof IStructuredSelection) {
-      IStructuredSelection ssel = (IStructuredSelection) selection;
-      if (ssel.size() > 1)
-        return;
-      Object obj = ssel.getFirstElement();
-      if (obj instanceof IResource) {
-        IContainer container;
-        if (obj instanceof IContainer)
-          container = (IContainer) obj;
-        else
-          container = ((IResource) obj).getParent();
-        containerText.setText(container.getFullPath().toString());
-        fileText.setFocus();
-      }
-    }
-    fileText.setText(INITIAL_FILENAME);
-  }
-
-  /**
-   * Uses the standard container selection dialog to choose the new value for
-   * the container field.
-   */
-
-  private void handleBrowse() {
-    ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(),
-        ResourcesPlugin.getWorkspace().getRoot(), false, PHPWizardMessages
-            .getString("WizardPage.selectNewFileContainer"));
-    if (dialog.open() == ContainerSelectionDialog.OK) {
-      Object[] results = dialog.getResult();
-      if (results.length == 1) {
-         Object result=results[0];
-         if (result instanceof IPath) {
-                 IPath ipath = (IPath) result;
-                 containerText.setText(ipath.toString());
-         }
-      }
-    }
-  }
-
-  /**
-   * Ensures that both text fields are set.
-   */
-  private void dialogChanged() {
-    String container = getContainerName();
-    String fileName = getFileName();
-
-    if (container.length() == 0) {
-      updateStatus(PHPWizardMessages
-          .getString("WizardPage.containerMustBeSpecified"));
-      return;
-    }
-    if (fileName.length() == 0) {
-      updateStatus("WizardPage.nameMustBeSpecified");
-      return;
-    }
-
-    updateStatus(null);
-  }
-
-  private void updateStatus(String message) {
-    setErrorMessage(message);
-    setPageComplete(message == null);
-  }
-
-  public String getContainerName() {
-    return containerText.getText();
-  }
-
-  public String getFileName() {
-    return fileText.getText();
-  }
-
-  /**
-   * @see WizardPage#isPageComplete()
-   */
-  public boolean isPageComplete() {
-    return !checkFolderForExistingFile() && super.isPageComplete();
-  }
-
-  /**
+       private static final String INITIAL_FILENAME = "file.php";
+
+       private Text containerText;
+
+       private Text fileText;
+
+       private ISelection selection;
+
+       /**
+        * Constructor for SampleNewWizardPage.
+        *
+        * @param pageName
+        */
+       public PHPFileWizardPage(ISelection selection) {
+               super("wizardPage");
+               setTitle(PHPWizardMessages.getString("WizardPage.title"));
+               setDescription(PHPWizardMessages.getString("WizardPage.description"));
+               this.selection = selection;
+       }
+
+       /**
+        * @see IDialogPage#createControl(Composite)
+        */
+       public void createControl(Composite parent) {
+               Composite container = new Composite(parent, SWT.NULL);
+               GridLayout layout = new GridLayout();
+               container.setLayout(layout);
+               layout.numColumns = 3;
+               layout.verticalSpacing = 9;
+               Label label = new Label(container, SWT.NULL);
+               label.setText(PHPWizardMessages.getString("WizardPage.containerLabel"));
+
+               containerText = new Text(container, SWT.BORDER | SWT.SINGLE);
+               GridData gd = new GridData(GridData.FILL_HORIZONTAL);
+               containerText.setLayoutData(gd);
+               containerText.addModifyListener(new ModifyListener() {
+                       public void modifyText(ModifyEvent e) {
+                               dialogChanged();
+                       }
+               });
+
+               Button button = new Button(container, SWT.PUSH);
+               button.setText(PHPWizardMessages.getString("WizardPage.browseButtonText"));
+               button.addSelectionListener(new SelectionAdapter() {
+                       public void widgetSelected(SelectionEvent e) {
+                               handleBrowse();
+                       }
+               });
+               label = new Label(container, SWT.NULL);
+               label.setText(PHPWizardMessages.getString("WizardPage.fileLabel"));
+
+               fileText = new Text(container, SWT.BORDER | SWT.SINGLE);
+               gd = new GridData(GridData.FILL_HORIZONTAL);
+               fileText.setLayoutData(gd);
+               fileText.addModifyListener(new ModifyListener() {
+                       public void modifyText(ModifyEvent e) {
+                               dialogChanged();
+                       }
+               });
+               initialize();
+               dialogChanged();
+               setControl(container);
+       }
+
+       /**
+        * Tests if the current workbench selection is a suitable container to use.
+        */
+
+       private void initialize() {
+               if (selection != null && selection.isEmpty() == false && selection instanceof IStructuredSelection) {
+                       IStructuredSelection ssel = (IStructuredSelection) selection;
+                       if (ssel.size() > 1)
+                               return;
+                       Object obj = ssel.getFirstElement();
+                       if (obj instanceof IResource) {
+                               IContainer container;
+                               if (obj instanceof IContainer)
+                                       container = (IContainer) obj;
+                               else
+                                       container = ((IResource) obj).getParent();
+                               containerText.setText(container.getFullPath().toString());
+                               fileText.setFocus();
+                       }
+               }
+               fileText.setText(INITIAL_FILENAME);
+       }
+
+       /**
+        * Uses the standard container selection dialog to choose the new value for
+        * the container field.
+        */
+
+       private void handleBrowse() {
+               ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(), ResourcesPlugin.getWorkspace().getRoot(), false,
+                               PHPWizardMessages.getString("WizardPage.selectNewFileContainer"));
+               if (dialog.open() == ContainerSelectionDialog.OK) {
+                       Object[] results = dialog.getResult();
+                       if (results.length == 1) {
+                               Object result = results[0];
+                               if (result instanceof IPath) {
+                                       IPath ipath = (IPath) result;
+                                       containerText.setText(ipath.toString());
+                               }
+                       }
+               }
+       }
+
+       /**
+        * Ensures that both text fields are set.
+        */
+       private void dialogChanged() {
+               String container = getContainerName();
+               String fileName = getFileName();
+
+               if (container.length() == 0) {
+                       updateStatus(PHPWizardMessages.getString("WizardPage.containerMustBeSpecified"));
+                       return;
+               }
+               if (fileName.length() == 0) {
+                       updateStatus("WizardPage.nameMustBeSpecified");
+                       return;
+               }
+
+               updateStatus(null);
+       }
+
+       private void updateStatus(String message) {
+               setErrorMessage(message);
+               setPageComplete(message == null);
+       }
+
+       public String getContainerName() {
+               return containerText.getText();
+       }
+
+       public String getFileName() {
+               return fileText.getText();
+       }
+
+       /**
+        * @see WizardPage#isPageComplete()
+        */
+       public boolean isPageComplete() {
+               return !checkFolderForExistingFile() && super.isPageComplete();
+       }
+
+       /**
         * Finds the current directory where the file should be created
         */
        protected boolean checkFolderForExistingFile() {
                IContainer container = getFileContainer();
                if (container != null) {
-                       IResource file = container.getFile(new Path(fileText.getText()
-                                       .trim()));
+                       IResource file = container.getFile(new Path(fileText.getText().trim()));
                        if (file != null && file.exists()) {
-                               this.setErrorMessage(PHPWizardMessages
-                                               .getString("WizardPage.fileAlreadyExists"));
+                               this.setErrorMessage(PHPWizardMessages.getString("WizardPage.fileAlreadyExists"));
                                return true;
                        }
 
-                       ((PHPFileWizard) this.getWizard()).setFileName(fileText.getText()
-                                       .trim());
+                       ((PHPFileWizard) this.getWizard()).setFileName(fileText.getText().trim());
                }
                return false;
        }
 
        private IContainer getFileContainer() {
                if (containerText.getText() != null) {
-                       IPath containerPath = new Path(containerText.getText().trim());
-                       IContainer container;
+                       IPath containerPath = new Path(containerText.getText().trim());
+                       IContainer container = null;
                        if (containerPath.segmentCount() > 1) {
-                               container = ResourcesPlugin.getWorkspace().getRoot().getFolder(
-                                               containerPath);
+                               container = ResourcesPlugin.getWorkspace().getRoot().getFolder(containerPath);
                        } else {
-                               // this is a project
-                               container = ResourcesPlugin.getWorkspace().getRoot()
-                                               .getProject(containerText.getText().trim());
+                               if (containerPath.segmentCount() == 1) {
+                                       // this is a project
+                                       container = ResourcesPlugin.getWorkspace().getRoot().getProject(containerText.getText().trim());
+                               }
                        }
                        if (container != null && container.exists()) {
                                return container;
@@ -232,11 +230,11 @@ private Text containerText;
        public void setVisible(boolean visible) {
                super.setVisible(visible);
                if (visible) {
-                       String fileName=fileText.getText().trim();
+                       String fileName = fileText.getText().trim();
                        if (getFileContainer() != null && fileName.equalsIgnoreCase(INITIAL_FILENAME)) {
                                fileText.setFocus();
                                fileText.setText(fileName);
-                               fileText.setSelection(0, fileName.length()-(new Path(INITIAL_FILENAME)).getFileExtension().length()-1);
+                               fileText.setSelection(0, fileName.length() - (new Path(INITIAL_FILENAME)).getFileExtension().length() - 1);
                        }
                }
        }