package org.jcae.netbeans.mesh;
/*
* Project Info: http://jcae.sourceforge.net
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
* (C) Copyright 2010, by EADS France
*/
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.io.PrintStream;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableModel;
/**
* @author Jerome Robert
*/
public abstract class PointMetricPanel extends JPanel {
private final static String[] COLUMN_TOOLTIPS={
"X coordinate of the point",
"Y coordinate of the point",
"Z coordinate of the point",
"Mesh size at the give point",
"", ""
};
/** Creates new form PointMetricPanel */
public PointMetricPanel() {
initComponents();
}
/** 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() {
GridBagConstraints gridBagConstraints;
JScrollPane scrollPane = new JScrollPane();
table = createTable();
JButton buttonAdd = new JButton();
JButton buttonRemove = new JButton();
setLayout(new GridBagLayout());
scrollPane.setPreferredSize(new Dimension(0, 100));
table.setModel(new DefaultTableModel(
new Object [][] {
},
new String [] {
"x", "y", "z", "Size(d=0)", "d0", "d1"
}
) {
Class[] types = new Class [] {
Double.class, Double.class, Double.class, Double.class, Double.class, Double.class
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
});
scrollPane.setViewportView(table);
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
add(scrollPane, gridBagConstraints);
buttonAdd.setText("Add");
buttonAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
buttonAddActionPerformed(evt);
}
});
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.weightx = 0.5;
add(buttonAdd, gridBagConstraints);
buttonRemove.setText("Remove");
buttonRemove.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
buttonRemoveActionPerformed(evt);
}
});
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 1;
gridBagConstraints.weightx = 0.5;
gridBagConstraints.insets = new Insets(5, 5, 5, 5);
add(buttonRemove, gridBagConstraints);
}// </editor-fold>//GEN-END:initComponents
private void buttonAddActionPerformed(ActionEvent evt) {//GEN-FIRST:event_buttonAddActionPerformed
DefaultTableModel m = (DefaultTableModel) table.getModel();
m.addRow(new Object[]{0, 0, 0, getDefaultSize() / 10, 0, getDefaultSize() * 2});
}//GEN-LAST:event_buttonAddActionPerformed
private void buttonRemoveActionPerformed(ActionEvent evt) {//GEN-FIRST:event_buttonRemoveActionPerformed
DefaultTableModel m = (DefaultTableModel) table.getModel();
for(int i:table.getSelectedRows())
m.removeRow(i);
}//GEN-LAST:event_buttonRemoveActionPerformed
protected abstract double getDefaultSize();
private JTable createTable() {
JTable t = new JTable() {
//Implement table header tool tips.
//Implement table header tool tips.
@Override
protected JTableHeader createDefaultTableHeader() {
return new JTableHeader(columnModel) {
@Override
public String getToolTipText(MouseEvent e) {
int index = columnModel.getColumnIndexAtX(e.getPoint().x);
int realIndex = columnModel.getColumn(index).getModelIndex();
return COLUMN_TOOLTIPS[realIndex];
}
};
}
};
t.setDefaultRenderer(Double.class, t.getDefaultRenderer(String.class));
return t;
}
// Variables declaration - do not modify//GEN-BEGIN:variables
JTable table;
// End of variables declaration//GEN-END:variables
public void writeTable(PrintStream out)
{
TableModel m = table.getModel();
int rowCount = m.getRowCount();
int colCount = m.getColumnCount();
for(int i = 0; i<rowCount; i++)
{
out.print("1 ");
for(int j = 0; j<colCount; j++)
{
out.print(m.getValueAt(i, j));
out.print(' ');
}
out.println();
}
}
public boolean isEmpty()
{
return table.getModel().getRowCount() == 0;
}
}