package bs.bs2d.gui.views;
import bs.bs2d.fea.FEAResults;
import bs.bs2d.fea.processing.Operation;
import bs.bs2d.fea.processing.ScalarFieldOp;
import bs.bs2d.gui.BSConstants;
import com.sun.org.apache.xml.internal.dtm.ref.NodeLocator;
import java.awt.Color;
import java.awt.Component;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractCellEditor;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
*
* @author Djen
*/
public class ResultProcessingViewControls extends javax.swing.JPanel {
private final ActionListener alUpdateThrsh;
private final ActionListener alUpdatePlot;
private float[] thresholds;
private final OperationsTableModel opModel;
private final FieldOpTableModel fieldOpModel;
/**
* Creates new form InfillView
* @param alUpdateThrsh listener to be notified on threshold value change
* @param alUpdatePlot listener to be notified for a complete update of the
* plot
*/
public ResultProcessingViewControls(ActionListener alUpdateThrsh,
ActionListener alUpdatePlot) {
this.alUpdateThrsh = alUpdateThrsh;
this.alUpdatePlot = alUpdatePlot;
thresholds = new float[0];
initComponents();
// threshold table
tblThresholds.setDefaultRenderer(Object.class, new ThresholdTableCellRenderer());
// set single click edit
DefaultCellEditor cellEditor = new DefaultCellEditor(new JTextField());
cellEditor.setClickCountToStart(1);
tblThresholds.setDefaultEditor(Object.class, cellEditor);
// operations table
OperationTableEditor editor = new OperationTableEditor();
tblOperations.setDefaultEditor(Object.class, editor);
opModel = new OperationsTableModel();
tblOperations.setModel(opModel);
editor = new OperationTableEditor();
tblFieldOps.setDefaultEditor(Object.class, editor);
fieldOpModel = new FieldOpTableModel();
tblFieldOps.setModel(fieldOpModel);
tblFieldOps.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
private int selectedIndex = 0;
@Override
public void valueChanged(ListSelectionEvent e) {
int i = tblFieldOps.getSelectedRow();
if(i >= fieldOpModel.getOperations().size())
return;
if(i >= 0){
ScalarFieldOp op = fieldOpModel.getOperations().get(i);
opModel.setOperations(op.getOperations());
selectedIndex = i;
} else {
tblFieldOps.getSelectionModel().setSelectionInterval(selectedIndex, selectedIndex);
}
}
});
refreshPresetList("Default");
}
public void init(FEAResults.Scalar scalar){
if(getOperations().isEmpty())
fieldOpModel.add(new ScalarFieldOp(ScalarFieldOp.Type.ADD, scalar));
tblFieldOps.getSelectionModel().setSelectionInterval(0, 0);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel3 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
tblThresholds = new javax.swing.JTable();
btnReset = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
spnResolution = new javax.swing.JSpinner();
jPanel4 = new javax.swing.JPanel();
btnNew = new javax.swing.JButton();
btnRemove = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
tblOperations = new javax.swing.JTable();
jLabel3 = new javax.swing.JLabel();
btnApply = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
jScrollPane3 = new javax.swing.JScrollPane();
tblFieldOps = new javax.swing.JTable();
btnNewField = new javax.swing.JButton();
btnRemoveField = new javax.swing.JButton();
cmbPreset = new javax.swing.JComboBox();
btnSave = new javax.swing.JButton();
btnLoad = new javax.swing.JButton();
setMinimumSize(new java.awt.Dimension(250, 380));
setPreferredSize(new java.awt.Dimension(250, 380));
jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Thresholds"));
tblThresholds.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null},
{null, null},
{null, null},
{null, null}
},
new String [] {
"Title 1", "Title 2"
}
));
tblThresholds.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_LAST_COLUMN);
tblThresholds.setFocusable(false);
tblThresholds.setRowSelectionAllowed(false);
jScrollPane1.setViewportView(tblThresholds);
btnReset.setText("Reset");
btnReset.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnResetActionPerformed(evt);
}
});
jLabel1.setText("Resolution:");
spnResolution.setModel(new javax.swing.SpinnerNumberModel(3, 1, 10, 1));
spnResolution.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
spnResolutionStateChanged(evt);
}
});
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(spnResolution, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnReset))
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(spnResolution, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnReset))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Data Processing"));
btnNew.setText("Add");
btnNew.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnNewActionPerformed(evt);
}
});
btnRemove.setText("Remove");
btnRemove.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnRemoveActionPerformed(evt);
}
});
tblOperations.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null},
{null, null},
{null, null}
},
new String [] {
"Title 1", "Title 2"
}
));
tblOperations.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_LAST_COLUMN);
tblOperations.setFocusable(false);
tblOperations.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
jScrollPane2.setViewportView(tblOperations);
jLabel3.setText("Operations:");
btnApply.setText("Apply");
btnApply.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnApplyActionPerformed(evt);
}
});
jLabel4.setText("Scalars:");
tblFieldOps.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null},
{null, null},
{null, null}
},
new String [] {
"Title 1", "Title 2"
}
));
tblFieldOps.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_LAST_COLUMN);
tblFieldOps.setFocusable(false);
tblFieldOps.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
jScrollPane3.setViewportView(tblFieldOps);
btnNewField.setText("Add");
btnNewField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnNewFieldActionPerformed(evt);
}
});
btnRemoveField.setText("Remove");
btnRemoveField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnRemoveFieldActionPerformed(evt);
}
});
cmbPreset.setEditable(true);
btnSave.setText("Save");
btnSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSaveActionPerformed(evt);
}
});
btnLoad.setText("Load");
btnLoad.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnLoadActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
jPanel4.setLayout(jPanel4Layout);
jPanel4Layout.setHorizontalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
.addGroup(jPanel4Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnApply))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
.addComponent(cmbPreset, 0, 114, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnLoad)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnSave))
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
.addGroup(jPanel4Layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
.addComponent(btnNew)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnRemove))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnNewField)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnRemoveField))
);
jPanel4Layout.setVerticalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(cmbPreset, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnSave)
.addComponent(btnLoad))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(btnNewField)
.addComponent(btnRemoveField))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(btnNew)
.addComponent(btnRemove))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnApply))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE))
);
}// </editor-fold>//GEN-END:initComponents
private void spnResolutionStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_spnResolutionStateChanged
alUpdatePlot.actionPerformed(null);
}//GEN-LAST:event_spnResolutionStateChanged
private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
alUpdatePlot.actionPerformed(null);
}//GEN-LAST:event_btnResetActionPerformed
private void btnNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewActionPerformed
opModel.add(new Operation(Operation.Type.SMOOTH));
}//GEN-LAST:event_btnNewActionPerformed
private void btnRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveActionPerformed
int index = tblOperations.getSelectedRow();
if(index != -1)
opModel.remove(index);
int size = opModel.ops.size();
if(size <= index)
index = size - 1;
if(index >= 0)
tblOperations.getSelectionModel().setSelectionInterval(index, index);
}//GEN-LAST:event_btnRemoveActionPerformed
private void btnApplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnApplyActionPerformed
tblOperations.getDefaultEditor(Object.class).stopCellEditing();
alUpdatePlot.actionPerformed(null);
}//GEN-LAST:event_btnApplyActionPerformed
private void btnNewFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewFieldActionPerformed
fieldOpModel.add(new ScalarFieldOp());
}//GEN-LAST:event_btnNewFieldActionPerformed
private void btnRemoveFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveFieldActionPerformed
int index = tblFieldOps.getSelectedRow();
if(index <= 0)
Toolkit.getDefaultToolkit().beep();
else
fieldOpModel.remove(index);
int size = fieldOpModel.ops.size();
if(size <= index)
index = size - 1;
tblFieldOps.getSelectionModel().setSelectionInterval(index, index);
}//GEN-LAST:event_btnRemoveFieldActionPerformed
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
try {
String presetName = cmbPreset.getSelectedItem().toString();
writePresets(presetName);
refreshPresetList(presetName);
} catch (ParserConfigurationException | TransformerException ex) {
ex.printStackTrace();
}
}//GEN-LAST:event_btnSaveActionPerformed
private void btnLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoadActionPerformed
String presetName = "";
try {
presetName = cmbPreset.getSelectedItem().toString();
List<ScalarFieldOp> ops = readPresets(presetName);
fieldOpModel.setOperations(ops);
tblFieldOps.getSelectionModel().setSelectionInterval(0, 0);
opModel.setOperations(ops.get(0).getOperations());
} catch(FileNotFoundException ex){
Toolkit.getDefaultToolkit().beep();
cmbPreset.removeItem(presetName);
} catch (ParserConfigurationException | IOException | SAXException ex) {
ex.printStackTrace();
}
}//GEN-LAST:event_btnLoadActionPerformed
private void refreshPresetList(String selection){
DefaultComboBoxModel cmbm = (DefaultComboBoxModel)cmbPreset.getModel();
cmbm.removeAllElements();
addPresets(getPresetFiles());
cmbm.setSelectedItem(selection);
}
private void addPresets(String[] names){
DefaultComboBoxModel cmbm = (DefaultComboBoxModel)cmbPreset.getModel();
for (String name : names) {
cmbm.addElement(name);
}
}
private String[] getPresetFiles(){
File f = FileUtils.getFile(BSConstants.SETTINGS_DIR, BSConstants.PRESET_DIR_NAME);
File[] flist = f.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".xml");
}
});
String[] names = new String[flist.length];
for (int i = 0; i < flist.length; i++) {
names[i] = FilenameUtils.getBaseName(flist[i].getName());
}
return names;
}
private List<ScalarFieldOp> readPresets(String presetName) throws ParserConfigurationException, SAXException, IOException {
System.out.println("reading presets...");
File xml = FileUtils.getFile(BSConstants.SETTINGS_DIR, BSConstants.PRESET_DIR_NAME, presetName + ".xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xml);
//optional, but recommended
//read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("Scalar");
ArrayList<ScalarFieldOp> ops = new ArrayList<>();
for (int i = 0; i < nList.getLength(); i++) {
Element el = (Element) nList.item(i);
ScalarFieldOp sfop = new ScalarFieldOp();
sfop.setScalar(FEAResults.Scalar.valueOf(el.getElementsByTagName("Name").item(0).getTextContent()));
sfop.setType(ScalarFieldOp.Type.valueOf(el.getElementsByTagName("Type").item(0).getTextContent()));
NodeList opList = el.getElementsByTagName("Operation");
for (int j = 0; j < opList.getLength(); j++) {
Element opEl = (Element) opList.item(j);
Operation op = new Operation(Operation.Type.SMOOTH);
String type = opEl.getElementsByTagName("OpType").item(0).getTextContent();
op.setType(Operation.Type.valueOf(type));
String param = opEl.getElementsByTagName("OpParam").item(0).getTextContent();
op.setParameter(Float.parseFloat(param));
sfop.addOperation(op);
}
ops.add(sfop);
}
System.out.println("done");
return ops;
}
private void writePresets(String presetName) throws ParserConfigurationException, TransformerConfigurationException, TransformerException {
System.out.println("writing presets");
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
// root elements
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("Processing");
doc.appendChild(rootElement);
for (ScalarFieldOp sop : getOperations()) {
// scalar element
Element scalar = doc.createElement("Scalar");
rootElement.appendChild(scalar);
Element name = doc.createElement("Name");
name.appendChild(doc.createTextNode(sop.getScalar().toString()));
scalar.appendChild(name);
Element type = doc.createElement("Type");
type.appendChild(doc.createTextNode(sop.getType().toString()));
scalar.appendChild(type);
for (Operation op : sop.getOperations()) {
Element operation = doc.createElement("Operation");
scalar.appendChild(operation);
Element opType = doc.createElement("OpType");
opType.appendChild(doc.createTextNode(op.getType().toString()));
operation.appendChild(opType);
Element opParam = doc.createElement("OpParam");
opParam.appendChild(doc.createTextNode("" + op.getParameter()));
operation.appendChild(opParam);
}
}
// write the content into xml file
File xml = FileUtils.getFile(BSConstants.SETTINGS_DIR, BSConstants.PRESET_DIR_NAME);
xml.mkdirs();
xml = FileUtils.getFile(xml, presetName + ".xml");
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(xml);
transformer.transform(source, result);
System.out.println("File saved!");
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btnApply;
private javax.swing.JButton btnLoad;
private javax.swing.JButton btnNew;
private javax.swing.JButton btnNewField;
private javax.swing.JButton btnRemove;
private javax.swing.JButton btnRemoveField;
private javax.swing.JButton btnReset;
private javax.swing.JButton btnSave;
private javax.swing.JComboBox cmbPreset;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JSpinner spnResolution;
private javax.swing.JTable tblFieldOps;
private javax.swing.JTable tblOperations;
private javax.swing.JTable tblThresholds;
// End of variables declaration//GEN-END:variables
public float[] getThresholds(){
return thresholds;
}
public void setThresholds(float[] thresholds) {
this.thresholds = thresholds;
spnResolution.setValue(thresholds.length - 1);
// set table model
tblThresholds.setModel(new ThresholdTableModel());
tblThresholds.getColumnModel().getColumn(0).setMaxWidth(40);
}
public int getResolution(){
return (int)spnResolution.getValue();
}
public List<ScalarFieldOp> getOperations(){
return fieldOpModel.getOperations();
}
// table model classes ///////////////
private class FieldOpTableModel extends AbstractTableModel {
private List<ScalarFieldOp> ops;
public FieldOpTableModel() {
ops = new ArrayList<>();
}
public List<ScalarFieldOp> getOperations() {
return ops;
}
public void add(ScalarFieldOp op){
ops.add(op);
fireTableDataChanged();
}
public void remove(int i){
ops.remove(i);
fireTableDataChanged();
}
@Override
public String getColumnName(int column) {
if(column == 0)
return "Scalar";
else
return "Operation";
}
@Override
public int getRowCount() {
return ops.size();
}
@Override
public int getColumnCount() {
return 2;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return !(columnIndex == 1 && rowIndex == 0);
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
ScalarFieldOp op = ops.get(rowIndex);
if(columnIndex == 0)
return op.getScalar();
else
return op.getType();
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
if(rowIndex >= ops.size())
return;
if(columnIndex == 0){
ops.get(rowIndex).setScalar((FEAResults.Scalar)aValue);
} else {
ops.get(rowIndex).setType((ScalarFieldOp.Type) aValue);
}
fireTableDataChanged();
}
public void setOperations(List<ScalarFieldOp> ops){
this.ops = ops;
fireTableDataChanged();
}
}
private class OperationsTableModel extends AbstractTableModel{
private List<Operation> ops;
public void setOperations(List<Operation> ops) {
this.ops = ops;
fireTableDataChanged();
}
public OperationsTableModel() {
ops = new ArrayList<>();
}
public List<Operation> getOperations() {
return ops;
}
public void add(Operation op){
ops.add(op);
fireTableDataChanged();
}
public void remove(int i){
if(i >= 0 && i < ops.size()){
ops.remove(i);
fireTableDataChanged();
}
}
@Override
public String getColumnName(int column) {
if(column == 0)
return "Type";
else
return "Parameter";
}
@Override
public int getRowCount() {
return ops.size();
}
@Override
public int getColumnCount() {
return 2;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
if(columnIndex == 0)
return true;
else
return ops.get(rowIndex).isParameterRequired();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Operation op = ops.get(rowIndex);
if(columnIndex == 0)
return op.getType();
else if (op.isParameterRequired())
return op.getParameter();
else
return "";
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
if(rowIndex >= ops.size())
return;
if(columnIndex == 0){
Operation.Type type = (Operation.Type) aValue;
ops.get(rowIndex).setType(type);
} else {
ops.get(rowIndex).setParameter(aValue);
}
fireTableDataChanged();
}
}
private class OperationTableEditor extends AbstractCellEditor implements TableCellEditor{
TableCellEditor tceCheckbox;
TableCellEditor tceTextfield;
TableCellEditor tceOperation;
TableCellEditor tceFieldOp;
TableCellEditor tceScalar;
TableCellEditor activeEditor;
public OperationTableEditor() {
tceCheckbox = new DefaultCellEditor(new JCheckBox());
tceOperation = new DefaultCellEditor(new JComboBox(Operation.Type.values()));
tceScalar = new DefaultCellEditor(new JComboBox(FEAResults.Scalar.values()));
tceFieldOp = new DefaultCellEditor(new JComboBox(ScalarFieldOp.Type.values()));
tceTextfield = new DefaultCellEditor(new JTextField());
CellEditorListener cel = new CellEditorListener() {
@Override
public void editingStopped(ChangeEvent e) {
fireEditingStopped();
}
@Override
public void editingCanceled(ChangeEvent e) {
fireEditingCanceled();
}
};
tceCheckbox.addCellEditorListener(cel);
tceOperation.addCellEditorListener(cel);
tceScalar.addCellEditorListener(cel);
tceFieldOp.addCellEditorListener(cel);
tceTextfield.addCellEditorListener(cel);
}
@Override
public Object getCellEditorValue() {
return activeEditor.getCellEditorValue();
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
if(value instanceof Boolean)
activeEditor = tceCheckbox;
else if(value instanceof FEAResults.Scalar)
activeEditor = tceScalar;
else if(value instanceof Operation.Type)
activeEditor = tceOperation;
else if (value instanceof ScalarFieldOp.Type)
activeEditor = tceFieldOp;
else
activeEditor = tceTextfield;
return activeEditor.getTableCellEditorComponent(table, value, isSelected, row, column);
}
}
private class ThresholdTableModel extends AbstractTableModel{
@Override
public String getColumnName(int column) {
if(column == 1)
return "Threshold";
else
return "";
}
@Override
public int getRowCount() {
return thresholds.length;
}
@Override
public int getColumnCount() {
return 2;
}
@Override
public Object getValueAt(int r, int c) {
int maxIndex = thresholds.length - 1;
if(c == 0){
if(r == 0)
return "Max";
else if (r == maxIndex)
return "Min";
else
return maxIndex - r;
} else {
return thresholds[maxIndex - r];
}
}
@Override
public boolean isCellEditable(int r, int c) {
return c == 1 && r > 0 && r < thresholds.length - 1;
}
/**
* @return the thresholds
*/
public float[] getThresholds() {
return thresholds;
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
try{
if(!(aValue instanceof String))
throw new IllegalArgumentException("Expecting string value!");
float newVal = Float.parseFloat((String) aValue);
int index = thresholds.length - rowIndex - 1;
// apply bounds
if(newVal < thresholds[index - 1])
newVal = thresholds[index - 1];
if(newVal > thresholds[index + 1])
newVal = thresholds[index + 1];
thresholds[index] = newVal;
alUpdateThrsh.actionPerformed(null);
} catch(IllegalArgumentException ex){
Toolkit.getDefaultToolkit().beep();
}
}
}
private class ThresholdTableCellRenderer extends DefaultTableCellRenderer{
public final Color EDITABLE_CELL_BACKGROUND = UIManager.getColor("TextField.background");
public final Color DISABLED_CELL_COLOR = UIManager.getColor("Panel.background");
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if(table.getModel().isCellEditable(row, column))
cell.setBackground(EDITABLE_CELL_BACKGROUND);
else
cell.setBackground(DISABLED_CELL_COLOR);
return cell;
}
}
}