changeset 50:63c1fd63d370

Make new bundle dialog work
author Omair Majid <omajid@redhat.com>
date Tue, 31 Dec 2013 12:32:04 -0500
parents b61d4ffb75f9
children 8a82967da2ac
files src/com/redhat/thermostat/plugin/eclipse/BundleInformation.java src/com/redhat/thermostat/plugin/eclipse/BundleInformationExtractor.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/editor/NewBundleDialog.java src/com/redhat/thermostat/plugin/eclipse/model/PluginModel.java test/com/redhat/thermostat/plugin/eclipse/model/PluginModelTest.java
diffstat 7 files changed, 130 insertions(+), 61 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/com/redhat/thermostat/plugin/eclipse/BundleInformation.java	Tue Dec 31 12:32:04 2013 -0500
@@ -0,0 +1,20 @@
+package com.redhat.thermostat.plugin.eclipse;
+
+public class BundleInformation {
+
+    final String name;
+    final String version;
+
+    public BundleInformation(String name, String version) {
+        this.name = name;
+        this.version = version;
+    }
+
+    public String getSymbolicName(){
+        return this.name;
+    }
+
+    public String getVersion(){
+        return this.version;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/com/redhat/thermostat/plugin/eclipse/BundleInformationExtractor.java	Tue Dec 31 12:32:04 2013 -0500
@@ -0,0 +1,21 @@
+package com.redhat.thermostat.plugin.eclipse;
+
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.jar.Attributes;
+import java.util.jar.JarInputStream;
+
+public class BundleInformationExtractor {
+
+    public BundleInformation extract(String path) throws FileNotFoundException, IOException {
+
+        try (JarInputStream in = new JarInputStream(new FileInputStream(path))) {
+            Attributes attr = in.getManifest().getMainAttributes();
+            String name = attr.getValue("Bundle-SymbolicName");
+            String version = attr.getValue("Bundle-Version");
+
+            return new BundleInformation(name, version);
+        }
+    }
+}
--- a/src/com/redhat/thermostat/plugin/eclipse/editor/CommandEditPage.java	Tue Dec 31 11:32:10 2013 -0500
+++ b/src/com/redhat/thermostat/plugin/eclipse/editor/CommandEditPage.java	Tue Dec 31 12:32:04 2013 -0500
@@ -30,8 +30,8 @@
 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.Bundle;
 import com.redhat.thermostat.plugin.eclipse.model.PluginModel.Command;
 import com.redhat.thermostat.plugin.eclipse.model.PluginModel.Option;
 import com.redhat.thermostat.plugin.eclipse.model.PluginModel.PluginModelChangeListener;
@@ -238,7 +238,7 @@
                 Object selectedItem = ((IStructuredSelection)bundleViewer.getSelection()).getFirstElement();
                 System.out.println("selected: " + selectedItem);
                 System.out.println(selectedItem.getClass());
-                List<Bundle> viewModel = (List<Bundle>)bundleViewer.getInput();
+                List<BundleInformation> viewModel = (List<BundleInformation>)bundleViewer.getInput();
                 viewModel.remove(selectedItem);
                 bundleViewer.setInput(viewModel);
             }
@@ -287,23 +287,23 @@
         }
     }
 
-    private List<String> getStrings(List<Bundle> input) {
+    private List<String> getStrings(List<BundleInformation> input) {
         ArrayList<String> result = new ArrayList<>(input.size());
-        for (Bundle bundle : input) {
+        for (BundleInformation bundle : input) {
             result.add(getString(bundle));
         }
         return result;
     }
 
-    static String getString(Bundle bundle) {
-        return bundle.getName() + " (" + bundle.getVersion() + ")";
+    static String getString(BundleInformation bundle) {
+        return bundle.getSymbolicName() + " (" + bundle.getVersion() + ")";
     }
 
-    private List<Bundle> getBundles(List<String> input) {
-        ArrayList<Bundle> result = new ArrayList<>(input.size());
+    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 Bundle(parts[0], parts[1].substring(1, parts[1].length()-1)));
+            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 11:32:10 2013 -0500
+++ b/src/com/redhat/thermostat/plugin/eclipse/editor/ExtensionEditPage.java	Tue Dec 31 12:32:04 2013 -0500
@@ -110,7 +110,7 @@
         toolkit.createLabel(basicContents, "Name:");
         extensionName = toolkit.createText(basicContents, "<extension name here>");
 
-        toolkit.createLabel(basicContents, "Bundle:");
+        toolkit.createLabel(basicContents, "BundleInformation:");
         extensionBundleName = toolkit.createText(basicContents, "<bundle name here>");
     }
     
--- a/src/com/redhat/thermostat/plugin/eclipse/editor/NewBundleDialog.java	Tue Dec 31 11:32:10 2013 -0500
+++ b/src/com/redhat/thermostat/plugin/eclipse/editor/NewBundleDialog.java	Tue Dec 31 12:32:04 2013 -0500
@@ -1,22 +1,27 @@
 package com.redhat.thermostat.plugin.eclipse.editor;
 
+import java.io.IOException;
+
 import org.eclipse.jface.dialogs.TitleAreaDialog;
 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.Control;
+import org.eclipse.swt.widgets.FileDialog;
 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;
+import com.redhat.thermostat.plugin.eclipse.BundleInformation;
+import com.redhat.thermostat.plugin.eclipse.BundleInformationExtractor;
 
 public class NewBundleDialog extends TitleAreaDialog {
 
-    private String name;
-    private String version;
+    private BundleInformation bundleInfo;
 
     private Button manualSelectionButton;
     private Text nameText;
@@ -24,7 +29,7 @@
 
     private Button fromJarButton;
     private Text fileLocationText;
-
+    private Button btnFromFile;
 
     public NewBundleDialog(Shell parentShell) {
         super(parentShell);
@@ -52,6 +57,17 @@
         container.setLayout(containerLayout);
 
         manualSelectionButton = new Button(container, SWT.RADIO);
+        manualSelectionButton.setSelection(true);
+        manualSelectionButton.addSelectionListener(new SelectionAdapter() {
+            @Override
+            public void widgetSelected(SelectionEvent e) {
+                nameText.setEnabled(true);
+                versionText.setEnabled(true);
+
+                fileLocationText.setEnabled(false);
+                btnFromFile.setEnabled(false);
+            }
+        });
         manualSelectionButton.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 3, 1));
         manualSelectionButton.setText("Specify Manually");
 
@@ -72,18 +88,41 @@
         versionText.setLayoutData(versionTextLayoutData);
 
         fromJarButton = new Button(container, SWT.RADIO);
+        fromJarButton.addSelectionListener(new SelectionAdapter() {
+            @Override
+            public void widgetSelected(SelectionEvent e) {
+                nameText.setEnabled(false);
+                versionText.setEnabled(false);
+
+                fileLocationText.setEnabled(true);
+                btnFromFile.setEnabled(true);
+            }
+        });
         fromJarButton.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 3, 1));
-        fromJarButton.setText("Extract from Jar/Bundle on disk");
+        fromJarButton.setText("Extract from Jar/BundleInformation 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.setEnabled(false);
         fileLocationText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
 
-        Button btnFromFile = new Button(container, SWT.NONE);
+        btnFromFile = new Button(container, SWT.NONE);
         btnFromFile.setText("...");
+        btnFromFile.setEnabled(false);
+        btnFromFile.addSelectionListener(new SelectionAdapter() {
+            @Override
+            public void widgetSelected(SelectionEvent e) {
+                FileDialog dialog = new FileDialog(getShell(), SWT.OPEN);
+                dialog.setFilterExtensions(new String[] { "*.jar" });
+                String result = dialog.open();
+                if (result != null) {
+                    fileLocationText.setText(result);
+                }
+            }
+        });
 
         // TODO implement extracting data from bundle/jar on disk
 
@@ -98,14 +137,19 @@
 
     private void saveInput() {
         if (manualSelectionButton.getSelection()) {
-            name = nameText.getText();
-            version = versionText.getText();
+            bundleInfo = new BundleInformation(nameText.getText(), versionText.getText());
         } else {
-            // TODO implement me
+            try {
+                BundleInformationExtractor extractor = new BundleInformationExtractor();
+                bundleInfo = extractor.extract(fileLocationText.getText());
+            } catch (IOException e) {
+                // FIXME doing something saner with this exception
+                e.printStackTrace();
+            }
         }
     }
 
-    public Bundle getBundle() {
-        return new Bundle(name, version);
+    public BundleInformation getBundle() {
+        return bundleInfo;
     }
 }
--- a/src/com/redhat/thermostat/plugin/eclipse/model/PluginModel.java	Tue Dec 31 11:32:10 2013 -0500
+++ b/src/com/redhat/thermostat/plugin/eclipse/model/PluginModel.java	Tue Dec 31 12:32:04 2013 -0500
@@ -32,6 +32,8 @@
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
 
+import com.redhat.thermostat.plugin.eclipse.BundleInformation;
+
 /**
  * Maintains a model of the plugin file.
  */
@@ -157,7 +159,7 @@
             List<String> arguments = parseArguments(commandNode);
             List<Option> options = parseOptions(commandNode);
             List<String> environments = parseEnvironments(commandNode);
-            List<Bundle> bundles = parseBundles(commandNode);
+            List<BundleInformation> bundles = parseBundles(commandNode);
 
             return new Command(name, description, usage, arguments, options, environments, bundles);
         } catch (XPathExpressionException e) {
@@ -196,11 +198,11 @@
         return environments;
     }
 
-    private List<Bundle> parseBundles(Node commandNode) throws XPathExpressionException {
+    private List<BundleInformation> parseBundles(Node commandNode) throws XPathExpressionException {
         String expressionString = "bundles/bundle";
         XPathExpression expr = xPathfactory.newXPath().compile(expressionString);
         NodeList nodes = (NodeList) expr.evaluate(commandNode, XPathConstants.NODESET);
-        List<Bundle> bundles = new ArrayList<>();
+        List<BundleInformation> bundles = new ArrayList<>();
         for (int i = 0; i < nodes.getLength(); i++) {
             NodeList children = nodes.item(i).getChildNodes();
 
@@ -217,7 +219,7 @@
                 }
             }
 
-            bundles.add(new Bundle(symbolicName, version));
+            bundles.add(new BundleInformation(symbolicName, version));
         }
         return bundles;
     }
@@ -386,7 +388,7 @@
         }
     }
 
-    private void updateBundles(List<Bundle> bundles, Node commandNode, Node bundlesNode) {
+    private void updateBundles(List<BundleInformation> bundles, Node commandNode, Node bundlesNode) {
         if (bundlesNode == null) {
             bundlesNode = doc.createElement("bundles");
             commandNode.appendChild(bundlesNode);
@@ -400,13 +402,13 @@
         }
 
         // add new children
-        for (Bundle bundle : bundles) {
+        for (BundleInformation bundle : bundles) {
             Element bundleNode = doc.createElement("bundle");
 
             Element nameNode = doc.createElement("symbolic-name");
-            nameNode.setTextContent(bundle.name);
+            nameNode.setTextContent(bundle.getSymbolicName());
             Element versionNode = doc.createElement("version");
-            versionNode.setTextContent(bundle.version);
+            versionNode.setTextContent(bundle.getVersion());
 
             bundleNode.appendChild(nameNode);
             bundleNode.appendChild(versionNode);
@@ -459,11 +461,11 @@
         public final List<String> arguments;
         public final List<Option> options;
         public final List<String> environments;
-        public final List<Bundle> bundles;
+        public final List<BundleInformation> bundles;
 
         public Command(String name, String description, String usage,
                 List<String> arguments, List<Option> options,
-                List<String> environments, List<Bundle> bundles) {
+                List<String> environments, List<BundleInformation> bundles) {
             this.name = name;
             this.description = description;
             this.usage = usage;
@@ -485,19 +487,19 @@
 
     public static class Extension {
         public final String name;
-        public final List<Bundle> bundles;
+        public final List<BundleInformation> bundles;
         
         public Extension(String name){
             this.name = name;
-            bundles = new ArrayList<Bundle>();
+            bundles = new ArrayList<BundleInformation>();
         }
         
-        public Extension(String name, List<Bundle> bundles) {
+        public Extension(String name, List<BundleInformation> bundles) {
             this.name = name;
             this.bundles = bundles;
         }
         
-        public void addBundle(Bundle bundle){
+        public void addBundle(BundleInformation bundle){
             this.bundles.add(bundle);
         }
         
@@ -505,29 +507,11 @@
             return this.name;
         }
         
-        public List<Bundle> getBundles(){
+        public List<BundleInformation> getBundles(){
             return this.bundles;
         }
     }
     
-    public static class Bundle {
-        private final String name;
-        private final String version;
-        
-        public Bundle(String name, String version) {
-            this.name = name;
-            this.version = version;
-        }
-        
-        public String getName(){
-            return this.name;
-        }
-        
-        public String getVersion(){
-            return this.version;
-        }
-    }
-
     public static interface PluginModelChangeListener {
         void modelChanged();
     }
@@ -555,8 +539,8 @@
         String expressionString;
         XPathExpression expr;
         NodeList nodeList;
-        ArrayList<Bundle> bundles = new ArrayList<Bundle>();
-        Bundle bundle;
+        ArrayList<BundleInformation> bundles = new ArrayList<BundleInformation>();
+        BundleInformation bundle;
 
         try {
             expressionString = "/plugin/extensions/extension[name/text() = '" + name + "']/bundles";
@@ -570,7 +554,7 @@
                 expr  = xpath.compile("//version");
                 String extensionVersion = (String) expr.evaluate(nodeList.item(i), XPathConstants.STRING);
                 
-                bundle = new Bundle (extensionName, extensionVersion);
+                bundle = new BundleInformation (extensionName, extensionVersion);
                 bundles.add(bundle);
             }
         } catch (XPathExpressionException e) {
--- a/test/com/redhat/thermostat/plugin/eclipse/model/PluginModelTest.java	Tue Dec 31 11:32:10 2013 -0500
+++ b/test/com/redhat/thermostat/plugin/eclipse/model/PluginModelTest.java	Tue Dec 31 12:32:04 2013 -0500
@@ -11,7 +11,7 @@
 
 import org.junit.Test;
 
-import com.redhat.thermostat.plugin.eclipse.model.PluginModel.Bundle;
+import com.redhat.thermostat.plugin.eclipse.BundleInformation;
 import com.redhat.thermostat.plugin.eclipse.model.PluginModel.Command;
 
 public class PluginModelTest {
@@ -55,11 +55,11 @@
 
         assertArrayEquals(new String[] {"cli"}, foo.environments.toArray(new String[0]));
 
-        List<Bundle> bundles = foo.bundles;
+        List<BundleInformation> bundles = foo.bundles;
         assertEquals(1, bundles.size());
 
-        Bundle bundle = bundles.get(0);
-        assertEquals("foo", bundle.getName());
+        BundleInformation bundle = bundles.get(0);
+        assertEquals("foo", bundle.getSymbolicName());
         assertEquals("1", bundle.getVersion());
     }
 }