use of javax.swing.table.TableRowSorter in project ACS by ACS-Community.
the class AlarmTable method initGUI.
/**
* Init the GUI
*/
private void initGUI() {
setShowHorizontalLines(true);
// Build and set the selection model
selectionModel = new DefaultListSelectionModel();
selectionModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
setSelectionModel(selectionModel);
this.setOpaque(false);
sorter = new TableRowSorter<AlarmTableModel>(model);
this.setRowSorter(sorter);
sorter.setMaxSortKeys(2);
sorter.setSortsOnUpdates(true);
// Initially sort by timestamp
List<RowSorter.SortKey> sortKeys = new ArrayList<RowSorter.SortKey>();
sortKeys.add(new RowSorter.SortKey(AlarmTableColumn.PRIORITY.ordinal(), SortOrder.ASCENDING));
sortKeys.add(new RowSorter.SortKey(AlarmTableColumn.TIME.ordinal(), SortOrder.DESCENDING));
sorter.setSortKeys(sortKeys);
// Remove all the columns not visible at startup
TableColumnModel colModel = getColumnModel();
columns = new TableColumn[colModel.getColumnCount()];
for (int t = 0; t < columns.length; t++) {
columns[t] = colModel.getColumn(t);
columns[t].setIdentifier(AlarmTableColumn.values()[t]);
if (columns[t].getIdentifier() == AlarmTableColumn.ICON || columns[t].getIdentifier() == AlarmTableColumn.IS_CHILD || columns[t].getIdentifier() == AlarmTableColumn.IS_PARENT) {
columns[t].setWidth(20);
columns[t].setResizable(false);
columns[t].setPreferredWidth(20);
columns[t].setMaxWidth(20);
columns[t].setMinWidth(20);
} else if (columns[t].getIdentifier() == AlarmTableColumn.PRIORITY) {
BufferedImage bImg = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
Graphics2D g2D = bImg.createGraphics();
FontMetrics fm = g2D.getFontMetrics();
int sz = fm.stringWidth(PriorityLabel.VERY_HIGH.description);
columns[t].setPreferredWidth(sz + 6);
columns[t].setMaxWidth(sz + 8);
}
}
for (AlarmTableColumn col : AlarmTableColumn.values()) {
if (!col.visibleAtStartup) {
colModel.removeColumn(columns[col.ordinal()]);
}
}
buildPopupMenu();
addMouseListener(mouseAdapter);
getTableHeader().addMouseListener(new AlarmHeaderMouseAdapter());
// Set the tooltip
ToolTipManager ttm = ToolTipManager.sharedInstance();
ttm.setDismissDelay(Integer.MAX_VALUE);
ttm.setLightWeightPopupEnabled(true);
}
use of javax.swing.table.TableRowSorter in project EnrichmentMapApp by BaderLab.
the class HeatMapMainPanel method createTableHeader.
private void createTableHeader(int expressionColumnWidth) {
JTableHeader header = table.getTableHeader();
header.setReorderingAllowed(false);
HeatMapTableModel tableModel = (HeatMapTableModel) table.getModel();
TableColumnModel columnModel = table.getColumnModel();
TableCellRenderer vertRenderer = new ColumnHeaderVerticalRenderer();
TableCellRenderer vertRendererPheno1 = new ColumnHeaderVerticalRenderer(EMStyleBuilder.Colors.LIGHTEST_PHENOTYPE_1);
TableCellRenderer vertRendererPheno2 = new ColumnHeaderVerticalRenderer(EMStyleBuilder.Colors.LIGHTEST_PHENOTYPE_2);
TableColumn rankColumn = columnModel.getColumn(HeatMapTableModel.RANK_COL);
rankColumn.setHeaderRenderer(columnHeaderRankOptionRendererFactory.create(this, HeatMapTableModel.RANK_COL));
rankColumn.setPreferredWidth(100);
((TableRowSorter<?>) table.getRowSorter()).setSortable(HeatMapTableModel.RANK_COL, false);
int colCount = tableModel.getColumnCount();
for (int col = HeatMapTableModel.DESC_COL_COUNT; col < colCount; col++) {
EMDataSet dataset = tableModel.getDataSet(col);
String pheno1 = dataset.getEnrichments().getPhenotype1();
String pheno2 = dataset.getEnrichments().getPhenotype2();
Optional<String> pheno = tableModel.getPhenotype(col);
TableCellRenderer renderer;
if (pheno.filter(p -> p.equals(pheno1)).isPresent())
renderer = vertRendererPheno1;
else if (pheno.filter(p -> p.equals(pheno2)).isPresent())
renderer = vertRendererPheno2;
else
renderer = vertRenderer;
TableColumn column = columnModel.getColumn(col);
column.setHeaderRenderer(renderer);
column.setPreferredWidth(expressionColumnWidth);
}
}
use of javax.swing.table.TableRowSorter in project JMRI by JMRI.
the class SignalGroupSubTableAction method editHead.
/**
* Open an editor to set the details of a Signal Head as part of a Signal Group.
* Called when user clicks the Edit button for a Head in the Add/Edit Signal Group pane.
* @see SignalGroupTableAction#signalHeadEditPressed(int) SignalGroupTableAction.signalHeadEditPressed
* @param g Parent Signal Head
* @param headName System or User Name of this Signal Head
*/
void editHead(SignalGroup g, String headName) {
curSignalGroup = g;
curHeadName = headName;
curSignalHead = jmri.InstanceManager.getDefault(jmri.SignalHeadManager.class).getSignalHead(curHeadName);
// shows i18n strings from signal head definition
_OnAppearance = new JComboBox<String>(curSignalHead.getValidStateNames());
_OffAppearance = new JComboBox<String>(curSignalHead.getValidStateNames());
_systemName = new JLabel(headName);
_systemName.setVisible(true);
jmri.TurnoutManager tm = InstanceManager.turnoutManagerInstance();
List<String> systemNameList = tm.getSystemNameList();
_turnoutList = new ArrayList<SignalGroupTurnout>(systemNameList.size());
Iterator<String> iter = systemNameList.iterator();
while (iter.hasNext()) {
String systemName = iter.next();
String userName = tm.getBySystemName(systemName).getUserName();
_turnoutList.add(new SignalGroupTurnout(systemName, userName));
}
jmri.SensorManager sm = InstanceManager.sensorManagerInstance();
systemNameList = sm.getSystemNameList();
_sensorList = new ArrayList<SignalGroupSensor>(systemNameList.size());
iter = systemNameList.iterator();
while (iter.hasNext()) {
String systemName = iter.next();
Sensor sen = sm.getBySystemName(systemName);
if (sen != null) {
String userName = sen.getUserName();
_sensorList.add(new SignalGroupSensor(systemName, userName));
}
}
initializeIncludedList();
// Set up sub panel for editing of a Signal Group Signal Head item
if (addSubFrame == null) {
// create one if not yet available
addSubFrame = new JmriJFrame((Bundle.getMessage("EditSignalGroup") + " - " + Bundle.getMessage("BeanNameSignalHead")), false, true);
addSubFrame.addHelpMenu("package.jmri.jmrit.beantable.SignalGroupAddEdit", true);
addSubFrame.setLocation(100, 30);
addSubFrame.getContentPane().setLayout(new BoxLayout(addSubFrame.getContentPane(), BoxLayout.Y_AXIS));
Container contentPane = addSubFrame.getContentPane();
// add system name label
JPanel ps = new JPanel();
ps.setLayout(new FlowLayout());
ps.add(nameLabel);
ps.add(_systemName);
contentPane.add(ps);
// add user name label
JPanel pc = new JPanel();
pc.setLayout(new FlowLayout());
pc.add(signalOnStateLabel);
// comboBox to set On Appearance
pc.add(_OnAppearance);
_OnAppearance.setToolTipText(Bundle.getMessage("StateWhenMetTooltip"));
pc.add(spacer);
pc.add(signalOffStateLabel);
// comboBox to set On Appearance
pc.add(_OffAppearance);
_OffAppearance.setToolTipText(Bundle.getMessage("StateWhenNotMetTooltip"));
contentPane.add(pc);
JPanel p = new JPanel();
p.setLayout(new FlowLayout());
p.add(userLabel);
contentPane.add(p);
// fill in info for the Signal Head being configured
if (curSignalHead.getClass().getName().contains("SingleTurnoutSignalHead")) {
jmri.implementation.SingleTurnoutSignalHead stsh = (jmri.implementation.SingleTurnoutSignalHead) InstanceManager.getDefault(jmri.SignalHeadManager.class).getByUserName(curHeadName);
// we may use a user name in the editing pane, so look for that first
if (stsh == null) {
stsh = (jmri.implementation.SingleTurnoutSignalHead) InstanceManager.getDefault(jmri.SignalHeadManager.class).getBySystemName(curHeadName);
// when user name is empty, get by user name
}
if (stsh != null) {
log.debug("SGsubTA #279 editHead: setting props for signal head {}", curHeadName);
if ((g.getHeadOnState(curSignalHead) == 0x00) && (g.getHeadOffState(curSignalHead) == 0x00)) {
g.setHeadOnState(curSignalHead, stsh.getOnAppearance());
g.setHeadOffState(curSignalHead, stsh.getOffAppearance());
}
} else {
// nothing found
log.error("Failed to get signal head object named {}", curHeadName);
}
}
setSignalHeadStateBox(g.getHeadOnState(curSignalHead), _OnAppearance);
setSignalHeadStateBox(g.getHeadOffState(curSignalHead), _OffAppearance);
// add Turnout Display Choice
JPanel py = new JPanel();
py.add(new JLabel(Bundle.getMessage("Show")));
selGroup = new ButtonGroup();
allButton = new JRadioButton(Bundle.getMessage("All"), true);
selGroup.add(allButton);
py.add(allButton);
allButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Setup for display of all Turnouts, if needed
if (!showAll) {
showAll = true;
_SignalGroupTurnoutModel.fireTableDataChanged();
_SignalGroupSensorModel.fireTableDataChanged();
}
}
});
includedButton = new JRadioButton(Bundle.getMessage("Included"), false);
selGroup.add(includedButton);
py.add(includedButton);
includedButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Setup for display of included Turnouts only, if needed
if (showAll) {
showAll = false;
initializeIncludedList();
_SignalGroupTurnoutModel.fireTableDataChanged();
_SignalGroupSensorModel.fireTableDataChanged();
}
}
});
py.add(new JLabel(" " + Bundle.getMessage("_and_", Bundle.getMessage("Turnouts"), Bundle.getMessage("Sensors"))));
contentPane.add(py);
// add turnout table
p2xt = new JPanel();
JPanel p2xtSpace = new JPanel();
p2xtSpace.setLayout(new BoxLayout(p2xtSpace, BoxLayout.Y_AXIS));
p2xtSpace.add(new JLabel("XXX"));
p2xt.add(p2xtSpace);
JPanel p21t = new JPanel();
p21t.setLayout(new BoxLayout(p21t, BoxLayout.Y_AXIS));
p21t.add(new JLabel(Bundle.getMessage("SelectInGroup", Bundle.getMessage("Turnouts"))));
p2xt.add(p21t);
_SignalGroupTurnoutModel = new SignalGroupTurnoutModel();
JTable SignalGroupTurnoutTable = new JTable(_SignalGroupTurnoutModel);
TableRowSorter<SignalGroupTurnoutModel> sgtSorter = new TableRowSorter<>(_SignalGroupTurnoutModel);
sgtSorter.setComparator(SignalGroupTurnoutModel.SNAME_COLUMN, new SystemNameComparator());
RowSorterUtil.setSortOrder(sgtSorter, SignalGroupTurnoutModel.SNAME_COLUMN, SortOrder.ASCENDING);
SignalGroupTurnoutTable.setRowSorter(sgtSorter);
SignalGroupTurnoutTable.setRowSelectionAllowed(false);
SignalGroupTurnoutTable.setPreferredScrollableViewportSize(new java.awt.Dimension(480, 80));
ROW_HEIGHT = SignalGroupTurnoutTable.getRowHeight();
JComboBox<String> stateTCombo = new JComboBox<String>();
stateTCombo.addItem(SET_TO_CLOSED);
stateTCombo.addItem(SET_TO_THROWN);
TableColumnModel SignalGroupTurnoutColumnModel = SignalGroupTurnoutTable.getColumnModel();
TableColumn includeColumnT = SignalGroupTurnoutColumnModel.getColumn(SignalGroupTurnoutModel.INCLUDE_COLUMN);
includeColumnT.setResizable(false);
includeColumnT.setMinWidth(50);
includeColumnT.setMaxWidth(60);
TableColumn sNameColumnT = SignalGroupTurnoutColumnModel.getColumn(SignalGroupTurnoutModel.SNAME_COLUMN);
sNameColumnT.setResizable(true);
sNameColumnT.setMinWidth(75);
sNameColumnT.setMaxWidth(95);
TableColumn uNameColumnT = SignalGroupTurnoutColumnModel.getColumn(SignalGroupTurnoutModel.UNAME_COLUMN);
uNameColumnT.setResizable(true);
uNameColumnT.setMinWidth(210);
uNameColumnT.setMaxWidth(260);
TableColumn stateColumnT = SignalGroupTurnoutColumnModel.getColumn(SignalGroupTurnoutModel.STATE_COLUMN);
stateColumnT.setCellEditor(new DefaultCellEditor(stateTCombo));
stateColumnT.setResizable(false);
stateColumnT.setMinWidth(90);
stateColumnT.setMaxWidth(100);
_SignalGroupTurnoutScrollPane = new JScrollPane(SignalGroupTurnoutTable);
p2xt.add(_SignalGroupTurnoutScrollPane, BorderLayout.CENTER);
contentPane.add(p2xt);
p2xt.setVisible(true);
JPanel po = new JPanel();
po.setLayout(new FlowLayout());
JLabel operLabel = new JLabel(Bundle.getMessage("ChooseOrAnd"));
po.add(operLabel);
po.add(_SensorTurnoutOper);
contentPane.add(po);
// add sensor table
p2xs = new JPanel();
JPanel p2xsSpace = new JPanel();
p2xsSpace.setLayout(new BoxLayout(p2xsSpace, BoxLayout.Y_AXIS));
p2xsSpace.add(new JLabel("XXX"));
p2xs.add(p2xsSpace);
JPanel p21s = new JPanel();
p21s.setLayout(new BoxLayout(p21s, BoxLayout.Y_AXIS));
p21s.add(new JLabel(Bundle.getMessage("SelectInGroup", Bundle.getMessage("Sensors"))));
p2xs.add(p21s);
_SignalGroupSensorModel = new SignalGroupSensorModel();
JTable SignalGroupSensorTable = new JTable(_SignalGroupSensorModel);
TableRowSorter<SignalGroupSensorModel> sgsSorter = new TableRowSorter<>(_SignalGroupSensorModel);
sgsSorter.setComparator(SignalGroupSensorModel.SNAME_COLUMN, new SystemNameComparator());
RowSorterUtil.setSortOrder(sgsSorter, SignalGroupSensorModel.SNAME_COLUMN, SortOrder.ASCENDING);
SignalGroupSensorTable.setRowSorter(sgsSorter);
SignalGroupSensorTable.setRowSelectionAllowed(false);
SignalGroupSensorTable.setPreferredScrollableViewportSize(new java.awt.Dimension(480, 80));
JComboBox<String> stateSCombo = new JComboBox<String>();
stateSCombo.addItem(SET_TO_ACTIVE);
stateSCombo.addItem(SET_TO_INACTIVE);
TableColumnModel SignalGroupSensorColumnModel = SignalGroupSensorTable.getColumnModel();
TableColumn includeColumnS = SignalGroupSensorColumnModel.getColumn(SignalGroupSensorModel.INCLUDE_COLUMN);
includeColumnS.setResizable(false);
includeColumnS.setMinWidth(50);
includeColumnS.setMaxWidth(60);
TableColumn sNameColumnS = SignalGroupSensorColumnModel.getColumn(SignalGroupSensorModel.SNAME_COLUMN);
sNameColumnS.setResizable(true);
sNameColumnS.setMinWidth(75);
sNameColumnS.setMaxWidth(95);
TableColumn uNameColumnS = SignalGroupSensorColumnModel.getColumn(SignalGroupSensorModel.UNAME_COLUMN);
uNameColumnS.setResizable(true);
uNameColumnS.setMinWidth(210);
uNameColumnS.setMaxWidth(260);
TableColumn stateColumnS = SignalGroupSensorColumnModel.getColumn(SignalGroupSensorModel.STATE_COLUMN);
stateColumnS.setCellEditor(new DefaultCellEditor(stateSCombo));
stateColumnS.setResizable(false);
stateColumnS.setMinWidth(90);
stateColumnS.setMaxWidth(100);
_SignalGroupSensorScrollPane = new JScrollPane(SignalGroupSensorTable);
p2xs.add(_SignalGroupSensorScrollPane, BorderLayout.CENTER);
contentPane.add(p2xs);
p2xs.setVisible(true);
// add notes panel
JPanel pa = new JPanel();
pa.setLayout(new BoxLayout(pa, BoxLayout.Y_AXIS));
JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout());
p1.add(status1);
pa.add(p1);
Border pBorder = BorderFactory.createEtchedBorder();
pa.setBorder(pBorder);
contentPane.add(pa);
// add buttons - Add SignalGroup button
JPanel pb = new JPanel();
pb.setLayout(new FlowLayout(FlowLayout.TRAILING));
// add Cancel button
pb.add(cancelButton);
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cancelSubPressed(e);
}
});
// add Update SignalGroup button
pb.add(updateSubButton);
updateSubButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateSubPressed(e, false);
}
});
updateSubButton.setToolTipText(Bundle.getMessage("TooltipUpdate"));
p2xtSpace.setVisible(false);
p2xsSpace.setVisible(false);
updateSubButton.setVisible(true);
contentPane.add(pb);
addSubFrame.pack();
}
// set listener for window closing
addSubFrame.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
addSubFrame.setVisible(false);
cancelSubEdit();
_SignalGroupSensorModel.dispose();
_SignalGroupTurnoutModel.dispose();
}
});
addSubFrame.setVisible(true);
// add AND/OR choice box
setoperBox(curSignalGroup.getSensorTurnoutOper(curSignalHead), _SensorTurnoutOper);
setSignalHeadStateBox(curSignalGroup.getHeadOnState(curSignalHead), _OnAppearance);
setSignalHeadStateBox(curSignalGroup.getHeadOffState(curSignalHead), _OffAppearance);
int setRow = 0;
for (int i = _turnoutList.size() - 1; i >= 0; i--) {
SignalGroupTurnout turnout = _turnoutList.get(i);
Turnout tTurnout = turnout.getTurnout();
if (curSignalGroup.isTurnoutIncluded(curSignalHead, tTurnout)) {
turnout.setIncluded(true);
turnout.setState(curSignalGroup.getTurnoutState(curSignalHead, tTurnout));
setRow = i;
} else {
turnout.setIncluded(false);
turnout.setState(Turnout.CLOSED);
}
}
setRow -= 1;
if (setRow < 0) {
setRow = 0;
}
_SignalGroupTurnoutScrollPane.getVerticalScrollBar().setValue(setRow * ROW_HEIGHT);
_SignalGroupTurnoutModel.fireTableDataChanged();
for (int i = _sensorList.size() - 1; i >= 0; i--) {
SignalGroupSensor sensor = _sensorList.get(i);
Sensor tSensor = sensor.getSensor();
if (curSignalGroup.isSensorIncluded(curSignalHead, tSensor)) {
sensor.setIncluded(true);
sensor.setState(curSignalGroup.getSensorState(curSignalHead, tSensor));
setRow = i;
} else {
sensor.setIncluded(false);
sensor.setState(Sensor.INACTIVE);
}
}
status1.setText(updateInst);
updateSubButton.setVisible(true);
}
use of javax.swing.table.TableRowSorter in project JMRI by JMRI.
the class TableFrames method makeSignalFrame.
/*
* ********************* SignalFrame *****************************
*/
protected JInternalFrame makeSignalFrame() {
JInternalFrame frame = new JInternalFrame(Bundle.getMessage("TitleSignalTable"), true, false, false, true);
_signalModel = new SignalTableModel(this);
_signalModel.init();
_signalTable = new JTable(_signalModel);
TableRowSorter<SignalTableModel> sorter = new TableRowSorter<>(_signalModel);
_signalTable.setRowSorter(sorter);
_signalTable.setTransferHandler(new jmri.util.DnDTableImportExportHandler(new int[] { SignalTableModel.UNITSCOL, SignalTableModel.DELETE_COL }));
_signalTable.setDragEnabled(true);
_signalTable.getColumnModel().getColumn(SignalTableModel.UNITSCOL).setCellRenderer(new MyBooleanRenderer(Bundle.getMessage("cm"), Bundle.getMessage("in")));
_signalTable.getColumnModel().getColumn(SignalTableModel.DELETE_COL).setCellEditor(new ButtonEditor(new JButton()));
_signalTable.getColumnModel().getColumn(SignalTableModel.DELETE_COL).setCellRenderer(new ButtonRenderer());
for (int i = 0; i < _signalModel.getColumnCount(); i++) {
int width = SignalTableModel.getPreferredWidth(i);
_signalTable.getColumnModel().getColumn(i).setPreferredWidth(width);
}
_signalTable.sizeColumnsToFit(-1);
int tableWidth = _signalTable.getPreferredSize().width;
_signalTable.setPreferredScrollableViewportSize(new java.awt.Dimension(tableWidth, ROW_HEIGHT * 8));
_signalTablePane = new JScrollPane(_signalTable);
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout(5, 5));
JLabel prompt = new JLabel(Bundle.getMessage("AddSignalPrompt"));
contentPane.add(prompt, BorderLayout.NORTH);
contentPane.add(_signalTablePane, BorderLayout.CENTER);
frame.setContentPane(contentPane);
frame.setLocation(200, 350);
frame.pack();
return frame;
}
use of javax.swing.table.TableRowSorter 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;
}
Aggregations