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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.ui_swing.dialogs.serversetting;
  18. import com.dmdirc.addons.ui_swing.UIUtilities;
  19. import com.dmdirc.addons.ui_swing.dialogs.StandardInputDialog;
  20. import com.dmdirc.addons.ui_swing.dialogs.StandardQuestionDialog;
  21. import com.dmdirc.interfaces.Connection;
  22. import com.dmdirc.parser.common.IgnoreList;
  23. import com.dmdirc.addons.ui_swing.components.IconManager;
  24. import com.dmdirc.util.validators.NotEmptyValidator;
  25. import com.dmdirc.util.validators.RegexValidator;
  26. import com.dmdirc.util.validators.ValidatorChain;
  27. import com.dmdirc.util.validators.ValidatorChainBuilder;
  28. import java.awt.Dialog.ModalityType;
  29. import java.awt.Window;
  30. import java.awt.event.ActionEvent;
  31. import java.awt.event.ActionListener;
  32. import javax.swing.JButton;
  33. import javax.swing.JCheckBox;
  34. import javax.swing.JLabel;
  35. import javax.swing.JList;
  36. import javax.swing.JPanel;
  37. import javax.swing.JScrollPane;
  38. import javax.swing.ListSelectionModel;
  39. import javax.swing.event.ListSelectionEvent;
  40. import javax.swing.event.ListSelectionListener;
  41. import net.miginfocom.swing.MigLayout;
  42. /**
  43. * Ignore list panel.
  44. */
  45. public final class IgnoreListPanel extends JPanel implements ActionListener, ListSelectionListener {
  46. /** Serial version UID. */
  47. private static final long serialVersionUID = 2;
  48. /** Parent connection. */
  49. private final Connection connection;
  50. /** Parent window. */
  51. private final Window parentWindow;
  52. /** Icon manager. */
  53. private final IconManager iconManager;
  54. /** Add button. */
  55. private JButton addButton;
  56. /** Remove button. */
  57. private JButton delButton;
  58. /** View toggle. */
  59. private JCheckBox viewToggle;
  60. /** Size label. */
  61. private JLabel sizeLabel;
  62. /** Ignore list. */
  63. private JList<String> list;
  64. /** Cached ignore list. */
  65. private IgnoreList cachedIgnoreList;
  66. /** Ignore list model . */
  67. private IgnoreListModel listModel;
  68. /**
  69. * Creates a new instance of IgnoreList.
  70. *
  71. * @param iconManager Icon manager
  72. * @param connection The connection whose ignore list should be displayed.
  73. * @param parentWindow Parent window
  74. */
  75. public IgnoreListPanel(final IconManager iconManager,
  76. final Connection connection, final Window parentWindow) {
  77. this.iconManager = iconManager;
  78. this.connection = connection;
  79. this.parentWindow = parentWindow;
  80. setOpaque(UIUtilities.getTabbedPaneOpaque());
  81. initComponents();
  82. addListeners();
  83. populateList();
  84. }
  85. /** Initialises the components. */
  86. private void initComponents() {
  87. cachedIgnoreList = new IgnoreList(connection.getIgnoreList().getRegexList());
  88. listModel = new IgnoreListModel(cachedIgnoreList);
  89. list = new JList<>(listModel);
  90. final JScrollPane scrollPane = new JScrollPane(list);
  91. list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  92. addButton = new JButton("Add");
  93. delButton = new JButton("Remove");
  94. sizeLabel = new JLabel("0 entries");
  95. viewToggle = new JCheckBox("Use advanced expressions");
  96. viewToggle.setOpaque(UIUtilities.getTabbedPaneOpaque());
  97. viewToggle.setSelected(!cachedIgnoreList.canConvert());
  98. viewToggle.setEnabled(cachedIgnoreList.canConvert());
  99. setLayout(new MigLayout("fill, wrap 1"));
  100. add(scrollPane, "grow, push");
  101. add(sizeLabel, "split 2, pushx, growx");
  102. add(viewToggle, "alignx center");
  103. add(addButton, "split 2, width 50%");
  104. add(delButton, "width 50%");
  105. }
  106. /** Updates the size label. */
  107. private void updateSizeLabel() {
  108. sizeLabel.setText(cachedIgnoreList.count() + " entr" + (cachedIgnoreList.count()
  109. == 1 ? "y" : "ies"));
  110. }
  111. /** Adds listeners to the components. */
  112. private void addListeners() {
  113. addButton.addActionListener(this);
  114. delButton.addActionListener(this);
  115. viewToggle.addActionListener(this);
  116. list.getSelectionModel().addListSelectionListener(this);
  117. }
  118. /** Populates the ignore list. */
  119. private void populateList() {
  120. if (list.getSelectedIndex() == -1) {
  121. delButton.setEnabled(false);
  122. }
  123. updateSizeLabel();
  124. }
  125. /** Updates the list. */
  126. private void updateList() {
  127. listModel.notifyUpdated();
  128. if (cachedIgnoreList.canConvert()) {
  129. viewToggle.setEnabled(true);
  130. } else {
  131. viewToggle.setEnabled(false);
  132. viewToggle.setSelected(true);
  133. }
  134. }
  135. /** Saves the ignore list. */
  136. public void saveList() {
  137. connection.getIgnoreList().clear();
  138. connection.getIgnoreList().addAll(cachedIgnoreList.getRegexList());
  139. connection.saveIgnoreList();
  140. }
  141. @Override
  142. public void actionPerformed(final ActionEvent e) {
  143. if (e.getSource() == addButton) {
  144. final ValidatorChainBuilder<String> validatorBuilder = ValidatorChain.builder();
  145. validatorBuilder.addValidator(new NotEmptyValidator());
  146. if (viewToggle.isSelected()) {
  147. validatorBuilder.addValidator(new RegexValidator());
  148. }
  149. new StandardInputDialog(parentWindow,
  150. ModalityType.MODELESS, iconManager, "New ignore list entry",
  151. "Please enter the new ignore list entry", validatorBuilder.build(),
  152. this::addNewIgnoreEntry).display();
  153. } else if (e.getSource() == delButton && list.getSelectedIndex() != -1) {
  154. new StandardQuestionDialog(parentWindow,
  155. ModalityType.APPLICATION_MODAL,
  156. "Confirm deletion",
  157. "Are you sure you want to delete this item?",
  158. () -> {
  159. cachedIgnoreList.remove(list.getSelectedIndex());
  160. updateList();
  161. }).display();
  162. } else if (e.getSource() == viewToggle) {
  163. listModel.setIsSimple(!viewToggle.isSelected());
  164. }
  165. }
  166. private void addNewIgnoreEntry(final String text) {
  167. if (viewToggle.isSelected()) {
  168. cachedIgnoreList.add(text);
  169. } else {
  170. cachedIgnoreList.addSimple(text);
  171. }
  172. updateList();
  173. }
  174. @Override
  175. public void valueChanged(final ListSelectionEvent e) {
  176. if (list.getSelectedIndex() == -1) {
  177. delButton.setEnabled(false);
  178. } else {
  179. delButton.setEnabled(true);
  180. }
  181. }
  182. }