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);
}
}
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);
}
}
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;
}
}
}
}
}
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());
}
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);
}
}
Aggregations