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.

ActionConditionDisplayPanel.java 8.9KB

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