yetanotheraprsc/yetanotheraprsc-code/.svn/pristine/0a/0a76f6e6f9a512488d7516925a2...

161 lines
5.5 KiB
Plaintext

package org.ka2ddo.yaac.gui.pluginadapter;
/*
* Copyright (C) 2011-2019 Andrew Pavlin, KA2DDO
* This file is part of YAAC (Yet Another APRS Client).
*
* YAAC is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* YAAC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* and GNU Lesser General Public License along with YAAC. If not,
* see <http://www.gnu.org/licenses/>.
*/
import org.ka2ddo.yaac.pluginapi.AbstractMenuAction;
import org.ka2ddo.yaac.pluginapi.AbstractMenuActionPropertyListener;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
/**
* This class defines a Java Swing specific wrapper around the now GUI-independent
* AbstractMenuAction class in the plugin API package.
* @author Andrew Pavlin, KA2DDO
*/
public class MenuAction implements Action, AbstractMenuActionPropertyListener, ItemListener {
private final AbstractMenuAction ama;
private ArrayList<PropertyChangeListener> listeners;
/**
* Create the AWT/Swing-specific wrapper around an AbstractMenuAction.
* @param abstractMenuAction AbstractMenuAction to instantiate in Java Swing
*/
public MenuAction(AbstractMenuAction abstractMenuAction) {
this.ama = abstractMenuAction;
abstractMenuAction.setPropertyListener(this);
}
/**
* Adds a <code>PropertyChange</code> listener. Containers and attached
* components use these methods to register interest in this
* <code>Action</code> object. When its enabled state or other property
* changes, the registered listeners are informed of the change.
*
* @param listener a <code>PropertyChangeListener</code> object
*/
public void addPropertyChangeListener(PropertyChangeListener listener) {
if (listeners == null) {
listeners = new ArrayList<PropertyChangeListener>();
}
if (!listeners.contains(listener)) {
listeners.add(listener);
}
}
/**
* Gets one of this object's properties
* using the associated key.
*
* @see #putValue
*/
public Object getValue(String key) {
return ama.getValue(key);
}
/**
* Sets one of this object's properties
* using the associated key. If the value has
* changed, a <code>PropertyChangeEvent</code> is sent
* to listeners.
*
* @param key a <code>String</code> containing the key
* @param value an <code>Object</code> value
*/
public void putValue(String key, Object value) {
ama.putValue(key, value);
}
/**
* Sets the enabled state of the <code>Action</code>. When enabled,
* any component associated with this object is active and
* able to fire this object's <code>actionPerformed</code> method.
* If the value has changed, a <code>PropertyChangeEvent</code> is sent
* to listeners.
*
* @param b true to enable this <code>Action</code>, false to disable it
*/
public void setEnabled(boolean b) {
ama.setEnabled(b);
}
/**
* Returns the enabled state of the <code>Action</code>. When enabled,
* any component associated with this object is active and
* able to fire this object's <code>actionPerformed</code> method.
*
* @return true if this <code>Action</code> is enabled
*/
public boolean isEnabled() {
return ama.isEnabled();
}
/**
* Removes a <code>PropertyChange</code> listener.
*
* @param listener a <code>PropertyChangeListener</code> object
* @see #addPropertyChangeListener
*/
public void removePropertyChangeListener(PropertyChangeListener listener) {
if (listeners != null) {
listeners.remove(listener);
if (listeners.size() == 0) {
listeners = null;
}
}
}
/**
* Invoked when an action occurs.
*/
public void actionPerformed(ActionEvent e) {
ama.actionPerformed(e.getSource());
}
/**
* Tell the listener that a property has changed.
*
* @param propertyName String name of property
* @param oldValue Object value before the change
* @param newValue Object value after the change
*/
public void menuPropertyChanged(String propertyName, Object oldValue, Object newValue) {
ArrayList<PropertyChangeListener> listeners;
if ((listeners = this.listeners) != null) { // avoid getfield code and possible NPE
PropertyChangeEvent pce = new PropertyChangeEvent(this, propertyName, oldValue, newValue);
for (int i = listeners.size() - 1; i >= 0; i--) {
listeners.get(i).propertyChange(pce);
}
}
}
/**
* DO NOT CALL. Handles association of JMenuItemCheckBox with MenuAction.
* @param e ItemEvent reporting JMenuItemCheckBox state change
*/
public void itemStateChanged(ItemEvent e) {
ama.actionPerformed(e.getSource());
}
}