package bs.bs2d.gui.views;
import bs.bs2d.fea.FEAResults;
import bs.bs2d.fea.processing.ResultProcessor;
import bs.bs2d.fea.processing.ScalarFieldOp;
import bs.bs2d.gui.BusyStateListener;
import bs.bs2d.gui.plot.ColorMap;
import bs.bs2d.gui.plot.PlotPanel;
import bs.bs2d.gui.plot.ScalarPlot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JComponent;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
/**
*
* @author Djen
*/
public class ResultProcessingView implements BSGUIView{
private static final String NAME = "Process Results";
// gui
private final PlotPanel content;
private final ResultProcessingViewControls controls;
private final JMenu[] menus;
ButtonGroup legendPosition;
// data
private FEAResults results;
private ScalarPlot scalarPlot;
private ColorMap.Style colorMapStyle;
BusyStateListener bsl;
public ResultProcessingView(){
content = new PlotPanel();
ActionListener alThrsh = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
scalarPlot.setThresholds(controls.getThresholds());
}
};
ActionListener alUpdate = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
update(true);
}
};
controls = new ResultProcessingViewControls(alThrsh, alUpdate);
menus = buildMenus();
}
private void update(boolean resetThrsh) {
List<ScalarFieldOp> ops = controls.getOperations();
float[] thresholds;
// process results
float[] scalar = ResultProcessor.process(results, ops);
// build new plot with uniform area thresholds
if(resetThrsh){
int res = controls.getResolution();
scalarPlot = ScalarPlot.createUniformAreaPlot(results.getMesh(), scalar, res, colorMapStyle);
controls.setThresholds(scalarPlot.getThresholds());
} else {
thresholds = controls.getThresholds();
scalarPlot = new ScalarPlot(results.getMesh(), scalar, thresholds, colorMapStyle);
}
content.setPlotObject(scalarPlot, false);
}
private JMenu[] buildMenus(){
JMenu[] ms = new JMenu[1];
JMenu m = new JMenu("View");
JMenu menuLegend = new JMenu("Legend Position");
JRadioButtonMenuItem[] mitLegendPos = new JRadioButtonMenuItem[4];
mitLegendPos[0] = new JRadioButtonMenuItem("Upper Left");
mitLegendPos[1] = new JRadioButtonMenuItem("Upper Right");
mitLegendPos[2] = new JRadioButtonMenuItem("Lower Left");
mitLegendPos[3] = new JRadioButtonMenuItem("Lower Right");
mitLegendPos[0].setSelected(true);
legendPosition = new ButtonGroup();
ActionListener al = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
AbstractButton btn = (AbstractButton)e.getSource();
switch (btn.getText()) {
case "Upper Left":
content.setLegendPosition(PlotPanel.UPPER_LEFT);
break;
case "Upper Right":
content.setLegendPosition(PlotPanel.UPPER_RIGHT);
break;
case "Lower Left":
content.setLegendPosition(PlotPanel.LOWER_LEFT);
break;
case "Lower Right":
content.setLegendPosition(PlotPanel.LOWER_RIGHT);
break;
}
}
};
for (JRadioButtonMenuItem item : mitLegendPos){
item.addActionListener(al);
legendPosition.add(item);
menuLegend.add(item);
}
m.add(menuLegend);
m.addSeparator();
JMenuItem mitResetView = new JMenuItem("Reset View");
mitResetView.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
content.resetView();
}
});
m.add(mitResetView);
ms[0] = m;
return ms;
}
@Override
public JComponent getContent() {
return content;
}
@Override
public JComponent getControls() {
return controls;
}
@Override
public JMenu[] getMenus() {
return menus;
}
@Override
public void init(Object data) {
if(data instanceof Object[]){
Object[] odata = (Object[]) data;
if(odata[0] instanceof FEAResults &&
odata[1] instanceof ColorMap.Style &&
odata[2] instanceof FEAResults.Scalar){
FEAResults newResults = (FEAResults) odata[0];
colorMapStyle = (ColorMap.Style) odata[1];
FEAResults.Scalar scalarID = (FEAResults.Scalar) odata[2];
results = newResults;
//update controls
controls.init(scalarID);
update(true);
content.resetView();
return;
}
}
// invalid data
results = null;
scalarPlot = null;
content.setPlotObject(null, true);
}
@Override
public Object getData() {
return new Object[]{scalarPlot.getAreas(),
scalarPlot.getColorMapStyle()};
}
@Override
public String getName() {
return NAME;
}
@Override
public void setBusyStateListener(BusyStateListener bsl) {
this.bsl = bsl;
}
}