Commits

Anonymous committed 8bb848f

Added simple lisp nature and new lisp file wizard.

Comments (0)

Files changed (6)

META-INF/MANIFEST.MF

  org.eclipse.debug.ui;bundle-version="3.5.2",
  org.lispdev.editor;bundle-version="0.0.1",
  org.lispdev.preferences;bundle-version="0.0.0",
- org.eclipse.ui.console;bundle-version="3.4.0"
+ org.eclipse.ui.console;bundle-version="3.4.0",
+ org.eclipse.ui.ide
 Bundle-ActivationPolicy: lazy
 Bundle-RequiredExecutionEnvironment: JavaSE-1.6
 Export-Package: org.lispdev.main
+Import-Package: org.eclipse.ui.dialogs
             name="Lisp">
       </perspective>
    </extension>
+   <extension
+         id="LispNature"
+         name="Lisp Nature"
+         point="org.eclipse.core.resources.natures">
+      <runtime>
+         <run
+               class="org.lispdev.main.LispProjectNature">
+         </run>
+      </runtime>
+   </extension>
+   <extension
+         point="org.eclipse.ui.newWizards">
+      <category
+            id="org.lispdev.wizards"
+            name="Lisp">
+      </category>
+      <wizard
+            category="org.lispdev.wizards"
+            class="org.lispdev.main.NewFileWizard"
+            finalPerspective="org.lispdev.main.LispPerspective"
+            icon="icons/lisp-file.gif"
+            id="org.lispdev.wizards.newFileWizard"
+            name="Lisp File"
+            preferredPerspectives="org.lispdev.main.LispPerspective">
+      </wizard>
+   </extension>
 
 </plugin>

src/org/lispdev/main/LispProjectNature.java

+package org.lispdev.main;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IProjectNature;
+import org.eclipse.core.runtime.CoreException;
+
+public class LispProjectNature implements IProjectNature
+{
+
+  public static final String ID = "org.lispdev.lispNature";
+  private IProject project;
+
+  @Override
+  public void configure() throws CoreException
+  {
+    // TODO Auto-generated method stub
+
+  }
+
+  @Override
+  public void deconfigure() throws CoreException
+  {
+    // TODO Auto-generated method stub
+
+  }
+
+  @Override
+  public IProject getProject()
+  {
+    return project;
+  }
+
+  @Override
+  public void setProject(IProject project)
+  {
+    this.project = project;
+  }
+
+}

src/org/lispdev/main/NewFileWizard.java

+package org.lispdev.main;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.InvocationTargetException;
+import java.util.Calendar;
+import java.util.TimeZone;
+
+import org.eclipse.core.resources.IContainer;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.IWorkspaceRoot;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.operation.IRunnableWithProgress;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.wizard.Wizard;
+import org.eclipse.ui.INewWizard;
+import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.ide.IDE;
+
+public class NewFileWizard extends Wizard implements INewWizard
+{
+  public static final String ID = "org.lispdev.wizards.newFileWizard";
+
+  private NewFileWizardPage page;
+  private ISelection selection;
+
+  public NewFileWizard()
+  {
+    super();
+    setNeedsProgressMonitor(true);
+  }
+
+  @Override
+  public void addPages()
+  {
+    page = new NewFileWizardPage(selection);
+    addPage(page);
+  }
+
+  @Override
+  public void init(IWorkbench workbench, IStructuredSelection selection)
+  {
+    this.selection = selection;
+  }
+
+  @Override
+  public boolean performFinish()
+  {
+    final String containerName = page.getContainerName();
+    final String fileName = page.getFileName();
+    IRunnableWithProgress op = new IRunnableWithProgress(){
+      public void run(IProgressMonitor monitor)
+          throws InvocationTargetException
+      {
+        try
+        {
+          doFinish(containerName, fileName, monitor);
+        }
+        catch(CoreException e)
+        {
+          throw new InvocationTargetException(e);
+        }
+        finally
+        {
+          monitor.done();
+        }
+      }
+    };
+    try
+    {
+      getContainer().run(true, false, op);
+    }
+    catch(InterruptedException e)
+    {
+      return false;
+    }
+    catch(InvocationTargetException e)
+    {
+      Throwable realException = e.getTargetException();
+      MessageDialog.openError(getShell(), "Error", realException.getMessage());
+      return false;
+    }
+    return true;
+  }
+
+  /**
+   * The worker method. It will find the container, create the
+   * file if missing or just replace its contents, and open
+   * the editor on the newly created file.
+   */
+  private void doFinish(String containerName, String fileName,
+      IProgressMonitor monitor) throws CoreException
+  {
+    // create a sample file
+    monitor.beginTask("Creating " + fileName, 2);
+    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
+    IResource resource = root.findMember(new Path(containerName));
+    if( !resource.exists() || !(resource instanceof IContainer) )
+    {
+      throwCoreException("Container \"" + containerName + "\" does not exist.");
+    }
+    IContainer container = (IContainer)resource;
+    final IFile file = container.getFile(new Path(fileName));
+    try
+    {
+      InputStream stream = openContentStream();
+      if( file.exists() )
+      {
+        file.setContents(stream, true, true, monitor);
+      }
+      else
+      {
+        file.create(stream, true, monitor);
+      }
+      stream.close();
+    }
+    catch(IOException e)
+    {}
+    monitor.worked(1);
+    monitor.setTaskName("Opening file for editing...");
+    getShell().getDisplay().asyncExec(new Runnable(){
+      public void run()
+      {
+        IWorkbenchPage page =
+            PlatformUI.getWorkbench().getActiveWorkbenchWindow()
+              .getActivePage();
+        try
+        {
+          IDE.openEditor(page, file, true);
+        }
+        catch(PartInitException e)
+        {}
+      }
+    });
+    monitor.worked(1);
+  }
+
+  /**
+   * We will initialize file contents with a sample text.
+   */
+  private InputStream openContentStream()
+  {
+    Calendar cal = Calendar.getInstance(TimeZone.getDefault());
+    String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
+    java.text.SimpleDateFormat sdf =
+        new java.text.SimpleDateFormat(DATE_FORMAT);
+    sdf.setTimeZone(TimeZone.getDefault());
+
+    String contents = ";;;; Created on " + sdf.format(cal.getTime()) + "\n";
+    return new ByteArrayInputStream(contents.getBytes());
+  }
+
+  private void throwCoreException(String message) throws CoreException
+  {
+    IStatus status =
+        new Status(IStatus.ERROR, Plugin.ID, IStatus.OK, message, null);
+    throw new CoreException(status);
+  }
+
+}

src/org/lispdev/main/NewFileWizardPage.java

+package org.lispdev.main;
+
+import org.eclipse.core.resources.IContainer;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.jface.dialogs.IDialogPage;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.wizard.WizardPage;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.ModifyEvent;
+import org.eclipse.swt.events.ModifyListener;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Text;
+import org.eclipse.ui.dialogs.ContainerSelectionDialog;
+
+public class NewFileWizardPage extends WizardPage
+{
+  private Text containerText;
+
+  private Text fileText;
+
+  private final ISelection selection;
+
+  /**
+   * Constructor for SampleNewWizardPage.
+   *
+   * @param pageName
+   */
+  public NewFileWizardPage(ISelection selection)
+  {
+    super("wizardPage");
+    setTitle("Lisp File");
+    setDescription("This wizard creates a new lisp file.");
+    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("&Container:");
+
+    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("Browse...");
+    button.addSelectionListener(new SelectionAdapter(){
+      @Override
+      public void widgetSelected(SelectionEvent e)
+      {
+        handleBrowse();
+      }
+    });
+    label = new Label(container, SWT.NULL);
+    label.setText("&File name:");
+
+    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);
+    fileText.setFocus();
+  }
+
+  /**
+   * 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.setText("new-file.lisp");
+    fileText.setFocus();
+  }
+
+  /**
+   * 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, "Select new file container");
+    if( dialog.open() == ContainerSelectionDialog.OK )
+    {
+      Object[] result = dialog.getResult();
+      if( result.length == 1 )
+      {
+        containerText.setText(((Path)result[0]).toString());
+      }
+    }
+  }
+
+  /**
+   * Ensures that both text fields are set.
+   */
+
+  private void dialogChanged()
+  {
+    IResource container =
+        ResourcesPlugin.getWorkspace().getRoot()
+          .findMember(new Path(getContainerName()));
+    String fileName = getFileName();
+
+    if( getContainerName().length() == 0 )
+    {
+      updateStatus("File container must be specified");
+      return;
+    }
+    if( container == null
+        || (container.getType() & (IResource.PROJECT | IResource.FOLDER)) == 0 )
+    {
+      updateStatus("File container must exist");
+      return;
+    }
+    if( !container.isAccessible() )
+    {
+      updateStatus("Project must be writable");
+      return;
+    }
+    if( fileName.length() == 0 )
+    {
+      updateStatus("File name must be specified");
+      return;
+    }
+    if( fileName.replace('\\', '/').indexOf('/', 1) > 0 )
+    {
+      updateStatus("File name must be valid");
+      return;
+    }
+    if( !fileName.endsWith(".lisp") && !fileName.endsWith(".cl")
+        && !fileName.endsWith(".asd") && !fileName.endsWith(".el") )
+    {
+      updateStatus("File extension must be lisp, cl, asd, or el.");
+      return;
+    }
+
+    // Make sure the file doesn't already exist
+
+    if( container instanceof IContainer )
+    {
+      final IFile file = ((IContainer)container).getFile(new Path(fileName));
+
+      if( file.exists() )
+      {
+        updateStatus("File already exists. Use another name.");
+        return;
+      } // if
+    }
+    updateStatus(null);
+  }
+
+  private void updateStatus(String message)
+  {
+    setErrorMessage(message);
+    setPageComplete(message == null);
+  }
+
+  public String getContainerName()
+  {
+    return containerText.getText();
+  }
+
+  public String getFileName()
+  {
+    return fileText.getText();
+  }
+}

src/org/lispdev/main/Plugin.java

 {
 
   // The plug-in ID
-  public static final String PLUGIN_ID = "org.lispdev.main";
+  public static final String ID = "org.lispdev.main";
 
   // The shared instance
   private static Plugin plugin;