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.

RelayChannelPanel.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) 2006-2013 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.relaybot;
  23. import com.dmdirc.config.IdentityManager;
  24. import com.dmdirc.config.prefs.PreferencesInterface;
  25. import com.dmdirc.plugins.Plugin;
  26. import java.awt.event.ActionEvent;
  27. import java.awt.event.ActionListener;
  28. import java.lang.reflect.InvocationTargetException;
  29. import java.lang.reflect.Method;
  30. import java.util.ArrayList;
  31. import java.util.List;
  32. import javax.swing.JButton;
  33. import javax.swing.JPanel;
  34. import javax.swing.JScrollPane;
  35. import javax.swing.JTable;
  36. import javax.swing.event.ListSelectionEvent;
  37. import javax.swing.event.ListSelectionListener;
  38. import javax.swing.table.DefaultTableModel;
  39. import net.miginfocom.swing.MigLayout;
  40. /**
  41. * Panel used for the relay bot channel and nickname settings in the plugin's
  42. * config dialog.
  43. */
  44. public class RelayChannelPanel extends JPanel implements ActionListener,
  45. PreferencesInterface, ListSelectionListener {
  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 RelayBotPlugin plugin;
  56. /** The table headings. */
  57. private static final String[] HEADERS = {"Channel", "Nickname", };
  58. /** Delete button. */
  59. private final JButton deleteButton;
  60. /**
  61. * Creates a new instance of NickColourPanel.
  62. *
  63. * @param controller The UI controller that owns this panel
  64. * @param plugin The plugin that owns this panel
  65. */
  66. public RelayChannelPanel(final Plugin controller,
  67. final RelayBotPlugin plugin) {
  68. super();
  69. this.plugin = plugin;
  70. final Object[][] data = plugin.getData();
  71. table = new JTable(new DefaultTableModel(data, HEADERS));
  72. final JScrollPane scrollPane = new JScrollPane(table);
  73. table.getSelectionModel().addListSelectionListener(this);
  74. table.setFillsViewportHeight(true);
  75. int height = 100;
  76. try {
  77. final Method getPrefsDialog = controller.getClass()
  78. .getDeclaredMethod("getPrefsDialog", (Class<?>) null);
  79. final Object prefsDialog = getPrefsDialog.invoke(controller,
  80. (Class<?>) null);
  81. final Method getPanelHeight = prefsDialog.getClass()
  82. .getDeclaredMethod("getPanelHeight", (Class<?>) null);
  83. final Object panelHeight = getPanelHeight.invoke(prefsDialog,
  84. (Class<?>) null);
  85. height = (Integer) panelHeight;
  86. } catch (IllegalAccessException ex) {
  87. height = 100;
  88. } catch (IllegalArgumentException ex) {
  89. height = 100;
  90. } catch (InvocationTargetException ex) {
  91. height = 100;
  92. } catch (NoSuchMethodException ex) {
  93. height = 100;
  94. } catch (SecurityException ex) {
  95. height = 100;
  96. }
  97. setLayout(new MigLayout("ins 0, fillx, hmax " + height));
  98. add(scrollPane, "grow, push, wrap, spanx");
  99. final JButton addButton = new JButton("Add");
  100. addButton.addActionListener(this);
  101. add(addButton, "sg button, growx, pushx");
  102. deleteButton = new JButton("Delete");
  103. deleteButton.addActionListener(this);
  104. add(deleteButton, "sg button, growx, pushx");
  105. deleteButton.setEnabled(false);
  106. }
  107. /**
  108. * {@inheritDoc}
  109. *
  110. * @param e Action event
  111. */
  112. @Override
  113. public void actionPerformed(final ActionEvent e) {
  114. final DefaultTableModel model = ((DefaultTableModel) table.getModel());
  115. if (e.getActionCommand().equals("Add")) {
  116. addRow("#changeme", "changeme");
  117. } else if (e.getActionCommand().equals("Delete")) {
  118. final int row = table.getSelectedRow();
  119. if (table.isEditing()) {
  120. table.getCellEditor().stopCellEditing();
  121. }
  122. if (row > -1) {
  123. model.removeRow(row);
  124. }
  125. }
  126. }
  127. /**
  128. * Removes a row from the table.
  129. *
  130. * @param row The row to be removed
  131. */
  132. public void removeRow(final int row) {
  133. ((DefaultTableModel) table.getModel()).removeRow(row);
  134. }
  135. /**
  136. * Adds a row to the table.
  137. *
  138. * @param channel The channel setting
  139. * @param nickname The nickname setting
  140. */
  141. public void addRow(final String channel, final String nickname) {
  142. final DefaultTableModel model = ((DefaultTableModel) table.getModel());
  143. model.addRow(new Object[]{channel, nickname, });
  144. }
  145. /**
  146. * Retrieves the current data in use by this panel.
  147. *
  148. * @return This panel's current data.
  149. */
  150. public List<String[]> getData() {
  151. final List<String[]> res = new ArrayList<String[]>();
  152. final DefaultTableModel model = ((DefaultTableModel) table.getModel());
  153. for (int i = 0; i < model.getRowCount(); i++) {
  154. res.add(new String[]{(String) model.getValueAt(i, 0),
  155. (String) model.getValueAt(i, 1), });
  156. }
  157. return res;
  158. }
  159. /** {@inheritDoc} */
  160. @Override
  161. public void save() {
  162. // Remove all old config entries
  163. for (String[] parts : plugin.getData()) {
  164. IdentityManager.getIdentityManager().getGlobalConfigIdentity()
  165. .unsetOption(plugin.getDomain(), parts[0]);
  166. }
  167. // And write the new ones
  168. for (String[] row : getData()) {
  169. IdentityManager.getIdentityManager().getGlobalConfigIdentity()
  170. .setOption(plugin.getDomain(), row[0], row[1]);
  171. }
  172. }
  173. /** {@inheritDoc} */
  174. @Override
  175. public void valueChanged(final ListSelectionEvent e) {
  176. deleteButton.setEnabled(table.getSelectedRow() > -1
  177. && table.getModel().getRowCount() > 0);
  178. }
  179. }