001package jmri.util;
002
003import java.awt.event.ActionEvent;
004import javax.swing.AbstractAction;
005import org.slf4j.Logger;
006import org.slf4j.LoggerFactory;
007
008/**
009 * Default form of action to create an object that's from a child class of
010 * JmriJFrame. By using reflection, this cuts the loader dependency on the
011 * loaded class.
012 *
013 * @author Bob Jacobsen Copyright (C) 2007
014 */
015public class JmriJFrameAction extends AbstractAction {
016
017    public JmriJFrameAction(String s) {
018        super(s);
019    }
020
021    /**
022     * Method to be overridden to make this work. Provide a completely qualified
023     * class name, must be castable to JmriJFrame
024     *
025     * @return the default implementation returns an empty String
026     */
027    // why isn't this abstract?
028    public String getName() {
029        return "";
030    }
031
032    @Override
033    public void actionPerformed(ActionEvent e) {
034        String name = getName();
035        JmriJFrame j = null;
036
037        if (!name.equals("")) {
038            try {
039                j = (JmriJFrame) Class.forName(name).getDeclaredConstructor().newInstance();
040                j.initComponents();
041                j.setVisible(true);
042            } catch (java.lang.ClassNotFoundException ex1) {
043                log.error("Couldn't create window, because couldn't find class", ex1);
044            } catch (Exception ex2) {
045                log.error("Exception creating frame", ex2);
046            }
047        }
048    }
049
050    private final static Logger log = LoggerFactory.getLogger(JmriJFrameAction.class);
051}