Search in sources :

Example 1 with HierarchyBoundsAdapter

use of java.awt.event.HierarchyBoundsAdapter in project omegat by omegat-org.

the class TableColumnSizer method init.

/**
 * Add various listeners to keep the columns in sync with the table's
 * current width.
 */
private void init() {
    table.getColumnModel().addColumnModelListener(colListener);
    table.getModel().addTableModelListener(modelListener);
    table.addPropertyChangeListener(new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            Object oldVal = evt.getOldValue();
            Object newVal = evt.getNewValue();
            if (newVal != null && newVal.equals(evt.getOldValue())) {
                return;
            }
            boolean shouldAdjust = false;
            if (evt.getPropertyName().equals("columnModel")) {
                if (newVal != null && newVal instanceof TableColumnModel) {
                    ((TableColumnModel) newVal).addColumnModelListener(colListener);
                    shouldAdjust = true;
                }
                if (oldVal != null && oldVal instanceof TableColumnModel) {
                    ((TableColumnModel) oldVal).removeColumnModelListener(colListener);
                }
            } else if (evt.getPropertyName().equals("model")) {
                if (newVal != null && newVal instanceof TableModel) {
                    ((TableModel) newVal).addTableModelListener(modelListener);
                    shouldAdjust = true;
                }
                if (oldVal != null && oldVal instanceof TableModel) {
                    ((TableModel) oldVal).removeTableModelListener(modelListener);
                }
            } else if (evt.getPropertyName().equals("font")) {
                if ((newVal != null && !newVal.equals(oldVal)) || (oldVal != null && !oldVal.equals(newVal))) {
                    shouldAdjust = true;
                }
            }
            if (shouldAdjust) {
                reset();
                // Queue up the adjustment because when the table has a
                // RowSorter things can be out of sync at this point.
                SwingUtilities.invokeLater(TableColumnSizer.this::adjustTableColumns);
            }
        }
    });
    table.addHierarchyBoundsListener(new HierarchyBoundsAdapter() {

        @Override
        public void ancestorResized(HierarchyEvent e) {
            adjustTableColumns();
        }
    });
}
Also used : PropertyChangeEvent(java.beans.PropertyChangeEvent) PropertyChangeListener(java.beans.PropertyChangeListener) HierarchyEvent(java.awt.event.HierarchyEvent) TableColumnModel(javax.swing.table.TableColumnModel) HierarchyBoundsAdapter(java.awt.event.HierarchyBoundsAdapter) TableModel(javax.swing.table.TableModel)

Aggregations

HierarchyBoundsAdapter (java.awt.event.HierarchyBoundsAdapter)1 HierarchyEvent (java.awt.event.HierarchyEvent)1 PropertyChangeEvent (java.beans.PropertyChangeEvent)1 PropertyChangeListener (java.beans.PropertyChangeListener)1 TableColumnModel (javax.swing.table.TableColumnModel)1 TableModel (javax.swing.table.TableModel)1