Browse Source

Some more work on exposing plugin licences nicely, they not get displayed in a tree under their associated plugin

Issue 2643: Expose plugin licences

Change-Id: Ia7213154f81e6647d18998dba89b3419ad9617e5
Reviewed-on: http://gerrit.dmdirc.com/768
Automatic-Compile: Chris Smith <chris@dmdirc.com>
Reviewed-by: Chris Smith <chris@dmdirc.com>
tags/0.6.3
Gregory Holmes 14 years ago
parent
commit
fb90c2c7f9

+ 1
- 1
src/com/dmdirc/addons/ui_swing/dialogs/about/AboutDialog.java View File

@@ -117,7 +117,7 @@ public final class AboutDialog extends StandardDialog implements
117 117
         tabbedPane.addChangeListener(this);
118 118
 
119 119
         getContentPane().setLayout(new MigLayout("ins rel, wrap 1, fill, " +
120
-                "wmin 550, wmax 550, hmin 400, hmax 400"));
120
+                "wmin 600, wmax 600, hmin 400, hmax 400"));
121 121
         getContentPane().add(tabbedPane, "grow, push");
122 122
         getContentPane().add(getOkButton(), "right");
123 123
     }

+ 70
- 27
src/com/dmdirc/addons/ui_swing/dialogs/about/LicenceLoader.java View File

@@ -1,5 +1,4 @@
1 1
 /*
2
- * 
3 2
  * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
4 3
  * 
5 4
  * Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -23,7 +22,6 @@
23 22
 
24 23
 package com.dmdirc.addons.ui_swing.dialogs.about;
25 24
 
26
-import com.dmdirc.addons.ui_swing.components.GenericListModel;
27 25
 import com.dmdirc.addons.ui_swing.components.LoggingSwingWorker;
28 26
 import com.dmdirc.logger.ErrorLevel;
29 27
 import com.dmdirc.logger.Logger;
@@ -39,20 +37,28 @@ import java.util.Map;
39 37
 import java.util.Map.Entry;
40 38
 import java.util.TreeMap;
41 39
 
40
+import javax.swing.JTree;
41
+import javax.swing.tree.DefaultMutableTreeNode;
42
+import javax.swing.tree.DefaultTreeModel;
43
+
42 44
 /**
43 45
  * Background loader of licences into a list.
44 46
  */
45 47
 public class LicenceLoader extends LoggingSwingWorker<Void, Void> {
46 48
 
49
+    /** Tree. */
50
+    private JTree tree;
47 51
     /** Model to load licences into. */
48
-    private GenericListModel<Licence> model;
52
+    private DefaultTreeModel model;
49 53
 
50 54
     /**
51 55
      * Instantiates a new licence loader.
52 56
      *
57
+     * @param tree Tree
53 58
      * @param model Model to load licences into
54 59
      */
55
-    public LicenceLoader(final GenericListModel<Licence> model) {
60
+    public LicenceLoader(final JTree tree, final DefaultTreeModel model) {
61
+        this.tree = tree;
56 62
         this.model = model;
57 63
     }
58 64
 
@@ -65,39 +71,76 @@ public class LicenceLoader extends LoggingSwingWorker<Void, Void> {
65 71
     protected Void doInBackground() throws Exception {
66 72
         final ResourceManager rm = ResourceManager.getResourceManager();
67 73
         if (rm == null) {
68
-            Logger.userError(ErrorLevel.LOW, "Unable to load licences, " +
69
-                    "no resource manager");
74
+            Logger.userError(ErrorLevel.LOW, "Unable to load licences, "
75
+                    + "no resource manager");
70 76
         } else {
71
-            final Map<String, InputStream> licences =
72
-                    new TreeMap<String, InputStream>(String.CASE_INSENSITIVE_ORDER);
73
-            licences.putAll(rm.getResourcesStartingWithAsInputStreams(
74
-                    "com/dmdirc/licences/"));
75
-            for (PluginInfo pi : PluginManager.getPluginManager().getPluginInfos()) {
76
-                licences.putAll(pi.getLicenceStreams());
77
-            }
78
-            for (Entry<String, InputStream> entry : licences.entrySet()) {
79
-                final String licenceString = entry.getKey().substring(entry.
80
-                        getKey().
81
-                        lastIndexOf('/') + 1);
82
-                if (licenceString.length() > 1) {
83
-                    final String licenceStringParts[] = licenceString.split(
84
-                            " - ");
85
-                    final Licence licence = new Licence(licenceStringParts[1],
86
-                            licenceStringParts[0], "<html><h1>" +
87
-                            licenceStringParts[1] + "</h1><p>" + readInputStream(
88
-                            entry.getValue()).replace("\n", "<br>") +
89
-                            "</p></html>");
90
-                    model.add(licence);
91
-                }
77
+            addCoreLicences(rm);
78
+            for (PluginInfo pi :
79
+                    PluginManager.getPluginManager().getPluginInfos()) {
80
+                addPluginLicences(pi);
92 81
             }
93 82
         }
94 83
 
95 84
         return null;
96 85
     }
97 86
 
87
+    private Licence createLicence(final Entry<String, InputStream> entry) {
88
+        final String licenceString = entry.getKey().substring(entry.getKey().
89
+                lastIndexOf('/') + 1);
90
+        if (licenceString.length() > 1) {
91
+            final String licenceStringParts[] = licenceString.split(
92
+                    " - ");
93
+            return new Licence(licenceStringParts[1], licenceStringParts[0],
94
+                    "<html><h1>" + licenceStringParts[1] + "</h1><p>"
95
+                    + readInputStream(entry.getValue()).replace("\n", "<br>")
96
+                    + "</p></html>");
97
+        } else {
98
+            return null;
99
+        }
100
+    }
101
+
102
+    private void addCoreLicences(final ResourceManager rm) {
103
+        final DefaultMutableTreeNode root = new DefaultMutableTreeNode("DMDirc");
104
+        final Map<String, InputStream> licences =
105
+                new TreeMap<String, InputStream>(String.CASE_INSENSITIVE_ORDER);
106
+        licences.putAll(rm.getResourcesStartingWithAsInputStreams(
107
+                "com/dmdirc/licences/"));
108
+        addLicensesToNode(licences, root);
109
+    }
110
+
111
+    private void addPluginLicences(final PluginInfo pi) throws IOException {
112
+        final Map<String, InputStream> licences = pi.getLicenceStreams();
113
+
114
+        if (licences.isEmpty()) {
115
+            return;
116
+        }
117
+
118
+        final DefaultMutableTreeNode root = new DefaultMutableTreeNode(pi);
119
+        addLicensesToNode(licences, root);
120
+    }
121
+
122
+    private void addLicensesToNode(final Map<String, InputStream> licences,
123
+            final DefaultMutableTreeNode root) {
124
+        model.insertNodeInto(root, (DefaultMutableTreeNode) model.getRoot(),
125
+                model.getChildCount(model.getRoot()));
126
+        for (Entry<String, InputStream> entry : licences.entrySet()) {
127
+            final Licence licence = createLicence(entry);
128
+            if (licence == null) {
129
+                continue;
130
+            }
131
+            model.insertNodeInto(new DefaultMutableTreeNode(licence), root,
132
+                    model.getChildCount(root));
133
+        }
134
+    }
135
+
98 136
     /** {@inheritDoc} */
99 137
     @Override
100 138
     protected void done() {
139
+        model.nodeStructureChanged((DefaultMutableTreeNode) model.getRoot());
140
+        for (int i = 0; i < tree.getRowCount(); i++) {
141
+            tree.expandRow(i);
142
+        }
143
+        tree.setSelectionRow(0);
101 144
         super.done();
102 145
     }
103 146
 

+ 50
- 27
src/com/dmdirc/addons/ui_swing/dialogs/about/LicencesPanel.java View File

@@ -23,28 +23,30 @@
23 23
 package com.dmdirc.addons.ui_swing.dialogs.about;
24 24
 
25 25
 import com.dmdirc.addons.ui_swing.UIUtilities;
26
-import com.dmdirc.addons.ui_swing.components.GenericListModel;
27
-import com.dmdirc.addons.ui_swing.components.ListScroller;
26
+import com.dmdirc.addons.ui_swing.components.TreeScroller;
27
+import com.dmdirc.plugins.PluginInfo;
28 28
 
29 29
 import java.awt.Font;
30
+import java.awt.Rectangle;
30 31
 
31 32
 import javax.swing.JEditorPane;
32
-import javax.swing.JList;
33 33
 import javax.swing.JPanel;
34 34
 import javax.swing.JScrollPane;
35
-import javax.swing.ListSelectionModel;
35
+import javax.swing.JTree;
36 36
 import javax.swing.UIManager;
37
-import javax.swing.event.ListSelectionEvent;
38
-import javax.swing.event.ListSelectionListener;
37
+import javax.swing.event.TreeSelectionEvent;
38
+import javax.swing.event.TreeSelectionListener;
39 39
 import javax.swing.text.html.HTMLDocument;
40 40
 import javax.swing.text.html.HTMLEditorKit;
41
+import javax.swing.tree.DefaultMutableTreeNode;
42
+import javax.swing.tree.DefaultTreeModel;
41 43
 
42 44
 import net.miginfocom.swing.MigLayout;
43 45
 
44 46
 /**
45 47
  * Licences panel.
46 48
  */
47
-public final class LicencesPanel extends JPanel implements ListSelectionListener {
49
+public final class LicencesPanel extends JPanel implements TreeSelectionListener {
48 50
 
49 51
     /**
50 52
      * A version number for this class. It should be changed whenever the class
@@ -55,11 +57,11 @@ public final class LicencesPanel extends JPanel implements ListSelectionListener
55 57
     /** Licence scroll pane. */
56 58
     private JScrollPane scrollPane;
57 59
     /** Licence list model */
58
-    private GenericListModel<Licence> listModel;
60
+    private DefaultTreeModel listModel;
59 61
     /** Licence textpane. */
60 62
     private JEditorPane licence;
61 63
     /** Licence list. */
62
-    private JList list;
64
+    private JTree list;
63 65
     /** Selected index. */
64 66
     private int selectedIndex;
65 67
 
@@ -70,15 +72,13 @@ public final class LicencesPanel extends JPanel implements ListSelectionListener
70 72
         initComponents();
71 73
         addListeners();
72 74
         layoutComponents();
73
-
74
-        list.setSelectedIndex(0);
75 75
     }
76 76
 
77 77
     /**
78 78
      * Adds the listeners to the components.
79 79
      */
80 80
     private void addListeners() {
81
-        list.addListSelectionListener(this);
81
+        list.addTreeSelectionListener(this);
82 82
     }
83 83
 
84 84
     /**
@@ -93,11 +93,27 @@ public final class LicencesPanel extends JPanel implements ListSelectionListener
93 93
     /** Initialises the components. */
94 94
     private void initComponents() {
95 95
         setOpaque(UIUtilities.getTabbedPaneOpaque());
96
-        listModel = new GenericListModel<Licence>();
97
-        list = new JList(listModel);
98
-        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
99
-        new ListScroller(list);
100
-        new LicenceLoader(listModel).execute();
96
+        listModel = new DefaultTreeModel(new DefaultMutableTreeNode());
97
+        list = new JTree(listModel) {
98
+
99
+            /**
100
+             * A version number for this class. It should be changed whenever the class
101
+             * structure is changed (or anything else that would prevent serialized
102
+             * objects being unserialized with the new class).
103
+             */
104
+            private static final long serialVersionUID = 1;
105
+
106
+            /** {@inheritDoc} */
107
+            @Override
108
+            public void scrollRectToVisible(final Rectangle aRect) {
109
+                final Rectangle rect = new Rectangle(0, aRect.y, aRect.width,
110
+                        aRect.height);
111
+                super.scrollRectToVisible(rect);
112
+            }
113
+        };
114
+        list.setRootVisible(false);
115
+        new TreeScroller(list);
116
+        new LicenceLoader(list, listModel).execute();
101 117
         licence = new JEditorPane();
102 118
         licence.setEditorKit(new HTMLEditorKit());
103 119
         final Font font = UIManager.getFont("Label.font");
@@ -110,15 +126,22 @@ public final class LicencesPanel extends JPanel implements ListSelectionListener
110 126
 
111 127
     /** {@inheritDoc} */
112 128
     @Override
113
-    public void valueChanged(final ListSelectionEvent e) {
114
-        if (!e.getValueIsAdjusting()) {
115
-            if (list.getSelectedIndex() == -1) {
116
-                list.setSelectedIndex(selectedIndex);
117
-            } else {
118
-                licence.setText(listModel.get(list.getSelectedIndex()).getBody());
119
-                UIUtilities.resetScrollPane(scrollPane);
120
-            }
121
-            selectedIndex = list.getSelectedIndex();
129
+    public void valueChanged(final TreeSelectionEvent e) {
130
+        list.scrollPathToVisible(e.getPath());
131
+        final Object userObject = ((DefaultMutableTreeNode) e.getPath().
132
+                getLastPathComponent()).getUserObject();
133
+        if (userObject instanceof Licence) {
134
+        licence.setText(((Licence) userObject).getBody());
135
+        } else if (userObject instanceof PluginInfo) {
136
+            final PluginInfo pi = (PluginInfo) userObject;
137
+            licence.setText("Name: " + pi.getNiceName() + "<br>"
138
+                    + "Description: " + pi.getDescription() + "<br>"
139
+                    + "Author: " + pi.getAuthor() + "<br>"
140
+                    + "Version: " + pi.getFriendlyVersion());
141
+        } else {
142
+            licence.setText("Name: DMDirc<br>"
143
+                    + "Desciption: The intelligent IRC client");
122 144
         }
145
+        UIUtilities.resetScrollPane(scrollPane);
123 146
     }
124
-}
147
+}

Loading…
Cancel
Save