changeset 48:a95d042f2204

Allow adding/removing bundles for a command
author Omair Majid <omajid@redhat.com>
date Mon, 30 Dec 2013 15:20:34 -0500
parents c870da2752e9
children b61d4ffb75f9
files src/com/redhat/thermostat/plugin/eclipse/editor/CommandEditPage.java src/com/redhat/thermostat/plugin/eclipse/editor/NewBundleDialog.java
diffstat 2 files changed, 198 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/com/redhat/thermostat/plugin/eclipse/editor/CommandEditPage.java	Mon Dec 30 10:17:57 2013 -0500
+++ b/src/com/redhat/thermostat/plugin/eclipse/editor/CommandEditPage.java	Mon Dec 30 15:20:34 2013 -0500
@@ -2,15 +2,25 @@
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
+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.events.SelectionListener;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.layout.RowLayout;
 import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Table;
 import org.eclipse.swt.widgets.Text;
 import org.eclipse.ui.forms.IDetailsPage;
 import org.eclipse.ui.forms.IFormPart;
@@ -40,6 +50,9 @@
     private Button cli;
     private Button shell;
 
+    // uses a List<String> as the backing model
+    private TableViewer bundleViewer;
+
     private PluginModelChangeListener modelRefresher = new PluginModelChangeListener() {
         @Override
         public void modelChanged() {
@@ -74,6 +87,9 @@
         if (shell.getSelection() ^ commandModel.environments.contains("shell")) {
             dirty = true;
         }
+        if (!commandModel.bundles.equals(getBundles((List<String>) bundleViewer.getInput()))) {
+            dirty = true;
+        }
         dirty = dirty || !(commandName.getText().equals(commandModel.name)
                 && commandDescription.getText().equals(commandModel.description)
                 && commandUsage.getText().equals(commandModel.usage));
@@ -142,7 +158,6 @@
         sectionContents.setLayout(new GridLayout());
 
         createBasicSection(sectionContents);
-
         createArgumentsAndOptionsSection(sectionContents);
         createBundlesSection(sectionContents);
     }
@@ -181,8 +196,54 @@
         // TODO Auto-generated method stub
     }
 
-    private void createBundlesSection(Composite sectionContents) {
-        // TODO Auto-generated method stub
+    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));
+
+        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);
+
+        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(CommandEditPage.getString(dialog.getBundle()));
+                    bundleViewer.setInput(viewModel);
+                }
+            }
+        });
+
+        Button remove = toolkit.createButton(buttonComposite, "Remove", SWT.NONE);
+        remove.addSelectionListener(new SelectionAdapter() {
+            @Override
+            public void widgetSelected(SelectionEvent e) {
+                Object selectedItem = ((IStructuredSelection)bundleViewer.getSelection()).getFirstElement();
+                System.out.println("selected: " + selectedItem);
+                System.out.println(selectedItem.getClass());
+                List<Bundle> viewModel = (List<Bundle>)bundleViewer.getInput();
+                viewModel.remove(selectedItem);
+                bundleViewer.setInput(viewModel);
+            }
+        });
+        remove.setLayoutData(buttonLayoutData);
     }
 
     private void updateFromModel() {
@@ -201,6 +262,7 @@
                 cli.setSelection(true);
             }
         }
+        bundleViewer.setInput(getStrings(commandModel.bundles));
     }
 
     private void updateModel() {
@@ -220,9 +282,29 @@
                     new ArrayList<String>(),
                     new ArrayList<Option>(),
                     environments,
-                    new ArrayList<Bundle>());
+                    getBundles((List<String>) bundleViewer.getInput()));
             model.updateCommand(updated);
         }
     }
 
-}
\ No newline at end of file
+    private List<String> getStrings(List<Bundle> input) {
+        ArrayList<String> result = new ArrayList<>(input.size());
+        for (Bundle bundle : input) {
+            result.add(getString(bundle));
+        }
+        return result;
+    }
+
+    static String getString(Bundle bundle) {
+        return bundle.getName() + " (" + bundle.getVersion() + ")";
+    }
+
+    private List<Bundle> getBundles(List<String> input) {
+        ArrayList<Bundle> result = new ArrayList<>(input.size());
+        for (String bundle : input) {
+            String[] parts = bundle.split(" ");
+            result.add(new Bundle(parts[0], parts[1].substring(1, parts[1].length()-1)));
+        }
+        return result;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/com/redhat/thermostat/plugin/eclipse/editor/NewBundleDialog.java	Mon Dec 30 15:20:34 2013 -0500
@@ -0,0 +1,111 @@
+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.Button;
+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;
+
+import com.redhat.thermostat.plugin.eclipse.model.PluginModel.Bundle;
+
+public class NewBundleDialog extends TitleAreaDialog {
+
+    private String name;
+    private String version;
+
+    private Button manualSelectionButton;
+    private Text nameText;
+    private Text versionText;
+
+    private Button fromJarButton;
+    private Text fileLocationText;
+
+
+    public NewBundleDialog(Shell parentShell) {
+        super(parentShell);
+    }
+
+    @Override
+    public void create() {
+        super.create();
+
+        setTitle("Add a new new bundle");
+        setMessage("Use this bundle with this 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));
+
+        GridLayout containerLayout = new GridLayout(3, false);
+        containerLayout.marginTop = 5;
+        containerLayout.marginRight = 5;
+        containerLayout.marginLeft = 5;
+        container.setLayout(containerLayout);
+
+        manualSelectionButton = new Button(container, SWT.RADIO);
+        manualSelectionButton.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 3, 1));
+        manualSelectionButton.setText("Specify Manually");
+
+        Label nameLabel = new Label(container, SWT.None);
+        nameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
+        nameLabel.setText("Symbolic Name:");
+        nameText = new Text(container, SWT.None);
+        GridData nameTextLayoutData = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
+        nameTextLayoutData.horizontalSpan = 2;
+        nameText.setLayoutData(nameTextLayoutData);
+
+        Label versionLabel = new Label(container, SWT.None);
+        versionLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
+        versionLabel.setText("Version:");
+        versionText = new Text(container, SWT.None);
+        GridData versionTextLayoutData = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
+        versionTextLayoutData.horizontalSpan = 2;
+        versionText.setLayoutData(versionTextLayoutData);
+
+        fromJarButton = new Button(container, SWT.RADIO);
+        fromJarButton.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 3, 1));
+        fromJarButton.setText("Extract from Jar/Bundle on disk");
+
+        Label locationLabel = new Label(container, SWT.NONE);
+        locationLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
+        locationLabel.setText("Location:");
+
+        fileLocationText = new Text(container, SWT.BORDER);
+        fileLocationText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
+
+        Button btnFromFile = new Button(container, SWT.NONE);
+        btnFromFile.setText("...");
+
+        // TODO implement extracting data from bundle/jar on disk
+
+        return area;
+    }
+
+    @Override
+    protected void okPressed() {
+        saveInput();
+        super.okPressed();
+    }
+
+    private void saveInput() {
+        if (manualSelectionButton.getSelection()) {
+            name = nameText.getText();
+            version = versionText.getText();
+        } else {
+            // TODO implement me
+        }
+    }
+
+    public Bundle getBundle() {
+        return new Bundle(name, version);
+    }
+}