changeset 44:eb2465736bea

Add environments for new commands
author Omair Majid <omajid@redhat.com>
date Fri, 27 Dec 2013 11:25:19 -0500
parents 182c44d8a058
children a75d3028e5e6
files src/com/redhat/thermostat/plugin/eclipse/editor/CommandEditPage.java src/com/redhat/thermostat/plugin/eclipse/model/PluginModel.java
diffstat 2 files changed, 103 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/com/redhat/thermostat/plugin/eclipse/editor/CommandEditPage.java	Wed Dec 11 16:23:28 2013 -0500
+++ b/src/com/redhat/thermostat/plugin/eclipse/editor/CommandEditPage.java	Fri Dec 27 11:25:19 2013 -0500
@@ -1,5 +1,8 @@
 package com.redhat.thermostat.plugin.eclipse.editor;
 
+import java.util.ArrayList;
+import java.util.List;
+
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.swt.SWT;
@@ -60,15 +63,23 @@
     public boolean isDirty() {
         Command commandModel = model.getCommand(command);
         // TODO change this is-content-different into is-content-dirty
-        boolean result = !(commandName.getText().equals(commandModel.name)
+        boolean dirty = false;
+        if (cli.getSelection() ^ commandModel.environments.contains("cli")) {
+            dirty = true;
+        }
+        if (shell.getSelection() ^ commandModel.environments.contains("shell")) {
+            dirty = true;
+        }
+        dirty = dirty || !(commandName.getText().equals(commandModel.name)
                 && commandDescription.getText().equals(commandModel.description)
                 && commandUsage.getText().equals(commandModel.usage));
-        // System.out.println("isDirty [" + command + "]: " + result);
-        return result;
+        System.out.println("[" + command + "] isDirty : " + dirty);
+        return dirty;
     }
 
     @Override
     public void commit(boolean onSave) {
+        System.out.println("[" + command + "] commit(" + onSave + ")");
         updateModel();
     }
 
@@ -86,6 +97,7 @@
     @Override
     public boolean isStale() {
         // FIXME check from model?
+        System.out.println("[" + command + "] isStale(): false");
         return false;
     }
 
@@ -99,6 +111,7 @@
         // TODO save any changes into temporary mode?
         command = (String) ((IStructuredSelection)selection).getFirstElement();
         updateFromModel();
+        System.out.println("[" + command + "] selectionChanged");
     }
 
     @Override
@@ -166,12 +179,34 @@
         commandName.setText(commandModel.name);
         commandDescription.setText(commandModel.description);
         commandUsage.setText(commandModel.usage);
+        shell.setSelection(false);
+        cli.setSelection(false);
+        for (String env : commandModel.environments) {
+            if (env.equals("shell")) {
+                shell.setSelection(true);
+            }
+
+            if (env.equals("cli")) {
+                cli.setSelection(true);
+            }
+        }
     }
 
     private void updateModel() {
-        Command updated;
         if (command.equals(commandName.getText())) {
-            updated = new Command(commandName.getText(), commandDescription.getText(), commandUsage.getText());
+            List<String> environments = new ArrayList<>();
+            if (cli.getSelection()) {
+                environments.add("cli");
+            }
+
+            if (shell.getSelection()) {
+                environments.add("shell");
+            }
+            
+            Command updated = new Command(commandName.getText(),
+                    commandDescription.getText(),
+                    commandUsage.getText(),
+                    environments);
             model.updateCommand(updated);
         }
     }
--- a/src/com/redhat/thermostat/plugin/eclipse/model/PluginModel.java	Wed Dec 11 16:23:28 2013 -0500
+++ b/src/com/redhat/thermostat/plugin/eclipse/model/PluginModel.java	Fri Dec 27 11:25:19 2013 -0500
@@ -5,7 +5,9 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
+import java.util.Objects;
 import java.util.concurrent.CopyOnWriteArrayList;
 
 import javax.xml.parsers.DocumentBuilder;
@@ -25,6 +27,7 @@
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.runtime.CoreException;
 import org.w3c.dom.Document;
+import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 import org.xml.sax.InputSource;
@@ -126,6 +129,8 @@
     }
 
     public Command getCommand(String name) {
+        Objects.requireNonNull(name);
+
         // FIXME sanitize name; it should be alphanumeric only!
 
         XPath xpath = xPathfactory.newXPath();
@@ -149,7 +154,17 @@
             node = (Node) expr.evaluate(doc, XPathConstants.NODE);
             String usage = node == null ? "" : node.getTextContent();
 
-            return new Command(name, description, usage);
+            expressionString = "/plugin/commands/command[name/text() = '" + name + "']/environments/environment";
+            expr = xpath.compile(expressionString);
+            NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
+            List<String> environments = new ArrayList<>();
+            for (int i = 0; i < nodes.getLength(); i++) {
+                node = nodes.item(i);
+                String env = node.getTextContent();
+                environments.add(env);
+            }
+            System.out.println(environments);
+            return new Command(name, description, usage, environments);
         } catch (XPathExpressionException e) {
             throw new AssertionError("bad xpath expression", e);
         }
@@ -219,6 +234,7 @@
 
             Node descriptionNode = null;
             Node usageNode = null;
+            Node environmentsNode = null;
 
             NodeList childNodes = commandNode.getChildNodes();
             for (int i = 0; i < childNodes.getLength(); i++) {
@@ -227,6 +243,8 @@
                     descriptionNode = node;
                 } else if (node.getNodeName().equals("usage")) {
                     usageNode = node;
+                } else if (node.getNodeName().equals("environments")) {
+                    environmentsNode = node;
                 }
             }
 
@@ -244,6 +262,25 @@
 
             usageNode.setTextContent(command.usage);
 
+            if (environmentsNode == null) {
+                environmentsNode = doc.createElement("environments");
+                commandNode.appendChild(environmentsNode);
+            }
+
+            // remove all existing children
+            childNodes = environmentsNode.getChildNodes();
+            while(childNodes.getLength() > 0) {
+                node = childNodes.item(0);
+                environmentsNode.removeChild(node);
+            }
+
+            // add new children
+            for (String env : command.environments) {
+                Element environmentNode = doc.createElement("environment");
+                environmentNode.setTextContent(env);
+                environmentsNode.appendChild(environmentNode);
+            }
+
         } catch (XPathExpressionException e) {
             throw new AssertionError("bad xpath expression", e);
         }
@@ -290,14 +327,35 @@
         public final String name;
         public final String description;
         public final String usage;
+        public final List<Argument> arguments;
+        public final List<Option> options;
+        public final List<String> environments;
+        public final List<Bundle> bundles;
 
-        public Command(String name, String description, String usage) {
+        public Command(String name, String description, String usage, List<String> environments) {
             this.name = name;
             this.description = description;
             this.usage = usage;
+
+            this.arguments = Collections.emptyList();
+            this.options = Collections.emptyList();
+            this.environments = environments;
+            this.bundles = Collections.emptyList();
         }
     }
-    
+
+    public static class Argument {
+        public final String name;
+
+        public Argument(String name) {
+            this.name = name;
+        }
+    }
+
+    public static class Option {
+        // TODO complete this
+    }
+
     public static class Extension {
         public final String name;
         public final List<Bundle> bundles;
@@ -364,6 +422,8 @@
     }
     
     public Extension getExtension(String name) {
+        Objects.requireNonNull(name);
+
         XPath xpath = xPathfactory.newXPath();
         String expressionString;
         XPathExpression expr;