changeset 56:6550737b4721

Add support for adding/removing extension bundles
author Omair Majid <omajid@redhat.com>
date Tue, 31 Dec 2013 15:14:27 -0500
parents 408f499d3fd8
children 6488ad7aefe6
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
diffstat 3 files changed, 139 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/com/redhat/thermostat/plugin/eclipse/editor/BundleInformationHelpers.java	Tue Dec 31 15:14:27 2013 -0500
@@ -0,0 +1,31 @@
+package com.redhat.thermostat.plugin.eclipse.editor;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.redhat.thermostat.plugin.eclipse.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	Tue Dec 31 14:51:33 2013 -0500
+++ b/src/com/redhat/thermostat/plugin/eclipse/editor/CommandEditPage.java	Tue Dec 31 15:14:27 2013 -0500
@@ -27,7 +27,6 @@
 import org.eclipse.ui.forms.widgets.TableWrapData;
 import org.eclipse.ui.forms.widgets.TableWrapLayout;
 
-import com.redhat.thermostat.plugin.eclipse.BundleInformation;
 import com.redhat.thermostat.plugin.eclipse.model.PluginModel;
 import com.redhat.thermostat.plugin.eclipse.model.PluginModel.Command;
 import com.redhat.thermostat.plugin.eclipse.model.PluginModel.Option;
@@ -57,11 +56,6 @@
         }
     };
 
-    @Override
-    public void initialize(IManagedForm form) {
-        toolkit = form.getToolkit();
-    }
-
     public void setModel(PluginModel model) {
         this.model = model;
 
@@ -69,6 +63,11 @@
     }
 
     @Override
+    public void initialize(IManagedForm form) {
+        toolkit = form.getToolkit();
+    }
+
+    @Override
     public void dispose() {
         model.removeModelChangeListener(modelRefresher);
     }
@@ -88,7 +87,7 @@
         if (shell.getSelection() ^ commandModel.environments.contains("shell")) {
             dirty = true;
         }
-        if (!commandModel.bundles.equals(getBundles((List<String>) bundleViewer.getInput()))) {
+        if (!commandModel.bundles.equals(BundleInformationHelpers.getBundles((List<String>) bundleViewer.getInput()))) {
             dirty = true;
         }
         dirty = dirty || !(commandName.getText().equals(commandModel.name)
@@ -129,7 +128,6 @@
 
     @Override
     public void selectionChanged(IFormPart part, ISelection selection) {
-        // TODO save any changes into temporary mode?
         command = (String) ((IStructuredSelection)selection).getFirstElement();
         updateFromModel();
         System.out.println("[" + command + "] selectionChanged");
@@ -226,7 +224,7 @@
                 dialog.create();
                 if (dialog.open() == Window.OK) {
                     List<String> viewModel = (List<String>)bundleViewer.getInput();
-                    viewModel.add(CommandEditPage.getString(dialog.getBundle()));
+                    viewModel.add(BundleInformationHelpers.getString(dialog.getBundle()));
                     bundleViewer.setInput(viewModel);
                 }
             }
@@ -271,7 +269,7 @@
                 cli.setSelection(true);
             }
         }
-        bundleViewer.setInput(getStrings(commandModel.bundles));
+        bundleViewer.setInput(BundleInformationHelpers.getStrings(commandModel.bundles));
     }
 
     private void updateModel() {
@@ -291,29 +289,8 @@
                     new ArrayList<String>(),
                     new ArrayList<Option>(),
                     environments,
-                    getBundles((List<String>) bundleViewer.getInput()));
+                    BundleInformationHelpers.getBundles((List<String>) bundleViewer.getInput()));
             model.updateCommand(updated);
         }
     }
-
-    private 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() + ")";
-    }
-
-    private 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/ExtensionEditPage.java	Tue Dec 31 14:51:33 2013 -0500
+++ b/src/com/redhat/thermostat/plugin/eclipse/editor/ExtensionEditPage.java	Tue Dec 31 15:14:27 2013 -0500
@@ -1,11 +1,21 @@
 package com.redhat.thermostat.plugin.eclipse.editor;
 
+import java.util.List;
+
+import org.eclipse.jface.viewers.ArrayContentProvider;
 import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredContentProvider;
+import org.eclipse.jface.viewers.IStructuredSelection;
+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.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Text;
+import org.eclipse.swt.widgets.Table;
 import org.eclipse.ui.forms.IDetailsPage;
 import org.eclipse.ui.forms.IFormPart;
 import org.eclipse.ui.forms.IManagedForm;
@@ -15,13 +25,22 @@
 import org.eclipse.ui.forms.widgets.TableWrapLayout;
 
 import com.redhat.thermostat.plugin.eclipse.model.PluginModel;
+import com.redhat.thermostat.plugin.eclipse.model.PluginModel.Extension;
+import com.redhat.thermostat.plugin.eclipse.model.PluginModel.PluginModelChangeListener;
 
 public class ExtensionEditPage implements IDetailsPage {
-    private Text extensionName;
-    private Text extensionBundleName;
     
     private FormToolkit toolkit;
     private PluginModel model;
+    private TableViewer bundleViewer;
+    private String extensionName;
+
+    private PluginModelChangeListener modelRefresher = new PluginModelChangeListener() {
+        @Override
+        public void modelChanged() {
+            updateFromModel();
+        }
+    };
 
     public void setModel(PluginModel model) {
         this.model = model;
@@ -34,20 +53,24 @@
 
     @Override
     public void dispose() {
-        // TODO Auto-generated method stub
-        
+        model.removeModelChangeListener(modelRefresher);
     }
 
     @Override
     public boolean isDirty() {
-        // TODO Auto-generated method stub
-        return false;
+        Extension extensionModel = model.getExtension(extensionName);
+        if (extensionModel == null) {
+            return false;
+        }
+
+        // TODO change this is-content-different into is-content-dirty
+        return !extensionModel.bundles.equals(BundleInformationHelpers.getBundles((List<String>) bundleViewer.getInput()));
     }
 
     @Override
     public void commit(boolean onSave) {
-        // TODO Auto-generated method stub
-        
+        System.out.println("[" + extensionName + "] commit(" + onSave + ")");
+        updateModel();
     }
 
     @Override
@@ -70,14 +93,14 @@
 
     @Override
     public void refresh() {
-        // TODO Auto-generated method stub
-        
+        updateFromModel();
     }
 
     @Override
     public void selectionChanged(IFormPart part, ISelection selection) {
-        // TODO Auto-generated method stub
-        
+        extensionName = (String) ((IStructuredSelection)selection).getFirstElement();
+        updateFromModel();
+        System.out.println("[" + extensionName + "] selectionChanged");
     }
 
     @Override
@@ -104,34 +127,77 @@
 
         sectionContents.setLayout(new GridLayout());
 
-        createBasicSection(sectionContents);
-
-        createArgumentsAndOptionsSection(sectionContents);
         createBundlesSection(sectionContents);
     }
-    
-    private void createBasicSection(Composite sectionContents) {
-        Section basics = toolkit.createSection(sectionContents, Section.TITLE_BAR);
-        basics.setText("Basics");
+
+    private void createBundlesSection(final Composite sectionContents) {
+        Section bundles = toolkit.createSection(sectionContents, Section.TITLE_BAR);
+        bundles.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
+        bundles.setText("Bundles to load");
+
+        Composite bundleContents = toolkit.createComposite(bundles);
+        bundles.setClient(bundleContents);
+        bundleContents.setLayout(new GridLayout(2, false));
 
-        Composite basicContents = toolkit.createComposite(basics);
-        basics.setClient(basicContents);
-        basicContents.setLayout(new GridLayout(2, false));
+        Table list = toolkit.createTable(bundleContents, SWT.BORDER | SWT.V_SCROLL);
+        list.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
+        bundleViewer = new TableViewer(list);
+        IStructuredContentProvider contentProvider = ArrayContentProvider.getInstance();
+        bundleViewer.setContentProvider(contentProvider);
+
+        Composite buttonComposite = toolkit.createComposite(bundleContents);
+        buttonComposite.setLayout(new GridLayout(1, false));
+
+        GridData buttonLayoutData = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
 
-        toolkit.createLabel(basicContents, "Name:");
-        extensionName = toolkit.createText(basicContents, "<extension name here>");
+        Button add = toolkit.createButton(buttonComposite, "Add", SWT.NONE);
+        add.setLayoutData(buttonLayoutData);
+        add.addSelectionListener(new SelectionAdapter() {
+            @Override
+            public void widgetSelected(SelectionEvent e) {
+                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);
+                }
+            }
+        });
 
-        toolkit.createLabel(basicContents, "BundleInformation:");
-        extensionBundleName = toolkit.createText(basicContents, "<bundle name here>");
-    }
-    
-    private void createArgumentsAndOptionsSection(Composite sectionContents) {
-        // TODO Auto-generated method stub
+        Button remove = toolkit.createButton(buttonComposite, "Remove", SWT.NONE);
+        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);
+            }
+        });
+        remove.setLayoutData(buttonLayoutData);
     }
 
-    private void createBundlesSection(Composite sectionContents) {
-        // TODO Auto-generated method stub
+    private void updateFromModel() {
+        if (extensionName == null) {
+            return;
+        }
+
+        Extension extensionModel = model.getExtension(extensionName);
+
+        // extension removed?
+        if (extensionModel == null) {
+            return;
+        }
+
+        bundleViewer.setInput(BundleInformationHelpers.getStrings(extensionModel.bundles));
     }
 
+    private void updateModel() {
+        Extension updated = new Extension(extensionName,
+                BundleInformationHelpers.getBundles((List<String>) bundleViewer.getInput()));
+
+        model.updateExtension(updated);
+    }
 
 }