Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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