Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

NickColourPanel.java 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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.addons.ui_swing.MainFrame;
  24. import com.dmdirc.config.prefs.PreferencesInterface;
  25. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  26. import com.dmdirc.interfaces.config.ConfigProvider;
  27. import com.dmdirc.ui.IconManager;
  28. import com.dmdirc.ui.messages.ColourManager;
  29. import java.awt.Color;
  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. /** Main frame instance to parent dialogs on. */
  62. private final MainFrame mainFrame;
  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 mainFrame Main frame to parent dialogs on
  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 MainFrame mainFrame, final IconManager iconManager,
  83. final ColourManager colourManager, final ConfigProvider userSettings,
  84. final AggregateConfigProvider config, final String domain) {
  85. this.mainFrame = mainFrame;
  86. this.iconManager = iconManager;
  87. this.colourManager = colourManager;
  88. this.configIdentity = userSettings;
  89. this.config = config;
  90. this.domain = domain;
  91. final Object[][] data = NickColourManager.getData(config, domain);
  92. table = new JTable(new DefaultTableModel(data, HEADERS)) {
  93. /** A version number for this class. */
  94. private static final long serialVersionUID = 1;
  95. /** The colour renderer we're using for colour cells. */
  96. private final ColourRenderer colourRenderer = new ColourRenderer(colourManager);
  97. /** {@inheritDoc} */
  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. /** {@inheritDoc} */
  108. @Override
  109. public boolean isCellEditable(final int row, final int column) {
  110. return false;
  111. }
  112. };
  113. final JScrollPane scrollPane = new JScrollPane(table);
  114. table.getSelectionModel().addListSelectionListener(this);
  115. table.setFillsViewportHeight(true);
  116. table.setDefaultRenderer(Color.class, new ColourRenderer(colourManager));
  117. setLayout(new MigLayout("ins 0, fillx, hmax 500"));
  118. add(scrollPane, "grow, push, wrap, spanx");
  119. final JButton addButton = new JButton("Add");
  120. addButton.addActionListener(this);
  121. add(addButton, "sg button, growx, pushx");
  122. editButton = new JButton("Edit");
  123. editButton.addActionListener(this);
  124. add(editButton, "sg button, growx, pushx");
  125. deleteButton = new JButton("Delete");
  126. deleteButton.addActionListener(this);
  127. add(deleteButton, "sg button, growx, pushx");
  128. editButton.setEnabled(false);
  129. deleteButton.setEnabled(false);
  130. }
  131. /**
  132. * {@inheritDoc}
  133. *
  134. * @param e Action event
  135. */
  136. @Override
  137. public void actionPerformed(final ActionEvent e) {
  138. final int row = table.getSelectedRow();
  139. switch (e.getActionCommand()) {
  140. case "Add":
  141. new NickColourInputDialog(mainFrame, colourManager, iconManager, this);
  142. break;
  143. case "Edit":
  144. final DefaultTableModel model = ((DefaultTableModel) table.getModel());
  145. final String network = (String) model.getValueAt(row, 0);
  146. final String nickname = (String) model.getValueAt(row, 1);
  147. String textcolour = (String) model.getValueAt(row, 2);
  148. String nickcolour = (String) model.getValueAt(row, 3);
  149. if (textcolour == null) {
  150. textcolour = "";
  151. }
  152. if (nickcolour == null) {
  153. nickcolour = "";
  154. }
  155. new NickColourInputDialog(mainFrame, colourManager, iconManager, this,
  156. row, nickname, network, textcolour, nickcolour);
  157. break;
  158. case "Delete":
  159. if (row > -1) {
  160. ((DefaultTableModel) table.getModel()).removeRow(row);
  161. }
  162. break;
  163. }
  164. }
  165. /**
  166. * Removes a row from the table.
  167. *
  168. * @param row The row to be removed
  169. */
  170. public void removeRow(final int row) {
  171. ((DefaultTableModel) table.getModel()).removeRow(row);
  172. }
  173. /**
  174. * Adds a row to the table.
  175. *
  176. * @param network The network setting
  177. * @param nickname The nickname setting
  178. * @param textcolour The textpane colour setting
  179. * @param nickcolour The nick list colour setting
  180. */
  181. public void addRow(final String network, final String nickname,
  182. final String textcolour, final String nickcolour) {
  183. final DefaultTableModel model = ((DefaultTableModel) table.getModel());
  184. model.addRow(new Object[]{network, nickname, textcolour, nickcolour});
  185. }
  186. /**
  187. * Retrieves the current data in use by this panel.
  188. *
  189. * @return This panel's current data.
  190. */
  191. private List<Object[]> getData() {
  192. final List<Object[]> res = new ArrayList<>();
  193. final DefaultTableModel model = ((DefaultTableModel) table.getModel());
  194. @SuppressWarnings("unchecked")
  195. final List<List<?>> rows = (List<List<?>>) model.getDataVector();
  196. for (List<?> row : rows) {
  197. res.add(new Object[]{row.get(0), row.get(1), row.get(2), row.get(3)});
  198. }
  199. return res;
  200. }
  201. /** {@inheritDoc} */
  202. @Override
  203. public void save() {
  204. // Remove all old config entries
  205. for (Object[] parts : NickColourManager.getData(config, domain)) {
  206. configIdentity.unsetOption(domain, "color:" + parts[0] + ":" + parts[1]);
  207. }
  208. // And write the new ones
  209. for (Object[] row : getData()) {
  210. configIdentity.
  211. setOption(domain, "color:" + row[0] + ":" + row[1], row[2] + ":" + row[3]);
  212. }
  213. }
  214. /** {@inheritDoc} */
  215. @Override
  216. public void valueChanged(final ListSelectionEvent e) {
  217. final boolean enable = table.getSelectedRow() > -1
  218. && table.getModel().getRowCount() > 0;
  219. editButton.setEnabled(enable);
  220. deleteButton.setEnabled(enable);
  221. }
  222. }