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.

IgnoreListPanel.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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.ui_swing.dialogs.serversetting;
  23. import com.dmdirc.addons.ui_swing.UIUtilities;
  24. import com.dmdirc.addons.ui_swing.dialogs.StandardInputDialog;
  25. import com.dmdirc.addons.ui_swing.dialogs.StandardQuestionDialog;
  26. import com.dmdirc.interfaces.Connection;
  27. import com.dmdirc.parser.common.IgnoreList;
  28. import com.dmdirc.ui.IconManager;
  29. import com.dmdirc.util.validators.NotEmptyValidator;
  30. import com.dmdirc.util.validators.RegexValidator;
  31. import com.dmdirc.util.validators.ValidatorChain;
  32. import com.dmdirc.util.validators.ValidatorChainBuilder;
  33. import java.awt.Dialog.ModalityType;
  34. import java.awt.Window;
  35. import java.awt.event.ActionEvent;
  36. import java.awt.event.ActionListener;
  37. import javax.swing.JButton;
  38. import javax.swing.JCheckBox;
  39. import javax.swing.JLabel;
  40. import javax.swing.JList;
  41. import javax.swing.JPanel;
  42. import javax.swing.JScrollPane;
  43. import javax.swing.ListSelectionModel;
  44. import javax.swing.event.ListSelectionEvent;
  45. import javax.swing.event.ListSelectionListener;
  46. import net.miginfocom.swing.MigLayout;
  47. /**
  48. * Ignore list panel.
  49. */
  50. public final class IgnoreListPanel extends JPanel implements ActionListener, ListSelectionListener {
  51. /** Serial version UID. */
  52. private static final long serialVersionUID = 2;
  53. /** Parent connection. */
  54. private final Connection connection;
  55. /** Parent window. */
  56. private final Window parentWindow;
  57. /** Icon manager. */
  58. private final IconManager iconManager;
  59. /** Add button. */
  60. private JButton addButton;
  61. /** Remove button. */
  62. private JButton delButton;
  63. /** View toggle. */
  64. private JCheckBox viewToggle;
  65. /** Size label. */
  66. private JLabel sizeLabel;
  67. /** Ignore list. */
  68. private JList<String> list;
  69. /** Cached ignore list. */
  70. private IgnoreList cachedIgnoreList;
  71. /** Ignore list model . */
  72. private IgnoreListModel listModel;
  73. /**
  74. * Creates a new instance of IgnoreList.
  75. *
  76. * @param iconManager Icon manager
  77. * @param connection The connection whose ignore list should be displayed.
  78. * @param parentWindow Parent window
  79. */
  80. public IgnoreListPanel(final IconManager iconManager,
  81. final Connection connection, final Window parentWindow) {
  82. this.iconManager = iconManager;
  83. this.connection = connection;
  84. this.parentWindow = parentWindow;
  85. setOpaque(UIUtilities.getTabbedPaneOpaque());
  86. initComponents();
  87. addListeners();
  88. populateList();
  89. }
  90. /** Initialises the components. */
  91. private void initComponents() {
  92. cachedIgnoreList = new IgnoreList(connection.getIgnoreList().getRegexList());
  93. listModel = new IgnoreListModel(cachedIgnoreList);
  94. list = new JList<>(listModel);
  95. final JScrollPane scrollPane = new JScrollPane(list);
  96. list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  97. addButton = new JButton("Add");
  98. delButton = new JButton("Remove");
  99. sizeLabel = new JLabel("0 entries");
  100. viewToggle = new JCheckBox("Use advanced expressions");
  101. viewToggle.setOpaque(UIUtilities.getTabbedPaneOpaque());
  102. viewToggle.setSelected(!cachedIgnoreList.canConvert());
  103. viewToggle.setEnabled(cachedIgnoreList.canConvert());
  104. setLayout(new MigLayout("fill, wrap 1"));
  105. add(scrollPane, "grow, push");
  106. add(sizeLabel, "split 2, pushx, growx");
  107. add(viewToggle, "alignx center");
  108. add(addButton, "split 2, width 50%");
  109. add(delButton, "width 50%");
  110. }
  111. /** Updates the size label. */
  112. private void updateSizeLabel() {
  113. sizeLabel.setText(cachedIgnoreList.count() + " entr" + (cachedIgnoreList.count()
  114. == 1 ? "y" : "ies"));
  115. }
  116. /** Adds listeners to the components. */
  117. private void addListeners() {
  118. addButton.addActionListener(this);
  119. delButton.addActionListener(this);
  120. viewToggle.addActionListener(this);
  121. list.getSelectionModel().addListSelectionListener(this);
  122. }
  123. /** Populates the ignore list. */
  124. private void populateList() {
  125. if (list.getSelectedIndex() == -1) {
  126. delButton.setEnabled(false);
  127. }
  128. updateSizeLabel();
  129. }
  130. /** Updates the list. */
  131. private void updateList() {
  132. listModel.notifyUpdated();
  133. if (cachedIgnoreList.canConvert()) {
  134. viewToggle.setEnabled(true);
  135. } else {
  136. viewToggle.setEnabled(false);
  137. viewToggle.setSelected(true);
  138. }
  139. }
  140. /** Saves the ignore list. */
  141. public void saveList() {
  142. connection.getIgnoreList().clear();
  143. connection.getIgnoreList().addAll(cachedIgnoreList.getRegexList());
  144. connection.saveIgnoreList();
  145. }
  146. @Override
  147. public void actionPerformed(final ActionEvent e) {
  148. if (e.getSource() == addButton) {
  149. final ValidatorChainBuilder<String> validatorBuilder = ValidatorChain.builder();
  150. validatorBuilder.addValidator(new NotEmptyValidator());
  151. if (viewToggle.isSelected()) {
  152. validatorBuilder.addValidator(new RegexValidator());
  153. }
  154. new StandardInputDialog(parentWindow,
  155. ModalityType.MODELESS, iconManager, "New ignore list entry",
  156. "Please enter the new ignore list entry", validatorBuilder.build()) {
  157. /** A version number for this class. */
  158. private static final long serialVersionUID = 2;
  159. @Override
  160. public boolean save() {
  161. if (viewToggle.isSelected()) {
  162. cachedIgnoreList.add(getText());
  163. } else {
  164. cachedIgnoreList.addSimple(getText());
  165. }
  166. updateList();
  167. return true;
  168. }
  169. @Override
  170. public void cancelled() {
  171. //Ignore
  172. }
  173. }.display();
  174. } else if (e.getSource() == delButton && list.getSelectedIndex() != -1) {
  175. new StandardQuestionDialog(parentWindow,
  176. ModalityType.APPLICATION_MODAL,
  177. "Confirm deletion",
  178. "Are you sure you want to delete this item?",
  179. () -> {
  180. cachedIgnoreList.remove(list.getSelectedIndex());
  181. updateList();
  182. }).display();
  183. } else if (e.getSource() == viewToggle) {
  184. listModel.setIsSimple(!viewToggle.isSelected());
  185. }
  186. }
  187. @Override
  188. public void valueChanged(final ListSelectionEvent e) {
  189. if (list.getSelectedIndex() == -1) {
  190. delButton.setEnabled(false);
  191. } else {
  192. delButton.setEnabled(true);
  193. }
  194. }
  195. }