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.

ActionsEditorDialog.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright (c) 2006-2007 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.ui.dialogs.actionseditor;
  23. import java.awt.BorderLayout;
  24. import java.awt.Dimension;
  25. import java.awt.event.ActionEvent;
  26. import java.awt.event.ActionListener;
  27. import javax.swing.BorderFactory;
  28. import javax.swing.Box;
  29. import javax.swing.BoxLayout;
  30. import javax.swing.JButton;
  31. import javax.swing.JPanel;
  32. import javax.swing.JTabbedPane;
  33. import com.dmdirc.actions.Action;
  34. import com.dmdirc.actions.ActionType;
  35. import com.dmdirc.ui.MainFrame;
  36. import com.dmdirc.ui.components.StandardDialog;
  37. import com.dmdirc.ui.dialogs.ActionsManagerDialog;
  38. import static com.dmdirc.ui.UIUtilities.SMALL_BORDER;
  39. /**
  40. * Actions editor dialog, used for adding and creating new actions.
  41. */
  42. public final class ActionsEditorDialog extends StandardDialog implements
  43. ActionListener {
  44. /**
  45. * A version number for this class. It should be changed whenever the class
  46. * structure is changed (or anything else that would prevent serialized
  47. * objects being unserialized with the new class).
  48. */
  49. private static final long serialVersionUID = 2;
  50. /** Previously created instance of ActionsEditorDialog. */
  51. private static ActionsEditorDialog me;
  52. /** Parent dialog, informed of changes on close. */
  53. private ActionsManagerDialog parent;
  54. /** Action being edited or null. */
  55. private Action action;
  56. /** Tabbed pane. */
  57. private JTabbedPane tabbedPane;
  58. /** Buttons panel. */
  59. private JPanel buttonsPanel;
  60. /**
  61. * Creates a new instance of ActionsEditorDialog.
  62. *
  63. * @param parent parent dialog
  64. * @param action actions to be edited
  65. */
  66. private ActionsEditorDialog(final ActionsManagerDialog parent,
  67. final Action action) {
  68. super(MainFrame.getMainFrame(), false);
  69. this.parent = parent;
  70. this.action = action;
  71. this.setTitle("Actions Editor");
  72. initComponents();
  73. addListeners();
  74. layoutComponents();
  75. this.setLocationRelativeTo(MainFrame.getMainFrame());
  76. this.setVisible(true);
  77. }
  78. /**
  79. * Creates the dialog if one doesn't exist, and displays it.
  80. *
  81. * @param parent parent dialog
  82. */
  83. public static synchronized void showActionsEditorDialog(
  84. final ActionsManagerDialog parent) {
  85. showActionsEditorDialog(parent, null);
  86. }
  87. /**
  88. * Creates the dialog if one doesn't exist, and displays it.
  89. *
  90. * @param parent parent dialog
  91. * @param action actions to be edited
  92. */
  93. public static synchronized void showActionsEditorDialog(
  94. final ActionsManagerDialog parent, final Action action) {
  95. if (me != null) {
  96. me.dispose();
  97. }
  98. me = new ActionsEditorDialog(parent, action);
  99. me.setVisible(true);
  100. me.requestFocus();
  101. }
  102. /** Initialises the components. */
  103. private void initComponents() {
  104. initButtonsPanel();
  105. tabbedPane = new JTabbedPane();
  106. tabbedPane.setPreferredSize(new Dimension(400, 160));
  107. tabbedPane.setBorder(BorderFactory.createEmptyBorder(SMALL_BORDER,
  108. SMALL_BORDER, SMALL_BORDER, SMALL_BORDER));
  109. tabbedPane.addTab("General", new GeneralTabPanel(this));
  110. tabbedPane.addTab("Conditions", new ConditionsTabPanel(this));
  111. tabbedPane.addTab("Response", new ResponseTabPanel(this));
  112. }
  113. /** Initialises the button panel. */
  114. private void initButtonsPanel() {
  115. orderButtons(new JButton(), new JButton());
  116. buttonsPanel = new JPanel();
  117. buttonsPanel.setBorder(BorderFactory.createEmptyBorder(0, SMALL_BORDER,
  118. SMALL_BORDER, SMALL_BORDER));
  119. buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.LINE_AXIS));
  120. buttonsPanel.add(Box.createHorizontalGlue());
  121. buttonsPanel.add(getLeftButton());
  122. buttonsPanel.add(Box.createHorizontalStrut(SMALL_BORDER));
  123. buttonsPanel.add(getRightButton());
  124. }
  125. /** Adds listeners to the components. */
  126. private void addListeners() {
  127. getOkButton().addActionListener(this);
  128. getCancelButton().addActionListener(this);
  129. }
  130. /** Lays out the components in the dialog. */
  131. private void layoutComponents() {
  132. this.setLayout(new BorderLayout());
  133. this.add(tabbedPane, BorderLayout.CENTER);
  134. this.add(buttonsPanel, BorderLayout.PAGE_END);
  135. pack();
  136. }
  137. /** {@inheritDoc}. */
  138. public void actionPerformed(final ActionEvent event) {
  139. if (event.getSource() == getOkButton()) {
  140. if (ConditionEditorDialog.getConditionEditorDialog() != null) {
  141. ConditionEditorDialog.getConditionEditorDialog().dispose();
  142. }
  143. saveSettings();
  144. this.dispose();
  145. } else if (event.getSource() == getCancelButton()) {
  146. if (ConditionEditorDialog.getConditionEditorDialog() != null) {
  147. ConditionEditorDialog.getConditionEditorDialog().dispose();
  148. }
  149. this.dispose();
  150. }
  151. }
  152. /** Clears all conditions. */
  153. public void clearConditions() {
  154. ((ConditionsTabPanel) tabbedPane.getComponentAt(1)).clearConditions();
  155. }
  156. /**
  157. * Sets the state of the further steps.
  158. *
  159. * @param state details tabs state.
  160. */
  161. public void setNewConditionButtonState(final boolean state) {
  162. ((ConditionsTabPanel) tabbedPane.getComponentAt(1)).setNewConditionButton(state);
  163. }
  164. /** Saves this (new|edited) actions. */
  165. private void saveSettings() {
  166. if (action == null) {
  167. action = new Action(parent.getSelectedGroup(),
  168. ((GeneralTabPanel) tabbedPane.getComponentAt(0)).getName(),
  169. ((GeneralTabPanel) tabbedPane.getComponentAt(0)).getTriggers().toArray(new ActionType[0]),
  170. ((ResponseTabPanel) tabbedPane.getComponentAt(2)).getResponses().split("\\n"),
  171. ((ConditionsTabPanel) tabbedPane.getComponentAt(1)).getConditions(),
  172. ((ResponseTabPanel) tabbedPane.getComponentAt(2)).getFormatter());
  173. } else {
  174. if (!action.getName().equals(((GeneralTabPanel) tabbedPane.getComponentAt(0)).getName())) {
  175. action.rename(((GeneralTabPanel) tabbedPane.getComponentAt(0)).getName());
  176. }
  177. action.setTriggers(((GeneralTabPanel) tabbedPane.getComponentAt(0)).getTriggers().toArray(new ActionType[0]));
  178. action.setConditions(((ConditionsTabPanel) tabbedPane.getComponentAt(1)).getConditions());
  179. action.setResponse(((ResponseTabPanel) tabbedPane.getComponentAt(2)).getResponses().split("\\n"));
  180. action.setNewFormat(((ResponseTabPanel) tabbedPane.getComponentAt(2)).getFormatter());
  181. }
  182. action.save();
  183. parent.loadGroups();
  184. }
  185. /**
  186. * Returns the action being edited.
  187. *
  188. * @return Action being edited (or null)
  189. */
  190. public Action getAction() {
  191. return action;
  192. }
  193. /**
  194. * Returns the active primary trigger.
  195. *
  196. * @return Selected primary trigger.
  197. */
  198. public ActionType getTrigger() {
  199. return ((GeneralTabPanel) tabbedPane.getComponentAt(0)).getTrigger();
  200. }
  201. /**
  202. * Returns the active primary trigger.
  203. *
  204. * @return Selected primary trigger.
  205. */
  206. public ActionType[] getTriggers() {
  207. return ((GeneralTabPanel) tabbedPane.getComponentAt(0)).getTriggers().toArray(new ActionType[0]);
  208. }
  209. /**
  210. * Returns the number of conditions in the tab panel.
  211. *
  212. * @return Number of Conditions in the dialog.
  213. */
  214. public int getConditionCount() {
  215. return ((ConditionsTabPanel) tabbedPane.getComponentAt(1)).getConditions().size();
  216. }
  217. }