Search in sources :

Example 1 with JTablePersistenceManager

use of jmri.swing.JTablePersistenceManager in project JMRI by JMRI.

the class OperationsPanel method loadTableDetails.

/**
     * Load the table width, position, and sorting status from the user
     * preferences file.
     *
     * @param table The table to be adjusted.
     * @return true if a default instance of the
     *         {@link jmri.swing.JTablePersistenceManager} is available; false
     *         otherwise
     */
public boolean loadTableDetails(JTable table) {
    if (table.getRowSorter() == null) {
        TableRowSorter<? extends TableModel> sorter = new TableRowSorter<>(table.getModel());
        table.setRowSorter(sorter);
        // only sort on columns that are String or Integer
        for (int i = 0; i < table.getColumnCount(); i++) {
            if (table.getColumnClass(i) == String.class || table.getColumnClass(i) == Integer.class) {
                // allow sorting
                continue;
            }
            sorter.setSortable(i, false);
        }
    }
    // set row height
    table.setRowHeight(new JComboBox<>().getPreferredSize().height);
    // have to shut off autoResizeMode to get horizontal scroll to work (JavaSwing p 541)
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    // give each cell a bit of space between the vertical lines and text
    table.setIntercellSpacing(new Dimension(3, 1));
    // table must have a name
    // NOI18N
    table.setName(getWindowFrameRef() + ":table");
    Optional<JTablePersistenceManager> manager = InstanceManager.getOptionalDefault(JTablePersistenceManager.class);
    if (manager.isPresent()) {
        manager.get().resetState(table);
        manager.get().persist(table);
        return true;
    }
    return false;
}
Also used : Dimension(java.awt.Dimension) JTablePersistenceManager(jmri.swing.JTablePersistenceManager) TableRowSorter(javax.swing.table.TableRowSorter)

Example 2 with JTablePersistenceManager

use of jmri.swing.JTablePersistenceManager in project JMRI by JMRI.

the class RosterFrame method createTop.

JComponent createTop() {
    Object selectedRosterGroup = prefsMgr.getProperty(getWindowFrameRef(), SELECTED_ROSTER_GROUP);
    groups = new RosterGroupsPanel((selectedRosterGroup != null) ? selectedRosterGroup.toString() : null);
    groups.setNewWindowMenuAction(this.getNewWindowAction());
    setTitle(groups.getSelectedRosterGroup());
    final JPanel rosters = new JPanel();
    rosters.setLayout(new BorderLayout());
    // set up roster table
    rtable = new RosterTable(true, ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    rtable.setRosterGroup(this.getSelectedRosterGroup());
    rtable.setRosterGroupSource(groups);
    rosters.add(rtable, BorderLayout.CENTER);
    // add selection listener
    rtable.getTable().getSelectionModel().addListSelectionListener((ListSelectionEvent e) -> {
        JTable table = rtable.getTable();
        if (!e.getValueIsAdjusting()) {
            if (rtable.getSelectedRosterEntries().length == 1 && table.getSelectedRow() >= 0) {
                log.debug("Selected row ", table.getSelectedRow());
                locoSelected(rtable.getModel().getValueAt(table.getRowSorter().convertRowIndexToModel(table.getSelectedRow()), RosterTableModel.IDCOL).toString());
            } else if (rtable.getSelectedRosterEntries().length > 1 || table.getSelectedRow() < 0) {
                locoSelected(null);
            }
        // leave last selected item visible if no selection
        }
    });
    //Set all the sort and width details of the table first.
    String rostertableref = getWindowFrameRef() + ":roster";
    rtable.getTable().setName(rostertableref);
    // Allow only one column to be sorted at a time - 
    // Java allows multiple column sorting, but to effectly persist that, we
    // need to be intelligent about which columns can be meaningfully sorted
    // with other columns; this bypasses the problem by only allowing the
    // last column sorted to affect sorting
    RowSorterUtil.addSingleSortableColumnListener(rtable.getTable().getRowSorter());
    // Reset and then persist the table's ui state
    JTablePersistenceManager tpm = InstanceManager.getNullableDefault(JTablePersistenceManager.class);
    if (tpm != null) {
        tpm.resetState(rtable.getTable());
        tpm.persist(rtable.getTable());
    }
    rtable.getTable().setDragEnabled(true);
    rtable.getTable().setTransferHandler(new TransferHandler() {

        @Override
        public int getSourceActions(JComponent c) {
            return TransferHandler.COPY;
        }

        @Override
        public Transferable createTransferable(JComponent c) {
            JTable table = rtable.getTable();
            ArrayList<String> Ids = new ArrayList<>(table.getSelectedRowCount());
            for (int i = 0; i < table.getSelectedRowCount(); i++) {
                Ids.add(rtable.getModel().getValueAt(table.getRowSorter().convertRowIndexToModel(table.getSelectedRows()[i]), RosterTableModel.IDCOL).toString());
            }
            return new RosterEntrySelection(Ids);
        }

        @Override
        public void exportDone(JComponent c, Transferable t, int action) {
        // nothing to do
        }
    });
    MouseListener rosterMouseListener = new rosterPopupListener();
    rtable.getTable().addMouseListener(rosterMouseListener);
    try {
        clickDelay = ((Integer) Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval"));
    } catch (RuntimeException e) {
        clickDelay = 500;
        log.debug("Unable to get the double click speed, Using JMRI default of half a second" + e.toString());
    }
    // assemble roster/groups splitpane
    rosterGroupSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, groups, rosters);
    rosterGroupSplitPane.setOneTouchExpandable(true);
    // emphasis rosters
    rosterGroupSplitPane.setResizeWeight(0);
    Object w = prefsMgr.getProperty(getWindowFrameRef(), "rosterGroupPaneDividerLocation");
    if (w != null) {
        groupSplitPaneLocation = (Integer) w;
        rosterGroupSplitPane.setDividerLocation(groupSplitPaneLocation);
    }
    if (!Roster.getDefault().getRosterGroupList().isEmpty()) {
        if (prefsMgr.getSimplePreferenceState(this.getClass().getName() + ".hideGroups")) {
            hideGroupsPane(true);
        }
    } else {
        enableRosterGroupMenuItems(false);
    }
    PropertyChangeListener propertyChangeListener = (PropertyChangeEvent changeEvent) -> {
        JSplitPane sourceSplitPane = (JSplitPane) changeEvent.getSource();
        String propertyName = changeEvent.getPropertyName();
        if (propertyName.equals(JSplitPane.LAST_DIVIDER_LOCATION_PROPERTY)) {
            int current = sourceSplitPane.getDividerLocation();
            hideGroups = current <= 1;
            Integer last = (Integer) changeEvent.getNewValue();
            if (current >= 2) {
                groupSplitPaneLocation = current;
            } else if (last >= 2) {
                groupSplitPaneLocation = last;
            }
        }
    };
    groups.addPropertyChangeListener(SELECTED_ROSTER_GROUP, new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent pce) {
            prefsMgr.setProperty(this.getClass().getName(), SELECTED_ROSTER_GROUP, pce.getNewValue());
            setTitle((String) pce.getNewValue());
        }
    });
    rosterGroupSplitPane.addPropertyChangeListener(propertyChangeListener);
    Roster.getDefault().addPropertyChangeListener((PropertyChangeEvent e) -> {
        if (e.getPropertyName().equals("RosterGroupAdded") && Roster.getDefault().getRosterGroupList().size() == 1) {
            // if the pane is hidden, show it when 1st group is created
            hideGroupsPane(false);
            enableRosterGroupMenuItems(true);
        } else if (!rtable.isVisible() && (e.getPropertyName().equals("saved"))) {
            if (firstHelpLabel != null) {
                firstHelpLabel.setVisible(false);
            }
            rtable.setVisible(true);
            rtable.resetColumnWidths();
        }
    });
    if (Roster.getDefault().numEntries() == 0) {
        try {
            BufferedImage myPicture = ImageIO.read(FileUtil.findURL(("resources/" + Bundle.getMessage("ThrottleFirstUseImage")), FileUtil.Location.INSTALLED));
            //rosters.add(new JLabel(new ImageIcon( myPicture )), BorderLayout.CENTER);
            firstHelpLabel = new JLabel(new ImageIcon(myPicture));
            rtable.setVisible(false);
            rosters.add(firstHelpLabel, BorderLayout.NORTH);
            //tableArea.add(firstHelpLabel);
            rtable.setVisible(false);
        } catch (IOException ex) {
        // handle exception...
        }
    }
    return rosterGroupSplitPane;
}
Also used : RosterEntrySelection(jmri.util.datatransfer.RosterEntrySelection) JPanel(javax.swing.JPanel) ImageIcon(javax.swing.ImageIcon) PropertyChangeListener(java.beans.PropertyChangeListener) ListSelectionEvent(javax.swing.event.ListSelectionEvent) ArrayList(java.util.ArrayList) BufferedImage(java.awt.image.BufferedImage) MouseListener(java.awt.event.MouseListener) BorderLayout(java.awt.BorderLayout) PropertyChangeEvent(java.beans.PropertyChangeEvent) JComponent(javax.swing.JComponent) Transferable(java.awt.datatransfer.Transferable) JLabel(javax.swing.JLabel) IOException(java.io.IOException) JTablePersistenceManager(jmri.swing.JTablePersistenceManager) JTable(javax.swing.JTable) TransferHandler(javax.swing.TransferHandler) JSplitPane(javax.swing.JSplitPane)

Example 3 with JTablePersistenceManager

use of jmri.swing.JTablePersistenceManager in project JMRI by JMRI.

the class BeanTableDataModel method persistTable.

/**
     * Persist the state of the table after first setting the table to the last
     * persisted state.
     *
     * @param table the table to persist
     * @throws NullPointerException if the name of the table is null
     */
public void persistTable(@Nonnull JTable table) throws NullPointerException {
    JTablePersistenceManager manager = InstanceManager.getNullableDefault(JTablePersistenceManager.class);
    if (manager != null) {
        // throws NPE if table name is null
        manager.resetState(table);
        manager.persist(table);
    }
}
Also used : JTablePersistenceManager(jmri.swing.JTablePersistenceManager)

Aggregations

JTablePersistenceManager (jmri.swing.JTablePersistenceManager)3 BorderLayout (java.awt.BorderLayout)1 Dimension (java.awt.Dimension)1 Transferable (java.awt.datatransfer.Transferable)1 MouseListener (java.awt.event.MouseListener)1 BufferedImage (java.awt.image.BufferedImage)1 PropertyChangeEvent (java.beans.PropertyChangeEvent)1 PropertyChangeListener (java.beans.PropertyChangeListener)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ImageIcon (javax.swing.ImageIcon)1 JComponent (javax.swing.JComponent)1 JLabel (javax.swing.JLabel)1 JPanel (javax.swing.JPanel)1 JSplitPane (javax.swing.JSplitPane)1 JTable (javax.swing.JTable)1 TransferHandler (javax.swing.TransferHandler)1 ListSelectionEvent (javax.swing.event.ListSelectionEvent)1 TableRowSorter (javax.swing.table.TableRowSorter)1 RosterEntrySelection (jmri.util.datatransfer.RosterEntrySelection)1