[jboss-svn-commits] JBL Code SVN: r14053 - labs/jbossrules/trunk/drools-eclipse/drools-eclipse-plugin/src/main/java/org/drools/eclipse/rulebuilder/wizards.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Aug 6 15:51:22 EDT 2007


Author: pombredanne
Date: 2007-08-06 15:51:22 -0400 (Mon, 06 Aug 2007)
New Revision: 14053

Modified:
   labs/jbossrules/trunk/drools-eclipse/drools-eclipse-plugin/src/main/java/org/drools/eclipse/rulebuilder/wizards/NewBrlFileWizard.java
   labs/jbossrules/trunk/drools-eclipse/drools-eclipse-plugin/src/main/java/org/drools/eclipse/rulebuilder/wizards/NewBrlFileWizardPage.java
Log:
JBRULES-1051: changed the new guided rule wizard to use the same file wizards as others wizards. This alos give for free dialog settings such as the last path used

Modified: labs/jbossrules/trunk/drools-eclipse/drools-eclipse-plugin/src/main/java/org/drools/eclipse/rulebuilder/wizards/NewBrlFileWizard.java
===================================================================
--- labs/jbossrules/trunk/drools-eclipse/drools-eclipse-plugin/src/main/java/org/drools/eclipse/rulebuilder/wizards/NewBrlFileWizard.java	2007-08-06 19:50:07 UTC (rev 14052)
+++ labs/jbossrules/trunk/drools-eclipse/drools-eclipse-plugin/src/main/java/org/drools/eclipse/rulebuilder/wizards/NewBrlFileWizard.java	2007-08-06 19:51:22 UTC (rev 14053)
@@ -1,168 +1,35 @@
-package org.drools.eclipse.rulebuilder.wizards;
-
-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.core.runtime.*;
-import org.eclipse.jface.operation.*;
-import java.lang.reflect.InvocationTargetException;
-import org.eclipse.jface.dialogs.MessageDialog;
-import org.eclipse.jface.viewers.ISelection;
-import org.eclipse.core.resources.*;
-import org.eclipse.core.runtime.CoreException;
-import java.io.*;
-import org.eclipse.ui.*;
-import org.eclipse.ui.ide.IDE;
-
-/**
- * This is a sample new wizard. Its role is to create a new file 
- * resource in the provided container. If the container resource
- * (a folder or a project) is selected in the workspace 
- * when the wizard is opened, it will accept it as the target
- * container. The wizard creates one file with the extension
- * "brl". If a sample multi-page editor (also available
- * as a template) is registered for the same extension, it will
- * be able to open it.
- */
-
-public class NewBrlFileWizard extends Wizard
-    implements
-    INewWizard {
-    private NewBrlFileWizardPage page;
-    private ISelection             selection;
-
-    /**
-     * Constructor for NewBrlFileWizard.
-     */
-    public NewBrlFileWizard() {
-        super();
-        setNeedsProgressMonitor( true );
-    }
-
-    /**
-     * Adding the page to the wizard.
-     */
-
-    public void addPages() {
-        page = new NewBrlFileWizardPage( selection );
-        addPage( page );
-    }
-
-    /**
-     * This method is called when 'Finish' button is pressed in
-     * the wizard. We will create an operation and run it
-     * using wizard as execution context.
-     */
-    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() {
-        //String contents =
-        //	"This is the initial file contents for *.brl file that should be word-sorted in the Preview page of the multi-page editor";
-        String contents = "";
-        return new ByteArrayInputStream( contents.getBytes() );
-    }
-
-    private void throwCoreException(String message) throws CoreException {
-        IStatus status = new Status( IStatus.ERROR,
-                                     "org.drools.eclipse.rulebuilder",
-                                     IStatus.OK,
-                                     message,
-                                     null );
-        throw new CoreException( status );
-    }
-
-    /**
-     * 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) {
-        this.selection = selection;
-    }
-}
\ No newline at end of file
+package org.drools.eclipse.rulebuilder.wizards;
+
+import org.drools.eclipse.DroolsEclipsePlugin;
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.wizard.Wizard;
+import org.eclipse.ui.INewWizard;
+import org.eclipse.ui.IWorkbench;
+
+/**
+ * A wizard to create a BRL guided rule file.
+ */
+public class NewBrlFileWizard extends Wizard implements INewWizard {
+
+    private IWorkbench workbench;
+    private IStructuredSelection selection;
+    private NewBrlFileWizardPage mainPage;
+
+    public void init(IWorkbench workbench, IStructuredSelection selection) {
+        this.workbench = workbench;
+        this.selection = selection;
+        setWindowTitle("New Guided Rule");
+    	ImageDescriptor desc = DroolsEclipsePlugin.getImageDescriptor("icons/drools-large.PNG");
+        setDefaultPageImageDescriptor(desc);
+    }
+
+    public void addPages() {
+        mainPage = new NewBrlFileWizardPage(workbench, selection);
+        addPage(mainPage);
+     }
+
+    public boolean performFinish() {
+        return mainPage.finish();
+    }
+}

Modified: labs/jbossrules/trunk/drools-eclipse/drools-eclipse-plugin/src/main/java/org/drools/eclipse/rulebuilder/wizards/NewBrlFileWizardPage.java
===================================================================
--- labs/jbossrules/trunk/drools-eclipse/drools-eclipse-plugin/src/main/java/org/drools/eclipse/rulebuilder/wizards/NewBrlFileWizardPage.java	2007-08-06 19:50:07 UTC (rev 14052)
+++ labs/jbossrules/trunk/drools-eclipse/drools-eclipse-plugin/src/main/java/org/drools/eclipse/rulebuilder/wizards/NewBrlFileWizardPage.java	2007-08-06 19:51:22 UTC (rev 14053)
@@ -1,189 +1,63 @@
-package org.drools.eclipse.rulebuilder.wizards;
-
-import org.eclipse.core.resources.IContainer;
-import org.eclipse.core.resources.IResource;
-import org.eclipse.core.resources.ResourcesPlugin;
-import org.eclipse.core.runtime.Path;
-import org.eclipse.jface.viewers.ISelection;
-import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.jface.window.Window;
-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;
-
-/**
- * The "New" wizard page allows setting the container for the new file as well
- * as the file name. The page will only accept file name without the extension
- * OR with the extension that matches the expected one (brl).
- */
-
-public class NewBrlFileWizardPage extends WizardPage {
-    private Text       containerText;
-
-    private Text       fileText;
-
-    private ISelection selection;
-
-    /**
-     * Constructor for SampleNewWizardPage.
-     * 
-     * @param pageName
-     */
-    public NewBrlFileWizardPage(ISelection selection) {
-        super( "wizardPage" );
-        setTitle( "Create new BRL rule" );
-        setDescription( "This wizard creates a new file with *.brl extension that will be opened by the guided editor." );
-        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() {
-            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 );
-    }
-
-    /**
-     * 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( "rules.brl" );
-    }
-
-    /**
-     * 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 folder (package) for rule" );
-        if ( dialog.open() == Window.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( "Folder must be specified" );
-            return;
-        }
-        if ( container == null || (container.getType() & (IResource.PROJECT | IResource.FOLDER)) == 0 ) {
-            updateStatus( "Folder 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;
-        }
-        int dotLoc = fileName.lastIndexOf( '.' );
-        if ( dotLoc != -1 ) {
-            String ext = fileName.substring( dotLoc + 1 );
-            if ( ext.equalsIgnoreCase( "brl" ) == false ) {
-                updateStatus( "File extension must be \"brl\"" );
-                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();
-    }
+package org.drools.eclipse.rulebuilder.wizards;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+
+import org.drools.eclipse.DroolsEclipsePlugin;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
+import org.eclipse.ui.ide.IDE;
+
+
+/**
+ * A page to create a new rule using the Guided Editor.
+ */
+public class NewBrlFileWizardPage extends WizardNewFileCreationPage {
+
+    private static final String BRL_EXTENSION = ".brl";
+    private IWorkbench workbench;
+
+    public NewBrlFileWizardPage(IWorkbench workbench, IStructuredSelection selection) {
+        super("createGuidedRuleFilePage", selection);
+        setTitle( "RuleBuilder Editor File" );
+        setDescription( "This wizard creates a new file with *.brl extension that can be opened by a multi-page editor." );
+        this.workbench = workbench;
+    }
+
+    public void createControl(Composite parent) {
+        super.createControl(parent);
+        setPageComplete(true);
+    }
+
+    public boolean finish() {
+        String fileName = getFileName();
+        if (!fileName.endsWith(BRL_EXTENSION)) {
+            setFileName(fileName + BRL_EXTENSION);
+        }
+        org.eclipse.core.resources.IFile newFile = createNewFile();
+        if (newFile == null)
+            return false;
+        try {
+            IWorkbenchWindow dwindow = workbench.getActiveWorkbenchWindow();
+            org.eclipse.ui.IWorkbenchPage page = dwindow.getActivePage();
+            if (page != null)
+                IDE.openEditor(page, newFile, true);
+        } catch (PartInitException e) {
+            DroolsEclipsePlugin.log(e);
+            return false;
+        }
+        return true;
+    }
+
+    protected InputStream getInitialContents() {
+//            return DroolsEclipsePlugin.getDefault().getBundle().getResource(
+//                "org/drools/eclipse/rulebuilder/wizards/template.brl").openStream();
+            String contents = "";
+            return new ByteArrayInputStream( contents.getBytes() );
+    }
+
 }
\ No newline at end of file




More information about the jboss-svn-commits mailing list