Author: stefanegli
Date: Wed Aug 28 15:06:58 2013
New Revision: 1518246

URL: http://svn.apache.org/r1518246
Log:
SLING-2985 : work-in-progress: should eventually allow to create a new node in 
the content-browser

Added:
    
sling/trunk/tooling/ide/eclipse-ui/src/org/apache/sling/ide/eclipse/ui/wizards/
    
sling/trunk/tooling/ide/eclipse-ui/src/org/apache/sling/ide/eclipse/ui/wizards/NewNodeWizard.java
   (with props)
    
sling/trunk/tooling/ide/eclipse-ui/src/org/apache/sling/ide/eclipse/ui/wizards/NewNodeWizardPage.java
   (with props)

Added: 
sling/trunk/tooling/ide/eclipse-ui/src/org/apache/sling/ide/eclipse/ui/wizards/NewNodeWizard.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/tooling/ide/eclipse-ui/src/org/apache/sling/ide/eclipse/ui/wizards/NewNodeWizard.java?rev=1518246&view=auto
==============================================================================
--- 
sling/trunk/tooling/ide/eclipse-ui/src/org/apache/sling/ide/eclipse/ui/wizards/NewNodeWizard.java
 (added)
+++ 
sling/trunk/tooling/ide/eclipse-ui/src/org/apache/sling/ide/eclipse/ui/wizards/NewNodeWizard.java
 Wed Aug 28 15:06:58 2013
@@ -0,0 +1,148 @@
+package org.apache.sling.ide.eclipse.ui.wizards;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.InvocationTargetException;
+
+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.IWorkbenchWizard;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.ide.IDE;
+
+public class NewNodeWizard extends Wizard implements INewWizard {
+
+       private NewNodeWizardPage page;
+       private ISelection selection;
+       
+       public NewNodeWizard() {
+               // TODO Auto-generated constructor stub
+       }
+       
+       /**
+        * Adding the page to the wizard.
+        */
+
+       public void addPages() {
+               page = new NewNodeWizardPage(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(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 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 *.mpe file that 
should be word-sorted in the Preview page of the multi-page editor";
+               return new ByteArrayInputStream(contents.getBytes());
+       }
+
+       private void throwCoreException(String message) throws CoreException {
+               IStatus status =
+                       new Status(IStatus.ERROR, 
"org.apache.sling.ide.eclipse-ui", 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;
+       }
+}

Propchange: 
sling/trunk/tooling/ide/eclipse-ui/src/org/apache/sling/ide/eclipse/ui/wizards/NewNodeWizard.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
sling/trunk/tooling/ide/eclipse-ui/src/org/apache/sling/ide/eclipse/ui/wizards/NewNodeWizardPage.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/tooling/ide/eclipse-ui/src/org/apache/sling/ide/eclipse/ui/wizards/NewNodeWizardPage.java?rev=1518246&view=auto
==============================================================================
--- 
sling/trunk/tooling/ide/eclipse-ui/src/org/apache/sling/ide/eclipse/ui/wizards/NewNodeWizardPage.java
 (added)
+++ 
sling/trunk/tooling/ide/eclipse-ui/src/org/apache/sling/ide/eclipse/ui/wizards/NewNodeWizardPage.java
 Wed Aug 28 15:06:58 2013
@@ -0,0 +1,183 @@
+package org.apache.sling.ide.eclipse.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;
+
+public class NewNodeWizardPage extends WizardPage {
+       //private Text containerText;
+
+       private Text fileText;
+
+       private ISelection selection;
+
+       /**
+        * Constructor for SampleNewWizardPage.
+        * 
+        * @param pageName
+        */
+       public NewNodeWizardPage(ISelection selection) {
+               super("wizardPage");
+               setTitle("Add a new JCR node");
+               setDescription("Create a new JCR node under the current 
selection");
+               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 label = new Label(container, SWT.NULL);
+               label.setText("Node name:");
+
+               fileText = new Text(container, SWT.BORDER | SWT.SINGLE);
+               GridData 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("new_file.mpe");
+       }
+
+       /**
+        * 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;
+//             }
+//             int dotLoc = fileName.lastIndexOf('.');
+//             if (dotLoc != -1) {
+//                     String ext = fileName.substring(dotLoc + 1);
+//                     if (ext.equalsIgnoreCase("mpe") == false) {
+//                             updateStatus("File extension must be \"mpe\"");
+//                             return;
+//                     }
+//             }
+               if (fileText.getText().length()==0) {
+                       updateStatus("Name must not be empty");
+                       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

Propchange: 
sling/trunk/tooling/ide/eclipse-ui/src/org/apache/sling/ide/eclipse/ui/wizards/NewNodeWizardPage.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain


Reply via email to