001package jmri.jmrit.beantable;
002
003import java.awt.event.ActionEvent;
004import javax.swing.AbstractAction;
005import org.slf4j.Logger;
006import org.slf4j.LoggerFactory;
007
008/**
009 * Table Action for dealing with all the JMRI NamedBean etc. tables in a single view with a list
010 * option to the left hand side. Enclosing frame is opened from the Tools - Tables menu.
011 *
012 * @author Bob Jacobsen Copyright (C) 2003
013 * @author Kevin Dickerson Copyright (C) 2009
014 */
015public class ListedTableAction extends AbstractAction {
016
017    String gotoListItem = null;
018    String title = Bundle.getMessage("TitleListedTable");
019
020    /**
021     * Create an action with a specific title.
022     * <p>
023     * Note that the argument is the Action title, not the title of the
024     * resulting frame. Perhaps this should be changed?
025     *
026     * @param s title of the action
027     * @param selection item representing this table in de index to the left side of the containing frame
028     */
029    public ListedTableAction(String s, String selection) {
030        super(s);
031        title = s;
032        gotoListItem = selection;
033    }
034
035    public ListedTableAction(String s, String selection, int divider) {
036        super(s);
037        title = s;
038        gotoListItem = selection;
039        dividerLocation = divider;
040    }
041
042    public ListedTableAction(String s, int divider) {
043        super(s);
044        title = s;
045        dividerLocation = divider;
046    }
047
048    public ListedTableAction(String s) {
049        super(s);
050        title = s;
051    }
052
053    public ListedTableAction() {
054        this(Bundle.getMessage("TitleListedTable"));
055    }
056
057    ListedTableFrame<?> f;
058    int dividerLocation = 0;
059
060    public void actionPerformed() {
061        // create the JTable model, with changes for specific NamedBean
062        /* create the frame outside of swing so that we do not
063         hog Swing/AWT execution, then finally display on Swing */
064        Runnable r = new Runnable() {
065            @Override
066            public void run() {
067                f = new ListedTableFrame<>(title);
068                f.initTables();
069                f.initComponents();
070                addToFrame(f);
071
072                try {
073                    javax.swing.SwingUtilities.invokeAndWait(()->{
074                        f.gotoListItem(gotoListItem);
075                        f.pack();
076                        f.setDividerLocation(dividerLocation);
077                        f.setVisible(true);
078                    });
079                } catch (java.lang.reflect.InvocationTargetException ex) {
080                    log.error("failed to set ListedTable visible", ex );
081                } catch (InterruptedException ex) {
082                    log.error("interrupted while setting ListedTable visible", ex );
083                }
084
085            }
086        };
087        Thread thr = jmri.util.ThreadingUtil.newThread(r, "Listed Table Generation");
088        thr.start();
089    }
090
091    @Override
092    public void actionPerformed(ActionEvent e) {
093        actionPerformed();
094    }
095
096    public void addToFrame(ListedTableFrame<?> f) {
097    }
098
099    String helpTarget() {
100        return "package.jmri.jmrit.beantable.ListedTableAction";
101    }
102
103    private final static Logger log = LoggerFactory.getLogger(ListedTableAction.class);
104
105}