Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ActionConditionDisplayPanel.java 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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.actions.ActionCondition;
  24. import com.dmdirc.addons.ui_swing.components.ImageButton;
  25. import com.dmdirc.addons.ui_swing.components.ImageToggleButton;
  26. import com.dmdirc.addons.ui_swing.components.text.TextLabel;
  27. import com.dmdirc.interfaces.actions.ActionType;
  28. import com.dmdirc.ui.IconManager;
  29. import com.dmdirc.util.collections.ListenerList;
  30. import java.awt.event.ActionEvent;
  31. import java.awt.event.ActionListener;
  32. import java.beans.PropertyChangeEvent;
  33. import java.beans.PropertyChangeListener;
  34. import javax.swing.JPanel;
  35. import javax.swing.JToggleButton;
  36. import net.miginfocom.swing.MigLayout;
  37. /**
  38. * Action condition display panel.
  39. */
  40. public class ActionConditionDisplayPanel extends JPanel implements
  41. ActionListener, PropertyChangeListener {
  42. /** Serial version UID. */
  43. private static final long serialVersionUID = 1;
  44. /** Info label. */
  45. private TextLabel label;
  46. /** Edit button. */
  47. private JToggleButton editButton;
  48. /** Delete button. */
  49. private ImageButton<Object> deleteButton;
  50. /** Edit panel. */
  51. private ActionConditionEditorPanel editPanel;
  52. /** Listeners. */
  53. private ListenerList listeners;
  54. /** Action condition. */
  55. private ActionCondition condition;
  56. /** Action trigger. */
  57. private ActionType trigger;
  58. /**
  59. * Instantiates the panel.
  60. *
  61. * @param iconManager Icon Manager
  62. * @param condition Action condition
  63. * @param trigger Action trigger
  64. */
  65. public ActionConditionDisplayPanel(final IconManager iconManager,
  66. final ActionCondition condition, final ActionType trigger) {
  67. this.trigger = trigger;
  68. this.condition = new ActionCondition(condition.getArg(),
  69. condition.getComponent(), condition.getComparison(),
  70. condition.getTarget());
  71. initComponents(iconManager);
  72. addListeners();
  73. layoutComponents();
  74. setTrigger(trigger);
  75. validate();
  76. layoutComponents();
  77. if (condition.getArg() == -1 && condition.getComponent() == null && condition.
  78. getComparison() == null
  79. && condition.getTarget().isEmpty()) {
  80. editPanel.setVisible(true);
  81. editButton.setSelected(true);
  82. }
  83. }
  84. /**
  85. * Sets the action trigger.
  86. *
  87. * @param trigger new trigger
  88. */
  89. void setTrigger(final ActionType trigger) {
  90. this.trigger = trigger;
  91. editPanel.setTrigger(trigger);
  92. editPanel.setVisible(trigger == null);
  93. editButton.setSelected(trigger == null);
  94. label.setText(updateSentence());
  95. }
  96. /**
  97. * Sets the action condition.
  98. *
  99. * @param condition new condition
  100. */
  101. protected void setCondition(final ActionCondition condition) {
  102. this.condition = condition;
  103. label.setText(updateSentence());
  104. }
  105. /**
  106. * Initialises the components.
  107. *
  108. * @param iconManager Icon Manager
  109. */
  110. private void initComponents(final IconManager iconManager) {
  111. label = new TextLabel("", false);
  112. editButton = new ImageToggleButton("edit", iconManager.
  113. getIcon("edit-inactive"), iconManager.getIcon("edit"));
  114. deleteButton = new ImageButton<>("delete", iconManager.
  115. getIcon("close-inactive"), iconManager.
  116. getIcon("close-inactive"), iconManager.getIcon("close-active"));
  117. editPanel = new ActionConditionEditorPanel(condition, trigger);
  118. listeners = new ListenerList();
  119. editPanel.setVisible(trigger == null);
  120. editButton.setSelected(trigger == null);
  121. }
  122. /** Adds the listeners. */
  123. private void addListeners() {
  124. editButton.addActionListener(this);
  125. deleteButton.addActionListener(this);
  126. editPanel.addPropertyChangeListener("edit", this);
  127. editPanel.addPropertyChangeListener("validationResult", this);
  128. }
  129. /** Lays out the components. */
  130. private void layoutComponents() {
  131. setLayout(new MigLayout("ins 0, fillx, hidemode 3, pack, wmax 90%"));
  132. add(label, "grow, push, wmax 85%");
  133. add(editButton, "right");
  134. add(deleteButton, "right, wrap");
  135. add(editPanel, "alignx right");
  136. }
  137. @Override
  138. public void actionPerformed(final ActionEvent e) {
  139. if (e.getSource().equals(deleteButton)) {
  140. fireConditionRemoved(this);
  141. } else if (e.getSource().equals(editButton)) {
  142. editPanel.setVisible(editButton.getModel().isSelected());
  143. }
  144. }
  145. /**
  146. * Adds an ActionConditionRemovalListener to the listener list.
  147. *
  148. * @param listener Listener to add
  149. */
  150. public void addConditionListener(
  151. final ActionConditionRemovalListener listener) {
  152. if (listener == null) {
  153. return;
  154. }
  155. listeners.add(ActionConditionRemovalListener.class, listener);
  156. }
  157. /**
  158. * Removes an ActionConditionRemovalListener from the listener list.
  159. *
  160. * @param listener Listener to remove
  161. */
  162. public void removeConditionListener(
  163. final ActionConditionRemovalListener listener) {
  164. listeners.remove(ActionConditionRemovalListener.class, listener);
  165. }
  166. /**
  167. * Fired when the an action condition is removed.
  168. *
  169. * @param condition Removed condition
  170. */
  171. protected void fireConditionRemoved(
  172. final ActionConditionDisplayPanel condition) {
  173. for (final ActionConditionRemovalListener listener : listeners
  174. .get(ActionConditionRemovalListener.class)) {
  175. listener.conditionRemoved(condition);
  176. }
  177. }
  178. @Override
  179. public void setEnabled(final boolean enabled) {
  180. editPanel.setEnabled(enabled);
  181. editButton.setEnabled(enabled);
  182. deleteButton.setEnabled(enabled);
  183. }
  184. /**
  185. * Updates the condition sentence.
  186. *
  187. * @return Updated sentence
  188. */
  189. private String updateSentence() {
  190. if (trigger == null) {
  191. return "...";
  192. } else {
  193. final StringBuilder sb = new StringBuilder("The ");
  194. if (condition.getArg() == -1) {
  195. sb.append(" ...");
  196. return sb.toString();
  197. }
  198. sb.append(trigger.getType().getArgNames()[condition.getArg()]);
  199. sb.append("'s ");
  200. if (condition.getComponent() == null) {
  201. sb.append(" ...");
  202. return sb.toString();
  203. }
  204. sb.append(condition.getComponent().getName());
  205. sb.append(' ');
  206. if (condition.getComparison() == null) {
  207. sb.append(" ...");
  208. return sb.toString();
  209. }
  210. sb.append(condition.getComparison().getName());
  211. sb.append(" '");
  212. if (condition.getTarget() == null) {
  213. sb.append(" ...");
  214. return sb.toString();
  215. }
  216. sb.append(condition.getTarget().replace("<", "&lt;"));
  217. sb.append('\'');
  218. return sb.toString();
  219. }
  220. }
  221. /**
  222. * Returns the action condition represented by this panel.
  223. *
  224. * @return Action condition
  225. */
  226. public ActionCondition getCondition() {
  227. return condition;
  228. }
  229. @Override
  230. public void propertyChange(final PropertyChangeEvent evt) {
  231. if ("edit".equals(evt.getPropertyName())) {
  232. label.setText(updateSentence());
  233. } else {
  234. firePropertyChange("validationResult", evt.getOldValue(),
  235. evt.getNewValue());
  236. }
  237. }
  238. /**
  239. * Checks if this editor panel has errored.
  240. *
  241. * @return true iif the content it valid
  242. */
  243. public boolean checkError() {
  244. return editPanel.checkError();
  245. }
  246. }