Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

NickColourPanel.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (c) 2006-2008 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.ui.swing.UIUtilities;
  24. import java.awt.BorderLayout;
  25. import java.awt.Color;
  26. import java.awt.Dimension;
  27. import java.awt.event.ActionEvent;
  28. import java.awt.event.ActionListener;
  29. import java.util.ArrayList;
  30. import java.util.List;
  31. import java.util.Vector;
  32. import javax.swing.BorderFactory;
  33. import javax.swing.JButton;
  34. import javax.swing.JPanel;
  35. import javax.swing.JScrollPane;
  36. import javax.swing.JTable;
  37. import javax.swing.table.DefaultTableModel;
  38. import javax.swing.table.TableCellRenderer;
  39. /**
  40. * Panel used for the custom nick colour settings component in the plugin's
  41. * config dialog.
  42. *
  43. * @author Chris
  44. */
  45. public class NickColourPanel extends JPanel implements ActionListener {
  46. /**
  47. * A version number for this class. It should be changed whenever the class
  48. * structure is changed (or anything else that would prevent serialized
  49. * objects being unserialized with the new class).
  50. */
  51. private static final long serialVersionUID = 1;
  52. /** The table used for displaying the options. */
  53. private final JTable table;
  54. /** The plugin we're associated with. */
  55. private final transient NickColourPlugin plugin;
  56. /** The table headings. */
  57. private final String[] headers = {"Network", "Nickname", "Text colour", "Nicklist colour"};
  58. /**
  59. * Creates a new instance of NickColourPanel.
  60. *
  61. * @param plugin The plugin that owns this panel
  62. */
  63. public NickColourPanel(final NickColourPlugin plugin) {
  64. super();
  65. this.plugin = plugin;
  66. final Object[][] data = plugin.getData();
  67. table = new JTable(new DefaultTableModel(data, headers)) {
  68. /**
  69. * A version number for this class. It should be changed whenever the class
  70. * structure is changed (or anything else that would prevent serialized
  71. * objects being unserialized with the new class).
  72. */
  73. private static final long serialVersionUID = 1;
  74. /** The colour renderer we're using for colour cells. */
  75. private final ColourRenderer colourRenderer = new ColourRenderer();
  76. /** {@inheritDoc} */
  77. @Override
  78. public TableCellRenderer getCellRenderer(final int row, final int column) {
  79. if (column == 2 || column == 3) {
  80. return colourRenderer;
  81. } else {
  82. return super.getCellRenderer(row, column);
  83. }
  84. }
  85. /** {@inheritDoc} */
  86. @Override
  87. public boolean isCellEditable(final int row, final int column) {
  88. return false;
  89. }
  90. };
  91. final JScrollPane scrollPane = new JScrollPane(table);
  92. table.setFillsViewportHeight(true);
  93. table.setDefaultRenderer(Color.class, new ColourRenderer());
  94. setLayout(new BorderLayout());
  95. add(scrollPane, BorderLayout.CENTER);
  96. final JPanel buttonPanel = new JPanel();
  97. buttonPanel.setBorder(BorderFactory.createEmptyBorder(UIUtilities.LARGE_BORDER, 0, 0, 0));
  98. buttonPanel.setPreferredSize(new Dimension(Short.MAX_VALUE, 25 + UIUtilities.LARGE_BORDER));
  99. buttonPanel.setLayout(new BorderLayout());
  100. JButton button;
  101. button = new JButton("Add");
  102. button.setPreferredSize(new Dimension(100, 20));
  103. button.setMaximumSize(new Dimension(100, 20));
  104. button.addActionListener(this);
  105. buttonPanel.add(button, BorderLayout.WEST);
  106. button = new JButton("Edit");
  107. button.setPreferredSize(new Dimension(100, 20));
  108. button.setMaximumSize(new Dimension(100, 20));
  109. button.addActionListener(this);
  110. buttonPanel.add(button, BorderLayout.CENTER);
  111. button = new JButton("Delete");
  112. button.setPreferredSize(new Dimension(100, 20));
  113. button.setMaximumSize(new Dimension(100, 20));
  114. button.addActionListener(this);
  115. buttonPanel.add(button, BorderLayout.EAST);
  116. add(buttonPanel, BorderLayout.SOUTH);
  117. }
  118. /** {@inheritDoc} */
  119. public void actionPerformed(final ActionEvent e) {
  120. final DefaultTableModel model = ((DefaultTableModel) table.getModel());
  121. if (e.getActionCommand().equals("Add")) {
  122. new NickColourInputDialog(this);
  123. } else if (e.getActionCommand().equals("Edit")) {
  124. final int row = table.getSelectedRow();
  125. final String network = (String) model.getValueAt(row, 0);
  126. final String nickname = (String) model.getValueAt(row, 1);
  127. String textcolour = (String) model.getValueAt(row, 2);
  128. String nickcolour = (String) model.getValueAt(row, 3);
  129. if (textcolour == null) {
  130. textcolour = "";
  131. }
  132. if (nickcolour == null) {
  133. nickcolour = "";
  134. }
  135. new NickColourInputDialog(this, row, nickname, network, textcolour, nickcolour);
  136. } else if (e.getActionCommand().equals("Delete")) {
  137. final int row = table.getSelectedRow();
  138. if (row > -1) {
  139. model.removeRow(row);
  140. }
  141. }
  142. }
  143. /**
  144. * Removes a row from the table.
  145. *
  146. * @param row The row to be removed
  147. */
  148. void removeRow(final int row) {
  149. ((DefaultTableModel) table.getModel()).removeRow(row);
  150. }
  151. /**
  152. * Adds a row to the table.
  153. *
  154. * @param network The network setting
  155. * @param nickname The nickname setting
  156. * @param textcolour The textpane colour setting
  157. * @param nickcolour The nick list colour setting
  158. */
  159. void addRow(final String network, final String nickname,
  160. final String textcolour, final String nickcolour) {
  161. final DefaultTableModel model = ((DefaultTableModel) table.getModel());
  162. model.addRow(new Object[]{network, nickname, textcolour, nickcolour});
  163. }
  164. /**
  165. * Retrieves the current data in use by this panel.
  166. *
  167. * @return This panel's current data.
  168. */
  169. List<Object[]> getData() {
  170. final List<Object[]> res = new ArrayList<Object[]>();
  171. final DefaultTableModel model = ((DefaultTableModel) table.getModel());
  172. for (Object row : model.getDataVector()) {
  173. final Vector vrow = (Vector) row;
  174. res.add(new Object[]{vrow.elementAt(0), vrow.elementAt(1), vrow.elementAt(2), vrow.elementAt(3)});
  175. }
  176. return res;
  177. }
  178. }