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.

PerformPanel.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.components.performpanel;
  18. import com.dmdirc.addons.ui_swing.components.inputfields.TextAreaInputField;
  19. import com.dmdirc.commandparser.auto.AutoCommand;
  20. import com.dmdirc.commandparser.auto.AutoCommandManager;
  21. import com.dmdirc.config.provider.AggregateConfigProvider;
  22. import com.dmdirc.addons.ui_swing.components.IconManager;
  23. import com.dmdirc.ui.messages.ColourManagerFactory;
  24. import java.util.Collection;
  25. import java.util.Collections;
  26. import java.util.HashMap;
  27. import java.util.Map;
  28. import javax.swing.JPanel;
  29. import javax.swing.JScrollPane;
  30. import javax.swing.JTextArea;
  31. import net.miginfocom.swing.MigLayout;
  32. /**
  33. * Creates a text area that fills whatever space it has available. This panel facilitates
  34. * modification of performs.
  35. *
  36. * @since 0.6.4
  37. */
  38. public class PerformPanel extends JPanel {
  39. /** Serial version UID. */
  40. private static final long serialVersionUID = 1;
  41. /** Text area that the perform is displayed in. */
  42. private final JTextArea performSpace;
  43. /** Perform wrapper to read/write performs to. */
  44. private final AutoCommandManager autoCommandManager;
  45. /** Map of original auto commands to their modified forms. */
  46. private final Map<AutoCommand, AutoCommand> autoCommands = new HashMap<>();
  47. /** The auto command that is displayed in the text area. */
  48. private AutoCommand visiblePerform;
  49. /**
  50. * Creates a new instance of PerformPanel that has no knowledge of any performs at the time of
  51. * creation.
  52. *
  53. * By default this panel displays a blank text area.
  54. *
  55. * @param iconManager Icon manager
  56. * @param config Config to read settings from
  57. * @param autoCommandManager Perform wrapper to read/write performs to.
  58. */
  59. public PerformPanel(
  60. final IconManager iconManager,
  61. final ColourManagerFactory colourManagerFactory,
  62. final AggregateConfigProvider config,
  63. final AutoCommandManager autoCommandManager) {
  64. this(iconManager, colourManagerFactory, config, autoCommandManager,
  65. Collections.<AutoCommand>emptyList());
  66. }
  67. /**
  68. * Creates a new instance of PerformPanel and prepares the list of PerformDescriptions passed to
  69. * it for viewing/modification.
  70. *
  71. * By default this panel displays a blank text area.
  72. *
  73. * @param iconManager Icon manager
  74. * @param config Config to read settings from
  75. * @param autoCommandManager Perform wrapper to read/write performs to.
  76. * @param performs Collection of PerformDescriptions to initialise
  77. */
  78. public PerformPanel(
  79. final IconManager iconManager,
  80. final ColourManagerFactory colourManagerFactory,
  81. final AggregateConfigProvider config,
  82. final AutoCommandManager autoCommandManager,
  83. final Collection<AutoCommand> performs) {
  84. this.autoCommandManager = autoCommandManager;
  85. performs.forEach(this::addCommand);
  86. setLayout(new MigLayout("ins 0, fill"));
  87. performSpace = new TextAreaInputField(iconManager, colourManagerFactory, config, "");
  88. add(new JScrollPane(performSpace), "grow, push");
  89. }
  90. /**
  91. * This will add a command to the internal cache to track changes.
  92. *
  93. * @param command Auto command to add
  94. */
  95. public void addCommand(final AutoCommand command) {
  96. autoCommands.putIfAbsent(command, command);
  97. }
  98. /**
  99. * Saves modifications to the provided performs.
  100. */
  101. public void savePerform() {
  102. if (visiblePerform != null) {
  103. autoCommands.put(visiblePerform,
  104. createAutoCommand(visiblePerform, performSpace.getText()));
  105. }
  106. autoCommands.entrySet().parallelStream()
  107. .filter(e -> !e.getKey().equals(e.getValue()))
  108. .forEach(e -> autoCommandManager.replaceAutoCommand(e.getKey(), e.getValue()));
  109. }
  110. /**
  111. * Displays the specified perform to the user. Edits made to any previously displayed perform
  112. * are stored, but are not saved until {@link #savePerform()} is called. If the specified
  113. * perform is not in this panel's cache, it will be added.
  114. *
  115. * @param perform Perform to display in the text area
  116. */
  117. public void switchPerform(final AutoCommand perform) {
  118. if (perform != null && !autoCommands.containsKey(perform)) {
  119. addCommand(perform);
  120. }
  121. if (visiblePerform != null) {
  122. autoCommands.put(visiblePerform,
  123. createAutoCommand(visiblePerform, performSpace.getText()));
  124. }
  125. if (perform == null) {
  126. performSpace.setText("");
  127. } else {
  128. performSpace.setText(perform.getResponse());
  129. }
  130. performSpace.setEnabled(perform != null);
  131. visiblePerform = perform;
  132. }
  133. /**
  134. * Creates a new auto command based on the existing one, but with a different response.
  135. *
  136. * @param existing The existing auto command to model on.
  137. * @param text The new text to use for the response.
  138. * @return A new AutoCommand with the same target as the existing one, and the given text.
  139. */
  140. private static AutoCommand createAutoCommand(final AutoCommand existing, final String text) {
  141. return AutoCommand.create(existing.getServer(), existing.getNetwork(),
  142. existing.getProfile(), text);
  143. }
  144. }