123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- /*
- * Copyright (c) 2006-2014 DMDirc Developers
- *
- * 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.addons.ui_swing.dialogs.actioneditor;
-
- import com.dmdirc.actions.ActionCondition;
- import com.dmdirc.addons.ui_swing.components.ImageButton;
- import com.dmdirc.addons.ui_swing.components.ImageToggleButton;
- import com.dmdirc.addons.ui_swing.components.text.TextLabel;
- import com.dmdirc.interfaces.actions.ActionType;
- import com.dmdirc.ui.IconManager;
- import com.dmdirc.util.collections.ListenerList;
-
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.beans.PropertyChangeEvent;
- import java.beans.PropertyChangeListener;
-
- import javax.swing.JPanel;
- import javax.swing.JToggleButton;
-
- import net.miginfocom.swing.MigLayout;
-
- /**
- * Action condition display panel.
- */
- public class ActionConditionDisplayPanel extends JPanel implements
- ActionListener, PropertyChangeListener {
-
- /** Serial version UID. */
- private static final long serialVersionUID = 1;
- /** Info label. */
- private TextLabel label;
- /** Edit button. */
- private JToggleButton editButton;
- /** Delete button. */
- private ImageButton<Object> deleteButton;
- /** Edit panel. */
- private ActionConditionEditorPanel editPanel;
- /** Listeners. */
- private ListenerList listeners;
- /** Action condition. */
- private ActionCondition condition;
- /** Action trigger. */
- private ActionType trigger;
-
- /**
- * Instantiates the panel.
- *
- * @param iconManager Icon Manager
- * @param condition Action condition
- * @param trigger Action trigger
- */
- public ActionConditionDisplayPanel(final IconManager iconManager,
- final ActionCondition condition, final ActionType trigger) {
-
- this.trigger = trigger;
- this.condition = new ActionCondition(condition.getArg(),
- condition.getComponent(), condition.getComparison(),
- condition.getTarget());
-
- initComponents(iconManager);
- addListeners();
- layoutComponents();
- setTrigger(trigger);
- validate();
- layoutComponents();
-
- if (condition.getArg() == -1 && condition.getComponent() == null && condition.
- getComparison() == null
- && condition.getTarget().isEmpty()) {
- editPanel.setVisible(true);
- editButton.setSelected(true);
- }
- }
-
- /**
- * Sets the action trigger.
- *
- * @param trigger new trigger
- */
- void setTrigger(final ActionType trigger) {
- this.trigger = trigger;
- editPanel.setTrigger(trigger);
-
- editPanel.setVisible(trigger == null);
- editButton.setSelected(trigger == null);
-
- label.setText(updateSentence());
- }
-
- /**
- * Sets the action condition.
- *
- * @param condition new condition
- */
- protected void setCondition(final ActionCondition condition) {
- this.condition = condition;
-
- label.setText(updateSentence());
- }
-
- /**
- * Initialises the components.
- *
- * @param iconManager Icon Manager
- */
- private void initComponents(final IconManager iconManager) {
- label = new TextLabel("", false);
- editButton = new ImageToggleButton("edit", iconManager.
- getIcon("edit-inactive"), iconManager.getIcon("edit"));
- deleteButton = new ImageButton<>("delete", iconManager.
- getIcon("close-inactive"), iconManager.
- getIcon("close-inactive"), iconManager.getIcon("close-active"));
-
- editPanel = new ActionConditionEditorPanel(condition, trigger);
- listeners = new ListenerList();
-
- editPanel.setVisible(trigger == null);
- editButton.setSelected(trigger == null);
- }
-
- /** Adds the listeners. */
- private void addListeners() {
- editButton.addActionListener(this);
- deleteButton.addActionListener(this);
- editPanel.addPropertyChangeListener("edit", this);
- editPanel.addPropertyChangeListener("validationResult", this);
- }
-
- /** Lays out the components. */
- private void layoutComponents() {
- setLayout(new MigLayout("ins 0, fillx, hidemode 3, pack, wmax 90%"));
- add(label, "grow, push, wmax 85%");
- add(editButton, "right");
- add(deleteButton, "right, wrap");
- add(editPanel, "alignx right");
- }
-
- @Override
- public void actionPerformed(final ActionEvent e) {
- if (e.getSource().equals(deleteButton)) {
- fireConditionRemoved(this);
- } else if (e.getSource().equals(editButton)) {
- editPanel.setVisible(editButton.getModel().isSelected());
- }
- }
-
- /**
- * Adds an ActionConditionRemovalListener to the listener list.
- *
- * @param listener Listener to add
- */
- public void addConditionListener(
- final ActionConditionRemovalListener listener) {
- if (listener == null) {
- return;
- }
-
- listeners.add(ActionConditionRemovalListener.class, listener);
- }
-
- /**
- * Removes an ActionConditionRemovalListener from the listener list.
- *
- * @param listener Listener to remove
- */
- public void removeConditionListener(
- final ActionConditionRemovalListener listener) {
- listeners.remove(ActionConditionRemovalListener.class, listener);
- }
-
- /**
- * Fired when the an action condition is removed.
- *
- * @param condition Removed condition
- */
- protected void fireConditionRemoved(
- final ActionConditionDisplayPanel condition) {
- for (final ActionConditionRemovalListener listener : listeners
- .get(ActionConditionRemovalListener.class)) {
- listener.conditionRemoved(condition);
- }
- }
-
- @Override
- public void setEnabled(final boolean enabled) {
- editPanel.setEnabled(enabled);
- editButton.setEnabled(enabled);
- deleteButton.setEnabled(enabled);
- }
-
- /**
- * Updates the condition sentence.
- *
- * @return Updated sentence
- */
- private String updateSentence() {
- if (trigger == null) {
- return "...";
- } else {
- final StringBuilder sb = new StringBuilder("The ");
-
- if (condition.getArg() == -1) {
- sb.append(" ...");
- return sb.toString();
- }
-
- sb.append(trigger.getType().getArgNames()[condition.getArg()]);
- sb.append("'s ");
-
- if (condition.getComponent() == null) {
- sb.append(" ...");
- return sb.toString();
- }
-
- sb.append(condition.getComponent().getName());
- sb.append(' ');
-
- if (condition.getComparison() == null) {
- sb.append(" ...");
- return sb.toString();
- }
-
- sb.append(condition.getComparison().getName());
- sb.append(" '");
-
- if (condition.getTarget() == null) {
- sb.append(" ...");
- return sb.toString();
- }
-
- sb.append(condition.getTarget().replace("<", "<"));
- sb.append('\'');
- return sb.toString();
- }
- }
-
- /**
- * Returns the action condition represented by this panel.
- *
- * @return Action condition
- */
- public ActionCondition getCondition() {
- return condition;
- }
-
- @Override
- public void propertyChange(final PropertyChangeEvent evt) {
- if ("edit".equals(evt.getPropertyName())) {
- label.setText(updateSentence());
- } else {
- firePropertyChange("validationResult", evt.getOldValue(),
- evt.getNewValue());
- }
- }
-
- /**
- * Checks if this editor panel has errored.
- *
- * @return true iif the content it valid
- */
- public boolean checkError() {
- return editPanel.checkError();
- }
-
- }
|