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

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