123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- /*
- * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.ui.dialogs.actionseditor;
-
- import java.awt.Dimension;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.ArrayList;
- import java.util.List;
-
- import javax.swing.DefaultComboBoxModel;
- import javax.swing.DefaultListModel;
- import javax.swing.JComboBox;
- import javax.swing.JLabel;
- import javax.swing.JList;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JTextField;
- import javax.swing.SpringLayout;
- import javax.swing.SwingUtilities;
-
- import com.dmdirc.actions.ActionManager;
- import com.dmdirc.actions.ActionType;
-
- import static com.dmdirc.ui.UIUtilities.SMALL_BORDER;
- import static com.dmdirc.ui.UIUtilities.layoutGrid;
-
- /**
- * General tab panel, name and trigger editing for the actions editor dialog.
- */
- public final class GeneralTabPanel extends JPanel implements ActionListener {
-
- /**
- * A version number for this class. It should be changed whenever the class
- * structure is changed (or anything else that would prevent serialized
- * objects being unserialized with the new class).
- */
- private static final long serialVersionUID = 1;
-
- /** Parent ActionsEditorDialog. */
- private ActionsEditorDialog owner;
-
- /** Name textfield. */
- private JTextField name;
- /** Primary trigger combobox. */
- private JComboBox trigger;
- /** Secondary trigger list. */
- private JList otherTriggers;
-
- /** Currently selected trigger. */
- private ActionType type;
-
- /**
- * Creates a new instance of GeneralTabPanel.
- *
- * @param owner Parent dialog
- */
- public GeneralTabPanel(final ActionsEditorDialog owner) {
- super();
-
- this.owner = owner;
-
- initComponents();
- addListeners();
- layoutComponents();
- }
-
- /** Initialises the components. */
- private void initComponents() {
- name = new JTextField();
- trigger = new JComboBox(new DefaultComboBoxModel());
- otherTriggers = new JList(new DefaultListModel());
-
- trigger.setRenderer(new ActionCellRenderer());
- otherTriggers.setCellRenderer(new ActionCellRenderer());
-
- ((DefaultComboBoxModel) trigger.getModel()).addElement("");
- for (ActionType thisType : ActionManager.getTypes().toArray(new ActionType[0])) {
- ((DefaultComboBoxModel) trigger.getModel()).addElement(thisType);
- }
-
- name.setPreferredSize(new Dimension(100, name.getFont().getSize()));
-
- trigger.setPreferredSize(new Dimension(100, trigger.getFont().getSize()));
-
- otherTriggers.setVisibleRowCount(2);
- otherTriggers.setEnabled(false);
-
- if (owner.getAction() == null) {
- return;
- }
-
- name.setText(owner.getAction().getName());
-
- trigger.setSelectedItem(owner.getAction().getTriggers()[0]);
- type = (ActionType) trigger.getSelectedItem();
- otherTriggers.setEnabled(true);
-
- populateOtherTriggers();
-
- selectOtherTriggers();
- }
-
- /** Populates the other triggers list with compatible types. */
- private void populateOtherTriggers() {
- otherTriggers.removeAll();
- if (trigger.getSelectedIndex() == 0) {
- otherTriggers.setEnabled(false);
- } else {
- for (ActionType thisType
- : ActionManager.getCompatibleTypes((ActionType) trigger.getSelectedItem())) {
- ((DefaultListModel) otherTriggers.getModel()).addElement(thisType);
- }
- }
- otherTriggers.repaint();
- }
-
- /** Selects other triggers that are part of this action. */
- private void selectOtherTriggers() {
- for (ActionType thisType : owner.getAction().getTriggers()) {
- final int index = ((DefaultListModel) otherTriggers.getModel()).indexOf(thisType);
- if (index != -1) {
- otherTriggers.getSelectionModel().addSelectionInterval(index, index);
- }
-
- }
- }
-
- /** Adds listeners to the components. */
- private void addListeners() {
- trigger.addActionListener(this);
- }
-
- /** Lays out components. */
- private void layoutComponents() {
- add(new JLabel("Name: "));
- add(name);
- add(new JLabel("Primary trigger: "));
- add(trigger);
- add(new JLabel("Additional triggers: "));
- add(new JScrollPane(otherTriggers));
-
- this.setLayout(new SpringLayout());
-
- layoutGrid(this, 3,
- 2, SMALL_BORDER, SMALL_BORDER, SMALL_BORDER, SMALL_BORDER);
- }
-
- /**
- * Returns the name for this panel.
- *
- * @return Action name
- */
- public String getName() {
- return name.getText();
- }
-
- /**
- * Returns the primary trigger for this panel.
- *
- * @return Primary trigger
- */
- public ActionType getTrigger() {
- if (trigger.getSelectedIndex() == 0) {
- return null;
- }
- return (ActionType) trigger.getSelectedItem();
- }
-
- /**
- * Returns the triggers for this panel.
- *
- * @return Trigger list
- */
- public List<ActionType> getTriggers() {
- final List<ActionType> triggers = new ArrayList<ActionType>();
-
- if (trigger.getSelectedIndex() == 0) {
- return null;
- }
- triggers.add((ActionType) trigger.getSelectedItem());
-
- for (Object thisType : otherTriggers.getSelectedValues()) {
- triggers.add((ActionType) thisType);
- }
-
- return triggers;
- }
-
- /** {@inheritDoc}. */
- public void actionPerformed(final ActionEvent event) {
- if (event.getSource() == trigger) {
- SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- handleTriggerChange();
- }
- });
- }
- }
-
- /** Prompts the user for confirmation of type change. */
- private void handleTriggerChange() {
- boolean compatible = false;
- if (trigger.getSelectedIndex() != 0) {
- owner.getOkButton().setEnabled(true);
- if (owner.getTrigger().getType().getArgNames().length > 0) {
- owner.setNewConditionButtonState(true);
- }
- } else {
- owner.getOkButton().setEnabled(false);
- owner.setNewConditionButtonState(false);
- }
- if (type == null || trigger.getSelectedIndex() == 0) {
- compatible = false;
- } else if (ActionManager.getCompatibleTypes(type).contains(trigger.getSelectedItem())
- || trigger.getSelectedItem() == type) {
- compatible = true;
- }
-
- if (compatible) {
- populateOtherTriggers();
- selectOtherTriggers();
- type = (ActionType) trigger.getSelectedItem();
- } else if (owner.getConditionCount() > 0) {
- final int response = JOptionPane.showConfirmDialog(this,
- "Changing to this trigger will remove your existing "
- + "conditions. Are you sure?", "Incompatible triggers",
- JOptionPane.OK_CANCEL_OPTION);
- if (response == JOptionPane.OK_OPTION) {
- type = (ActionType) trigger.getSelectedItem();
- owner.clearConditions();
- } else {
- trigger.setSelectedItem(type);
- }
- }
- }
-
- }
|