Fix copy workspace & delete artifacts commands
authorBruno Sinou <bsinou@argeo.org>
Tue, 26 Jun 2012 08:43:46 +0000 (08:43 +0000)
committerBruno Sinou <bsinou@argeo.org>
Tue, 26 Jun 2012 08:43:46 +0000 (08:43 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@5347 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

eclipse/plugins/org.argeo.slc.client.ui.dist/META-INF/spring/commands.xml
eclipse/plugins/org.argeo.slc.client.ui.dist/plugin.xml
eclipse/plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/commands/CopyWorkspace.java
eclipse/plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/commands/DeleteArtifacts.java [new file with mode: 0644]
eclipse/plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/commands/RefreshDistributionOverviewPage.java [new file with mode: 0644]
eclipse/plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/editors/DistributionOverviewPage.java

index 61f60e2a2910a1516c09ea701c3ca6144ddd639b..63d63517e7a7a736f4c33616efcac0a06d3251ac 100644 (file)
                class="org.argeo.slc.client.ui.dist.commands.RefreshDistributionsView"
                scope="prototype">
        </bean>
+       <bean id="org.argeo.slc.client.ui.dist.deleteArtifacts"
+               class="org.argeo.slc.client.ui.dist.commands.DeleteArtifacts"
+               scope="prototype">
+               <property name="repository" ref="javaRepository" />
+       </bean>
+       <bean id="refreshDistributionOverviewPage"
+               class="org.argeo.slc.client.ui.dist.commands.RefreshDistributionOverviewPage"
+               scope="prototype">
+       </bean>
 </beans>
index 3b426c6b380283398d06e36031e296dc719faecc..8750f012f8bcec503e05137f3463e4560e0d04ef 100644 (file)
             defaultHandler="org.argeo.eclipse.spring.SpringCommandHandler"
             name="Refresh Distributions View">
        </command>
+       <command
+            id="org.argeo.slc.client.ui.dist.refreshDistributionOverviewPage"
+            defaultHandler="org.argeo.eclipse.spring.SpringCommandHandler"
+            name="Refresh distribution overview page">
+       </command>
+       <command
+            id="org.argeo.slc.client.ui.dist.deleteArtifacts"
+            defaultHandler="org.argeo.eclipse.spring.SpringCommandHandler"
+            name="Remove selected artifacts">
+       </command>
     </extension>
        <extension
          id="menu:org.eclipse.ui.main.menu"
index 5e011d19949d914dec9788ef87979808a13b97e8..08286ae3b1fb3c25704767a0d7a5bd313254ad39 100644 (file)
@@ -6,6 +6,7 @@ import javax.jcr.Property;
 import javax.jcr.PropertyIterator;
 import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
+import javax.jcr.Session;
 import javax.jcr.nodetype.NodeType;
 
 import org.apache.commons.logging.Log;
@@ -13,10 +14,13 @@ import org.apache.commons.logging.LogFactory;
 import org.argeo.ArgeoException;
 import org.argeo.jcr.JcrUtils;
 import org.argeo.slc.client.ui.dist.DistPlugin;
+import org.argeo.slc.client.ui.dist.utils.CommandHelpers;
+import org.argeo.slc.jcr.SlcTypes;
 import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
-import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.dialogs.InputDialog;
+import org.eclipse.ui.IWorkbenchWindow;
 
 /**
  * Create a copy of the chosen workspace in the current repository.
@@ -39,64 +43,77 @@ public class CopyWorkspace extends AbstractHandler {
                if (log.isTraceEnabled())
                        log.debug("WORKSPACE " + srcWorkspaceName + " About to be copied");
 
-               MessageDialog.openWarning(DistPlugin.getDefault()
-                               .getWorkbench().getDisplay().getActiveShell(),
-                               "WARNING", "Not yet implemented");
-               return null;
-               //
-               //
-               // IWorkbenchWindow iww = DistPlugin.getDefault().getWorkbench()
-               // .getActiveWorkbenchWindow();
-               // InputDialog inputDialog = new InputDialog(iww.getShell(),
-               // "New copy of the current workspace",
-               // "Choose a name for the workspace to create", "", null);
-               // inputDialog.open();
-               // String newWorkspaceName = inputDialog.getValue();
-               // Session srcSession = null;
-               // Session newSession = null;
-               // try {
-               // srcSession = repository.login(srcWorkspaceName);
-               // // FIXME: simple call to Workspace.create(newName, oldName) does not
-               // // work
-               //
-               // srcSession.getWorkspace().createWorkspace(newWorkspaceName,
-               // srcWorkspaceName);
-               //
-               // // // Create the workspace
-               // // srcSession.getWorkspace().createWorkspace(newWorkspaceName);
-               // // Node srcRootNode = srcSession.getRootNode();
-               // // // log in the newly created workspace
-               // // newSession = repository.login(newWorkspaceName);
-               // // newSession.save();
-               // // Node newRootNode = newSession.getRootNode();
-               // // copy(srcRootNode, newRootNode);
-               // // newSession.save();
-               //
-               // CommandHelpers.callCommand(RefreshDistributionsView.ID);
-               // } catch (RepositoryException re) {
-               // throw new ArgeoException(
-               // "Unexpected error while creating the new workspace.", re);
-               // } finally {
-               // if (srcSession != null)
-               // srcSession.logout();
-               // if (newSession != null)
-               // newSession.logout();
-               // }
+               // MessageDialog.openWarning(DistPlugin.getDefault()
+               // .getWorkbench().getDisplay().getActiveShell(),
+               // "WARNING", "Not yet implemented");
                // return null;
+
+               IWorkbenchWindow iww = DistPlugin.getDefault().getWorkbench()
+                               .getActiveWorkbenchWindow();
+               InputDialog inputDialog = new InputDialog(iww.getShell(),
+                               "New copy of the current workspace",
+                               "Choose a name for the workspace to create", "", null);
+               inputDialog.open();
+               String newWorkspaceName = inputDialog.getValue();
+               Session srcSession = null;
+               Session newSession = null;
+               try {
+                       srcSession = repository.login(srcWorkspaceName);
+
+                       // Create the workspace
+                       srcSession.getWorkspace().createWorkspace(newWorkspaceName);
+                       Node srcRootNode = srcSession.getRootNode();
+                       // log in the newly created workspace
+                       newSession = repository.login(newWorkspaceName);
+                       newSession.save();
+                       Node newRootNode = newSession.getRootNode();
+                       copy(srcRootNode, newRootNode);
+                       newSession.save();
+
+                       CommandHelpers.callCommand(RefreshDistributionsView.ID);
+               } catch (RepositoryException re) {
+                       throw new ArgeoException(
+                                       "Unexpected error while creating the new workspace.", re);
+               } finally {
+                       if (srcSession != null)
+                               srcSession.logout();
+                       if (newSession != null)
+                               newSession.logout();
+               }
+               return null;
        }
 
        // FIXME : commons is frozen, cannot fix the problem directly there.
        // test and report corresponding patch
        private void copy(Node fromNode, Node toNode) {
-
                try {
-                       // cannot manipulate security nodes this way:
-                       if (fromNode.isNodeType("rep:ACL"))
+                       if (log.isDebugEnabled())
+                               log.debug("copy node :" + fromNode.getPath());
+       
+                       
+                       // FIXME : small hack to enable specific workspace copy
+                       if (fromNode.isNodeType("rep:ACL") || fromNode.isNodeType("rep:system")){
+                               if (log.isTraceEnabled())
+                                       log.trace("node skipped");
                                return;
+                       }
+                       
+                       // add mixins
+                       for (NodeType mixinType : fromNode.getMixinNodeTypes()) {
+                               toNode.addMixin(mixinType.getName());
+                       }
+                       
+                       // Double check
+                       for (NodeType mixinType : toNode.getMixinNodeTypes()) {
+                               log.debug(mixinType.getName());
+                       }
+                       
                        // process properties
                        PropertyIterator pit = fromNode.getProperties();
                        properties: while (pit.hasNext()) {
                                Property fromProperty = pit.nextProperty();
+                               String propName = fromProperty.getName();
+                               try{
                                String propertyName = fromProperty.getName();
                                if (toNode.hasProperty(propertyName)
                                                && toNode.getProperty(propertyName).getDefinition()
@@ -117,19 +134,12 @@ public class CopyWorkspace extends AbstractHandler {
                                } else {
                                        toNode.setProperty(propertyName, fromProperty.getValue());
                                }
+                               } catch (RepositoryException e) {
+                                       throw new ArgeoException("Cannot property " + propName, e);
+                               }
                        }
 
-                       // update jcr:lastModified and jcr:lastModifiedBy in toNode in case
-                       // they existed, before adding the mixins
-                       if (!toNode.getDefinition().isProtected())
-                               JcrUtils.updateLastModified(toNode);
-
-                       // add mixins
-                       for (NodeType mixinType : fromNode.getMixinNodeTypes()) {
-                               toNode.addMixin(mixinType.getName());
-                       }
-
-                       // process children nodes
+                       // recursively process children nodes
                        NodeIterator nit = fromNode.getNodes();
                        while (nit.hasNext()) {
                                Node fromChild = nit.nextNode();
@@ -144,7 +154,16 @@ public class CopyWorkspace extends AbstractHandler {
                                copy(fromChild, toChild);
                        }
 
-                       toNode.getSession().save();
+                       // update jcr:lastModified and jcr:lastModifiedBy in toNode in case
+                       // they existed
+                       if (!toNode.getDefinition().isProtected()
+                                       && toNode.isNodeType(NodeType.MIX_LAST_MODIFIED))
+                               JcrUtils.updateLastModified(toNode);
+                       
+                       // Workaround to reduce session size: artifact is a saveable unity
+                       if (toNode.isNodeType(SlcTypes.SLC_ARTIFACT))
+                               toNode.getSession().save();
+                       
                } catch (RepositoryException e) {
                        throw new ArgeoException("Cannot copy " + fromNode + " to "
                                        + toNode, e);
diff --git a/eclipse/plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/commands/DeleteArtifacts.java b/eclipse/plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/commands/DeleteArtifacts.java
new file mode 100644 (file)
index 0000000..5ced160
--- /dev/null
@@ -0,0 +1,81 @@
+package org.argeo.slc.client.ui.dist.commands;
+
+import java.util.Iterator;
+
+import javax.jcr.Node;
+import javax.jcr.Repository;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+
+import org.argeo.ArgeoException;
+import org.argeo.slc.client.ui.dist.DistPlugin;
+import org.argeo.slc.client.ui.dist.utils.CommandHelpers;
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IWorkbenchPart;
+
+/**
+ * Delete chosen artifacts from the current workspace.
+ */
+
+public class DeleteArtifacts extends AbstractHandler {
+       // private static final Log log = LogFactory.getLog(DeleteWorkspace.class);
+       public final static String ID = DistPlugin.ID + ".deleteArtifacts";
+       public final static String DEFAULT_LABEL = "Delete selected items";
+       public final static String DEFAULT_ICON_PATH = "icons/removeItem.gif";
+
+       /* DEPENDENCY INJECTION */
+       private Repository repository;
+
+       public Object execute(ExecutionEvent event) throws ExecutionException {
+               String msg = "Your are about to definitively remove these artifacts.\n"
+                               + "Do you really want to proceed ?";
+
+               boolean result = MessageDialog.openConfirm(DistPlugin.getDefault()
+                               .getWorkbench().getDisplay().getActiveShell(),
+                               "Confirm deletion", msg);
+               if (result) {
+                       Session session = null;
+                       try {
+                               session = repository.login();
+                               IWorkbenchPart activePart = DistPlugin.getDefault()
+                                               .getWorkbench().getActiveWorkbenchWindow()
+                                               .getActivePage().getActivePart();
+
+                               if (activePart instanceof IEditorPart) {
+                                       ISelection selector = ((IEditorPart) activePart)
+                                                       .getEditorSite().getSelectionProvider()
+                                                       .getSelection();
+                                       if (selector != null
+                                                       && selector instanceof IStructuredSelection) {
+                                               Iterator<?> it = ((IStructuredSelection) selector)
+                                                               .iterator();
+                                               while (it.hasNext()) {
+                                                       Node node = (Node) it.next();
+                                                       node.remove();
+                                               }
+                                       }
+                                       session.save();
+                               }
+                               CommandHelpers.callCommand(RefreshDistributionOverviewPage.ID);
+                       } catch (RepositoryException re) {
+                               throw new ArgeoException(
+                                               "Unexpected error while deleting artifacts.", re);
+                       } finally {
+                               if (session != null)
+                                       session.logout();
+                       }
+               }
+               return null;
+       }
+
+       /* DEPENDENCY INJECTION */
+       public void setRepository(Repository repository) {
+               this.repository = repository;
+       }
+}
diff --git a/eclipse/plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/commands/RefreshDistributionOverviewPage.java b/eclipse/plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/commands/RefreshDistributionOverviewPage.java
new file mode 100644 (file)
index 0000000..d6d86e1
--- /dev/null
@@ -0,0 +1,37 @@
+package org.argeo.slc.client.ui.dist.commands;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.argeo.slc.client.ui.dist.DistPlugin;
+import org.argeo.slc.client.ui.dist.editors.DistributionEditor;
+import org.argeo.slc.client.ui.dist.editors.DistributionOverviewPage;
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.forms.editor.IFormPage;
+
+/**
+ * Force refresh of the DistributionsView
+ */
+
+public class RefreshDistributionOverviewPage extends AbstractHandler {
+       private static final Log log = LogFactory
+                       .getLog(RefreshDistributionOverviewPage.class);
+       public final static String ID = DistPlugin.ID
+                       + ".refreshDistributionOverviewPage";
+       public final static String DEFAULT_LABEL = "Refresh the distribution overview";
+       public final static String DEFAULT_ICON_PATH = "icons/refresh.png";
+
+       public Object execute(ExecutionEvent event) throws ExecutionException {
+               IWorkbenchPart activePart = DistPlugin.getDefault().getWorkbench()
+                               .getActiveWorkbenchWindow().getActivePage().getActivePart();
+               if (activePart instanceof DistributionEditor) {
+                       IFormPage ifp = ((DistributionEditor) activePart)
+                                       .getActivePageInstance();
+                       if (ifp instanceof DistributionOverviewPage)
+                               ((DistributionOverviewPage) ifp).refresh();
+               }
+               return null;
+       }
+}
index d7d578a03eb5ce3d811caface211539a46a54d90..bdd243d8301a2575e10202d3252c219616d321aa 100644 (file)
@@ -17,9 +17,15 @@ import javax.jcr.query.qom.Selector;
 
 import org.argeo.eclipse.ui.ErrorFeedback;
 import org.argeo.jcr.JcrUtils;
+import org.argeo.slc.client.ui.dist.DistPlugin;
+import org.argeo.slc.client.ui.dist.commands.DeleteArtifacts;
+import org.argeo.slc.client.ui.dist.utils.CommandHelpers;
 import org.argeo.slc.client.ui.dist.utils.NodeViewerComparator;
 import org.argeo.slc.jcr.SlcNames;
 import org.argeo.slc.jcr.SlcTypes;
+import org.eclipse.jface.action.IMenuListener;
+import org.eclipse.jface.action.IMenuManager;
+import org.eclipse.jface.action.MenuManager;
 import org.eclipse.jface.viewers.ColumnLabelProvider;
 import org.eclipse.jface.viewers.IStructuredContentProvider;
 import org.eclipse.jface.viewers.TableViewer;
@@ -30,7 +36,9 @@ 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.Menu;
 import org.eclipse.swt.widgets.Table;
+import org.eclipse.ui.IWorkbenchWindow;
 import org.eclipse.ui.forms.IManagedForm;
 import org.eclipse.ui.forms.editor.FormEditor;
 import org.eclipse.ui.forms.editor.FormPage;
@@ -38,7 +46,7 @@ import org.eclipse.ui.forms.widgets.ScrolledForm;
 import org.osgi.framework.Constants;
 
 /** Table giving an overview of an OSGi distribution */
-class DistributionOverviewPage extends FormPage implements SlcNames {
+public class DistributionOverviewPage extends FormPage implements SlcNames {
        private TableViewer viewer;
        private Session session;
 
@@ -123,11 +131,24 @@ class DistributionOverviewPage extends FormPage implements SlcNames {
                table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
 
                viewer.setContentProvider(new DistributionsContentProvider());
+               getSite().setSelectionProvider(viewer);
+               
                viewer.setInput(session);
                comparator = new NodeViewerComparator(1,
                                NodeViewerComparator.DESCENDING, propertiesList,
                                propertyTypesList);
                viewer.setComparator(comparator);
+
+               MenuManager menuManager = new MenuManager();
+               Menu menu = menuManager.createContextMenu(viewer.getTable());
+               menuManager.addMenuListener(new IMenuListener() {
+                       public void menuAboutToShow(IMenuManager manager) {
+                               contextMenuAboutToShow(manager);
+                       }
+               });
+               viewer.getTable().setMenu(menu);
+               getSite().registerContextMenu(menuManager, viewer);
+
        }
 
        @Override
@@ -135,6 +156,25 @@ class DistributionOverviewPage extends FormPage implements SlcNames {
                viewer.getTable().setFocus();
        }
 
+       /** force refresh of the artifact list */
+       public void refresh() {
+               viewer.refresh();
+       }
+
+       /** Programatically configure the context menu */
+       protected void contextMenuAboutToShow(IMenuManager menuManager) {
+               IWorkbenchWindow window = DistPlugin.getDefault().getWorkbench()
+                               .getActiveWorkbenchWindow();
+
+               // Build conditions depending on element type (repo or workspace)
+
+               // Delete selected artifacts
+               CommandHelpers.refreshCommand(menuManager, window, DeleteArtifacts.ID,
+                               DeleteArtifacts.DEFAULT_LABEL,
+                               DeleteArtifacts.DEFAULT_ICON_PATH, true);
+
+       }
+
        static NodeIterator listBundleArtifacts(Session session)
                        throws RepositoryException {
                QueryManager queryManager = session.getWorkspace().getQueryManager();
@@ -198,5 +238,4 @@ class DistributionOverviewPage extends FormPage implements SlcNames {
                }
 
        }
-
 }
\ No newline at end of file