/*
This file is part of the software framework JStatCom,
a library for building applications for data based analysis.
Copyright (C) 2000-2005 Alexander Benkwitz, Markus Kraetzig
This library 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 2.1 of the
License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
*/
package com.jstatcom.component;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import org.apache.log4j.Logger;
/**
* An action for the task of showing a "card" component in a
* CardDisplayPanel.
*
* The "card" component is only constructed when getComponent is
* called the first time. This way the component gets only created when the
* respective CardPanelAction is actually called. This may led to
* faster program start.
*
* @author A. Benkwitz, M. Kraetzig
* @see com.jstatcom.component.CardDisplayPanel
*/
public final class CardPanelAction extends AbstractAction {
private static final Logger log = Logger.getLogger(CardPanelAction.class);
private final CardDisplayPanel cardPanel;
private JComponent component = null;
private final String className;
/**
* Creates an action object that constructs a component on demand according
* to the arguments.
*
* @param className
* the string with the fully qualified class name of the
* component to construct when this action is called
* @param parent
* the parent panel
* @param enabled
* true if this action should be enabled
* @param name
* the name of this action
*
* @throws IllegalArgumentException
* if (className == null) or
* if (parent == null)
*/
public CardPanelAction(String className, CardDisplayPanel parent,
boolean enabled, String name) {
this(className, parent, enabled, name, null);
}
/**
* Creates an action object that constructs a component on demand according
* to the arguments.
*
* @param className
* the string with the fully qualified class name of the
* component to construct when this action is called
* @param parent
* the parent panel
* @param enabled
* true if this action should be enabled
* @param name
* the name of this action
* @param icon
* an icon to be displayed by components taking this
* AbstractAction as argument
*
* @throws IllegalArgumentException
* if (className == null) or
* if (parent == null)
*/
public CardPanelAction(String className, CardDisplayPanel parent,
boolean enabled, String name, Icon icon) {
super(name, icon);
if (className == null)
throw new IllegalArgumentException("Class name was null.");
if (parent == null)
throw new IllegalArgumentException("Parent was null.");
this.className = className;
cardPanel = parent;
setEnabled(enabled);
}
/**
* Invoked when this action is called. Creates the respective component if
* it has not been created before and calls showCard of the
* parent CardDisplayPanel.
*
* @param arg1
* argument is ignored and can be null
*/
public void actionPerformed(ActionEvent arg1) {
cardPanel.showCard(getComponent());
}
/**
* Gets the component defined via className in the
* constructor. If this method is called the first time, the component is
* first created.
*
* @return the component that has been defined via its class name in the
* constructor of this action, if the object construction failes, am
* empty JPanel with a message is returned
*/
public JComponent getComponent() {
if (component == null) {
try {
component = (JComponent) Class.forName(className).newInstance();
} catch (Throwable thr) {
String msg = "Problem creating \"" + className + "\".";
log.error(msg, thr);
component = new JPanel();
component.setLayout(new BorderLayout());
MultiLineLabel label = new MultiLineLabel(
msg
+ "\nDisplaying an empty JPanel instead. Check log messages for more.");
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setForeground(Color.red);
component.add(label, "Center");
}
}
return component;
}
}
Ads help maintain this website.