|
@@ -23,19 +23,12 @@
|
23
|
23
|
package com.dmdirc.addons.ui_swing.dialogs.about;
|
24
|
24
|
|
25
|
25
|
import com.dmdirc.DMDircMBassador;
|
26
|
|
-import com.dmdirc.addons.ui_swing.UIUtilities;
|
27
|
26
|
import com.dmdirc.addons.ui_swing.components.LoggingSwingWorker;
|
28
|
|
-import com.dmdirc.plugins.PluginInfo;
|
|
27
|
+import com.dmdirc.interfaces.ui.AboutDialogModel;
|
|
28
|
+import com.dmdirc.ui.core.about.LicensedComponent;
|
29
|
29
|
import com.dmdirc.util.resourcemanager.ResourceManager;
|
30
|
30
|
|
31
|
|
-import java.io.BufferedReader;
|
32
|
31
|
import java.io.IOException;
|
33
|
|
-import java.io.InputStream;
|
34
|
|
-import java.io.InputStreamReader;
|
35
|
|
-import java.util.Collection;
|
36
|
|
-import java.util.Map;
|
37
|
|
-import java.util.Map.Entry;
|
38
|
|
-import java.util.TreeMap;
|
39
|
32
|
|
40
|
33
|
import javax.swing.JTree;
|
41
|
34
|
import javax.swing.tree.DefaultMutableTreeNode;
|
|
@@ -52,117 +45,49 @@ public class LicenceLoader extends LoggingSwingWorker<Void, Void> {
|
52
|
45
|
|
53
|
46
|
/** Tree to add licenses to. */
|
54
|
47
|
private final JTree tree;
|
|
48
|
+ private final AboutDialogModel model;
|
55
|
49
|
/** Model to load licences into. */
|
56
|
|
- private final DefaultTreeModel model;
|
57
|
|
- /** List of plugins to get licenses for. */
|
58
|
|
- private final Collection<PluginInfo> plugins;
|
|
50
|
+ private final DefaultTreeModel treeModel;
|
59
|
51
|
|
60
|
52
|
/**
|
61
|
53
|
* Instantiates a new licence loader.
|
62
|
54
|
*
|
63
|
|
- * @param plugins List of plugins to get licenses from
|
64
|
55
|
* @param tree Tree to add licenses to
|
65
|
56
|
* @param model Model to load licences into
|
66
|
57
|
* @param eventBus The event bus to post errors to
|
67
|
58
|
*/
|
68
|
|
- public LicenceLoader(final Collection<PluginInfo> plugins, final JTree tree,
|
69
|
|
- final DefaultTreeModel model, final DMDircMBassador eventBus) {
|
|
59
|
+ public LicenceLoader(final AboutDialogModel model, final JTree tree,
|
|
60
|
+ final DefaultTreeModel treeModel, final DMDircMBassador eventBus) {
|
70
|
61
|
super(eventBus);
|
71
|
|
- this.plugins = plugins;
|
72
|
62
|
this.tree = tree;
|
73
|
63
|
this.model = model;
|
|
64
|
+ this.treeModel = treeModel;
|
74
|
65
|
}
|
75
|
66
|
|
76
|
67
|
@Override
|
77
|
68
|
protected Void doInBackground() throws IOException {
|
78
|
69
|
final ResourceManager rm = ResourceManager.getResourceManager();
|
79
|
70
|
checkNotNull(rm, "Unable to find resource manager");
|
80
|
|
- addCoreLicences(rm);
|
81
|
|
- for (PluginInfo pi : plugins) {
|
82
|
|
- addPluginLicences(pi);
|
83
|
|
- }
|
|
71
|
+ model.getLicensedComponents().forEach(this::addLicensedComponent);
|
84
|
72
|
return null;
|
85
|
73
|
}
|
86
|
74
|
|
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
|
|
- return new Licence(licenceStringParts[1], licenceStringParts[0],
|
93
|
|
- "<html><h1>" + licenceStringParts[1] + "</h1><p>" +
|
94
|
|
- readInputStream(entry.getValue()).replace("\n", "<br>") +
|
95
|
|
- "</p></html>");
|
96
|
|
- } else {
|
97
|
|
- return null;
|
98
|
|
- }
|
99
|
|
- }
|
100
|
|
-
|
101
|
|
- private void addCoreLicences(final ResourceManager rm) {
|
102
|
|
- final DefaultMutableTreeNode root = new DefaultMutableTreeNode("DMDirc");
|
103
|
|
- final Map<String, InputStream> licences = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
|
104
|
|
- licences.putAll(rm.getResourcesStartingWithAsInputStreams("com/dmdirc/licences/"));
|
105
|
|
- addLicensesToNode(licences, root);
|
106
|
|
- }
|
107
|
|
-
|
108
|
|
- private void addPluginLicences(final PluginInfo pi) throws IOException {
|
109
|
|
- final Map<String, InputStream> licences = pi.getLicenceStreams();
|
110
|
|
-
|
111
|
|
- if (licences.isEmpty()) {
|
112
|
|
- return;
|
113
|
|
- }
|
114
|
|
-
|
115
|
|
- final DefaultMutableTreeNode root = new DefaultMutableTreeNode(pi);
|
116
|
|
- addLicensesToNode(licences, root);
|
117
|
|
- }
|
118
|
|
-
|
119
|
|
- private void addLicensesToNode(final Map<String, InputStream> licences,
|
120
|
|
- final DefaultMutableTreeNode root) {
|
121
|
|
- UIUtilities.invokeAndWait(() -> model.insertNodeInto(root,
|
122
|
|
- (MutableTreeNode) model.getRoot(), model.getChildCount(model.getRoot())));
|
123
|
|
- for (Entry<String, InputStream> entry : licences.entrySet()) {
|
124
|
|
- final Licence licence = createLicence(entry);
|
125
|
|
- if (licence == null) {
|
126
|
|
- continue;
|
127
|
|
- }
|
128
|
|
- UIUtilities.invokeAndWait(
|
129
|
|
- () -> model.insertNodeInto(new DefaultMutableTreeNode(licence), root,
|
130
|
|
- model.getChildCount(root)));
|
131
|
|
- }
|
|
75
|
+ private void addLicensedComponent(final LicensedComponent component) {
|
|
76
|
+ final MutableTreeNode componentNode = new DefaultMutableTreeNode(component);
|
|
77
|
+ treeModel.insertNodeInto(componentNode, (MutableTreeNode) treeModel.getRoot(),
|
|
78
|
+ treeModel.getChildCount(treeModel.getRoot()));
|
|
79
|
+ component.getLicences().forEach(l -> treeModel.insertNodeInto(
|
|
80
|
+ new DefaultMutableTreeNode(l), componentNode,
|
|
81
|
+ treeModel.getChildCount(componentNode)));
|
132
|
82
|
}
|
133
|
83
|
|
134
|
84
|
@Override
|
135
|
85
|
protected void done() {
|
136
|
|
- model.nodeStructureChanged((TreeNode) model.getRoot());
|
|
86
|
+ treeModel.nodeStructureChanged((TreeNode) treeModel.getRoot());
|
137
|
87
|
for (int i = 0; i < tree.getRowCount(); i++) {
|
138
|
88
|
tree.expandRow(i);
|
139
|
89
|
}
|
140
|
90
|
tree.setSelectionRow(0);
|
141
|
91
|
super.done();
|
142
|
92
|
}
|
143
|
|
-
|
144
|
|
- /**
|
145
|
|
- * Converts an input stream into a string.
|
146
|
|
- *
|
147
|
|
- * @param stream Stream to convert
|
148
|
|
- *
|
149
|
|
- * @return Contents of the input stream
|
150
|
|
- */
|
151
|
|
- private String readInputStream(final InputStream stream) {
|
152
|
|
- String line;
|
153
|
|
- final StringBuilder text = new StringBuilder();
|
154
|
|
-
|
155
|
|
- try (BufferedReader input = new BufferedReader(new InputStreamReader(stream))) {
|
156
|
|
- line = input.readLine();
|
157
|
|
- while (line != null) {
|
158
|
|
- text.append(line).append('\n');
|
159
|
|
- line = input.readLine();
|
160
|
|
- }
|
161
|
|
- } catch (IOException ex) {
|
162
|
|
- //Ignore
|
163
|
|
- }
|
164
|
|
-
|
165
|
|
- return text.toString();
|
166
|
|
- }
|
167
|
|
-
|
168
|
93
|
}
|