Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ActionsManagerDialog.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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;
  23. import java.awt.Dimension;
  24. import java.awt.GridBagConstraints;
  25. import java.awt.GridBagLayout;
  26. import java.awt.Insets;
  27. import java.awt.event.ActionEvent;
  28. import java.awt.event.ActionListener;
  29. import java.util.Arrays;
  30. import java.util.List;
  31. import java.util.Map;
  32. import javax.swing.BorderFactory;
  33. import javax.swing.JButton;
  34. import javax.swing.JDialog;
  35. import javax.swing.JLabel;
  36. import javax.swing.JOptionPane;
  37. import javax.swing.JTabbedPane;
  38. import javax.swing.JTable;
  39. import com.dmdirc.actions.Action;
  40. import com.dmdirc.actions.ActionManager;
  41. import com.dmdirc.ui.MainFrame;
  42. import com.dmdirc.ui.components.ActionsGroupPanel;
  43. import com.dmdirc.ui.components.StandardDialog;
  44. import com.dmdirc.ui.dialogs.actionseditor.ActionsEditorDialog;
  45. import static com.dmdirc.ui.UIUtilities.LARGE_BORDER;
  46. import static com.dmdirc.ui.UIUtilities.SMALL_BORDER;
  47. /**
  48. * Allows the user to manage actions.
  49. */
  50. public final class ActionsManagerDialog extends StandardDialog
  51. implements ActionListener {
  52. /**
  53. * A version number for this class. It should be changed whenever the class
  54. * structure is changed (or anything else that would prevent serialized
  55. * objects being unserialized with the new class).
  56. */
  57. private static final long serialVersionUID = 2;
  58. /** Previously created instance of ActionsManagerDialog. */
  59. private static ActionsManagerDialog me;
  60. /** Height of the buttons, in pixels. */
  61. private static final int BUTTON_HEIGHT = 25;
  62. /** Width of the ubttons, in pixels. */
  63. private static final int BUTTON_WIDTH = 100;
  64. /** The tapped pane used for displaying groups. */
  65. private JTabbedPane groups;
  66. /** Add action button. */
  67. private JButton addAction;
  68. /** Edit action button. */
  69. private JButton editAction;
  70. /** Delete action button. */
  71. private JButton deleteAction;
  72. /** No groups label. */
  73. private JLabel noGroups;
  74. /** Group delete button. */
  75. private JButton deleteGroup;
  76. /** Group rename button. */
  77. private JButton renameGroup;
  78. /** Creates a new instance of ActionsManagerDialog. */
  79. private ActionsManagerDialog() {
  80. super(MainFrame.getMainFrame(), false);
  81. initComponents();
  82. setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  83. setTitle("Action Manager");
  84. setResizable(false);
  85. setLocationRelativeTo(MainFrame.getMainFrame());
  86. setVisible(true);
  87. }
  88. /** Creates the dialog if one doesn't exist, and displays it. */
  89. public static synchronized void showActionsManagerDialog() {
  90. if (me == null) {
  91. me = new ActionsManagerDialog();
  92. } else {
  93. me.loadGroups();
  94. me.setVisible(true);
  95. me.requestFocus();
  96. }
  97. }
  98. /** Initialiases the components for this dialog. */
  99. private void initComponents() {
  100. orderButtons(new JButton(), new JButton());
  101. getCancelButton().addActionListener(this);
  102. getOkButton().addActionListener(this);
  103. setLayout(new GridBagLayout());
  104. final GridBagConstraints constraints = new GridBagConstraints();
  105. constraints.gridx = 0;
  106. constraints.gridy = 0;
  107. constraints.fill = GridBagConstraints.BOTH;
  108. constraints.gridwidth = 7;
  109. final JLabel blurb = new JLabel("Actions allow you to make DMDirc "
  110. + "respond automatically to events.");
  111. blurb.setBorder(BorderFactory.createEmptyBorder(LARGE_BORDER, LARGE_BORDER,
  112. SMALL_BORDER, LARGE_BORDER));
  113. add(blurb, constraints);
  114. constraints.gridy++;
  115. groups = new JTabbedPane();
  116. groups.setPreferredSize(new Dimension(400, 200));
  117. groups.setBorder(BorderFactory.createEmptyBorder(SMALL_BORDER, LARGE_BORDER,
  118. SMALL_BORDER, LARGE_BORDER));
  119. add(groups, constraints);
  120. noGroups = new JLabel("You have no action groups.");
  121. noGroups.setHorizontalAlignment(JLabel.CENTER);
  122. noGroups.setPreferredSize(new Dimension(400, 200));
  123. noGroups.setBorder(BorderFactory.createEmptyBorder(SMALL_BORDER, LARGE_BORDER,
  124. SMALL_BORDER, LARGE_BORDER));
  125. add(noGroups, constraints);
  126. constraints.gridy++;
  127. constraints.gridwidth = 1;
  128. constraints.insets.set(SMALL_BORDER, LARGE_BORDER, SMALL_BORDER, 0);
  129. JButton myButton = new JButton("Add Group");
  130. myButton.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
  131. myButton.setActionCommand("group.add");
  132. myButton.addActionListener(this);
  133. myButton.setMargin(new Insets(0, 0, 0, 0));
  134. add(myButton, constraints);
  135. constraints.gridx++;
  136. constraints.insets.set(SMALL_BORDER, SMALL_BORDER, SMALL_BORDER, 0);
  137. deleteGroup = new JButton("Delete Group");
  138. deleteGroup.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
  139. deleteGroup.setActionCommand("group.delete");
  140. deleteGroup.addActionListener(this);
  141. deleteGroup.setMargin(new Insets(0, 0, 0, 0));
  142. add(deleteGroup, constraints);
  143. constraints.gridx++;
  144. constraints.insets.set(SMALL_BORDER, SMALL_BORDER, SMALL_BORDER, 0);
  145. renameGroup = new JButton("Rename Group");
  146. renameGroup.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
  147. renameGroup.setActionCommand("group.rename");
  148. renameGroup.addActionListener(this);
  149. renameGroup.setMargin(new Insets(0, 0, 0, 0));
  150. add(renameGroup, constraints);
  151. constraints.gridx++;
  152. constraints.insets.set(SMALL_BORDER, LARGE_BORDER, SMALL_BORDER, 0);
  153. addAction = new JButton("New Action");
  154. addAction.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
  155. addAction.setActionCommand("action.new");
  156. addAction.addActionListener(this);
  157. addAction.setMargin(new Insets(0, 0, 0, 0));
  158. add(addAction, constraints);
  159. constraints.gridx++;
  160. constraints.insets.set(SMALL_BORDER, SMALL_BORDER, SMALL_BORDER, 0);
  161. editAction = new JButton("Edit Action");
  162. editAction.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
  163. editAction.setActionCommand("action.edit");
  164. editAction.addActionListener(this);
  165. editAction.setMargin(new Insets(0, 0, 0, 0));
  166. editAction.setEnabled(false);
  167. add(editAction, constraints);
  168. constraints.gridx++;
  169. constraints.insets.set(SMALL_BORDER, SMALL_BORDER, SMALL_BORDER, 0);
  170. deleteAction = new JButton("Delete Action");
  171. deleteAction.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
  172. deleteAction.setActionCommand("action.delete");
  173. deleteAction.addActionListener(this);
  174. deleteAction.setMargin(new Insets(0, 0, 0, 0));
  175. deleteAction.setEnabled(false);
  176. add(deleteAction, constraints);
  177. constraints.gridx++;
  178. constraints.insets.set(SMALL_BORDER, LARGE_BORDER, SMALL_BORDER,
  179. LARGE_BORDER);
  180. myButton = new JButton("Close");
  181. myButton.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
  182. myButton.setActionCommand("close");
  183. myButton.addActionListener(this);
  184. myButton.setMargin(new Insets(0, 0, 0, 0));
  185. add(myButton, constraints);
  186. loadGroups();
  187. pack();
  188. }
  189. /**
  190. * Enable or disable the edit and delete action button.
  191. *
  192. * @param state new State for the buttons
  193. */
  194. public void setEditState(final boolean state) {
  195. editAction.setEnabled(state);
  196. deleteAction.setEnabled(state);
  197. }
  198. /**
  199. * Retrieves known actions from the action manager and displays the
  200. * appropriate groups in the dialog.
  201. */
  202. public void loadGroups() {
  203. final int selectedGroup = groups.getSelectedIndex();
  204. groups.removeAll();
  205. final Map<String, List<Action>> actionGroups = ActionManager.getGroups();
  206. final Object[] keys = actionGroups.keySet().toArray();
  207. Arrays.sort(keys);
  208. for (Object group : keys) {
  209. groups.addTab((String) group,
  210. new ActionsGroupPanel(this, actionGroups.get(group)));
  211. }
  212. if (groups.getTabCount() == 0) {
  213. groups.setVisible(false);
  214. noGroups.setVisible(true);
  215. deleteGroup.setEnabled(false);
  216. renameGroup.setEnabled(false);
  217. addAction.setEnabled(false);
  218. } else {
  219. groups.setVisible(true);
  220. noGroups.setVisible(false);
  221. deleteGroup.setEnabled(true);
  222. renameGroup.setEnabled(true);
  223. addAction.setEnabled(true);
  224. }
  225. groups.setSelectedIndex(selectedGroup == -1 ? 0 : selectedGroup);
  226. }
  227. /**
  228. * Returns the currently selected group.
  229. *
  230. * @return Selected groups name
  231. */
  232. public String getSelectedGroup() {
  233. return groups.getTitleAt(groups.getSelectedIndex());
  234. }
  235. /** {@inheritDoc} */
  236. public void actionPerformed(final ActionEvent e) {
  237. if (e.getActionCommand().equals("close")
  238. || e.getSource() == getCancelButton() || e.getSource() == getOkButton()) {
  239. dispose();
  240. } else if (e.getActionCommand().equals("group.add")) {
  241. final String newGroup = JOptionPane.showInputDialog(this,
  242. "Please enter the name of the group to be created.");
  243. if (newGroup != null && newGroup.length() > 0) {
  244. ActionManager.makeGroup(newGroup);
  245. loadGroups();
  246. }
  247. } else if (e.getActionCommand().equals("group.delete")) {
  248. final String group = groups.getTitleAt(groups.getSelectedIndex());
  249. final Map<String, List<Action>> actionGroups = ActionManager.getGroups();
  250. final int response = JOptionPane.showConfirmDialog(this,
  251. "Are you sure you wish to delete the '" + group
  252. + "' group and all actions within it?",
  253. "Confirm deletion", JOptionPane.YES_NO_OPTION);
  254. if (response == JOptionPane.YES_OPTION) {
  255. ActionManager.removeGroup(group);
  256. loadGroups();
  257. }
  258. } else if (e.getActionCommand().equals("group.rename")) {
  259. final String group = groups.getTitleAt(groups.getSelectedIndex());
  260. final Map<String, List<Action>> actionGroups = ActionManager.getGroups();
  261. final String newName = JOptionPane.showInputDialog(this,
  262. "Please enter a new name for the '" + group
  263. + "' group.",
  264. "Group rename", JOptionPane.QUESTION_MESSAGE);
  265. if (newName != null && newName.length() > 0) {
  266. ActionManager.renameGroup(group, newName);
  267. loadGroups();
  268. }
  269. } else if (e.getActionCommand().equals("action.edit")) {
  270. final JTable table = ((ActionsGroupPanel) groups.getSelectedComponent()).getTable();
  271. final int row = table.getRowSorter().convertRowIndexToModel(table.getSelectedRow());
  272. ActionsEditorDialog.showActionsEditorDialog(this,
  273. ((ActionsGroupPanel) groups.getSelectedComponent()).getAction(row));
  274. } else if (e.getActionCommand().equals("action.new")) {
  275. ActionsEditorDialog.showActionsEditorDialog(this);
  276. } else if (e.getActionCommand().equals("action.delete")) {
  277. final int response = JOptionPane.showConfirmDialog(this,
  278. "Are you sure you wish to delete this action?",
  279. "Confirm deletion", JOptionPane.YES_NO_OPTION);
  280. if (response == JOptionPane.YES_OPTION) {
  281. final JTable table = ((ActionsGroupPanel) groups.getSelectedComponent()).getTable();
  282. final int row = table.getRowSorter().convertRowIndexToModel(table.getSelectedRow());
  283. ActionManager.deleteAction(((ActionsGroupPanel) groups.getSelectedComponent()).getAction(row));
  284. loadGroups();
  285. }
  286. }
  287. }
  288. }