Author: DartPeng
Date: 2008-10-08 04:11:10 -0400 (Wed, 08 Oct 2008)
New Revision: 10730
Modified:
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/ui/wizards/SmooksConfigFileNewWizard.java
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/ui/wizards/SmooksConfigFileNewWizardPage.java
Log:
JBIDE-2836
Change the new smooks file wizard page
Modified:
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/ui/wizards/SmooksConfigFileNewWizard.java
===================================================================
---
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/ui/wizards/SmooksConfigFileNewWizard.java 2008-10-08
07:42:08 UTC (rev 10729)
+++
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/ui/wizards/SmooksConfigFileNewWizard.java 2008-10-08
08:11:10 UTC (rev 10730)
@@ -1,22 +1,27 @@
package org.jboss.tools.smooks.ui.wizards;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.InvocationTargetException;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.operation.IRunnableWithProgress;
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.IWorkbenchPage;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
import org.jboss.tools.smooks.graphical.util.GraphicalInformationSaver;
import org.jboss.tools.smooks.ui.editors.TypeIDSelectionWizardPage;
-import org.jboss.tools.smooks.ui.modelparser.SmooksConfigurationFileGenerateContext;
/**
* This is a sample new wizard. Its role is to create a new file resource in the
@@ -29,9 +34,12 @@
public class SmooksConfigFileNewWizard extends Wizard implements INewWizard {
private SmooksConfigFileNewWizardPage page;
- private ISelection selection;
private TypeIDSelectionWizardPage typeIDPage;
+ private IStructuredSelection selection;
+
+ private IWorkbench workbench;
+
/**
* Constructor for SmooksConfigFileNewWizard.
*/
@@ -43,9 +51,9 @@
/**
* Adding the page to the wizard.
*/
-
public void addPages() {
- page = new SmooksConfigFileNewWizardPage(selection);
+ page = new SmooksConfigFileNewWizardPage("newSmooksFile1",
+ getSelection());
addPage(page);
typeIDPage = new TypeIDSelectionWizardPage("");
addPage(typeIDPage);
@@ -56,13 +64,14 @@
* 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();
+ // final String containerName = page.getContainerName();
+ // final String fileName = page.getFileName();
+ final IFile file = page.createNewFile();
IRunnableWithProgress op = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor)
throws InvocationTargetException {
try {
- doFinish(containerName, fileName, monitor);
+ doFinish(file, monitor);
} catch (CoreException e) {
throw new InvocationTargetException(e);
} finally {
@@ -89,21 +98,12 @@
* file.
*/
- private void doFinish(String containerName, String fileName,
- IProgressMonitor monitor) throws CoreException {
+ private void doFinish(final IFile file, IProgressMonitor monitor)
+ throws CoreException {
// create a sample file
- monitor.beginTask("Creating " + fileName, 2);
String sourceTypeID = typeIDPage.getSourceID();
String targetTypeID = typeIDPage.getTargetID();
- 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()) {
@@ -137,8 +137,8 @@
*/
private InputStream openContentStream() {
- String contents = "<?xml version=\"1.0\"
encoding=\"UTF-8\"?>\n"+
- " <smooks-resource-list
xmlns=\"http://www.milyn.org/xsd/smooks-1.0.xsd\"/>";
+ String contents = "<?xml version=\"1.0\"
encoding=\"UTF-8\"?>\n"
+ + " <smooks-resource-list
xmlns=\"http://www.milyn.org/xsd/smooks-1.0.xsd\"/>";
return new ByteArrayInputStream(contents.getBytes());
}
@@ -148,13 +148,16 @@
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.workbench = workbench;
this.selection = selection;
}
+
+ public IStructuredSelection getSelection() {
+ return selection;
+ }
+
+ public void setSelection(IStructuredSelection selection) {
+ this.selection = selection;
+ }
}
\ No newline at end of file
Modified:
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/ui/wizards/SmooksConfigFileNewWizardPage.java
===================================================================
---
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/ui/wizards/SmooksConfigFileNewWizardPage.java 2008-10-08
07:42:08 UTC (rev 10729)
+++
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/ui/wizards/SmooksConfigFileNewWizardPage.java 2008-10-08
08:11:10 UTC (rev 10730)
@@ -1,186 +1,37 @@
package org.jboss.tools.smooks.ui.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.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;
+import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
/**
- * 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 (smooks).
+ *
+ * @author dart
+ *
*/
-
-public class SmooksConfigFileNewWizardPage extends WizardPage {
- private Text containerText;
-
- private Text fileText;
-
- private ISelection selection;
-
- /**
- * Constructor for SampleNewWizardPage.
- *
- * @param pageName
- */
- public SmooksConfigFileNewWizardPage(ISelection selection) {
- super("wizardPage");
- setTitle("Smooks Configuration File");
- setDescription("This wizard creates a new smooks configuration file with *.smooks
extension that can be opened by a Smooks editor.");
- this.selection = selection;
+public class SmooksConfigFileNewWizardPage extends WizardNewFileCreationPage {
+ public SmooksConfigFileNewWizardPage(String pageName,
+ IStructuredSelection selection) {
+ super(pageName, selection);
+ setFileExtension("smooks");
}
- /**
- * @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:");
+ @Override
+ protected boolean validatePage() {
+ boolean flag = super.validatePage();
+ String name = this.getFileName();
+ if (name.indexOf(".") == -1)
+ return flag;
+ String extensionName = name.substring(name.indexOf(".") + 1, name
+ .length());
+ String error = null;
+ if (extensionName.equalsIgnoreCase(this.getFileExtension())) {
- 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());
- }
+ } else {
+ error = "file extension must be \"smooks\"";
}
- fileText.setText("SmooksConfigurationFile.smooks");
- }
-
-
- /**
- * 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());
- }
+ if (error != null) {
+ this.setErrorMessage(error);
}
+ return (error == null);
}
-
- /**
- * 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;
- }
- int dotLoc = fileName.lastIndexOf('.');
- if (dotLoc != -1) {
- String ext = fileName.substring(dotLoc + 1);
- if (ext.equalsIgnoreCase("smooks") == false) {
- updateStatus("File extension must be \"smooks\"");
- 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();
- }
}
\ No newline at end of file