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

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