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.

NickColourPanel.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.nickcolours;
  23. import com.dmdirc.addons.ui_swing.components.GenericTableModel;
  24. import com.dmdirc.addons.ui_swing.components.IconManager;
  25. import com.dmdirc.config.prefs.PreferencesInterface;
  26. import com.dmdirc.ui.messages.ColourManager;
  27. import com.dmdirc.util.colours.Colour;
  28. import java.awt.Color;
  29. import java.awt.Window;
  30. import java.awt.event.ActionEvent;
  31. import java.awt.event.ActionListener;
  32. import java.util.HashMap;
  33. import java.util.Map;
  34. import javax.swing.JButton;
  35. import javax.swing.JPanel;
  36. import javax.swing.JScrollPane;
  37. import javax.swing.JTable;
  38. import javax.swing.event.ListSelectionEvent;
  39. import javax.swing.event.ListSelectionListener;
  40. import net.miginfocom.swing.MigLayout;
  41. /**
  42. * Panel used for the custom nick colour settings component in the plugin's config dialog.
  43. */
  44. public class NickColourPanel extends JPanel implements ActionListener,
  45. PreferencesInterface, ListSelectionListener {
  46. /** A version number for this class. */
  47. private static final long serialVersionUID = 1;
  48. /** The table used for displaying the options. */
  49. private final JTable table;
  50. /** Edit button. */
  51. private final JButton editButton;
  52. /** Delete button. */
  53. private final JButton deleteButton;
  54. /** Parent window that will own any new dialogs. */
  55. private final Window parentWindow;
  56. /** Icon manager to retrieve icons from. */
  57. private final IconManager iconManager;
  58. /** Colour manage to use to parse colours. */
  59. private final ColourManager colourManager;
  60. private final NickColourManager manager;
  61. private final GenericTableModel<NickColourEntry> model;
  62. /**
  63. * Creates a new instance of NickColourPanel.
  64. *
  65. * @param parentWindow Parent window that will own any new dialogs.
  66. * @param iconManager Icon manager to load icons from.
  67. * @param colourManager The colour manager to use to parse colours.
  68. * @param nickColours
  69. */
  70. public NickColourPanel(final Window parentWindow, final IconManager iconManager,
  71. final ColourManager colourManager, final NickColourManager manager,
  72. final Map<String, Color> nickColours) {
  73. this.parentWindow = parentWindow;
  74. this.iconManager = iconManager;
  75. this.colourManager = colourManager;
  76. this.manager = manager;
  77. model = new GenericTableModel<>(NickColourEntry.class,
  78. "getUser", "getNetwork", "getColor");
  79. nickColours.forEach((description, colour) -> model.addValue(NickColourEntry
  80. .create(description.split(":")[0], description.split(":")[1], colour)));
  81. model.setHeaderNames("User", "Network", "Colour");
  82. table = new JTable(model);
  83. table.setDefaultRenderer(Color.class, new ColourRenderer());
  84. final JScrollPane scrollPane = new JScrollPane(table);
  85. table.getSelectionModel().addListSelectionListener(this);
  86. table.setFillsViewportHeight(true);
  87. setLayout(new MigLayout("ins 0, fillx, hmax 500"));
  88. add(scrollPane, "grow, push, wrap, spanx");
  89. final JButton addButton = new JButton("Add");
  90. addButton.addActionListener(this);
  91. add(addButton, "sg button, growx, pushx");
  92. editButton = new JButton("Edit");
  93. editButton.addActionListener(this);
  94. add(editButton, "sg button, growx, pushx");
  95. deleteButton = new JButton("Delete");
  96. deleteButton.addActionListener(this);
  97. add(deleteButton, "sg button, growx, pushx");
  98. editButton.setEnabled(false);
  99. deleteButton.setEnabled(false);
  100. }
  101. @Override
  102. public void actionPerformed(final ActionEvent e) {
  103. final int row = table.getSelectedRow();
  104. switch (e.getActionCommand()) {
  105. case "Add":
  106. new NickColourInputDialog(parentWindow, colourManager, iconManager, model);
  107. break;
  108. case "Edit":
  109. final NickColourEntry entry = model.getValue(row);
  110. final String network = entry.getNetwork();
  111. final String nickname = entry.getUser();
  112. final Color textcolour = entry.getColor();
  113. new NickColourInputDialog(parentWindow, colourManager, iconManager, model,
  114. row, nickname, network, textcolour);
  115. break;
  116. case "Delete":
  117. if (row > -1) {
  118. model.removeValue(model.getValue(row));
  119. }
  120. break;
  121. }
  122. }
  123. /**
  124. * Removes a row from the table.
  125. *
  126. * @param row The row to be removed
  127. */
  128. public void removeRow(final int row) {
  129. model.removeValue(model.getValue(row));
  130. }
  131. /**
  132. * Adds a row to the table.
  133. *
  134. * @param network The network setting
  135. * @param nickname The nickname setting
  136. * @param textcolour The textpane colour setting
  137. */
  138. public void addRow(final String network, final String nickname,
  139. final String textcolour) {
  140. final Colour colour = colourManager.getColourFromString(textcolour, null);
  141. model.addValue(NickColourEntry.create(network, nickname, new Color(colour.getRed(),
  142. colour.getGreen(), colour.getBlue())));
  143. }
  144. @Override
  145. public void save() {
  146. final Map<String, Color> values = new HashMap<>();
  147. for (int i =0; i < model.getRowCount(); i++) {
  148. final NickColourEntry entry = model.getValue(i);
  149. values.put(entry.getNetwork()+ ':' +entry.getUser(), entry.getColor());
  150. }
  151. manager.saveNickColourStore(values);
  152. }
  153. @Override
  154. public void valueChanged(final ListSelectionEvent e) {
  155. final boolean enable = table.getSelectedRow() > -1
  156. && table.getModel().getRowCount() > 0;
  157. editButton.setEnabled(enable);
  158. deleteButton.setEnabled(enable);
  159. }
  160. }