use of javax.swing.table.TableModel in project jdk8u_jdk by JetBrains.
the class ImageableAreaTest method createAuthorTable.
private static JTable createAuthorTable(int rows) {
final String[] headers = { "Book", "Title" };
final Object[][] data = new Object[rows][2];
for (int i = 0; i < rows; i++) {
int n = i + 1;
data[i] = new Object[] { "Book: " + n, "Title: " + n };
}
TableModel dataModel = new AbstractTableModel() {
public int getColumnCount() {
return headers.length;
}
public int getRowCount() {
return data.length;
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public String getColumnName(int column) {
return headers[column];
}
public Class getColumnClass(int col) {
return getValueAt(0, col).getClass();
}
public void setValueAt(Object aValue, int row, int column) {
data[row][column] = aValue;
}
public boolean isCellEditable(int row, int col) {
return false;
}
};
JTable table = new JTable(dataModel);
table.setGridColor(Color.BLUE);
table.setBackground(Color.WHITE);
table.setForeground(Color.BLACK);
table.setSize(600, 800);
frame = new JFrame();
frame.setSize(400, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(table);
frame.setVisible(true);
return table;
}
use of javax.swing.table.TableModel in project jdk8u_jdk by JetBrains.
the class bug6937798 method createCountryTable.
protected JTable createCountryTable() {
// Column headers
final String[] headers = { "Name", "Capital City", "Language(s)", "Monetary Unit(s)", "EC Member" };
// Table data
final Object[][] data = { { "Albania", "Tirane", "Albanian, Greek", "Lek", new Boolean(false) }, { "Andorra", "Andorra la Vella", "Catalan, French, Spanish", "French Franc, Spanish Peseta", new Boolean(false) }, { "Austria", "Vienna", "German, Slovenian, Croatian", "Schilling", new Boolean(false) }, { "Belarus", "Minsk", "Byelorussian, Russian", "Belarusian Rubel", new Boolean(false) }, { "Belgium", "Brussels", "French, Flemish, German", "Belgian Franc", new Boolean(true) }, { "Bosnia & Herzegovina", "Sarajevo", "Serbo-Croatian", "Dinar", new Boolean(false) }, { "Bulgaria", "Sofia", "Bulgarian, Turkish", "Lev", new Boolean(false) }, { "Croatia", "Zagreb", "Serbo-Croatian", "Croatian Kuna", new Boolean(false) }, { "Czech Republic", "Prague", "Czech, Slovak", "Koruna", new Boolean(false) }, { "Denmark", "Copenhagen", "Danish", "Krone", new Boolean(true) }, { "Estonia", "Tallinn", "Estonian, Latvian, Lithuanian, Russian", "Estonian Kroon", new Boolean(false) }, { "Finland", "Helsinki", "Finnish, Swedish, Lappish", "Markka", new Boolean(false) }, { "France", "Paris", "French", "Franc", new Boolean(true) }, { "Germany", "Berlin", "German", "Deutsche Mark", new Boolean(true) }, { "Greece", "Athens", "Greek, English, French", "Drachma", new Boolean(true) }, { "Hungary", "Budapest", "Hungarian", "Forint", new Boolean(false) }, { "Iceland", "Reykjavik", "Icelandic", "Icelandic Krona", new Boolean(false) }, { "Ireland", "Dublin", "Irish, English", "Pound", new Boolean(true) }, { "Italy", "Rome", "Italian", "Lira", new Boolean(true) }, { "Latvia", "Riga", "Lettish, Lithuanian, Russian", "Lat", new Boolean(false) }, { "Liechtenstein", "Vaduz", "German", "Swiss Franc", new Boolean(false) }, { "Lithuania", "Vilnius", "Lithuanian, Polish, Russian", "Lita", new Boolean(false) }, { "Luxembourg", "Luxembourg", "French, German, Letzeburgesch", "Luxembourg Franc", new Boolean(true) }, { "Macedonia", "Skopje", "Macedonian, Albanian, Turkish, Serbo-Croatian", "Denar", new Boolean(false) }, { "Malta", "Valletta", "Maltese, English", "Maltese Lira", new Boolean(false) }, { "Moldova", "Chisinau", "Moldovan, Russian", "Leu", new Boolean(false) }, { "Monaco", "Monaco", "French, English, Italian", "French Franc", new Boolean(false) }, { "the Netherlands", "Amsterdam", "Dutch", "Guilder", new Boolean(true) }, { "Norway", "Oslo", "Norwegian", "Krone", new Boolean(false) }, { "Poland", "Warsaw", "Polish", "Zloty", new Boolean(false) }, { "Portugal", "Lisbon", "Portuguese", "Escudo", new Boolean(true) }, { "Romania", "Bucharest", "Romanian", "Leu", new Boolean(false) }, { "Russia", "Moscow", "Russian", "Ruble", new Boolean(false) }, { "San Marino", "San Marino", "Italian", "Italian Lira", new Boolean(false) }, { "Slovakia", "Bratislava", "Slovak, Hungarian", "Koruna", new Boolean(false) }, { "Slovenia", "Ljubljana", "Slovenian, Serbo-Croatian", "Tolar", new Boolean(false) }, { "Spain", "Madrid", "Spanish", "Peseta", new Boolean(true) }, { "Sweden", "Stockholm", "Swedish", "Krona", new Boolean(false) }, { "Switzerland", "Bern", "German, French, Italian", "Swiss Franc", new Boolean(false) }, { "Turkey", "Ankara", "Turkish", "Turkish Lira", new Boolean(false) }, { "Ukraine", "Kiev", "Ukranian, Russian, Romanian, Polish, Hungarian", "Hryvnia", new Boolean(false) }, { "United Kingdom", "London", "English, Welsh", "British Pound", new Boolean(true) }, { "Yugoslavia", "Belgrade", "Serbo-Croatian, Slovenian, Macedonian", "Dinar", new Boolean(false) } };
// Table model
TableModel dataModel = new AbstractTableModel() {
public int getColumnCount() {
return headers.length;
}
public int getRowCount() {
return data.length;
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public String getColumnName(int column) {
return headers[column];
}
public Class getColumnClass(int col) {
return getValueAt(0, col).getClass();
}
public void setValueAt(Object aValue, int row, int column) {
data[row][column] = aValue;
}
public boolean isCellEditable(int row, int col) {
return (col == 4);
}
};
// Create table with table model
JTable countryTable = new JTable(dataModel);
countryTable.setGridColor(Color.pink);
countryTable.setBackground(Color.white);
countryTable.setForeground(Color.black);
return countryTable;
}
use of javax.swing.table.TableModel in project JMRI by JMRI.
the class NodeConfigFrame method initComponents.
/**
* Initialize the config window
*/
@Override
public void initComponents() {
setTitle(rb.getString("WindowTitle"));
Container contentPane = getContentPane();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
// Set up node address and node type
JPanel panel1 = new JPanel();
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
JPanel panel11 = new JPanel();
panel11.setLayout(new FlowLayout());
// NOI18N
panel11.add(new JLabel(rb.getString("LabelNodeAddress") + " "));
panel11.add(nodeAddrField);
// NOI18N
nodeAddrField.setToolTipText(rb.getString("TipNodeAddress"));
nodeAddrField.setText("0");
panel11.add(nodeAddrStatic);
nodeAddrStatic.setVisible(false);
// NOI18N
panel11.add(new JLabel(" " + rb.getString("LabelNodeType") + " "));
nodeTypeBox = new JComboBox<>();
panel11.add(nodeTypeBox);
// NOI18N
nodeTypeBox.addItem("SMINI");
// NOI18N
nodeTypeBox.addItem("USIC_SUSIC");
// Here add code for other types of nodes
nodeTypeBox.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent event) {
String s = (String) nodeTypeBox.getSelectedItem();
if (s.equals("SMINI")) {
// NOI18N
panel2.setVisible(false);
panel2a.setVisible(true);
cardSizeText.setVisible(false);
cardSizeBox.setVisible(false);
nodeType = SerialNode.SMINI;
} else if (s.equals("USIC_SUSIC")) {
// NOI18N
panel2.setVisible(true);
panel2a.setVisible(false);
cardSizeText.setVisible(true);
cardSizeBox.setVisible(true);
nodeType = SerialNode.USIC_SUSIC;
}
// Here add code for other types of nodes
// reset notes as appropriate
resetNotes();
}
});
// NOI18N
nodeTypeBox.setToolTipText(rb.getString("TipNodeType"));
JPanel panel12 = new JPanel();
panel12.setLayout(new FlowLayout());
// NOI18N
panel12.add(new JLabel(rb.getString("LabelDelay") + " "));
panel12.add(receiveDelayField);
// NOI18N
receiveDelayField.setToolTipText(rb.getString("TipDelay"));
receiveDelayField.setText("0");
panel12.add(cardSizeText);
cardSizeBox = new JComboBox<>();
panel12.add(cardSizeBox);
// NOI18N
cardSizeBox.addItem(rb.getString("CardSize24"));
// NOI18N
cardSizeBox.addItem(rb.getString("CardSize32"));
// here add code for other node types, if required
cardSizeBox.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent event) {
String s = (String) cardSizeBox.getSelectedItem();
if (s.equals(rb.getString("CardSize24"))) {
// NOI18N
bitsPerCard = 24;
} else if (s.equals(rb.getString("CardSize32"))) {
// NOI18N
bitsPerCard = 32;
}
// here add code for other node types, if required
}
});
// NOI18N
cardSizeBox.setToolTipText(rb.getString("TipCardSize"));
cardSizeText.setVisible(false);
cardSizeBox.setVisible(false);
JPanel panel13 = new JPanel();
panel13.setLayout(new FlowLayout());
// NOI18N
panel13.add(new JLabel(rb.getString("LabelPulseWidth") + " "));
panel13.add(pulseWidthField);
// NOI18N
pulseWidthField.setToolTipText(rb.getString("TipPulseWidth"));
pulseWidthField.setText("500");
// NOI18N
panel13.add(new JLabel(rb.getString("LabelMilliseconds")));
panel1.add(panel11);
panel1.add(panel12);
panel1.add(panel13);
contentPane.add(panel1);
// Set up USIC/SUSIC card type configuration table
JPanel panel21 = new JPanel();
panel21.setLayout(new BoxLayout(panel21, BoxLayout.Y_AXIS));
// NOI18N
panel21.add(new JLabel(rb.getString("HintCardTypePartA")));
// NOI18N
panel21.add(new JLabel(" " + rb.getString("HintCardTypePartB")));
// NOI18N
panel21.add(new JLabel(" " + rb.getString("HintCardTypePartC")));
// NOI18N
panel21.add(new JLabel(" "));
// NOI18N
panel21.add(new JLabel(rb.getString("HintCardTypePartD")));
// NOI18N
panel21.add(new JLabel(" " + rb.getString("HintCardTypePartE")));
// NOI18N
panel21.add(new JLabel(" " + rb.getString("HintCardTypePartF")));
panel2.add(panel21);
TableModel cardConfigModel = new CardConfigModel();
JTable cardConfigTable = new JTable(cardConfigModel);
cardConfigTable.setRowSelectionAllowed(false);
cardConfigTable.setPreferredScrollableViewportSize(new java.awt.Dimension(180, 100));
JComboBox<String> cardTypeCombo = new JComboBox<>();
// NOI18N
cardTypeCombo.addItem(rb.getString("CardTypeOutput"));
// NOI18N
cardTypeCombo.addItem(rb.getString("CardTypeInput"));
// NOI18N
cardTypeCombo.addItem(rb.getString("CardTypeNone"));
TableColumnModel typeColumnModel = cardConfigTable.getColumnModel();
TableColumn addressColumn = typeColumnModel.getColumn(CardConfigModel.ADDRESS_COLUMN);
addressColumn.setMinWidth(70);
addressColumn.setMaxWidth(80);
TableColumn cardTypeColumn = typeColumnModel.getColumn(CardConfigModel.TYPE_COLUMN);
cardTypeColumn.setCellEditor(new DefaultCellEditor(cardTypeCombo));
cardTypeColumn.setResizable(false);
cardTypeColumn.setMinWidth(90);
cardTypeColumn.setMaxWidth(100);
JScrollPane cardScrollPane = new JScrollPane(cardConfigTable);
panel2.add(cardScrollPane, BorderLayout.CENTER);
contentPane.add(panel2);
panel2.setVisible(false);
// Set up SMINI oscillating 2-lead searchlight configuration table
JPanel panel2a1 = new JPanel();
panel2a1.setLayout(new BoxLayout(panel2a1, BoxLayout.Y_AXIS));
// NOI18N
panel2a1.add(new JLabel(rb.getString("HintSearchlightPartA")));
// NOI18N
panel2a1.add(new JLabel(" " + rb.getString("HintSearchlightPartB")));
// NOI18N
panel2a1.add(new JLabel(" " + rb.getString("HintSearchlightPartC")));
// NOI18N
panel2a1.add(new JLabel(" "));
// NOI18N
panel2a1.add(new JLabel(rb.getString("HintSearchlightPartD")));
// NOI18N
panel2a1.add(new JLabel(" " + rb.getString("HintSearchlightPartE")));
// NOI18N
panel2a1.add(new JLabel(" " + rb.getString("HintSearchlightPartF")));
panel2a.add(panel2a1);
TableModel searchlightConfigModel = new SearchlightConfigModel();
JTable searchlightConfigTable = new JTable(searchlightConfigModel);
searchlightConfigTable.setRowSelectionAllowed(false);
searchlightConfigTable.setPreferredScrollableViewportSize(new java.awt.Dimension(208, 100));
TableColumnModel searchlightColumnModel = searchlightConfigTable.getColumnModel();
TableColumn portColumn = searchlightColumnModel.getColumn(SearchlightConfigModel.PORT_COLUMN);
portColumn.setMinWidth(90);
portColumn.setMaxWidth(100);
JScrollPane searchlightScrollPane = new JScrollPane(searchlightConfigTable);
panel2a.add(searchlightScrollPane, BorderLayout.CENTER);
contentPane.add(panel2a);
// Set up the notes panel
JPanel panel3 = new JPanel();
panel3.setLayout(new BoxLayout(panel3, BoxLayout.Y_AXIS));
JPanel panel31 = new JPanel();
panel31.setLayout(new FlowLayout());
statusText1.setText(stdStatus1);
statusText1.setVisible(true);
panel31.add(statusText1);
JPanel panel32 = new JPanel();
panel32.setLayout(new FlowLayout());
statusText2.setText(stdStatus2);
statusText2.setVisible(true);
panel32.add(statusText2);
JPanel panel33 = new JPanel();
panel33.setLayout(new FlowLayout());
statusText3.setText(stdStatus3);
statusText3.setVisible(true);
panel33.add(statusText3);
panel3.add(panel31);
panel3.add(panel32);
panel3.add(panel33);
Border panel3Border = BorderFactory.createEtchedBorder();
Border panel3Titled = BorderFactory.createTitledBorder(panel3Border, rb.getString("BoxLabelNotes"));
panel3.setBorder(panel3Titled);
contentPane.add(panel3);
// Set up buttons
JPanel panel4 = new JPanel();
panel4.setLayout(new FlowLayout());
// NOI18N
addButton.setText(rb.getString("ButtonAdd"));
addButton.setVisible(true);
// NOI18N
addButton.setToolTipText(rb.getString("TipAddButton"));
addButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
addButtonActionPerformed();
}
});
panel4.add(addButton);
// NOI18N
editButton.setText(rb.getString("ButtonEdit"));
editButton.setVisible(true);
// NOI18N
editButton.setToolTipText(rb.getString("TipEditButton"));
panel4.add(editButton);
editButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
editButtonActionPerformed();
}
});
panel4.add(deleteButton);
// NOI18N
deleteButton.setText(rb.getString("ButtonDelete"));
deleteButton.setVisible(true);
// NOI18N
deleteButton.setToolTipText(rb.getString("TipDeleteButton"));
panel4.add(deleteButton);
deleteButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
deleteButtonActionPerformed();
}
});
panel4.add(doneButton);
// NOI18N
doneButton.setText(rb.getString("ButtonDone"));
doneButton.setVisible(true);
// NOI18N
doneButton.setToolTipText(rb.getString("TipDoneButton"));
panel4.add(doneButton);
doneButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
doneButtonActionPerformed();
}
});
panel4.add(updateButton);
// NOI18N
updateButton.setText(rb.getString("ButtonUpdate"));
updateButton.setVisible(true);
// NOI18N
updateButton.setToolTipText(rb.getString("TipUpdateButton"));
panel4.add(updateButton);
updateButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
updateButtonActionPerformed();
}
});
updateButton.setVisible(false);
panel4.add(cancelButton);
// NOI18N
cancelButton.setText(rb.getString("ButtonCancel"));
cancelButton.setVisible(true);
// NOI18N
cancelButton.setToolTipText(rb.getString("TipCancelButton"));
panel4.add(cancelButton);
cancelButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
cancelButtonActionPerformed();
}
});
cancelButton.setVisible(false);
contentPane.add(panel4);
// pack for display
pack();
}
use of javax.swing.table.TableModel in project JMRI by JMRI.
the class EcosLocoTableAction method createModel.
@Override
protected void createModel() {
m = new BeanTableDataModel() {
//We have to set a manager first off, but this gets replaced.
@Override
protected EcosLocoAddressManager getManager() {
return locoManager;
}
protected String getRosterAttribute() {
return rosterAttribute;
}
@Override
public String getValue(String s) {
return "Set";
}
@Override
protected String getMasterClassName() {
return getClassName();
}
@Override
public void clickOn(jmri.NamedBean t) {
}
@Override
protected synchronized void updateNameList() {
// first, remove listeners from the individual objects
if (ecosObjectIdList != null) {
for (int i = 0; i < ecosObjectIdList.size(); i++) {
// if object has been deleted, it's not here; ignore it
jmri.jmrix.ecos.EcosLocoAddress b = getByEcosObject(ecosObjectIdList.get(i));
if (b != null) {
b.removePropertyChangeListener(this);
}
}
}
ecosObjectIdList = getManager().getEcosObjectList();
// and add them back in
for (int i = 0; i < ecosObjectIdList.size(); i++) {
getByEcosObject(ecosObjectIdList.get(i)).addPropertyChangeListener(this);
}
}
@Override
public void propertyChange(java.beans.PropertyChangeEvent e) {
updateNameList();
if (e.getPropertyName().equals("length")) {
// a new jmri.jmrix.ecos.EcosLocoAddressManager is available in the manager
updateNameList();
fireTableDataChanged();
} else if (matchPropertyName(e)) {
// a value changed. Find it, to avoid complete redraw
String object = ((jmri.jmrix.ecos.EcosLocoAddress) e.getSource()).getEcosObject();
// since we can add columns, the entire row is marked as updated
int row = ecosObjectIdList.indexOf(object);
fireTableRowsUpdated(row, row);
}
}
@Override
public int getColumnCount() {
return STOP + 1;
}
@Override
public void setValueAt(Object value, int row, int col) {
if (col == COMMENTCOL) {
RosterEntry re;
String ecosObjectNo = ecosObjectIdList.get(row);
if (value == null) {
return;
} else if (value instanceof RosterEntry) {
re = (RosterEntry) value;
if ((re.getAttribute(getRosterAttribute()) != null && !re.getAttribute(getRosterAttribute()).equals(""))) {
JOptionPane.showMessageDialog(f, ecosObjectNo + " This roster entry already has an ECOS loco assigned to it ");
log.error(ecosObjectNo + " This roster entry already has an ECOS loco assigned to it ");
return;
}
String oldRoster = getByEcosObject(ecosObjectNo).getRosterId();
RosterEntry oldre;
if (oldRoster != null) {
oldre = Roster.getDefault().getEntryForId(oldRoster);
if (oldre != null) {
oldre.deleteAttribute(getRosterAttribute());
}
}
re.putAttribute(getRosterAttribute(), ecosObjectNo);
getByEcosObject(ecosObjectNo).setRosterId(re.getId());
re.updateFile();
} else if (value instanceof String) {
List<RosterEntry> r = Roster.getDefault().getEntriesWithAttributeKeyValue(getRosterAttribute(), ecosObjectNo);
if (r.isEmpty()) {
r.get(0).deleteAttribute(getRosterAttribute());
getByEcosObject(ecosObjectNo).setRosterId(null);
r.get(0).updateFile();
}
}
Roster.getDefault().writeRoster();
} else if (col == ADDTOROSTERCOL) {
addToRoster(row, col);
} else if (col == STOP) {
stopLoco(row, col);
} else if (col == DELETECOL) {
// button fired, delete Bean
deleteLoco(row, col);
} else if (col == USERNAMECOL) {
jmri.jmrix.ecos.EcosLocoAddress b = getByEcosObject(ecosObjectIdList.get(row));
EcosMessage m = new EcosMessage("request(" + b.getEcosObject() + ", control, force)");
adaptermemo.getTrafficController().sendEcosMessage(m, null);
m = new EcosMessage("set(" + b.getEcosObject() + ", name[\"" + (String) value + "\"])");
adaptermemo.getTrafficController().sendEcosMessage(m, null);
m = new EcosMessage("release(" + b.getEcosObject() + ", control)");
adaptermemo.getTrafficController().sendEcosMessage(m, null);
}
}
/**
* {@inheritDoc }
*/
@Override
@Deprecated
public JTable makeJTable(TableSorter sorter) {
return this.makeJTable((TableModel) sorter);
}
@Override
public JTable makeJTable(@Nonnull String name, @Nonnull TableModel model, @Nullable RowSorter sorter) {
JTable table = this.makeJTable(model);
table.setName(name);
table.setRowSorter(sorter);
return table;
}
private JTable makeJTable(@Nonnull TableModel model) {
JTable table = new JTable(model) {
@Override
public TableCellRenderer getCellRenderer(int row, int column) {
if (column == COMMENTCOL) {
return getRenderer(row);
} else {
return super.getCellRenderer(row, column);
}
}
@Override
public TableCellEditor getCellEditor(int row, int column) {
if (column == COMMENTCOL) {
return getEditor(row);
} else {
return super.getCellEditor(row, column);
}
}
TableCellRenderer getRenderer(int row) {
TableCellRenderer retval = rendererMap.get(ecosObjectIdList.get(row));
if (retval == null) {
jmri.jmrix.ecos.EcosLocoAddress b = getByEcosObject(ecosObjectIdList.get(row));
RosterEntry re = null;
if (b != null) {
re = Roster.getDefault().getEntryForId(b.getRosterId());
}
retval = new RosterBoxRenderer(re);
rendererMap.put(ecosObjectIdList.get(row), retval);
}
return retval;
}
Hashtable<Object, TableCellRenderer> rendererMap = new Hashtable<>();
TableCellEditor getEditor(int row) {
TableCellEditor retval = editorMap.get(ecosObjectIdList.get(row));
if (retval == null) {
jmri.jmrix.ecos.EcosLocoAddress b = getByEcosObject(ecosObjectIdList.get(row));
RosterEntry re = null;
if (b != null) {
re = Roster.getDefault().getEntryForId(b.getRosterId());
}
GlobalRosterEntryComboBox cb = new GlobalRosterEntryComboBox();
cb.setNonSelectedItem(" ");
if (re == null) {
cb.setSelectedIndex(0);
} else {
cb.setSelectedItem(re);
}
// create a new one with right aspects
retval = new RosterComboBoxEditor(cb);
editorMap.put(ecosObjectIdList.get(row), retval);
}
return retval;
}
Hashtable<Object, TableCellEditor> editorMap = new Hashtable<>();
};
table.getTableHeader().setReorderingAllowed(true);
table.setColumnModel(new XTableColumnModel());
table.createDefaultColumnsFromModel();
addMouseListenerToHeader(table);
return table;
}
/**
* Is this property event announcing a change this table should
* display?
* <P>
* Note that events will come both from the
* jmri.jmrix.ecos.EcosLocoAddressManagers and also from the manager
*/
@Override
protected boolean matchPropertyName(java.beans.PropertyChangeEvent e) {
if (!showLocoMonitor && (e.getPropertyName().equals("Speed") || e.getPropertyName().equals("Direction"))) {
return false;
}
return true;
}
@Override
public String getColumnName(int col) {
switch(col) {
case SYSNAMECOL:
return "ECoS Object Id";
case USERNAMECOL:
return "ECoS Descritpion";
case VALUECOL:
return "ECoS Address";
case COMMENTCOL:
return "JMRI Roster Id";
case DELETECOL:
return "Delete";
case PROTOCOL:
return "ECOS Protocol";
case ADDTOROSTERCOL:
return "Add to Roster";
case SPEEDDIR:
return "Speed Direction";
case STOP:
return "Stop";
default:
return "unknown";
}
}
@Override
public Class<?> getColumnClass(int col) {
switch(col) {
case SYSNAMECOL:
case USERNAMECOL:
case VALUECOL:
case PROTOCOL:
case SPEEDDIR:
return String.class;
case ADDTOROSTERCOL:
case DELETECOL:
case STOP:
return JButton.class;
case COMMENTCOL:
return JComboBox.class;
default:
return null;
}
}
@Override
public boolean isCellEditable(int row, int col) {
switch(col) {
case COMMENTCOL:
return true;
case ADDTOROSTERCOL:
jmri.jmrix.ecos.EcosLocoAddress b = getByEcosObject(ecosObjectIdList.get(row));
if (b.getRosterId() == null || b.getRosterId().equals("")) {
return true;
} else {
return false;
}
case USERNAMECOL:
case DELETECOL:
case STOP:
return true;
default:
return false;
}
}
@Override
public int getPreferredWidth(int col) {
switch(col) {
case SYSNAMECOL:
return new JTextField(5).getPreferredSize().width;
case COMMENTCOL:
return new JTextField(20).getPreferredSize().width;
case USERNAMECOL:
return new JTextField(20).getPreferredSize().width;
case // not actually used due to the configureTable, setColumnToHoldButton, configureButton
ADDTOROSTERCOL:
return new JTextField(12).getPreferredSize().width;
case // not actually used due to the configureTable, setColumnToHoldButton, configureButton
STOP:
return new JTextField(6).getPreferredSize().width;
case VALUECOL:
return new JTextField(5).getPreferredSize().width;
case SPEEDDIR:
return new JTextField(10).getPreferredSize().width;
case PROTOCOL:
return new JTextField(5).getPreferredSize().width;
default:
//log.warn("Unexpected column in getPreferredWidth: "+col);
return super.getPreferredWidth(col);
}
}
@Override
public void configureTable(JTable tbl) {
table = tbl;
setColumnToHoldButton(table, ADDTOROSTERCOL, new JButton("Add to Roster"));
setColumnToHoldButton(table, STOP, stopButton());
super.configureTable(table);
XTableColumnModel columnModel = (XTableColumnModel) table.getColumnModel();
TableColumn column = columnModel.getColumnByModelIndex(SPEEDDIR);
columnModel.setColumnVisible(column, false);
column = columnModel.getColumnByModelIndex(STOP);
columnModel.setColumnVisible(column, false);
}
@Override
public NamedBean getBySystemName(String name) {
return null;
}
@Override
public NamedBean getByUserName(String name) {
return null;
}
@Override
public synchronized void dispose() {
showLocoMonitor = false;
getManager().removePropertyChangeListener(this);
if (ecosObjectIdList != null) {
for (int i = 0; i < ecosObjectIdList.size(); i++) {
jmri.jmrix.ecos.EcosLocoAddress b = getByEcosObject(ecosObjectIdList.get(i));
if (b != null) {
b.removePropertyChangeListener(this);
}
}
}
}
protected void deleteLoco(final int row, int col) {
if (row >= ecosObjectIdList.size()) {
log.debug("row is greater than list size");
return;
}
jmri.jmrix.ecos.EcosLocoAddress b = getByEcosObject(ecosObjectIdList.get(row));
final JDialog dialog = new JDialog();
dialog.setTitle("Remove Loco From ECOS");
dialog.setLocation(300, 200);
dialog.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
JPanel container = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
container.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JLabel question = new JLabel("Are you sure that you want to remove loco " + b.getEcosDescription() + " from the ECOS ?");
question.setAlignmentX(Component.CENTER_ALIGNMENT);
container.add(question);
JButton yesButton = new JButton("Yes");
JButton noButton = new JButton("No");
JPanel button = new JPanel();
button.setAlignmentX(Component.CENTER_ALIGNMENT);
button.add(yesButton);
button.add(noButton);
container.add(button);
noButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dialog.dispose();
}
});
yesButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
RemoveObjectFromEcos removeObjectFromEcos = new RemoveObjectFromEcos();
removeObjectFromEcos.removeObjectFromEcos(ecosObjectIdList.get(row), adaptermemo.getTrafficController());
dialog.dispose();
}
});
container.setAlignmentX(Component.CENTER_ALIGNMENT);
container.setAlignmentY(Component.CENTER_ALIGNMENT);
dialog.getContentPane().add(container);
dialog.pack();
dialog.setModal(true);
dialog.setVisible(true);
}
@Override
public int getRowCount() {
return ecosObjectIdList.size();
}
@Override
public Object getValueAt(int row, int col) {
if (row >= ecosObjectIdList.size()) {
log.debug("row is greater than list size");
return null;
}
jmri.jmrix.ecos.EcosLocoAddress b;
switch(col) {
case SYSNAMECOL:
return ecosObjectIdList.get(row);
case // return user name
USERNAMECOL:
// sometimes, the TableSorter invokes this on rows that no longer exist, so we check
b = getByEcosObject(ecosObjectIdList.get(row));
return (b != null) ? b.getEcosDescription() : null;
case //
VALUECOL:
b = getByEcosObject(ecosObjectIdList.get(row));
return (b != null) ? b.getNumber() : null;
case COMMENTCOL:
b = getByEcosObject(ecosObjectIdList.get(row));
RosterEntry re = null;
if (b != null) {
re = Roster.getDefault().getEntryForId(b.getRosterId());
}
GlobalRosterEntryComboBox cb = (GlobalRosterEntryComboBox) table.getCellRenderer(row, col);
if (re == null) {
cb.setSelectedIndex(0);
} else {
cb.setSelectedItem(re);
}
return re;
case PROTOCOL:
b = getByEcosObject(ecosObjectIdList.get(row));
return (b != null) ? b.getECOSProtocol() : null;
case //
ADDTOROSTERCOL:
b = getByEcosObject(ecosObjectIdList.get(row));
if (b.getRosterId() == null || b.getRosterId().equals("")) {
return "Add To Roster";
} else {
return " ";
}
case STOP:
return "Stop";
case SPEEDDIR:
b = getByEcosObject(ecosObjectIdList.get(row));
return (b != null) ? (b.getDirectionAsString() + " : " + b.getSpeed()) : null;
case //
DELETECOL:
return Bundle.getMessage("ButtonDelete");
default:
//log.error("internal state inconsistent with table requst for "+row+" "+col);
return null;
}
}
@Override
protected String getBeanType() {
return "Ecos Loco";
}
@Override
protected void showPopup(MouseEvent e) {
}
};
}
use of javax.swing.table.TableModel in project jabref by JabRef.
the class IntegrityCheckAction method actionPerformed.
@Override
public void actionPerformed(ActionEvent e) {
IntegrityCheck check = new IntegrityCheck(frame.getCurrentBasePanel().getBibDatabaseContext(), Globals.prefs.getFileDirectoryPreferences(), Globals.prefs.getBibtexKeyPatternPreferences(), Globals.journalAbbreviationLoader.getRepository(Globals.prefs.getJournalAbbreviationPreferences()));
List<IntegrityMessage> messages = check.checkBibtexDatabase();
if (messages.isEmpty()) {
JOptionPane.showMessageDialog(frame.getCurrentBasePanel(), Localization.lang("No problems found."));
} else {
Map<String, Boolean> showMessage = new HashMap<>();
// prepare data model
Object[][] model = new Object[messages.size()][4];
int i = 0;
for (IntegrityMessage message : messages) {
model[i][0] = message.getEntry().getId();
model[i][1] = message.getEntry().getCiteKeyOptional().orElse("");
model[i][2] = message.getFieldName();
model[i][3] = message.getMessage();
showMessage.put(message.getMessage(), true);
i++;
}
// construct view
JTable table = new JTable(model, new Object[] { "ID", Localization.lang("BibTeX key"), Localization.lang("Field"), Localization.lang("Message") });
// hide IDs
TableColumnModel columnModel = table.getColumnModel();
columnModel.removeColumn(columnModel.getColumn(0));
RowFilter<Object, Object> filter = new RowFilter<Object, Object>() {
@Override
public boolean include(Entry<?, ?> entry) {
return showMessage.get(entry.getStringValue(3));
}
};
TableRowSorter<TableModel> sorter = new TableRowSorter<>(table.getModel());
sorter.setRowFilter(filter);
table.setRowSorter(sorter);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setDefaultEditor(Object.class, null);
ListSelectionModel selectionModel = table.getSelectionModel();
selectionModel.addListSelectionListener(event -> {
if (!event.getValueIsAdjusting()) {
try {
String entryId = (String) model[table.convertRowIndexToModel(table.getSelectedRow())][0];
String fieldName = (String) model[table.convertRowIndexToModel(table.getSelectedRow())][2];
frame.getCurrentBasePanel().editEntryByIdAndFocusField(entryId, fieldName);
} catch (ArrayIndexOutOfBoundsException exception) {
}
}
});
// BibTeX key
table.getColumnModel().getColumn(0).setPreferredWidth(100);
// field name
table.getColumnModel().getColumn(1).setPreferredWidth(60);
// message
table.getColumnModel().getColumn(2).setPreferredWidth(400);
table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
JScrollPane scrollPane = new JScrollPane(table);
String title = Localization.lang("%0 problem(s) found", String.valueOf(messages.size()));
JDialog dialog = new JDialog(frame, title, false);
JPopupMenu menu = new JPopupMenu();
for (String messageString : showMessage.keySet()) {
JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(messageString, true);
menuItem.addActionListener(event -> {
showMessage.put(messageString, menuItem.isSelected());
((AbstractTableModel) table.getModel()).fireTableDataChanged();
});
menu.add(menuItem);
}
JButton menuButton = new JButton(Localization.lang("Filter"));
menuButton.addActionListener(entry -> menu.show(menuButton, 0, menuButton.getHeight()));
FormBuilder builder = FormBuilder.create().layout(new FormLayout("fill:pref:grow", "fill:pref:grow, 2dlu, pref"));
builder.add(scrollPane).xy(1, 1);
builder.add(menuButton).xy(1, 3, "c, b");
dialog.add(builder.getPanel());
dialog.setSize(600, 600);
// show view
dialog.setVisible(true);
}
}
Aggregations