changeset 66:5c4aae25081e

Use JFace binding with Command edit page
author Omair Majid <omajid@redhat.com>
date Thu, 02 Jan 2014 17:04:42 -0500
parents 1f27354dcdcf
children f6c371a0cb17
files src/com/redhat/thermostat/plugin/eclipse/editor/BundleInformationHelpers.java src/com/redhat/thermostat/plugin/eclipse/editor/CommandEditPage.java src/com/redhat/thermostat/plugin/eclipse/editor/ExtensionEditPage.java src/com/redhat/thermostat/plugin/eclipse/model/Command.java src/com/redhat/thermostat/plugin/eclipse/model/Environments.java src/com/redhat/thermostat/plugin/eclipse/model/PluginModel.java test/com/redhat/thermostat/plugin/eclipse/model/CommandTest.java test/com/redhat/thermostat/plugin/eclipse/model/PluginModelTest.java
diffstat 8 files changed, 137 insertions(+), 145 deletions(-) [+]
line wrap: on
line diff
--- a/src/com/redhat/thermostat/plugin/eclipse/editor/BundleInformationHelpers.java	Thu Jan 02 11:50:26 2014 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,31 +0,0 @@
-package com.redhat.thermostat.plugin.eclipse.editor;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import com.redhat.thermostat.plugin.eclipse.model.BundleInformation;
-
-public class BundleInformationHelpers {
-
-    static List<String> getStrings(List<BundleInformation> input) {
-        ArrayList<String> result = new ArrayList<>(input.size());
-        for (BundleInformation bundle : input) {
-            result.add(getString(bundle));
-        }
-        return result;
-    }
-
-    static String getString(BundleInformation bundle) {
-        return bundle.getSymbolicName() + " (" + bundle.getVersion() + ")";
-    }
-
-    static List<BundleInformation> getBundles(List<String> input) {
-        ArrayList<BundleInformation> result = new ArrayList<>(input.size());
-        for (String bundle : input) {
-            String[] parts = bundle.split(" ");
-            result.add(new BundleInformation(parts[0], parts[1].substring(1, parts[1].length()-1)));
-        }
-        return result;
-    }
-
-}
--- a/src/com/redhat/thermostat/plugin/eclipse/editor/CommandEditPage.java	Thu Jan 02 11:50:26 2014 -0500
+++ b/src/com/redhat/thermostat/plugin/eclipse/editor/CommandEditPage.java	Thu Jan 02 17:04:42 2014 -0500
@@ -1,8 +1,13 @@
 package com.redhat.thermostat.plugin.eclipse.editor;
 
-import java.util.ArrayList;
 import java.util.List;
 
+import org.eclipse.core.databinding.DataBindingContext;
+import org.eclipse.core.databinding.beans.BeanProperties;
+import org.eclipse.core.databinding.observable.list.IObservableList;
+import org.eclipse.core.databinding.observable.value.IObservableValue;
+import org.eclipse.jface.databinding.swt.WidgetProperties;
+import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
 import org.eclipse.jface.viewers.ArrayContentProvider;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.IStructuredContentProvider;
@@ -27,10 +32,10 @@
 import org.eclipse.ui.forms.widgets.TableWrapData;
 import org.eclipse.ui.forms.widgets.TableWrapLayout;
 
+import com.redhat.thermostat.plugin.eclipse.model.BundleInformation;
 import com.redhat.thermostat.plugin.eclipse.model.Command;
-import com.redhat.thermostat.plugin.eclipse.model.Option;
+import com.redhat.thermostat.plugin.eclipse.model.Environments;
 import com.redhat.thermostat.plugin.eclipse.model.PluginModel;
-import com.redhat.thermostat.plugin.eclipse.model.PluginModel.PluginModelChangeListener;
 
 class CommandEditPage implements IDetailsPage {
 
@@ -38,7 +43,7 @@
     private PluginModel model;
     private boolean isStale = false;
 
-    private String command;
+    private Command commandModel;
 
     private Text commandName;
     private Text commandUsage;
@@ -46,20 +51,17 @@
     private Button cli;
     private Button shell;
 
-    // uses a List<String> as the backing model
-    private TableViewer bundleViewer;
+    // uses a List<BundleInformation> as the backing model
+    private TableViewer bundlesTableViewer;
 
-    private PluginModelChangeListener modelRefresher = new PluginModelChangeListener() {
-        @Override
-        public void modelChanged() {
-            updateFromModel();
-        }
-    };
+    private DataBindingContext bindingContext;
+
+    public CommandEditPage() {
+        // empty no-arg java bean constructor
+    }
 
     public void setModel(PluginModel model) {
         this.model = model;
-
-        model.addModelChangeListener(modelRefresher);
     }
 
     @Override
@@ -69,38 +71,29 @@
 
     @Override
     public void dispose() {
-        model.removeModelChangeListener(modelRefresher);
+        if (bindingContext != null) {
+            bindingContext.dispose();
+        }
     }
 
     @Override
     public boolean isDirty() {
-        Command commandModel = model.getCommand(command);
         if (commandModel == null) {
             return false;
         }
 
-        // TODO change this is-content-different into is-content-dirty
-        boolean dirty = false;
-        if (cli.getSelection() ^ commandModel.getEnvironments().contains("cli")) {
-            dirty = true;
-        }
-        if (shell.getSelection() ^ commandModel.getEnvironments().contains("shell")) {
-            dirty = true;
+        Command latestCommandInModel = model.getCommand(commandModel.getName());
+        if (latestCommandInModel == null) {
+            return false;
         }
-        if (!commandModel.getBundles().equals(BundleInformationHelpers.getBundles((List<String>) bundleViewer.getInput()))) {
-            dirty = true;
-        }
-        dirty = dirty || !(commandName.getText().equals(commandModel.getName())
-                && commandDescription.getText().equals(commandModel.getDescription())
-                && commandUsage.getText().equals(commandModel.getUsage()));
-        System.out.println("[" + command + "] isDirty : " + dirty);
-        return dirty;
+
+        // TODO convert is-command-different into is-command-dirty operation
+        return !latestCommandInModel.equals(commandModel);
     }
 
     @Override
     public void commit(boolean onSave) {
-        System.out.println("[" + command + "] commit(" + onSave + ")");
-        updateModel();
+        model.updateCommand(commandModel);
     }
 
     @Override
@@ -117,20 +110,23 @@
     @Override
     public boolean isStale() {
         // FIXME check from model?
-        System.out.println("[" + command + "] isStale(): false");
         return false;
     }
 
     @Override
     public void refresh() {
-        updateFromModel();
+        // no-op
     }
 
     @Override
     public void selectionChanged(IFormPart part, ISelection selection) {
-        command = (String) ((IStructuredSelection)selection).getFirstElement();
-        updateFromModel();
-        System.out.println("[" + command + "] selectionChanged");
+        String commandName = (String) ((IStructuredSelection)selection).getFirstElement();
+        commandModel = model.getCommand(commandName);
+        if (commandModel == null) {
+            return;
+        }
+
+        bindingContext = initDataBindings();
     }
 
     @Override
@@ -205,9 +201,9 @@
 
         Table list = toolkit.createTable(bundleContents, SWT.BORDER | SWT.V_SCROLL);
         list.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
-        bundleViewer = new TableViewer(list);
+        bundlesTableViewer = new TableViewer(list);
         IStructuredContentProvider contentProvider = ArrayContentProvider.getInstance();
-        bundleViewer.setContentProvider(contentProvider);
+        bundlesTableViewer.setContentProvider(contentProvider);
 
         Composite buttonComposite = toolkit.createComposite(bundleContents);
         buttonComposite.setLayout(new GridLayout(1, false));
@@ -220,9 +216,7 @@
                 NewBundleDialog dialog = new NewBundleDialog(sectionContents.getShell());
                 dialog.create();
                 if (dialog.open() == Window.OK) {
-                    List<String> viewModel = (List<String>)bundleViewer.getInput();
-                    viewModel.add(BundleInformationHelpers.getString(dialog.getBundle()));
-                    bundleViewer.setInput(viewModel);
+                    commandModel.addBundle(dialog.getBundle());
                 }
             }
         });
@@ -232,63 +226,44 @@
         remove.addSelectionListener(new SelectionAdapter() {
             @Override
             public void widgetSelected(SelectionEvent e) {
-                Object selectedItem = ((IStructuredSelection)bundleViewer.getSelection()).getFirstElement();
-                List<?> viewModel = (List<?>) bundleViewer.getInput();
-                viewModel.remove(selectedItem);
-                bundleViewer.setInput(viewModel);
+                BundleInformation selectedItem = (BundleInformation)
+                        ((IStructuredSelection)bundlesTableViewer.getSelection()).getFirstElement();
+                commandModel.removeBundle(selectedItem);
             }
         });
     }
 
-    private void updateFromModel() {
-        if (command == null) {
-            return;
-        }
+    private DataBindingContext initDataBindings() {
+        DataBindingContext bindingContext = new DataBindingContext();
 
-        Command commandModel = model.getCommand(command);
+        IObservableValue nameWidgetValue = WidgetProperties.text(SWT.Modify).observe(commandName);
+        IObservableValue nameModelValue = BeanProperties.value(Command.class, "name").observe(commandModel);
+        bindingContext.bindValue(nameWidgetValue, nameModelValue);
 
-        // command removed?
-        if (commandModel == null) {
-            return;
-        }
+        IObservableValue usageWidgetValue = WidgetProperties.text(SWT.Modify).observe(commandUsage);
+        IObservableValue usageModelValue = BeanProperties.value(Command.class, "usage").observe(commandModel);
+        bindingContext.bindValue(usageWidgetValue, usageModelValue);
 
-        commandName.setText(commandModel.getName());
-        commandDescription.setText(commandModel.getDescription());
-        commandUsage.setText(commandModel.getUsage());
-        shell.setSelection(false);
-        cli.setSelection(false);
-        for (String env : commandModel.getEnvironments()) {
-            if (env.equals("shell")) {
-                shell.setSelection(true);
-            }
+        IObservableValue descriptionWidgetValue = WidgetProperties.text(SWT.Modify).observe(commandDescription);
+        IObservableValue descriptionModelValue = BeanProperties.value(Command.class, "description").observe(commandModel);
+        bindingContext.bindValue(descriptionWidgetValue, descriptionModelValue);
 
-            if (env.equals("cli")) {
-                cli.setSelection(true);
-            }
-        }
+        IObservableValue environmentsModelValue = BeanProperties.value(Command.class, "environments").observe(commandModel);
 
-        bundleViewer.setInput(BundleInformationHelpers.getStrings(commandModel.getBundles()));
-    }
+        IObservableValue cliWidgetValue = WidgetProperties.selection().observe(cli);
+        IObservableValue cliModelValue = BeanProperties.value(Environments.class, "cli").observeDetail(environmentsModelValue);
+        bindingContext.bindValue(cliWidgetValue, cliModelValue);
 
-    private void updateModel() {
-        if (command.equals(commandName.getText())) {
-            List<String> environments = new ArrayList<>();
-            if (cli.getSelection()) {
-                environments.add("cli");
-            }
+        IObservableValue shellWidgetValue = WidgetProperties.selection().observe(shell);
+        IObservableValue shellModelValue = BeanProperties.value(Environments.class, "shell").observeDetail(environmentsModelValue);
+        bindingContext.bindValue(shellWidgetValue, shellModelValue);
 
-            if (shell.getSelection()) {
-                environments.add("shell");
-            }
-            
-            Command updated = new Command(commandName.getText(),
-                    commandDescription.getText(),
-                    commandUsage.getText(),
-                    new ArrayList<String>(),
-                    new ArrayList<Option>(),
-                    environments,
-                    BundleInformationHelpers.getBundles((List<String>) bundleViewer.getInput()));
-            model.updateCommand(updated);
-        }
+        ObservableListContentProvider listContentProvider = new ObservableListContentProvider();
+        bundlesTableViewer.setContentProvider(listContentProvider);
+
+        IObservableList bundlesModel = BeanProperties.list(Command.class, "bundles").observe(commandModel);
+        bundlesTableViewer.setInput(bundlesModel);
+
+        return bindingContext;
     }
 }
--- a/src/com/redhat/thermostat/plugin/eclipse/editor/ExtensionEditPage.java	Thu Jan 02 11:50:26 2014 -0500
+++ b/src/com/redhat/thermostat/plugin/eclipse/editor/ExtensionEditPage.java	Thu Jan 02 17:04:42 2014 -0500
@@ -1,6 +1,7 @@
 package com.redhat.thermostat.plugin.eclipse.editor;
 
 import org.eclipse.core.databinding.DataBindingContext;
+import org.eclipse.core.databinding.beans.BeanProperties;
 import org.eclipse.core.databinding.beans.BeansObservables;
 import org.eclipse.core.databinding.observable.list.IObservableList;
 import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
@@ -188,7 +189,7 @@
         ObservableListContentProvider listContentProvider = new ObservableListContentProvider();
         bundlesTableViewer.setContentProvider(listContentProvider);
 
-        IObservableList bundlesModel = BeansObservables.observeList(extensionModel, "bundles");
+        IObservableList bundlesModel = BeanProperties.list(Extension.class, "bundles").observe(extensionModel);
         bundlesTableViewer.setInput(bundlesModel);
 
         return bindingContext;
--- a/src/com/redhat/thermostat/plugin/eclipse/model/Command.java	Thu Jan 02 11:50:26 2014 -0500
+++ b/src/com/redhat/thermostat/plugin/eclipse/model/Command.java	Thu Jan 02 17:04:42 2014 -0500
@@ -1,6 +1,7 @@
 package com.redhat.thermostat.plugin.eclipse.model;
 
 import java.util.List;
+import java.util.Objects;
 
 public class Command extends ModelObject {
 
@@ -9,7 +10,7 @@
     private String usage;
     private List<String> arguments;
     private List<Option> options;
-    private List<String> environments;
+    private Environments environments;
     private List<BundleInformation> bundles;
 
     public Command() {
@@ -18,7 +19,7 @@
 
     public Command(String name, String description, String usage,
             List<String> arguments, List<Option> options,
-            List<String> environments, List<BundleInformation> bundles) {
+            Environments environments, List<BundleInformation> bundles) {
         this.name = name;
         this.description = description;
         this.usage = usage;
@@ -79,24 +80,14 @@
         firePropertyChange("options", this.options, this.options = options);
     }
 
-    public List<String> getEnvironments() {
+    public Environments getEnvironments() {
         return environments;
     }
 
-    public void setEnvironments(List<String> environments) {
+    public void setEnvironments(Environments environments) {
         firePropertyChange("environments", this.environments, this.environments = environments);
     }
 
-    public void addEnvironment(String environment) {
-        this.environments.add(environment);
-        firePropertyChange("environments", null, this.environments);
-    }
-
-    public void removeEnvironment(String environment) {
-        this.environments.remove(environment);
-        firePropertyChange("environments", null, this.environments);
-    }
-
     public List<BundleInformation> getBundles() {
         return bundles;
     }
@@ -114,4 +105,19 @@
         this.bundles.remove(bundle);
         firePropertyChange("bundles", null, this.bundles);
     }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (!(obj instanceof Command)) {
+            return false;
+        }
+        Command other = (Command) obj;
+        return Objects.equals(this.name, other.name) &&
+                Objects.equals(this.description, other.description) &&
+                Objects.equals(this.usage, other.usage) &&
+                Objects.equals(this.arguments, other.arguments) &&
+                Objects.equals(this.options, other.options) &&
+                Objects.equals(this.environments, other.environments) &&
+                Objects.equals(this.bundles, other.bundles);
+    }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/com/redhat/thermostat/plugin/eclipse/model/Environments.java	Thu Jan 02 17:04:42 2014 -0500
@@ -0,0 +1,29 @@
+package com.redhat.thermostat.plugin.eclipse.model;
+
+public class Environments extends ModelObject {
+
+    private boolean cli;
+    private boolean shell;
+
+    public Environments() {
+        // no-arg bean constructor
+    }
+
+    public boolean isCli() {
+        return cli;
+    }
+
+    public void setCli(boolean cli) {
+        firePropertyChange("cli", this.cli, this.cli = cli);
+    }
+
+    public boolean isShell() {
+        return shell;
+    }
+
+    public void setShell(boolean shell) {
+        firePropertyChange("shell", this.shell, this.shell = shell);
+    }
+
+}
+
--- a/src/com/redhat/thermostat/plugin/eclipse/model/PluginModel.java	Thu Jan 02 11:50:26 2014 -0500
+++ b/src/com/redhat/thermostat/plugin/eclipse/model/PluginModel.java	Thu Jan 02 17:04:42 2014 -0500
@@ -160,7 +160,7 @@
 
             List<String> arguments = parseArguments(commandNode);
             List<Option> options = parseOptions(commandNode);
-            List<String> environments = parseEnvironments(commandNode);
+            Environments environments = parseEnvironments(commandNode);
             List<BundleInformation> bundles = parseBundles(commandNode);
 
             return new Command(name, description, usage, arguments, options, environments, bundles);
@@ -187,15 +187,19 @@
         return new ArrayList<>();
     }
 
-    private List<String> parseEnvironments(Node commandNode) throws XPathExpressionException {
+    private Environments parseEnvironments(Node commandNode) throws XPathExpressionException {
         String expressionString = "environments/environment";
         XPathExpression expr = xPathfactory.newXPath().compile(expressionString);
         NodeList nodes = (NodeList) expr.evaluate(commandNode, XPathConstants.NODESET);
-        List<String> environments = new ArrayList<>();
+        Environments environments = new Environments();
         for (int i = 0; i < nodes.getLength(); i++) {
             Node node = nodes.item(i);
             String env = node.getTextContent();
-            environments.add(env);
+            if (env.equals("cli")) {
+                environments.setCli(true);
+            } else if (env.equals("shell")) {
+                environments.setShell(true);
+            }
         }
         return environments;
     }
@@ -314,7 +318,7 @@
         // TODO implement me
     }
 
-    private void updateEnvironments(List<String> environments, Node commandNode, Node environmentsNode) {
+    private void updateEnvironments(Environments environments, Node commandNode, Node environmentsNode) {
         if (environmentsNode == null) {
             environmentsNode = doc.createElement("environments");
             commandNode.appendChild(environmentsNode);
@@ -328,9 +332,14 @@
         }
 
         // add new children
-        for (String env : environments) {
+        if (environments.isCli()) {
             Element environmentNode = doc.createElement("environment");
-            environmentNode.setTextContent(env);
+            environmentNode.setTextContent("cli");
+            environmentsNode.appendChild(environmentNode);
+        }
+        if (environments.isShell()) {
+            Element environmentNode = doc.createElement("environment");
+            environmentNode.setTextContent("shell");
             environmentsNode.appendChild(environmentNode);
         }
     }
--- a/test/com/redhat/thermostat/plugin/eclipse/model/CommandTest.java	Thu Jan 02 11:50:26 2014 -0500
+++ b/test/com/redhat/thermostat/plugin/eclipse/model/CommandTest.java	Thu Jan 02 17:04:42 2014 -0500
@@ -28,7 +28,7 @@
         Command command = new Command("name", "description", "usage",
                 Collections.<String> emptyList(),
                 Collections.<Option> emptyList(),
-                Collections.<String> emptyList(),
+                new Environments(),
                 Collections.<BundleInformation> emptyList());
 
         assertNotNull(command);
--- a/test/com/redhat/thermostat/plugin/eclipse/model/PluginModelTest.java	Thu Jan 02 11:50:26 2014 -0500
+++ b/test/com/redhat/thermostat/plugin/eclipse/model/PluginModelTest.java	Thu Jan 02 17:04:42 2014 -0500
@@ -2,7 +2,9 @@
 
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
@@ -50,7 +52,8 @@
         assertEquals("foo bar", foo.getUsage());
         assertEquals("foos the bar if possible", foo.getDescription());
 
-        assertArrayEquals(new String[] {"cli"}, foo.getEnvironments().toArray(new String[0]));
+        assertTrue(foo.getEnvironments().isCli());
+        assertFalse(foo.getEnvironments().isShell());
 
         List<BundleInformation> bundles = foo.getBundles();
         assertEquals(1, bundles.size());