You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by st...@apache.org on 2014/05/16 13:41:33 UTC

svn commit: r1595163 - in /sling/trunk/tooling/ide: eclipse-core/src/org/apache/sling/ide/eclipse/core/ eclipse-ui/src/org/apache/sling/ide/eclipse/ui/actions/

Author: stefanegli
Date: Fri May 16 11:41:32 2014
New Revision: 1595163

URL: http://svn.apache.org/r1595163
Log:
SLING-2985 : for creating a new node: provide a dialog that allows to define the nodeType - the nodeType is calculated via the NodeTypeRegistry and only allows primary nodeTypes are shown.

Added:
    sling/trunk/tooling/ide/eclipse-ui/src/org/apache/sling/ide/eclipse/ui/actions/NewNodeDialog.java   (with props)
Modified:
    sling/trunk/tooling/ide/eclipse-core/src/org/apache/sling/ide/eclipse/core/ServerUtil.java
    sling/trunk/tooling/ide/eclipse-ui/src/org/apache/sling/ide/eclipse/ui/actions/JcrNewNodeAction.java

Modified: sling/trunk/tooling/ide/eclipse-core/src/org/apache/sling/ide/eclipse/core/ServerUtil.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/ide/eclipse-core/src/org/apache/sling/ide/eclipse/core/ServerUtil.java?rev=1595163&r1=1595162&r2=1595163&view=diff
==============================================================================
--- sling/trunk/tooling/ide/eclipse-core/src/org/apache/sling/ide/eclipse/core/ServerUtil.java (original)
+++ sling/trunk/tooling/ide/eclipse-core/src/org/apache/sling/ide/eclipse/core/ServerUtil.java Fri May 16 11:41:32 2014
@@ -25,12 +25,53 @@ import org.apache.sling.ide.transport.Re
 import org.apache.sling.ide.transport.RepositoryException;
 import org.apache.sling.ide.transport.RepositoryFactory;
 import org.apache.sling.ide.transport.RepositoryInfo;
+import org.eclipse.core.resources.IProject;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.NullProgressMonitor;
 import org.eclipse.core.runtime.Status;
+import org.eclipse.wst.server.core.IModule;
 import org.eclipse.wst.server.core.IServer;
+import org.eclipse.wst.server.core.ServerCore;
 
 public abstract class ServerUtil {
+    
+    public static Repository getDefaultRepository(IProject project) {
+        IServer server = getDefaultServer(project);
+        try {
+            return getRepository(server, new NullProgressMonitor());
+        } catch (CoreException e) {
+            e.printStackTrace();
+            return null;
+        }
+    }
+
+    private static IServer getDefaultServer(IProject project) {
+        IModule module = org.eclipse.wst.server.core.ServerUtil.getModule(project);
+        if (module==null) {
+            // if there's no module for a project then there's no IServer for sure - which 
+            // is what we need to create a RepositoryInfo
+            return null;
+        }
+        IServer server = ServerCore.getDefaultServer(module);
+        if (server!=null) {
+            return server;
+        }
+        // then we cannot create a repository
+        IServer[] allServers = ServerCore.getServers();
+        out: for (int i = 0; i < allServers.length; i++) {
+            IServer aServer = allServers[i];
+            IModule[] allModules = aServer.getModules();
+            for (int j = 0; j < allModules.length; j++) {
+                IModule aMoudle = allModules[j];
+                if (aMoudle.equals(module)) {
+                    server = aServer;
+                    break out;
+                }
+            }
+        }
+        return server;
+    }
 
     public static Repository getRepository(IServer server, IProgressMonitor monitor) throws CoreException {
         RepositoryFactory repository = Activator.getDefault().getRepositoryFactory();

Modified: sling/trunk/tooling/ide/eclipse-ui/src/org/apache/sling/ide/eclipse/ui/actions/JcrNewNodeAction.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/ide/eclipse-ui/src/org/apache/sling/ide/eclipse/ui/actions/JcrNewNodeAction.java?rev=1595163&r1=1595162&r2=1595163&view=diff
==============================================================================
--- sling/trunk/tooling/ide/eclipse-ui/src/org/apache/sling/ide/eclipse/ui/actions/JcrNewNodeAction.java (original)
+++ sling/trunk/tooling/ide/eclipse-ui/src/org/apache/sling/ide/eclipse/ui/actions/JcrNewNodeAction.java Fri May 16 11:41:32 2014
@@ -16,16 +16,18 @@
  */
 package org.apache.sling.ide.eclipse.ui.actions;
 
+import org.apache.sling.ide.eclipse.core.ServerUtil;
+import org.apache.sling.ide.eclipse.ui.internal.Activator;
 import org.apache.sling.ide.eclipse.ui.nav.model.JcrNode;
+import org.apache.sling.ide.transport.NodeTypeRegistry;
+import org.apache.sling.ide.transport.Repository;
+import org.apache.sling.ide.transport.RepositoryException;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.jface.action.IAction;
-import org.eclipse.jface.dialogs.IInputValidator;
-import org.eclipse.jface.dialogs.InputDialog;
 import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.swt.widgets.Shell;
-import org.eclipse.swt.widgets.Widget;
 import org.eclipse.ui.IObjectActionDelegate;
 import org.eclipse.ui.IWorkbenchPart;
 
@@ -38,28 +40,31 @@ public class JcrNewNodeAction implements
 	}
 
 	@Override
-	public void run(IAction action) {
-		if (selection==null || !(selection instanceof IStructuredSelection)) {
-			return;
-		}
-		IStructuredSelection ss = (IStructuredSelection)selection;
-		JcrNode node = (JcrNode) ss.getFirstElement();
-		InputDialog id = new InputDialog(shell, "Enter JCR node name", 
-				"Enter name for new JCR node under '"+node.getName()+"':", "", new IInputValidator() {
-					
-					@Override
-					public String isValid(String newText) {
-						if (newText!=null && newText.trim().length()>0 && newText.trim().equals(newText)) {
-							return null;
-						} else {
-							return "Invalid input";
-						}
-					}
-				});
-		if (id.open() == IStatus.OK) {
-			node.createChild(id.getValue(), "nt:unstructured");
-		}
-	}
+    public void run(IAction action) {
+        if (selection==null || !(selection instanceof IStructuredSelection)) {
+            return;
+        }
+        IStructuredSelection ss = (IStructuredSelection)selection;
+        JcrNode node = (JcrNode) ss.getFirstElement();
+        if (!node.canCreateChild()) {
+            MessageDialog.openInformation(shell, "Cannot create node", "Node not in filter.xml");
+            return;
+        }
+        Repository repository = ServerUtil.getDefaultRepository(node.getProject());
+        NodeTypeRegistry ntManager = repository.getNodeTypeRegistry();
+        
+        try {
+            final NewNodeDialog nnd = new NewNodeDialog(shell, node, ntManager);
+            if (nnd.open() == IStatus.OK) {
+                node.createChild(nnd.getValue(), nnd.getChosenNodeType());
+                return;
+            }
+        } catch (RepositoryException e1) {
+            Activator.getDefault().getPluginLogger().warn(
+                    "Could not open NewNodeDialog due to "+e1, e1);
+        }
+        
+    }
 
 	@Override
 	public void selectionChanged(IAction action, ISelection selection) {

Added: sling/trunk/tooling/ide/eclipse-ui/src/org/apache/sling/ide/eclipse/ui/actions/NewNodeDialog.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/ide/eclipse-ui/src/org/apache/sling/ide/eclipse/ui/actions/NewNodeDialog.java?rev=1595163&view=auto
==============================================================================
--- sling/trunk/tooling/ide/eclipse-ui/src/org/apache/sling/ide/eclipse/ui/actions/NewNodeDialog.java (added)
+++ sling/trunk/tooling/ide/eclipse-ui/src/org/apache/sling/ide/eclipse/ui/actions/NewNodeDialog.java Fri May 16 11:41:32 2014
@@ -0,0 +1,108 @@
+package org.apache.sling.ide.eclipse.ui.actions;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedList;
+
+import org.apache.sling.ide.eclipse.ui.nav.model.JcrNode;
+import org.apache.sling.ide.transport.NodeTypeRegistry;
+import org.apache.sling.ide.transport.RepositoryException;
+import org.eclipse.jface.dialogs.IDialogConstants;
+import org.eclipse.jface.dialogs.InputDialog;
+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.widgets.Combo;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Shell;
+
+public class NewNodeDialog extends InputDialog {
+
+    private final String parentNodeType;
+    private final NodeTypeRegistry ntManager;
+    protected String comboSelection;
+    private Collection<String> allowedChildren;
+
+    public NewNodeDialog(Shell parentShell, JcrNode node,
+            NodeTypeRegistry ntManager) throws RepositoryException {
+        super(parentShell, "Enter JCR node name", 
+                "Enter name for new node under:\n path: "+node.getJcrPath(), "", null);
+        this.parentNodeType = node.getPrimaryType();
+        this.ntManager = ntManager;
+        final LinkedList<String> ac = new LinkedList<String>(ntManager.getAllowedPrimaryChildNodeTypes(parentNodeType));
+        Collections.sort(ac);
+        this.allowedChildren = ac;
+    }
+
+    @Override
+    protected Control createDialogArea(Composite parent) {
+        Composite composite = (Composite) super.createDialogArea(parent);
+        
+        Control[] children = composite.getChildren();
+        Control errorMessageText = children[children.length-1];
+        
+        // now add the node type dropbox-combo
+        Label label = new Label(composite, SWT.WRAP);
+        label.moveAbove(errorMessageText);
+        label.setText("Define node type");
+        GridData data = new GridData(GridData.GRAB_HORIZONTAL
+                | GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL
+                | GridData.VERTICAL_ALIGN_CENTER);
+        data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
+        label.setLayoutData(data);
+        label.setFont(parent.getFont());
+
+        final Combo combo = new Combo(composite, SWT.DROP_DOWN);
+        combo.moveAbove(errorMessageText);
+        combo.setItems(allowedChildren.toArray(new String[0]));
+        combo.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
+                | GridData.HORIZONTAL_ALIGN_FILL));
+        combo.addSelectionListener(new SelectionAdapter() {
+            @Override
+            public void widgetSelected(SelectionEvent e) {
+                comboSelection = combo.getText();
+                validateInput();
+            }
+        });
+        combo.addModifyListener(new ModifyListener() {
+
+            @Override
+            public void modifyText(ModifyEvent e) {
+                comboSelection = combo.getText();
+                validateInput();
+            }
+            
+        });
+        if (allowedChildren.size()==1) {
+            combo.setText(allowedChildren.iterator().next());
+        }
+        
+        return composite;
+    }
+    
+    public String getChosenNodeType() {
+        return comboSelection;
+    }
+
+    protected void validateInput() {
+        final String firstInput = getValue();
+        final String secondInput = comboSelection;
+        try {
+            if (secondInput==null || secondInput.length()==0) {
+                setErrorMessage("");
+            } else if (ntManager.isAllowedPrimaryChildNodeType(parentNodeType, secondInput)) {
+                setErrorMessage(null);
+            } else {
+                setErrorMessage("Error: Invalid child node type of "+parentNodeType);
+            }
+        } catch(RepositoryException e) {
+            setErrorMessage("RepositoryException: "+e);
+        }
+        
+    };
+}

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