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.

ActionResponsePanel.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.actioneditor;
  23. import com.dmdirc.addons.ui_swing.UIUtilities;
  24. import com.dmdirc.addons.ui_swing.components.inputfields.TextAreaInputField;
  25. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  26. import com.dmdirc.ui.IconManager;
  27. import com.dmdirc.ui.messages.ColourManagerFactory;
  28. import java.util.Collection;
  29. import java.util.TreeSet;
  30. import javax.swing.BorderFactory;
  31. import javax.swing.DefaultComboBoxModel;
  32. import javax.swing.JComboBox;
  33. import javax.swing.JLabel;
  34. import javax.swing.JPanel;
  35. import javax.swing.JScrollPane;
  36. import javax.swing.JTextArea;
  37. import javax.swing.MutableComboBoxModel;
  38. import javax.swing.UIManager;
  39. import net.miginfocom.swing.MigLayout;
  40. /**
  41. * Action response panel.
  42. */
  43. public class ActionResponsePanel extends JPanel {
  44. /** Serial version UID. */
  45. private static final long serialVersionUID = 1;
  46. /** Response text area. */
  47. private JTextArea response;
  48. /** Formatter combo box. */
  49. private JComboBox<String> formatter;
  50. /** Response scrollpane. */
  51. private JScrollPane scrollPane;
  52. /**
  53. * Instantiates the panel.
  54. *
  55. * @param iconManager Icon manager
  56. * @param config Config
  57. */
  58. public ActionResponsePanel(final IconManager iconManager,
  59. final ColourManagerFactory colourManagerFactory, final AggregateConfigProvider config) {
  60. initComponents(iconManager, colourManagerFactory, config);
  61. addListeners();
  62. layoutComponents();
  63. }
  64. private void initComponents(final IconManager iconManager,
  65. final ColourManagerFactory colourManagerFactory, final AggregateConfigProvider config) {
  66. response = new TextAreaInputField(iconManager, colourManagerFactory, config, "");
  67. scrollPane = new JScrollPane(response);
  68. response.setRows(4);
  69. formatter = new JComboBox<>(new DefaultComboBoxModel<String>());
  70. final MutableComboBoxModel<String> model =
  71. (MutableComboBoxModel<String>) formatter.getModel();
  72. model.addElement("No change");
  73. model.addElement("No response");
  74. final Collection<String> formatters = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
  75. formatters.addAll(config.getOptions("formatter").keySet());
  76. for (final String format : formatters) {
  77. model.addElement(format);
  78. }
  79. }
  80. /** Adds the listeners. */
  81. private void addListeners() {
  82. }
  83. /** Lays out the components. */
  84. private void layoutComponents() {
  85. setBorder(BorderFactory.createTitledBorder(UIManager.getBorder(
  86. "TitledBorder.border"), "Response"));
  87. setLayout(new MigLayout("fill, wrap 1"));
  88. add(new JLabel("Execute these commands: "));
  89. add(scrollPane, "grow, push");
  90. add(new JLabel("Alter the event's formatter"));
  91. add(formatter, "growx, pushx");
  92. }
  93. /**
  94. * Sets the response.
  95. *
  96. * @param response new response
  97. */
  98. public void setResponse(final String... response) {
  99. final StringBuilder sb = new StringBuilder();
  100. for (String responseLine : response) {
  101. responseLine = responseLine.replace("\n", "\\n");
  102. sb.append(responseLine).append('\n');
  103. }
  104. if (sb.length() > 0) {
  105. this.response.setText(sb.substring(0, sb.length() - 1));
  106. }
  107. UIUtilities.resetScrollPane(scrollPane);
  108. }
  109. /**
  110. * Sets the new formatter for the response panel.
  111. *
  112. * @param newFormat new formatter.
  113. */
  114. public void setFormatter(final String newFormat) {
  115. if (newFormat == null) {
  116. formatter.setSelectedIndex(0);
  117. } else if (newFormat.isEmpty()) {
  118. formatter.setSelectedIndex(1);
  119. } else {
  120. formatter.setSelectedItem(newFormat);
  121. }
  122. }
  123. /**
  124. * Returns the current response.
  125. *
  126. * @return Response text
  127. */
  128. public String[] getResponse() {
  129. final String[] text = response.getText().split("\n");
  130. for (int i = 0; i < text.length; i++) {
  131. text[i] = text[i].replace("\\n", "\n");
  132. }
  133. return text;
  134. }
  135. /**
  136. * Returns the current formatter.
  137. *
  138. * @return Formatter text
  139. */
  140. public String getFormatter() {
  141. final String newFormat = (String) formatter.getSelectedItem();
  142. switch (newFormat) {
  143. case "No change":
  144. return null;
  145. case "No response":
  146. return "";
  147. default:
  148. return newFormat;
  149. }
  150. }
  151. @Override
  152. public void setEnabled(final boolean enabled) {
  153. response.setEnabled(enabled);
  154. formatter.setEnabled(enabled);
  155. }
  156. }