Search in sources :

Example 71 with TableColumnModel

use of javax.swing.table.TableColumnModel in project JMRI by JMRI.

the class JmriJTablePersistenceManager method resetState.

@Override
public void resetState(JTable table) {
    Objects.requireNonNull(table.getName(), "table name must be nonnull");
    boolean persisting = this.listeners.containsKey(table.getName());
    // while setting table state, don't listen to changes in table state
    this.stopPersisting(table);
    TableColumnModel model = table.getColumnModel();
    Objects.requireNonNull(model, "table " + table.getName() + " has a null columnModel");
    RowSorter sorter = table.getRowSorter();
    boolean isXModel = model instanceof XTableColumnModel;
    Enumeration<TableColumn> e;
    if (isXModel) {
        e = ((XTableColumnModel) model).getColumns(false);
    } else {
        e = model.getColumns();
    }
    while (e.hasMoreElements()) {
        TableColumn column = e.nextElement();
        if (column.getIdentifier() == null) {
            column.setIdentifier(column.getHeaderValue().toString());
        }
    }
    Map<Integer, String> indexes = new HashMap<>();
    if (this.columns.get(table.getName()) == null) {
        this.columns.put(table.getName(), new HashMap<>());
    }
    this.columns.get(table.getName()).entrySet().stream().forEach((entry) -> {
        int index = entry.getValue().getOrder();
        indexes.put(index, entry.getKey());
    });
    // order columns
    for (int i = 0; i < model.getColumnCount(); i++) {
        String name = indexes.get(i);
        if (name != null) {
            int dataModelIndex = -1;
            for (int j = 0; j < model.getColumnCount(); j++) {
                if (table.getColumnName(j).equals(name)) {
                    dataModelIndex = j;
                    break;
                }
            }
            if (dataModelIndex != -1 && (dataModelIndex != i)) {
                model.moveColumn(dataModelIndex, i);
            }
        }
    }
    // configure columns
    if (isXModel) {
        e = ((XTableColumnModel) model).getColumns(false);
    } else {
        e = model.getColumns();
    }
    while (e.hasMoreElements()) {
        TableColumn column = e.nextElement();
        String name = column.getIdentifier().toString();
        TableColumnPreferences preferences = this.columns.get(table.getName()).get(name);
        if (preferences != null) {
            column.setPreferredWidth(preferences.getWidth());
            if (isXModel) {
                ((XTableColumnModel) model).setColumnVisible(column, !preferences.getHidden());
            }
        }
    }
    if (sorter != null && this.sortKeys.get(table.getName()) != null) {
        sorter.setSortKeys(this.sortKeys.get(table.getName()));
    }
    if (persisting) {
        this.persist(table);
    }
}
Also used : RowSorter(javax.swing.RowSorter) HashMap(java.util.HashMap) TableColumnModel(javax.swing.table.TableColumnModel) XTableColumnModel(jmri.util.swing.XTableColumnModel) XTableColumnModel(jmri.util.swing.XTableColumnModel) TableColumn(javax.swing.table.TableColumn)

Example 72 with TableColumnModel

use of javax.swing.table.TableColumnModel in project jabref by JabRef.

the class ImportInspectionDialog method setWidths.

private void setWidths() {
    TableColumnModel cm = glTable.getColumnModel();
    cm.getColumn(0).setPreferredWidth(55);
    cm.getColumn(0).setMinWidth(55);
    cm.getColumn(0).setMaxWidth(55);
    for (int i = 1; i < PAD; i++) {
        // Lock the width of icon columns.
        cm.getColumn(i).setPreferredWidth(GUIGlobals.WIDTH_ICON_COL);
        cm.getColumn(i).setMinWidth(GUIGlobals.WIDTH_ICON_COL);
        cm.getColumn(i).setMaxWidth(GUIGlobals.WIDTH_ICON_COL);
    }
    for (int i = 0; i < INSPECTION_FIELDS.size(); i++) {
        int width = InternalBibtexFields.getFieldLength(INSPECTION_FIELDS.get(i));
        glTable.getColumnModel().getColumn(i + PAD).setPreferredWidth(width);
    }
}
Also used : TableColumnModel(javax.swing.table.TableColumnModel)

Example 73 with TableColumnModel

use of javax.swing.table.TableColumnModel in project jabref by JabRef.

the class MainTable method setWidths.

private void setWidths() {
    // Setting column widths:
    int ncWidth = Globals.prefs.getInt(JabRefPreferences.NUMBER_COL_WIDTH);
    List<String> widthsFromPreferences = Globals.prefs.getStringList(JabRefPreferences.COLUMN_WIDTHS);
    TableColumnModel cm = getColumnModel();
    cm.getColumn(0).setPreferredWidth(ncWidth);
    for (int i = 1; i < cm.getColumnCount(); i++) {
        MainTableColumn mainTableColumn = tableFormat.getTableColumn(cm.getColumn(i).getModelIndex());
        if (SpecialField.RANKING.getFieldName().equals(mainTableColumn.getColumnName())) {
            cm.getColumn(i).setPreferredWidth(GUIGlobals.WIDTH_ICON_COL_RANKING);
            cm.getColumn(i).setMinWidth(GUIGlobals.WIDTH_ICON_COL_RANKING);
            cm.getColumn(i).setMaxWidth(GUIGlobals.WIDTH_ICON_COL_RANKING);
        } else if (mainTableColumn.isIconColumn()) {
            cm.getColumn(i).setPreferredWidth(GUIGlobals.WIDTH_ICON_COL);
            cm.getColumn(i).setMinWidth(GUIGlobals.WIDTH_ICON_COL);
            cm.getColumn(i).setMaxWidth(GUIGlobals.WIDTH_ICON_COL);
        } else {
            List<String> allColumns = Globals.prefs.getStringList(JabRefPreferences.COLUMN_NAMES);
            // find index of current mainTableColumn in allColumns
            for (int j = 0; j < allColumns.size(); j++) {
                if (allColumns.get(j).equalsIgnoreCase(mainTableColumn.getDisplayName())) {
                    try {
                        // set preferred width by using found index j in the width array
                        cm.getColumn(i).setPreferredWidth(Integer.parseInt(widthsFromPreferences.get(j)));
                    } catch (NumberFormatException e) {
                        LOGGER.info("Exception while setting column widths. Choosing default.", e);
                        cm.getColumn(i).setPreferredWidth(BibtexSingleField.DEFAULT_FIELD_LENGTH);
                    }
                    break;
                }
            }
        }
    }
}
Also used : TableColumnModel(javax.swing.table.TableColumnModel) SortedList(ca.odell.glazedlists.SortedList) List(java.util.List) ArrayList(java.util.ArrayList) EventList(ca.odell.glazedlists.EventList) Point(java.awt.Point)

Example 74 with TableColumnModel

use of javax.swing.table.TableColumnModel in project jabref by JabRef.

the class ProtectedTermsDialog method setupTable.

private void setupTable() {
    tableModel = new TermTableModel();
    table = new JTable(tableModel);
    TableColumnModel cm = table.getColumnModel();
    cm.getColumn(0).setMinWidth((cm.getColumn(0).getPreferredWidth() * 11) / 10);
    cm.getColumn(0).setMaxWidth((cm.getColumn(0).getPreferredWidth() * 11) / 10);
    cm.getColumn(1).setPreferredWidth(100);
    cm.getColumn(2).setPreferredWidth(100);
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    table.addMouseListener(new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent mouseEvent) {
            if (mouseEvent.isPopupTrigger()) {
                tablePopup(mouseEvent);
            }
        }

        @Override
        public void mouseReleased(MouseEvent mouseEvent) {
            if (mouseEvent.isPopupTrigger()) {
                tablePopup(mouseEvent);
            }
        }

        @Override
        public void mouseClicked(MouseEvent mouseEvent) {
            if (mouseEvent.getClickCount() == 2) {
                getSelectedTermsList().ifPresent(list -> list.setEnabled(!list.isEnabled()));
                tableModel.fireTableDataChanged();
            }
        }
    });
    table.getSelectionModel().addListSelectionListener(new EntrySelectionListener());
}
Also used : JDialog(javax.swing.JDialog) IconTheme(org.jabref.gui.IconTheme) JabRefPreferences(org.jabref.preferences.JabRefPreferences) JCheckBoxMenuItem(javax.swing.JCheckBoxMenuItem) JabRefFrame(org.jabref.gui.JabRefFrame) MouseAdapter(java.awt.event.MouseAdapter) UnknownExternalFileType(org.jabref.gui.externalfiletype.UnknownExternalFileType) BorderLayout(java.awt.BorderLayout) ListSelectionEvent(javax.swing.event.ListSelectionEvent) ProtectedTermsLoader(org.jabref.logic.protectedterms.ProtectedTermsLoader) Path(java.nio.file.Path) ExternalFileType(org.jabref.gui.externalfiletype.ExternalFileType) KeyStroke(javax.swing.KeyStroke) DefaultTableModel(javax.swing.table.DefaultTableModel) BibEntry(org.jabref.model.entry.BibEntry) BorderFactory(javax.swing.BorderFactory) FXDialogService(org.jabref.gui.FXDialogService) DefaultTaskExecutor(org.jabref.gui.util.DefaultTaskExecutor) Objects(java.util.Objects) Dimension(java.awt.Dimension) JabRefDialog(org.jabref.gui.JabRefDialog) AbstractAction(javax.swing.AbstractAction) FormLayout(com.jgoodies.forms.layout.FormLayout) Optional(java.util.Optional) JTable(javax.swing.JTable) LogFactory(org.apache.commons.logging.LogFactory) ProtectedTermsList(org.jabref.logic.protectedterms.ProtectedTermsList) KeyBinding(org.jabref.gui.keyboard.KeyBinding) ListSelectionModel(javax.swing.ListSelectionModel) ActionListener(java.awt.event.ActionListener) TableColumnModel(javax.swing.table.TableColumnModel) JTextField(javax.swing.JTextField) ButtonBarBuilder(com.jgoodies.forms.builder.ButtonBarBuilder) JabRefDesktop(org.jabref.gui.desktop.JabRefDesktop) DialogService(org.jabref.gui.DialogService) Action(javax.swing.Action) FileExtensions(org.jabref.logic.util.FileExtensions) JMenuItem(javax.swing.JMenuItem) Localization(org.jabref.logic.l10n.Localization) BibDatabaseContext(org.jabref.model.database.BibDatabaseContext) ActionMap(javax.swing.ActionMap) JComponent(javax.swing.JComponent) FormBuilder(com.jgoodies.forms.builder.FormBuilder) JButton(javax.swing.JButton) FileDialogConfiguration(org.jabref.gui.util.FileDialogConfiguration) JPopupMenu(javax.swing.JPopupMenu) IOException(java.io.IOException) JOptionPane(javax.swing.JOptionPane) ActionEvent(java.awt.event.ActionEvent) MouseEvent(java.awt.event.MouseEvent) Globals(org.jabref.Globals) JScrollPane(javax.swing.JScrollPane) WindowLocation(org.jabref.gui.util.WindowLocation) Log(org.apache.commons.logging.Log) JTextArea(javax.swing.JTextArea) ExternalFileTypes(org.jabref.gui.externalfiletype.ExternalFileTypes) InputMap(javax.swing.InputMap) ListSelectionListener(javax.swing.event.ListSelectionListener) MouseEvent(java.awt.event.MouseEvent) JTable(javax.swing.JTable) MouseAdapter(java.awt.event.MouseAdapter) TableColumnModel(javax.swing.table.TableColumnModel)

Example 75 with TableColumnModel

use of javax.swing.table.TableColumnModel in project sling by apache.

the class Util method setupColumnWidths.

static void setupColumnWidths(final TableColumnModel tcm, final String propertyName) {
    PropertyChangeListener pcl = new PropertyChangeListener() {

        private final String pclPropName = propertyName;

        private final TableColumnModel pclTcm = tcm;

        public void propertyChange(PropertyChangeEvent evt) {
            if ("width".equals(evt.getPropertyName())) {
                int[] colWidths = new int[pclTcm.getColumnCount()];
                for (int i = 0; i < colWidths.length; i++) {
                    colWidths[i] = pclTcm.getColumn(i).getWidth();
                }
                setPreference(pclPropName, colWidths, true);
            }
        }
    };
    int[] colWidths = getPreference(propertyName, new int[0]);
    for (int i = 0; i < colWidths.length && i < tcm.getColumnCount(); i++) {
        tcm.getColumn(i).setPreferredWidth(colWidths[i]);
    }
    for (int i = 0; i < tcm.getColumnCount(); i++) {
        tcm.getColumn(i).addPropertyChangeListener(pcl);
    }
}
Also used : PropertyChangeEvent(java.beans.PropertyChangeEvent) PropertyChangeListener(java.beans.PropertyChangeListener) TableColumnModel(javax.swing.table.TableColumnModel)

Aggregations

TableColumnModel (javax.swing.table.TableColumnModel)168 TableColumn (javax.swing.table.TableColumn)66 JTable (javax.swing.JTable)34 JScrollPane (javax.swing.JScrollPane)31 JButton (javax.swing.JButton)30 BoxLayout (javax.swing.BoxLayout)24 TableCellEditor (javax.swing.table.TableCellEditor)23 ButtonEditor (jmri.util.table.ButtonEditor)23 ButtonRenderer (jmri.util.table.ButtonRenderer)23 JLabel (javax.swing.JLabel)22 JPanel (javax.swing.JPanel)22 ActionEvent (java.awt.event.ActionEvent)20 ActionListener (java.awt.event.ActionListener)16 FlowLayout (java.awt.FlowLayout)15 Border (javax.swing.border.Border)13 Iterator (java.util.Iterator)12 Set (java.util.Set)12 JTableHeader (javax.swing.table.JTableHeader)12 TableCellRenderer (javax.swing.table.TableCellRenderer)12 Container (java.awt.Container)10