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.

LicensePanel.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.GenericListModel;
  25. import com.dmdirc.addons.ui_swing.components.ListScroller;
  26. import java.awt.Font;
  27. import javax.swing.JEditorPane;
  28. import javax.swing.JList;
  29. import javax.swing.JPanel;
  30. import javax.swing.JScrollPane;
  31. import javax.swing.ListSelectionModel;
  32. import javax.swing.SwingUtilities;
  33. import javax.swing.UIManager;
  34. import javax.swing.event.ListSelectionEvent;
  35. import javax.swing.event.ListSelectionListener;
  36. import javax.swing.text.html.HTMLDocument;
  37. import javax.swing.text.html.HTMLEditorKit;
  38. import net.miginfocom.swing.MigLayout;
  39. /**
  40. * License panel.
  41. */
  42. public final class LicensePanel extends JPanel implements ListSelectionListener {
  43. /**
  44. * A version number for this class. It should be changed whenever the class
  45. * structure is changed (or anything else that would prevent serialized
  46. * objects being unserialized with the new class).
  47. */
  48. private static final long serialVersionUID = 3;
  49. /** License scroll pane. */
  50. private JScrollPane scrollPane;
  51. /** License list model */
  52. private GenericListModel<License> listModel;
  53. /** License textpane. */
  54. private JEditorPane license;
  55. /** License list. */
  56. private JList list;
  57. /** Selected index. */
  58. private int selectedIndex;
  59. /** Creates a new instance of LicensePanel. */
  60. public LicensePanel() {
  61. super();
  62. initComponents();
  63. addListeners();
  64. layoutComponents();
  65. list.setSelectedIndex(0);
  66. }
  67. /**
  68. * Adds the listeners to the components.
  69. */
  70. private void addListeners() {
  71. list.addListSelectionListener(this);
  72. }
  73. /**
  74. * Lays out the components.
  75. */
  76. private void layoutComponents() {
  77. setLayout(new MigLayout("ins rel, fill"));
  78. add(new JScrollPane(list), "growy, pushy, w 150!");
  79. add(scrollPane, "grow, push");
  80. }
  81. /** Initialises the components. */
  82. private void initComponents() {
  83. setOpaque(UIUtilities.getTabbedPaneOpaque());
  84. listModel = new GenericListModel<License>();
  85. list = new JList(listModel);
  86. list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  87. new ListScroller(list);
  88. new LicenseLoader(listModel).execute();
  89. license = new JEditorPane();
  90. license.setEditorKit(new HTMLEditorKit());
  91. final Font font = UIManager.getFont("Label.font");
  92. ((HTMLDocument) license.getDocument()).getStyleSheet().addRule("body " +
  93. "{ font-family: " + font.getFamily() + "; " + "font-size: " +
  94. font.getSize() + "pt; }");
  95. license.setEditable(false);
  96. scrollPane = new JScrollPane(license);
  97. }
  98. /** {@inheritDoc} */
  99. @Override
  100. public void valueChanged(final ListSelectionEvent e) {
  101. if (!e.getValueIsAdjusting()) {
  102. if (list.getSelectedIndex() == -1) {
  103. list.setSelectedIndex(selectedIndex);
  104. } else {
  105. license.setText(listModel.get(list.getSelectedIndex()).getBody());
  106. UIUtilities.resetScrollPane(scrollPane);
  107. }
  108. selectedIndex = list.getSelectedIndex();
  109. }
  110. }
  111. }