Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

NickColourPanel.java 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (c) 2006-2014 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.config.prefs.PreferencesInterface;
  24. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  25. import com.dmdirc.interfaces.config.ConfigProvider;
  26. import com.dmdirc.ui.IconManager;
  27. import com.dmdirc.ui.messages.ColourManager;
  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.ArrayList;
  33. import java.util.List;
  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 javax.swing.table.DefaultTableModel;
  41. import javax.swing.table.TableCellRenderer;
  42. import net.miginfocom.swing.MigLayout;
  43. /**
  44. * Panel used for the custom nick colour settings component in the plugin's config dialog.
  45. */
  46. public class NickColourPanel extends JPanel implements ActionListener,
  47. PreferencesInterface, ListSelectionListener {
  48. /** A version number for this class. */
  49. private static final long serialVersionUID = 1;
  50. /** The table headings. */
  51. private static final String[] HEADERS
  52. = {"Network", "Nickname", "Text colour", "Nicklist colour"};
  53. /** The table used for displaying the options. */
  54. private final JTable table;
  55. /** The identity to write settings to. */
  56. private final ConfigProvider configIdentity;
  57. /** Edit button. */
  58. private final JButton editButton;
  59. /** Delete button. */
  60. private final JButton deleteButton;
  61. /** Parent window that will own any new dialogs. */
  62. private final Window parentWindow;
  63. /** Icon manager to retrieve icons from. */
  64. private final IconManager iconManager;
  65. /** Colour manage to use to parse colours. */
  66. private final ColourManager colourManager;
  67. /** Config provider to read settings from. */
  68. private final AggregateConfigProvider config;
  69. /** The plugin's config domain. */
  70. private final String domain;
  71. /**
  72. * Creates a new instance of NickColourPanel.
  73. *
  74. * @param parentWindow Parent window that will own any new dialogs.
  75. * @param iconManager Icon manager to load icons from.
  76. * @param colourManager The colour manager to use to parse colours.
  77. * @param userSettings The provider to write user settings to.
  78. * @param config The config provider to read settings from.
  79. * @param domain The plugin's config domain
  80. */
  81. public NickColourPanel(
  82. final Window parentWindow, final IconManager iconManager,
  83. final ColourManager colourManager,
  84. final ConfigProvider userSettings,
  85. final AggregateConfigProvider config, final String domain) {
  86. this.parentWindow = parentWindow;
  87. this.iconManager = iconManager;
  88. this.colourManager = colourManager;
  89. this.configIdentity = userSettings;
  90. this.config = config;
  91. this.domain = domain;
  92. final Object[][] data = NickColourManager.getData(config, domain);
  93. table = new JTable(new DefaultTableModel(data, HEADERS)) {
  94. /** A version number for this class. */
  95. private static final long serialVersionUID = 1;
  96. /** The colour renderer we're using for colour cells. */
  97. private final ColourRenderer colourRenderer = new ColourRenderer(colourManager);
  98. @Override
  99. public TableCellRenderer getCellRenderer(final int row,
  100. final int column) {
  101. if (column == 2 || column == 3) {
  102. return colourRenderer;
  103. } else {
  104. return super.getCellRenderer(row, column);
  105. }
  106. }
  107. @Override
  108. public boolean isCellEditable(final int row, final int column) {
  109. return false;
  110. }
  111. };
  112. final JScrollPane scrollPane = new JScrollPane(table);
  113. table.getSelectionModel().addListSelectionListener(this);
  114. table.setFillsViewportHeight(true);
  115. table.setDefaultRenderer(Color.class, new ColourRenderer(colourManager));
  116. setLayout(new MigLayout("ins 0, fillx, hmax 500"));
  117. add(scrollPane, "grow, push, wrap, spanx");
  118. final JButton addButton = new JButton("Add");
  119. addButton.addActionListener(this);
  120. add(addButton, "sg button, growx, pushx");
  121. editButton = new JButton("Edit");
  122. editButton.addActionListener(this);
  123. add(editButton, "sg button, growx, pushx");
  124. deleteButton = new JButton("Delete");
  125. deleteButton.addActionListener(this);
  126. add(deleteButton, "sg button, growx, pushx");
  127. editButton.setEnabled(false);
  128. deleteButton.setEnabled(false);
  129. }
  130. @Override
  131. public void actionPerformed(final ActionEvent e) {
  132. final int row = table.getSelectedRow();
  133. switch (e.getActionCommand()) {
  134. case "Add":
  135. new NickColourInputDialog(parentWindow, colourManager, iconManager, this);
  136. break;
  137. case "Edit":
  138. final DefaultTableModel model = ((DefaultTableModel) table.getModel());
  139. final String network = (String) model.getValueAt(row, 0);
  140. final String nickname = (String) model.getValueAt(row, 1);
  141. String textcolour = (String) model.getValueAt(row, 2);
  142. String nickcolour = (String) model.getValueAt(row, 3);
  143. if (textcolour == null) {
  144. textcolour = "";
  145. }
  146. if (nickcolour == null) {
  147. nickcolour = "";
  148. }
  149. new NickColourInputDialog(parentWindow, colourManager, iconManager, this,
  150. row, nickname, network, textcolour, nickcolour);
  151. break;
  152. case "Delete":
  153. if (row > -1) {
  154. ((DefaultTableModel) table.getModel()).removeRow(row);
  155. }
  156. break;
  157. }
  158. }
  159. /**
  160. * Removes a row from the table.
  161. *
  162. * @param row The row to be removed
  163. */
  164. public void removeRow(final int row) {
  165. ((DefaultTableModel) table.getModel()).removeRow(row);
  166. }
  167. /**
  168. * Adds a row to the table.
  169. *
  170. * @param network The network setting
  171. * @param nickname The nickname setting
  172. * @param textcolour The textpane colour setting
  173. * @param nickcolour The nick list colour setting
  174. */
  175. public void addRow(final String network, final String nickname,
  176. final String textcolour, final String nickcolour) {
  177. final DefaultTableModel model = ((DefaultTableModel) table.getModel());
  178. model.addRow(new Object[]{network, nickname, textcolour, nickcolour});
  179. }
  180. /**
  181. * Retrieves the current data in use by this panel.
  182. *
  183. * @return This panel's current data.
  184. */
  185. private List<Object[]> getData() {
  186. final List<Object[]> res = new ArrayList<>();
  187. final DefaultTableModel model = ((DefaultTableModel) table.getModel());
  188. @SuppressWarnings("unchecked")
  189. final List<List<?>> rows = (List<List<?>>) model.getDataVector();
  190. for (List<?> row : rows) {
  191. res.add(new Object[]{row.get(0), row.get(1), row.get(2), row.get(3)});
  192. }
  193. return res;
  194. }
  195. @Override
  196. public void save() {
  197. // Remove all old config entries
  198. for (Object[] parts : NickColourManager.getData(config, domain)) {
  199. configIdentity.unsetOption(domain, "color:" + parts[0] + ":" + parts[1]);
  200. }
  201. // And write the new ones
  202. for (Object[] row : getData()) {
  203. configIdentity.
  204. setOption(domain, "color:" + row[0] + ":" + row[1], row[2] + ":" + row[3]);
  205. }
  206. }
  207. @Override
  208. public void valueChanged(final ListSelectionEvent e) {
  209. final boolean enable = table.getSelectedRow() > -1
  210. && table.getModel().getRowCount() > 0;
  211. editButton.setEnabled(enable);
  212. deleteButton.setEnabled(enable);
  213. }
  214. }