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 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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.nickcolours;
  23. import com.dmdirc.Main;
  24. import com.dmdirc.addons.ui_swing.SwingController;
  25. import com.dmdirc.config.IdentityManager;
  26. import com.dmdirc.config.prefs.PreferencesInterface;
  27. import java.awt.Color;
  28. import java.awt.event.ActionEvent;
  29. import java.awt.event.ActionListener;
  30. import java.util.ArrayList;
  31. import java.util.List;
  32. import java.util.Vector;
  33. import javax.swing.JButton;
  34. import javax.swing.JPanel;
  35. import javax.swing.JScrollPane;
  36. import javax.swing.JTable;
  37. import javax.swing.event.ListSelectionEvent;
  38. import javax.swing.event.ListSelectionListener;
  39. import javax.swing.table.DefaultTableModel;
  40. import javax.swing.table.TableCellRenderer;
  41. import net.miginfocom.swing.MigLayout;
  42. /**
  43. * Panel used for the custom nick colour settings component in the plugin's
  44. * config dialog.
  45. *
  46. * @author Chris
  47. */
  48. public class NickColourPanel extends JPanel implements ActionListener,
  49. PreferencesInterface, ListSelectionListener {
  50. /**
  51. * A version number for this class. It should be changed whenever the class
  52. * structure is changed (or anything else that would prevent serialized
  53. * objects being unserialized with the new class).
  54. */
  55. private static final long serialVersionUID = 1;
  56. /** The table used for displaying the options. */
  57. private final JTable table;
  58. /** The plugin we're associated with. */
  59. private final transient NickColourPlugin plugin;
  60. /** The table headings. */
  61. private static final String[] headers = {"Network", "Nickname",
  62. "Text colour", "Nicklist colour"};
  63. /** Edit and delete buttons. */
  64. private final JButton editButton, deleteButton;
  65. /**
  66. * Creates a new instance of NickColourPanel.
  67. *
  68. * @param plugin The plugin that owns this panel
  69. */
  70. public NickColourPanel(final NickColourPlugin plugin) {
  71. super();
  72. this.plugin = plugin;
  73. final Object[][] data = plugin.getData();
  74. table = new JTable(new DefaultTableModel(data, headers)) {
  75. /**
  76. * A version number for this class. It should be changed whenever the class
  77. * structure is changed (or anything else that would prevent serialized
  78. * objects being unserialized with the new class).
  79. */
  80. private static final long serialVersionUID = 1;
  81. /** The colour renderer we're using for colour cells. */
  82. private final ColourRenderer colourRenderer = new ColourRenderer();
  83. /** {@inheritDoc} */
  84. @Override
  85. public TableCellRenderer getCellRenderer(final int row,
  86. final int column) {
  87. if (column == 2 || column == 3) {
  88. return colourRenderer;
  89. } else {
  90. return super.getCellRenderer(row, column);
  91. }
  92. }
  93. /** {@inheritDoc} */
  94. @Override
  95. public boolean isCellEditable(final int row, final int column) {
  96. return false;
  97. }
  98. };
  99. final JScrollPane scrollPane = new JScrollPane(table);
  100. table.getSelectionModel().addListSelectionListener(this);
  101. table.setFillsViewportHeight(true);
  102. table.setDefaultRenderer(Color.class, new ColourRenderer());
  103. setLayout(new MigLayout("ins 0, fillx, hmax " + ((SwingController) Main.
  104. getUI()).getPrefsDialog().getPanelHeight()));
  105. add(scrollPane, "grow, push, wrap, spanx");
  106. final JButton addButton = new JButton("Add");
  107. addButton.addActionListener(this);
  108. add(addButton, "sg button, growx, pushx");
  109. editButton = new JButton("Edit");
  110. editButton.addActionListener(this);
  111. add(editButton, "sg button, growx, pushx");
  112. deleteButton = new JButton("Delete");
  113. deleteButton.addActionListener(this);
  114. add(deleteButton, "sg button, growx, pushx");
  115. editButton.setEnabled(false);
  116. deleteButton.setEnabled(false);
  117. }
  118. /**
  119. * {@inheritDoc}
  120. *
  121. * @param e Action event
  122. */
  123. @Override
  124. public void actionPerformed(final ActionEvent e) {
  125. final DefaultTableModel model = ((DefaultTableModel) table.getModel());
  126. if (e.getActionCommand().equals("Add")) {
  127. new NickColourInputDialog(this);
  128. } else if (e.getActionCommand().equals("Edit")) {
  129. final int row = table.getSelectedRow();
  130. final String network = (String) model.getValueAt(row, 0);
  131. final String nickname = (String) model.getValueAt(row, 1);
  132. String textcolour = (String) model.getValueAt(row, 2);
  133. String nickcolour = (String) model.getValueAt(row, 3);
  134. if (textcolour == null) {
  135. textcolour = "";
  136. }
  137. if (nickcolour == null) {
  138. nickcolour = "";
  139. }
  140. new NickColourInputDialog(this, row, nickname, network, textcolour,
  141. nickcolour);
  142. } else if (e.getActionCommand().equals("Delete")) {
  143. final int row = table.getSelectedRow();
  144. if (row > -1) {
  145. model.removeRow(row);
  146. }
  147. }
  148. }
  149. /**
  150. * Removes a row from the table.
  151. *
  152. * @param row The row to be removed
  153. */
  154. void removeRow(final int row) {
  155. ((DefaultTableModel) table.getModel()).removeRow(row);
  156. }
  157. /**
  158. * Adds a row to the table.
  159. *
  160. * @param network The network setting
  161. * @param nickname The nickname setting
  162. * @param textcolour The textpane colour setting
  163. * @param nickcolour The nick list colour setting
  164. */
  165. void addRow(final String network, final String nickname,
  166. final String textcolour, final String nickcolour) {
  167. final DefaultTableModel model = ((DefaultTableModel) table.getModel());
  168. model.addRow(new Object[]{network, nickname, textcolour, nickcolour});
  169. }
  170. /**
  171. * Retrieves the current data in use by this panel.
  172. *
  173. * @return This panel's current data.
  174. */
  175. List<Object[]> getData() {
  176. final List<Object[]> res = new ArrayList<Object[]>();
  177. final DefaultTableModel model = ((DefaultTableModel) table.getModel());
  178. for (Object row : model.getDataVector()) {
  179. final Vector vrow = (Vector) row;
  180. res.add(new Object[]{vrow.elementAt(0), vrow.elementAt(1), vrow.
  181. elementAt(2), vrow.elementAt(3)});
  182. }
  183. return res;
  184. }
  185. /** {@inheritDoc} */
  186. @Override
  187. public void save() {
  188. // Remove all old config entries
  189. for (Object[] parts : plugin.getData()) {
  190. IdentityManager.getConfigIdentity().unsetOption(
  191. plugin.getDomain(), "color:" + parts[0] + ":" + parts[1]);
  192. }
  193. // And write the new ones
  194. for (Object[] row : getData()) {
  195. IdentityManager.getConfigIdentity().setOption(plugin.getDomain(),
  196. "color:" + row[0] + ":" + row[1], row[2] + ":" + row[3]);
  197. }
  198. }
  199. /** {@inheritDoc} */
  200. @Override
  201. public void valueChanged(ListSelectionEvent e) {
  202. boolean enable = table.getSelectedRow() > -1 && table.getModel().getRowCount() > 0;
  203. editButton.setEnabled(enable);
  204. deleteButton.setEnabled(enable);
  205. }
  206. }