Add a rename node command
authorBruno Sinou <bsinou@argeo.org>
Wed, 22 Jul 2015 20:50:10 +0000 (20:50 +0000)
committerBruno Sinou <bsinou@argeo.org>
Wed, 22 Jul 2015 20:50:10 +0000 (20:50 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@8283 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

org.argeo.eclipse.ui.workbench/icons/rename.gif [new file with mode: 0644]
org.argeo.eclipse.ui.workbench/plugin.xml
org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/commands/DumpNode.java
org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/commands/RenameNode.java [new file with mode: 0644]
org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/jcr/internal/parts/GenericPropertyPage.java

diff --git a/org.argeo.eclipse.ui.workbench/icons/rename.gif b/org.argeo.eclipse.ui.workbench/icons/rename.gif
new file mode 100644 (file)
index 0000000..8048405
Binary files /dev/null and b/org.argeo.eclipse.ui.workbench/icons/rename.gif differ
index 210046381fcd4f45043dd08fed5431108b707f9d..5e281b29a3ed22b75e3a285594fa73e1a4a2bcfc 100644 (file)
                name="Dump Current Selected Node">
                </command>
                <command
+               defaultHandler="org.argeo.eclipse.ui.workbench.commands.RenameNode"
+               id="org.argeo.eclipse.ui.workbench.renameNode"
+               name="Rename Current Selected Node">
+               </command>
+               <command
                defaultHandler="org.argeo.eclipse.ui.workbench.commands.NodeConfigurableDump"
                id="org.argeo.eclipse.ui.workbench.nodeConfigurableDump"
                name="Configurable dump of the selected Node">
                                </visibleWhen>
                        </command>
                        <command
+                       commandId="org.argeo.eclipse.ui.workbench.renameNode"
+                       icon="icons/rename.gif"
+                               label="Rename..."
+                               style="push">
+                               <visibleWhen>
+                                       <iterate> <and>
+                                               <instanceof value="org.argeo.eclipse.ui.workbench.jcr.internal.model.SingleJcrNodeElem"></instanceof>
+                               <with variable="activeMenuSelection"><count value="1"/></with>
+                                       </and> </iterate>
+                               </visibleWhen>
+                       </command>
+                       <command
                        commandId="org.argeo.eclipse.ui.workbench.nodeConfigurableDump"
                        icon="icons/dumpNode.gif"
                                label="Configurable dump..."
index 79d807625ab4832a8659979215f28c9735c340b2..9712e727eda525f973963dbfbc42984ee64cc2b6 100644 (file)
@@ -27,13 +27,14 @@ import java.util.Map;
 
 import javax.jcr.Node;
 import javax.jcr.RepositoryException;
+import javax.jcr.Session;
 
 import org.argeo.ArgeoException;
 import org.argeo.eclipse.ui.specific.OpenFile;
 import org.argeo.eclipse.ui.workbench.CommandUtils;
 import org.argeo.eclipse.ui.workbench.WorkbenchUiPlugin;
-import org.argeo.jcr.JcrUtils;
 import org.argeo.eclipse.ui.workbench.jcr.internal.model.SingleJcrNodeElem;
+import org.argeo.jcr.JcrUtils;
 import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
@@ -42,13 +43,12 @@ import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.ui.handlers.HandlerUtil;
 
 /**
- * If the method
- * <code> HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getSelection() </code>
- * exits and has a SingleJcrNodeElem as first element, it canonically calls the
- * JCR Session.exportSystemView() method on the underlying node with both
- * skipBinary & noRecurse boolean flags set to false.
+ * Canonically call JCR {@link Session#exportSystemView()} on the first element
+ * returned by {@link HandlerUtil#getActiveWorkbenchWindow()}
+ * (...getActivePage().getSelection()), if it is a {@link SingleJcrNodeElem},
+ * with both skipBinary & noRecurse boolean flags set to false.
  * 
- * Resulting stream is saved in a tmp file and opened via the "open file"
+ * Resulting stream is saved in a tmp file and opened via the {@link OpenFile}
  * single-sourced command.
  */
 public class DumpNode extends AbstractHandler {
@@ -86,8 +86,9 @@ public class DumpNode extends AbstractHandler {
                                                        .getTime());
                                        node.getSession().exportSystemView(node.getPath(), fos,
                                                        true, false);
-                                       openGeneratedFile(tmpFile.getAbsolutePath(),
-                                                       "Dump-" + JcrUtils.replaceInvalidChars(node.getName())+ "-" + dateVal + ".xml");
+                                       openGeneratedFile(tmpFile.getAbsolutePath(), "Dump-"
+                                                       + JcrUtils.replaceInvalidChars(node.getName())
+                                                       + "-" + dateVal + ".xml");
                                } catch (RepositoryException e) {
                                        throw new ArgeoException(
                                                        "Unable to perform SystemExport on " + node, e);
diff --git a/org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/commands/RenameNode.java b/org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/commands/RenameNode.java
new file mode 100644 (file)
index 0000000..aaab727
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2007-2012 Argeo GmbH
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *         http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.argeo.eclipse.ui.workbench.commands;
+
+import java.util.Iterator;
+
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+
+import org.argeo.ArgeoException;
+import org.argeo.eclipse.ui.dialogs.SingleValue;
+import org.argeo.eclipse.ui.workbench.WorkbenchUiPlugin;
+import org.argeo.eclipse.ui.workbench.jcr.JcrBrowserView;
+import org.argeo.eclipse.ui.workbench.jcr.internal.model.SingleJcrNodeElem;
+import org.argeo.jcr.JcrUtils;
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.handlers.HandlerUtil;
+
+/**
+ * Canonically call JCR {@link Session#move(String, String)} on the first
+ * element returned by {@link HandlerUtil#getActiveWorkbenchWindow()}
+ * (...getActivePage().getSelection()), if it is a {@link SingleJcrNodeElem}.
+ * The user must then fill a new name in and confirm
+ */
+public class RenameNode extends AbstractHandler {
+       public final static String ID = WorkbenchUiPlugin.ID + ".renameNode";
+
+       public Object execute(ExecutionEvent event) throws ExecutionException {
+               IWorkbenchPage iwp = HandlerUtil.getActiveWorkbenchWindow(event)
+                               .getActivePage();
+
+               ISelection selection = iwp.getSelection();
+               if (selection == null || !(selection instanceof IStructuredSelection))
+                       return null;
+
+               Iterator<?> lst = ((IStructuredSelection) selection).iterator();
+               if (lst.hasNext()) {
+                       Object element = lst.next();
+                       if (element instanceof SingleJcrNodeElem) {
+                               SingleJcrNodeElem sjn = (SingleJcrNodeElem) element;
+                               Node node = sjn.getNode();
+                               Session session = null;
+                               String newName = null;
+                               String oldPath = null;
+                               try {
+                                       newName = SingleValue.ask("New node name",
+                                                       "Please provide a new name for [" + node.getName()
+                                                                       + "]");
+                                       // TODO sanity check and user feedback
+                                       newName = JcrUtils.replaceInvalidChars(newName);
+                                       oldPath = node.getPath();
+                                       session = node.getSession();
+                                       session.move(oldPath, JcrUtils.parentPath(oldPath) + "/"
+                                                       + newName);
+                                       session.save();
+
+                                       // Manually refresh the browser view. Must be enhanced
+                                       if (iwp.getActivePart() instanceof JcrBrowserView)
+                                               ((JcrBrowserView) iwp.getActivePart()).refresh(sjn);
+                               } catch (RepositoryException e) {
+                                       throw new ArgeoException("Unable to rename " + node
+                                                       + " to " + newName, e);
+                               }
+                       }
+               }
+               return null;
+       }
+}
\ No newline at end of file
index 194720253f8a3bbf877a5ae3d87dd9ad488c3adc..0de0af824cb5402026e434665fcb65c9899a3461 100644 (file)
@@ -28,6 +28,8 @@ import org.argeo.ArgeoException;
 import org.argeo.eclipse.ui.workbench.WorkbenchConstants;
 import org.argeo.eclipse.ui.workbench.WorkbenchUiPlugin;
 import org.argeo.eclipse.ui.workbench.jcr.internal.PropertyLabelProvider;
+import org.eclipse.jface.layout.TreeColumnLayout;
+import org.eclipse.jface.viewers.ColumnWeightData;
 import org.eclipse.jface.viewers.ITreeContentProvider;
 import org.eclipse.jface.viewers.TreeViewer;
 import org.eclipse.jface.viewers.Viewer;
@@ -45,7 +47,6 @@ import org.eclipse.ui.forms.widgets.ScrolledForm;
  * Generic editor property page. Lists all properties of current node as a
  * complex tree. TODO: enable editing
  */
-
 public class GenericPropertyPage extends FormPage implements WorkbenchConstants {
        // private final static Log log =
        // LogFactory.getLog(GenericPropertyPage.class);
@@ -61,39 +62,52 @@ public class GenericPropertyPage extends FormPage implements WorkbenchConstants
        protected void createFormContent(IManagedForm managedForm) {
                ScrolledForm form = managedForm.getForm();
                form.setText(WorkbenchUiPlugin.getMessage("genericNodePageTitle"));
+
+               Composite body = form.getBody();
                FillLayout layout = new FillLayout();
                layout.marginHeight = 5;
                layout.marginWidth = 5;
-               form.getBody().setLayout(layout);
-
-               createComplexTree(form.getBody());
+               body.setLayout(layout);
+               createComplexTree(body);
+               // TODO TreeColumnLayout triggers a scroll issue with the form:
+               // The inside body is always to big and a scroll bar is shown
+               // Composite tableCmp = new Composite(body, SWT.NO_FOCUS);
+               // createComplexTree(tableCmp);
        }
 
        private TreeViewer createComplexTree(Composite parent) {
                int style = SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION;
                Tree tree = new Tree(parent, style);
-               createColumn(tree, "Property", SWT.LEFT, 200);
-               createColumn(tree, "Value(s)", SWT.LEFT, 300);
-               createColumn(tree, "Attributes", SWT.LEFT, 65);
+               TreeColumnLayout tableColumnLayout = new TreeColumnLayout();
+
+               createColumn(tree, tableColumnLayout, "Property", SWT.LEFT, 200, 30);
+               createColumn(tree, tableColumnLayout, "Value(s)", SWT.LEFT, 300, 60);
+               createColumn(tree, tableColumnLayout, "Attributes", SWT.LEFT, 75, 0);
+               // Do not apply the treeColumnLayout it does not work yet
+               // parent.setLayout(tableColumnLayout);
+
                tree.setLinesVisible(true);
                tree.setHeaderVisible(true);
 
-               TreeViewer result = new TreeViewer(tree);
-               result.setContentProvider(new TreeContentProvider());
-               result.setLabelProvider(new PropertyLabelProvider());
-               result.setInput(currentNode);
-               result.expandAll();
-               return result;
+               TreeViewer treeViewer = new TreeViewer(tree);
+               treeViewer.setContentProvider(new TreeContentProvider());
+               treeViewer.setLabelProvider(new PropertyLabelProvider());
+               treeViewer.setInput(currentNode);
+               treeViewer.expandAll();
+               return treeViewer;
        }
 
-       private static TreeColumn createColumn(Tree parent, String name, int style,
-                       int width) {
-               TreeColumn result = new TreeColumn(parent, style);
-               result.setText(name);
-               result.setWidth(width);
-               result.setMoveable(true);
-               result.setResizable(true);
-               return result;
+       private static TreeColumn createColumn(Tree parent,
+                       TreeColumnLayout tableColumnLayout, String name, int style,
+                       int width, int weight) {
+               TreeColumn column = new TreeColumn(parent, style);
+               column.setText(name);
+               column.setWidth(width);
+               column.setMoveable(true);
+               column.setResizable(true);
+               tableColumnLayout.setColumnData(column, new ColumnWeightData(weight,
+                               width, true));
+               return column;
        }
 
        //