changeset 36:cd2af2bb2307

Allow adding/removing commands
author Omair Majid <omajid@redhat.com>
date Thu, 05 Dec 2013 18:43:55 -0500
parents d5e3a71b8390
children 6e0fdbbec5d7
files src/com/redhat/thermostat/plugin/eclipse/editor/CommandsMasterDetailsBlock.java src/com/redhat/thermostat/plugin/eclipse/editor/NewNameDialog.java src/com/redhat/thermostat/plugin/eclipse/model/PluginModel.java
diffstat 3 files changed, 162 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/com/redhat/thermostat/plugin/eclipse/editor/CommandsMasterDetailsBlock.java	Thu Dec 05 15:37:14 2013 -0500
+++ b/src/com/redhat/thermostat/plugin/eclipse/editor/CommandsMasterDetailsBlock.java	Thu Dec 05 18:43:55 2013 -0500
@@ -3,9 +3,14 @@
 import org.eclipse.jface.viewers.ArrayContentProvider;
 import org.eclipse.jface.viewers.ISelectionChangedListener;
 import org.eclipse.jface.viewers.IStructuredContentProvider;
+import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.jface.viewers.SelectionChangedEvent;
 import org.eclipse.jface.viewers.TableViewer;
+import org.eclipse.jface.window.Window;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.events.SelectionListener;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Button;
@@ -47,7 +52,7 @@
     }
 
     @Override
-    protected void createMasterPart(final IManagedForm managedForm, Composite parent) {
+    protected void createMasterPart(final IManagedForm managedForm, final Composite parent) {
         FormToolkit toolkit = managedForm.getToolkit();
         final Section section = toolkit.createSection(parent, Section.TITLE_BAR);
         section.setText("Commands provided by the plugin");
@@ -90,9 +95,25 @@
 
         Button add = toolkit.createButton(buttons, "Add", SWT.NONE);
         add.setLayoutData(buttonLayoutData);
-        // TODO handle add event
+        add.addSelectionListener(new SelectionAdapter() {
+            @Override
+            public void widgetSelected(SelectionEvent e) {
+                NewNameDialog dialog = new NewNameDialog(parent.getShell());
+                dialog.create();
+                if (dialog.open() == Window.OK) {
+                    model.addNewCommand(dialog.getName());
+                }
+            }
+        });
+
         Button remove = toolkit.createButton(buttons, "Remove", SWT.NONE);
         remove.setLayoutData(buttonLayoutData);
-        // TODO handle remove event
+        remove.addSelectionListener(new SelectionAdapter() {
+            @Override
+            public void widgetSelected(SelectionEvent e) {
+                Object selectedItem = ((IStructuredSelection)viewer.getSelection()).getFirstElement();
+                model.removeCommand((String) selectedItem);
+            }
+        });
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/com/redhat/thermostat/plugin/eclipse/editor/NewNameDialog.java	Thu Dec 05 18:43:55 2013 -0500
@@ -0,0 +1,63 @@
+package com.redhat.thermostat.plugin.eclipse.editor;
+
+import org.eclipse.jface.dialogs.TitleAreaDialog;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.swt.widgets.Text;
+
+/** Asks the name for a new command/extension */
+public class NewNameDialog extends TitleAreaDialog {
+
+    private String name;
+    private Text nameText;
+
+    public NewNameDialog(Shell parentShell) {
+        super(parentShell);
+
+    }
+
+    @Override
+    public void create() {
+        super.create();
+
+        setTitle("Enter the name for the new command");
+        setMessage("This is the command users will use to trigger your plugin");
+    }
+
+    @Override
+    protected Control createDialogArea(Composite parent) {
+        Composite area = (Composite) super.createDialogArea(parent);
+
+        Composite container = new Composite(area, SWT.NONE);
+        container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,true));
+
+        container.setLayout(new GridLayout(2, false));
+
+        Label label = new Label(container, SWT.None);
+        label.setText("Name:");
+        nameText = new Text(container, SWT.None);
+        nameText.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
+
+        return area;
+    }
+
+    @Override
+    protected void okPressed() {
+        saveInput();
+        super.okPressed();
+    }
+
+    private void saveInput() {
+        name = nameText.getText();
+    }
+
+    public String getName() {
+        return name;
+    }
+
+}
--- a/src/com/redhat/thermostat/plugin/eclipse/model/PluginModel.java	Thu Dec 05 15:37:14 2013 -0500
+++ b/src/com/redhat/thermostat/plugin/eclipse/model/PluginModel.java	Thu Dec 05 18:43:55 2013 -0500
@@ -97,6 +97,7 @@
 
             return new String(outputStream.toByteArray());
 
+            // TODO pretty print or auto-format this generated xml to look both nice and consistent
         } catch (TransformerException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
@@ -125,6 +126,8 @@
     }
 
     public NewCommand getNewCommand(String name) {
+        // FIXME sanitize name; it should be alphanumeric only!
+
         XPath xpath = xPathfactory.newXPath();
         String expressionString;
         XPathExpression expr;
@@ -148,11 +151,80 @@
 
             return new NewCommand(name, description, usage);
         } catch (XPathExpressionException e) {
-            e.printStackTrace();
+            throw new AssertionError("bad xpath expression", e);
         }
-        return null;
     }
 
+
+    public void addNewCommand(String name) {
+        // FIXME sanitize name; it should be alphanumeric only!
+
+        Node root = doc;
+        NodeList rootChildren = doc.getChildNodes();
+
+        Node pluginNode = null;
+        for (int i = 0; i < rootChildren.getLength(); i++) {
+            Node node = rootChildren.item(i);
+            if (node.getNodeName().equals("plugin")) {
+                pluginNode = node;
+                break;
+            }
+        }
+
+        if (pluginNode == null) {
+            pluginNode = doc.createElement("plugin");
+            root.appendChild(pluginNode);
+        }
+
+        NodeList pluginChildren = pluginNode.getChildNodes();
+        Node commandsNode = null;
+        for (int i = 0; i < pluginChildren.getLength(); i++) {
+            Node node = pluginChildren.item(i);
+            if (node.getNodeName().equals("commands")) {
+                commandsNode = node;
+                break;
+            }
+        }
+
+        if (commandsNode == null) {
+            commandsNode = doc.createElement("commands");
+            pluginNode.appendChild(commandsNode);
+        }
+
+        Node commandNode = doc.createElement("command");
+        commandsNode.appendChild(commandNode);
+
+        Node commandNameNode = doc.createElement("name");
+        commandNode.appendChild(commandNameNode);
+
+        Node commandNameTextNode = doc.createTextNode(name);
+        commandNameNode.appendChild(commandNameTextNode);
+
+        fireModelChanged();
+    }
+
+    public void removeCommand(String name) {
+        // FIXME sanitize name; it should be alphanumeric only!
+
+        XPath xpath = xPathfactory.newXPath();
+
+        try {
+            String expressionString = "/plugin/commands/command[name/text() = '" + name + "']";
+            XPathExpression expr = xpath.compile(expressionString);
+            Node node = (Node) expr.evaluate(doc, XPathConstants.NODE);
+
+            if (node == null) {
+                // no match found
+                return;
+            }
+
+            node.getParentNode().removeChild(node);
+
+            fireModelChanged();
+        } catch (XPathExpressionException e) {
+            throw new AssertionError(e);
+        }
+    }
     public void addModelChangeListener(PluginModelChangeListener listener) {
         listeners.add(listener);
     }
@@ -182,4 +254,5 @@
     public static interface PluginModelChangeListener {
         void modelChanged();
     }
+
 }