package org.esa.beam.processor.baer.ui;
import org.esa.beam.dataio.dimap.DimapProductConstants;
import org.esa.beam.framework.dataio.ProductIO;
import org.esa.beam.framework.dataio.ProductIOPlugInManager;
import org.esa.beam.framework.datamodel.Product;
import org.esa.beam.framework.param.ParamChangeEvent;
import org.esa.beam.framework.param.ParamChangeListener;
import org.esa.beam.framework.param.ParamGroup;
import org.esa.beam.framework.param.ParamValidateException;
import org.esa.beam.framework.param.Parameter;
import org.esa.beam.framework.param.editors.BooleanExpressionEditor;
import org.esa.beam.framework.processor.DefaultRequestElementFactory;
import org.esa.beam.framework.processor.ProcessorConstants;
import org.esa.beam.framework.processor.ProcessorException;
import org.esa.beam.framework.processor.ProductRef;
import org.esa.beam.framework.processor.Request;
import org.esa.beam.framework.processor.ui.AbstractProcessorUI;
import org.esa.beam.framework.processor.ui.ProcessorApp;
import org.esa.beam.framework.ui.GridBagUtils;
import org.esa.beam.processor.baer.BaerConstants;
import org.esa.beam.processor.baer.BaerRequestElementFactory;
import org.esa.beam.util.Debug;
import org.esa.beam.util.Guardian;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Logger;
public class BaerUi extends AbstractProcessorUI
implements ParamChangeListener {
private Logger _logger;
private JComponent _uiComponent;
private ParamGroup _paramGroup;
private JComboBox _fileFormatCombo;
private String[] _fileFormatNames;
private String _outFileFormat;
private static final String OUT_FORMAT_LABEL = "Output product format:";
private File _requestFile;
_logger = Logger.getLogger(BaerConstants.LOGGER_NAME);
createParameterGroup();
}
if (_uiComponent == null) {
scanWriterFormatStrings();
createUI();
try {
setDefaultRequests();
} catch (ProcessorException e) {
_logger.severe(e.getMessage());
}
}
return _uiComponent;
}
public Vector<Request>
getRequests()
throws ProcessorException {
Request request = new Request();
request.setType(BaerConstants.REQUEST_TYPE);
request.setFile(_requestFile);
getInputProduct(request);
getOutputProduct(request);
getParameter(request);
Vector<Request> vRet = new Vector<Request>();
vRet.add(request);
return vRet;
}
public void setRequests(Vector requests)
throws ProcessorException {
Guardian.assertNotNull("requests", requests);
if (requests.size() > 0) {
Request request = (Request) requests.elementAt(0);
_requestFile = request.getFile();
setInputProduct(request);
scanForBitmasksAndAssign();
setOutputProduct(request);
setParameter(request);
}
}
BaerRequestElementFactory factory = BaerRequestElementFactory.getInstance();
Request request = new Request();
request.setType(BaerConstants.REQUEST_TYPE);
request.addInputProduct(createDefaultInputProduct());
request.addOutputProduct(createDefaultOutputProduct());
request.addParameter(factory.createParamWithDefaultValueSet(BaerConstants.BITMASK_PARAM_NAME));
request.addParameter(factory.createParamWithDefaultValueSet(BaerConstants.SMAC_PARAM_NAME));
request.addParameter(factory.createParamWithDefaultValueSet(BaerConstants.USE_BAER_PARAM_NAME));
request.addParameter(factory.createParamWithDefaultValueSet(BaerConstants.USE_ATM_COR_PARAM_NAME));
request.addParameter(factory.createParamWithDefaultValueSet(BaerConstants.USE_CLOUD_PARAM_NAME));
request.addParameter(factory.createDefaultLogPatternParameter(BaerConstants.DEFAULT_LOG_PREFIX));
try {
Parameter aerParam = factory.createParamWithDefaultValueSet(BaerConstants.AER_PHASE_PARAM_NAME);
aerParam.setValue(aerParam.getProperties().getValueSet()[0]);
request.addParameter(aerParam);
request.addParameter(factory.createLogToOutputParameter("false"));
} catch (ParamValidateException e) {
_logger.severe(e.getMessage());
}
Vector<Request> vSet = new Vector<Request>();
vSet.add(request);
setRequests(vSet);
}
public void setApp(ProcessorApp processorApp) {
}
if (event.getParameter().getName().equalsIgnoreCase(DefaultRequestElementFactory.INPUT_PRODUCT_PARAM_NAME)) {
scanForBitmasksAndAssign();
}
}
Parameter param = _paramGroup.getParameter(BaerConstants.AER_PHASE_PARAM_NAME);
param.getProperties().setValueSet(aerPhaseNames);
final boolean valueIsSet = param.getValue() == null || param.getValueAsText().equals("");
if (!valueIsSet && aerPhaseNames.length > 0) {
param.setValue(aerPhaseNames[0], null);
}
}
int line = 0;
JPanel ioParamPanel = GridBagUtils.createDefaultEmptyBorderPanel();
GridBagConstraints gbc = GridBagUtils.createConstraints(null);
addParameterToPanel(ioParamPanel, DefaultRequestElementFactory.INPUT_PRODUCT_PARAM_NAME, line, 12, gbc);
line += 2;
addParameterToPanel(ioParamPanel, DefaultRequestElementFactory.OUTPUT_PRODUCT_PARAM_NAME, line, 12, gbc);
line += 10;
final JLabel outFormatLabel = new JLabel(OUT_FORMAT_LABEL);
_fileFormatCombo = new JComboBox(_fileFormatNames);
_fileFormatCombo.addActionListener(new ActionListener() {
updateOutFileType();
}
});
GridBagUtils.setAttributes(gbc,
"anchor=SOUTHWEST, fill=NONE, insets.top=12, weightx = 0, weighty=0.5, gridy=" + String.valueOf(line++));
GridBagUtils.addToPanel(ioParamPanel, outFormatLabel, gbc);
GridBagUtils.setAttributes(gbc, "anchor=NORTHWEST, weighty=0.5, insets.top=0 ,gridy=" + String.valueOf(line++));
GridBagUtils.addToPanel(ioParamPanel, _fileFormatCombo, gbc);
JPanel procParamPanel = GridBagUtils.createDefaultEmptyBorderPanel();
gbc = GridBagUtils.createConstraints(null);
addParameterToPanel2(procParamPanel, BaerConstants.USE_CLOUD_PARAM_NAME, line++, 12, false, gbc);
addParameterToPanel2(procParamPanel, BaerConstants.USE_BAER_PARAM_NAME, line++, 2, false, gbc);
addParameterToPanel2(procParamPanel, BaerConstants.USE_ATM_COR_PARAM_NAME, line++, 2, false, gbc);
addParameterToPanel2(procParamPanel, BaerConstants.SMAC_PARAM_NAME, line++, 12, true, gbc);
addParameterToPanel2(procParamPanel, BaerConstants.AER_PHASE_PARAM_NAME, line++, 2, true, gbc);
addParameterToPanel2(procParamPanel, BaerConstants.BITMASK_PARAM_NAME, line++, 12, false, gbc);
getParameter(BaerConstants.USE_CLOUD_PARAM_NAME).getEditor().setEnabled(false);
final JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("I/O Parameters", ioParamPanel);
tabbedPane.addTab("Processing Parameters", procParamPanel);
_uiComponent = tabbedPane;
}
ProductIOPlugInManager manager = ProductIOPlugInManager.getInstance();
_fileFormatNames = manager.getAllProductWriterFormatStrings();
}
BaerRequestElementFactory factory = BaerRequestElementFactory.getInstance();
try {
_paramGroup = new ParamGroup();
final Parameter inputProductParameter = factory.createDefaultInputProductParameter();
inputProductParameter.addParamChangeListener(new ParamChangeListener() {
handleInputProductChanged();
}
});
_paramGroup.addParameter(inputProductParameter);
_paramGroup.addParameter(factory.createDefaultOutputProductParameter());
_paramGroup.addParameter(factory.createParamWithDefaultValueSet(BaerConstants.BITMASK_PARAM_NAME));
_paramGroup.addParameter(factory.createParamWithDefaultValueSet(BaerConstants.AER_PHASE_PARAM_NAME));
_paramGroup.addParameter(factory.createParamWithDefaultValueSet(BaerConstants.USE_BAER_PARAM_NAME));
_paramGroup.addParameter(factory.createParamWithDefaultValueSet(BaerConstants.USE_ATM_COR_PARAM_NAME));
_paramGroup.addParameter(factory.createParamWithDefaultValueSet(BaerConstants.USE_CLOUD_PARAM_NAME));
_paramGroup.addParameter(factory.createParamWithDefaultValueSet(BaerConstants.SMAC_PARAM_NAME));
_paramGroup.addParameter(factory.createDefaultLogPatternParameter(BaerConstants.DEFAULT_LOG_PREFIX));
_paramGroup.addParameter(factory.createLogToOutputParameter("false"));
_paramGroup.addParamChangeListener(createParamChangeListener());
} catch (ParamValidateException e) {
_logger.severe(e.getMessage());
}
}
private void addParameterToPanel(JPanel panel, String name,
int line,
int insets, GridBagConstraints gbc) {
Parameter param = _paramGroup.getParameter(name);
GridBagUtils.setAttributes(gbc,
"anchor=SOUTHWEST, fill=HORIZONTAL, weightx=1, gridy=" + String.valueOf(line++));
if (insets > 0) {
GridBagUtils.setAttributes(gbc, "insets.top=" + String.valueOf(insets));
}
JComponent label = param.getEditor().getLabelComponent();
if (label != null) {
GridBagUtils.addToPanel(panel, label, gbc);
}
GridBagUtils.setAttributes(gbc, "anchor=NORTHWEST, insets.top=0, gridy=" + String.valueOf(line++));
GridBagUtils.addToPanel(panel, param.getEditor().getEditorComponent(), gbc);
}
private void addParameterToPanel2(JPanel panel, String name,
int line,
int insets,
boolean nofill, GridBagConstraints gbc) {
Parameter param = _paramGroup.getParameter(name);
GridBagUtils.setAttributes(gbc,
"anchor=WEST, fill=HORIZONTAL, gridy=" + line + ", insets.top=" + insets);
JComponent label = param.getEditor().getLabelComponent();
if (label != null) {
gbc.gridwidth = nofill ? 2 : 1;
gbc.weightx = nofill ? 1 : 0;
gbc.gridx = 0;
GridBagUtils.addToPanel(panel, label, gbc);
gbc.gridwidth = nofill ? 1 : 2;
gbc.weightx = nofill ? 0 : 1;
gbc.gridx = nofill ? 2 : 1;
GridBagUtils.addToPanel(panel, param.getEditor().getEditorComponent(), gbc);
} else {
gbc.gridwidth = 3;
gbc.gridx = 0;
gbc.weightx = 1;
GridBagUtils.addToPanel(panel, param.getEditor().getEditorComponent(), gbc);
}
}
_outFileFormat = (String) _fileFormatCombo.getSelectedItem();
}
Parameter param = _paramGroup.getParameter(DefaultRequestElementFactory.INPUT_PRODUCT_PARAM_NAME);
File inputFile = (File) param.getValue();
if ((inputFile != null) && inputFile.exists() && inputFile.isFile()) {
try {
Product inProduct = ProductIO.readProduct(inputFile);
if (inProduct != null) {
String[] bitmaskFlags = inProduct.getAllFlagNames();
Parameter bitmask = _paramGroup.getParameter(BaerConstants.BITMASK_PARAM_NAME);
bitmask.getProperties().setValueSet(bitmaskFlags);
}
} catch (IOException e) {
Debug.trace(e);
}
}
}
Parameter inProdParam = BaerRequestElementFactory.getInstance().createDefaultInputProductParameter();
ProductRef ref = null;
File inProd = (File) inProdParam.getValue();
ref = new ProductRef(inProd);
return ref;
}
Parameter outProdParam = BaerRequestElementFactory.getInstance().createDefaultOutputProductParameter();
File outProd = (File) outProdParam.getValue();
ProductRef ref = null;
ref = new ProductRef(outProd);
return ref;
}
if (request.getNumInputProducts() > 0) {
ProductRef prodRef = request.getInputProductAt(0);
Parameter param = _paramGroup.getParameter(DefaultRequestElementFactory.INPUT_PRODUCT_PARAM_NAME);
File file = new File(prodRef.getFilePath());
param.setValue(file, null);
}
}
final File file = (File) _paramGroup.getParameter(DefaultRequestElementFactory.INPUT_PRODUCT_PARAM_NAME).getValue();
Product inProduct = null;
try {
inProduct = ProductIO.readProduct(file);
} catch (IOException e) {
Debug.trace(e);
}
final Parameter parameter = _paramGroup.getParameter(BaerConstants.BITMASK_PARAM_NAME);
parameter.getProperties().setPropertyValue(BooleanExpressionEditor.PROPERTY_KEY_SELECTED_PRODUCT,
inProduct);
}
if (request.getNumOutputProducts() > 0) {
ProductRef outputProduct = request.getOutputProductAt(0);
Parameter param = _paramGroup.getParameter(DefaultRequestElementFactory.OUTPUT_PRODUCT_PARAM_NAME);
File file = new File(outputProduct.getFilePath());
param.setValue(file, null);
_outFileFormat = outputProduct.getFileFormat();
if (_outFileFormat != null) {
_fileFormatCombo.setSelectedItem(_outFileFormat);
} else {
_outFileFormat = DimapProductConstants.DIMAP_FORMAT_NAME;
}
}
}
return new ParamChangeListener() {
parameterChanged(e);
}
};
}
Parameter param = e.getParameter();
String paramName = param.getName();
if (BaerConstants.USE_BAER_PARAM_NAME.equals(paramName)) {
handleParameterChangedEventUseBaer(param);
} else if (BaerConstants.USE_ATM_COR_PARAM_NAME.equals(paramName)) {
handleParameterChangedEventUseAtmCorr(param);
} else {
final File file = (File) _paramGroup.getParameter(DefaultRequestElementFactory.INPUT_PRODUCT_PARAM_NAME).getValue();
if (file != null && file.exists()) {
Product inProduct = null;
try {
inProduct = ProductIO.readProduct(file);
} catch (IOException i) {
Debug.trace(i);
}
final Parameter parameter = _paramGroup.getParameter(BaerConstants.BITMASK_PARAM_NAME);
parameter.getProperties().setPropertyValue(BooleanExpressionEditor.PROPERTY_KEY_SELECTED_PRODUCT,
inProduct);
}
}
}
boolean enable = ((Boolean)param.getValue()).booleanValue();
boolean enableatm;
Parameter toUpdate;
Parameter newParam = getParameter(BaerConstants.USE_BAER_PARAM_NAME);
if (newParam != null) {
toUpdate = getParameter(BaerConstants.USE_ATM_COR_PARAM_NAME);
toUpdate.setValue(newParam.getValue(), null);
}
toUpdate = getParameter(BaerConstants.USE_ATM_COR_PARAM_NAME);
enableatm = ((Boolean)toUpdate.getValue()).booleanValue();
getParameter(BaerConstants.USE_ATM_COR_PARAM_NAME).getEditor().setEnabled(enable);
getParameter(BaerConstants.SMAC_PARAM_NAME).getEditor().setEnabled(enable);
getParameter(BaerConstants.BITMASK_PARAM_NAME).getEditor().setEnabled(enable);
getParameter(BaerConstants.AER_PHASE_PARAM_NAME).getEditor().setEnabled(enable);
}
boolean enable = ((Boolean)param.getValue()).booleanValue();
getParameter(BaerConstants.SMAC_PARAM_NAME).getEditor().setEnabled(enable);
}
private void setParameter(Request request)
throws ProcessorException {
Parameter toUpdate = null;
Parameter newParam = null;
try {
newParam = request.getParameter(BaerConstants.BITMASK_PARAM_NAME);
if (newParam != null) {
toUpdate = _paramGroup.getParameter(BaerConstants.BITMASK_PARAM_NAME);
toUpdate.setValue(newParam.getValue());
}
newParam = request.getParameter(BaerConstants.AER_PHASE_PARAM_NAME);
if (newParam != null) {
toUpdate = _paramGroup.getParameter(BaerConstants.AER_PHASE_PARAM_NAME);
toUpdate.setValue(newParam.getValue());
}
newParam = request.getParameter(BaerConstants.USE_BAER_PARAM_NAME);
if (newParam != null) {
toUpdate = _paramGroup.getParameter(BaerConstants.USE_BAER_PARAM_NAME);
toUpdate.setValue(newParam.getValue());
}
newParam = request.getParameter(BaerConstants.USE_ATM_COR_PARAM_NAME);
if (newParam != null) {
toUpdate = _paramGroup.getParameter(BaerConstants.USE_ATM_COR_PARAM_NAME);
toUpdate.setValue(newParam.getValue());
}
newParam = request.getParameter(BaerConstants.USE_CLOUD_PARAM_NAME);
if (newParam != null) {
toUpdate = _paramGroup.getParameter(BaerConstants.USE_CLOUD_PARAM_NAME);
toUpdate.setValue(newParam.getValue());
}
newParam = request.getParameter(BaerConstants.SMAC_PARAM_NAME);
if (newParam != null) {
toUpdate = _paramGroup.getParameter(BaerConstants.SMAC_PARAM_NAME);
toUpdate.setValue(newParam.getValue());
}
newParam = request.getParameter(ProcessorConstants.LOG_PREFIX_PARAM_NAME);
if (newParam != null) {
toUpdate = _paramGroup.getParameter(ProcessorConstants.LOG_PREFIX_PARAM_NAME);
toUpdate.setValue(newParam.getValue());
}
newParam = request.getParameter(ProcessorConstants.LOG_TO_OUTPUT_PARAM_NAME);
if (newParam != null) {
toUpdate = _paramGroup.getParameter(ProcessorConstants.LOG_TO_OUTPUT_PARAM_NAME);
toUpdate.setValue(newParam.getValue());
}
} catch (ParamValidateException e) {
throw new ProcessorException(e.getMessage());
}
}
File inputFile = ((File) _paramGroup.getParameter(DefaultRequestElementFactory.INPUT_PRODUCT_PARAM_NAME).getValue());
request.addInputProduct(new ProductRef(inputFile, null, null));
}
File outputFile = ((File) _paramGroup.getParameter(DefaultRequestElementFactory.OUTPUT_PRODUCT_PARAM_NAME).getValue());
request.addOutputProduct(new ProductRef(outputFile, _outFileFormat, null));
}
request.addParameter(_paramGroup.getParameter(BaerConstants.BITMASK_PARAM_NAME));
request.addParameter(_paramGroup.getParameter(BaerConstants.AER_PHASE_PARAM_NAME));
request.addParameter(_paramGroup.getParameter(DefaultRequestElementFactory.LOG_PREFIX_PARAM_NAME));
request.addParameter(_paramGroup.getParameter(DefaultRequestElementFactory.LOG_TO_OUTPUT_PARAM_NAME));
request.addParameter(_paramGroup.getParameter(BaerConstants.USE_BAER_PARAM_NAME));
request.addParameter(_paramGroup.getParameter(BaerConstants.USE_ATM_COR_PARAM_NAME));
request.addParameter(_paramGroup.getParameter(BaerConstants.USE_CLOUD_PARAM_NAME));
request.addParameter(_paramGroup.getParameter(BaerConstants.SMAC_PARAM_NAME));
}
return _paramGroup.getParameter(name);
}
}