You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LicencesPanel.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.ui_swing.dialogs.about;
  23. import com.dmdirc.addons.ui_swing.UIUtilities;
  24. import com.dmdirc.addons.ui_swing.components.TreeScroller;
  25. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  26. import com.dmdirc.interfaces.ui.AboutDialogModel;
  27. import com.dmdirc.ui.core.about.Licence;
  28. import com.dmdirc.ui.core.about.LicensedComponent;
  29. import java.awt.Font;
  30. import java.awt.Rectangle;
  31. import javax.swing.BorderFactory;
  32. import javax.swing.JEditorPane;
  33. import javax.swing.JPanel;
  34. import javax.swing.JScrollPane;
  35. import javax.swing.JTree;
  36. import javax.swing.UIManager;
  37. import javax.swing.event.TreeSelectionEvent;
  38. import javax.swing.event.TreeSelectionListener;
  39. import javax.swing.text.html.HTMLDocument;
  40. import javax.swing.text.html.HTMLEditorKit;
  41. import javax.swing.tree.DefaultMutableTreeNode;
  42. import javax.swing.tree.DefaultTreeModel;
  43. import javax.swing.tree.MutableTreeNode;
  44. import javax.swing.tree.TreeNode;
  45. import javax.swing.tree.TreeSelectionModel;
  46. import net.miginfocom.layout.PlatformDefaults;
  47. import net.miginfocom.swing.MigLayout;
  48. /**
  49. * Licences panel.
  50. */
  51. public class LicencesPanel extends JPanel implements TreeSelectionListener {
  52. /** Serial version UID. */
  53. private static final long serialVersionUID = 3;
  54. private final AboutDialogModel model;
  55. /** Config manager. */
  56. private final AggregateConfigProvider config;
  57. /** Licence scroll pane. */
  58. private JScrollPane scrollPane;
  59. /** Licence list model */
  60. private DefaultTreeModel listModel;
  61. /** Licence textpane. */
  62. private JEditorPane licence;
  63. /** Licence list. */
  64. private JTree list;
  65. public LicencesPanel(final AboutDialogModel model,
  66. final AggregateConfigProvider globalConfig) {
  67. this.model = model;
  68. config = globalConfig;
  69. initComponents();
  70. addListeners();
  71. initLicenses();
  72. layoutComponents();
  73. }
  74. /**
  75. * Adds the listeners to the components.
  76. */
  77. private void addListeners() {
  78. list.addTreeSelectionListener(this);
  79. }
  80. /**
  81. * Lays out the components.
  82. */
  83. private void layoutComponents() {
  84. setLayout(new MigLayout("ins rel, fill"));
  85. add(new JScrollPane(list), "growy, pushy, w 150!");
  86. add(scrollPane, "grow, push");
  87. }
  88. /** Initialises the components. */
  89. private void initComponents() {
  90. setOpaque(UIUtilities.getTabbedPaneOpaque());
  91. listModel = new DefaultTreeModel(new DefaultMutableTreeNode());
  92. list = new JTree(listModel) {
  93. /**
  94. * A version number for this class.
  95. */
  96. private static final long serialVersionUID = 1;
  97. @Override
  98. public void scrollRectToVisible(final Rectangle aRect) {
  99. final Rectangle rect = new Rectangle(0, aRect.y, aRect.width,
  100. aRect.height);
  101. super.scrollRectToVisible(rect);
  102. }
  103. };
  104. list.setBorder(BorderFactory.createEmptyBorder(
  105. (int) PlatformDefaults.getUnitValueX("related").getValue(),
  106. (int) PlatformDefaults.getUnitValueX("related").getValue(),
  107. (int) PlatformDefaults.getUnitValueX("related").getValue(),
  108. (int) PlatformDefaults.getUnitValueX("related").getValue()));
  109. list.setCellRenderer(new LicenceRenderer());
  110. list.setRootVisible(false);
  111. list.setOpaque(false);
  112. list.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
  113. new TreeScroller(list);
  114. licence = new JEditorPane();
  115. licence.setEditorKit(new HTMLEditorKit());
  116. final Font font = UIManager.getFont("Label.font");
  117. ((HTMLDocument) licence.getDocument()).getStyleSheet().addRule("body " + "{ font-family: "
  118. + font.getFamily() + "; " + "font-size: " + font.getSize() + "pt; }");
  119. licence.setEditable(false);
  120. scrollPane = new JScrollPane(licence);
  121. }
  122. @Override
  123. public void valueChanged(final TreeSelectionEvent e) {
  124. if (list.getSelectionCount() == 0) {
  125. list.setSelectionPath(e.getOldLeadSelectionPath());
  126. }
  127. list.scrollPathToVisible(e.getPath());
  128. final Object userObject = ((DefaultMutableTreeNode) e.getPath().
  129. getLastPathComponent()).getUserObject();
  130. if (userObject instanceof Licence) {
  131. licence.setText(
  132. "<h3 style='margin: 3px; padding: 0px 0px 5px 0px;'>"
  133. + ((Licence) userObject).getName() + "</h3>"
  134. + ((Licence) userObject).getBody().replaceAll("\\n", "<br>"));
  135. } else if (userObject instanceof LicensedComponent) {
  136. final LicensedComponent lc = (LicensedComponent) userObject;
  137. licence.setText("<b>Name:</b> "
  138. + lc.getName() + "<br>");
  139. } else {
  140. licence.setText("<b>Name:</b> DMDirc<br>"
  141. + "<b>Version:</b> " + config
  142. .getOption("version", "version") + "<br>"
  143. + "<b>Description:</b> The intelligent IRC client");
  144. }
  145. UIUtilities.resetScrollPane(scrollPane);
  146. }
  147. private void initLicenses() {
  148. model.getLicensedComponents().forEach(this::addLicensedComponent);
  149. listModel.nodeStructureChanged((TreeNode) listModel.getRoot());
  150. for (int i = 0; i < list.getRowCount(); i++) {
  151. list.expandRow(i);
  152. }
  153. list.setSelectionRow(0);
  154. }
  155. private void addLicensedComponent(final LicensedComponent component) {
  156. final MutableTreeNode componentNode = new DefaultMutableTreeNode(component);
  157. listModel.insertNodeInto(componentNode, (MutableTreeNode) listModel.getRoot(),
  158. listModel.getChildCount(listModel.getRoot()));
  159. component.getLicences().forEach(l -> listModel.insertNodeInto(
  160. new DefaultMutableTreeNode(l), componentNode,
  161. listModel.getChildCount(componentNode)));
  162. }
  163. }