ArduinoTableValues.java
/*******************************************************************************
* jArduino: Arduino C++ Code Generation From Java
* Copyright 2020 Tony Washer
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package net.sourceforge.jarduino.gui;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import net.sourceforge.jarduino.message.ArduinoValues;
/**
* Table for values display.
*/
public class ArduinoTableValues {
/**
* The table.
*/
private final JTable theTable;
/**
* The model.
*/
private final ArduinoValuesModel theModel;
/**
* The selected values.
*/
private final List<Number> theValues;
/**
* The selected names.
*/
private final List<String> theNames;
/**
* The selected values.
*/
private ArduinoValues theSelected;
/**
* Constructor.
*/
ArduinoTableValues() {
/* Create the lists */
theValues = new ArrayList<>();
theNames = new ArrayList<>();
/* Create the table */
theTable = new JTable();
theTable.setAutoCreateRowSorter(true);
/* Create the table model */
theModel = new ArduinoValuesModel();
theTable.setModel(theModel);
}
/**
* Obtain the component.
* @return the component
*/
JComponent getComponent() {
return theTable;
}
/**
* Configure the table.
*
* @param pSelected the selected object
*/
void configureTable(final ArduinoValues pSelected) {
/* store the details */
theSelected = pSelected;
/* Refresh the table model */
theModel.refresh();
}
/**
* Table Model.
*/
private class ArduinoValuesModel
extends AbstractTableModel {
/**
* Serial Id.
*/
private static final long serialVersionUID = -5981115401584506612L;
/**
* The name column.
*/
private static final int COL_NAME = 0;
/**
* The value column.
*/
private static final int COL_VALUE = COL_NAME + 1;
/**
* Constructor.
*/
ArduinoValuesModel() {
}
/**
* Refresh.
*/
void refresh() {
/* Clear the names */
theNames.clear();
theValues.clear();
/* Loop through the values */
for (Entry<Number, String> myEntry : theSelected.getValues().entrySet()) {
/* Add to lists */
theNames.add(myEntry.getValue());
theValues.add(myEntry.getKey());
}
/* Refresh the table */
fireTableDataChanged();
}
@Override
public int getRowCount() {
return theNames.size();
}
@Override
public String getColumnName(final int pColIndex) {
switch (pColIndex) {
case COL_NAME:
return "Name";
case COL_VALUE:
return "Value";
default:
return null;
}
}
@Override
public int getColumnCount() {
return COL_VALUE + 1;
}
@Override
public boolean isCellEditable(final int pRowIndex,
final int pColIndex) {
return false;
}
@Override
public Object getValueAt(final int pRowIndex,
final int pColIndex) {
switch (pColIndex) {
case COL_NAME:
return theNames.get(pRowIndex);
case COL_VALUE:
return theValues.get(pRowIndex);
default:
return null;
}
}
}
}