String selected = (String) data.TinaNonlinearControlsRows[pIdx].getNonlinearParamsCmb().getSelectedItem();
XForm xForm = getCurrXForm();
if (xForm != null && selected != null && selected.length() > 0) {
saveUndoPoint();
if (pIdx < xForm.getVariationCount()) {
final Variation var = xForm.getVariation(pIdx);
int idx;
if ((idx = var.getFunc().getParameterIndex(selected)) >= 0) {
String valStr = data.TinaNonlinearControlsRows[pIdx].getNonlinearParamsREd().getText();
if (valStr == null || valStr.length() == 0) {
valStr = "0";
}
// round the delta to whole numbers if the parameter is of type integer
if (Math.abs(pDelta) > MathLib.EPSILON) {
Object val = var.getFunc().getParameterValues()[idx];
if (val != null && val instanceof Integer) {
if (Math.abs(pDelta) < 1.0) {
pDelta = pDelta < 0 ? -1 : 1;
}
else {
pDelta = Math.round(pDelta);
}
}
}
double val = Tools.stringToDouble(valStr) + pDelta;
var.getFunc().setParameter(selected, val);
data.TinaNonlinearControlsRows[pIdx].getNonlinearParamsREd().setText(Tools.doubleToString(val));
}
else if ((idx = var.getFunc().getRessourceIndex(selected)) >= 0) {
final String rName = var.getFunc().getRessourceNames()[idx];
RessourceType resType = var.getFunc().getRessourceType(rName);
switch (resType) {
case FONT_NAME: {
String oldFontname = null;
{
byte val[] = var.getFunc().getRessourceValues()[idx];
if (val != null) {
oldFontname = new String(val);
}
}
Font oldFont = new Font(oldFontname != null ? oldFontname : "Arial", Font.PLAIN, 24);
Font selectedFont = JFontChooser.showDialog(centerPanel, "Choose font", oldFont);
if (selectedFont != null) {
String valStr = selectedFont.getFontName();
byte[] valByteArray = valStr != null ? valStr.getBytes() : null;
var.getFunc().setRessource(rName, valByteArray);
}
}
break;
case IMAGE_FILENAME: {
String oldFilename = null;
{
byte val[] = var.getFunc().getRessourceValues()[idx];
if (val != null) {
oldFilename = new String(val);
}
}
JFileChooser chooser = new ImageFileChooser(Tools.FILEEXT_PNG);
if (oldFilename != null && oldFilename.length() > 0) {
try {
chooser.setCurrentDirectory(new File(oldFilename).getAbsoluteFile().getParentFile());
chooser.setSelectedFile(new File(oldFilename));
}
catch (Exception ex) {
ex.printStackTrace();
}
}
else {
if (prefs.getInputImagePath() != null) {
try {
chooser.setCurrentDirectory(new File(prefs.getInputImagePath()));
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
if (chooser.showOpenDialog(centerPanel) == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
String valStr = file.getAbsolutePath();
byte[] valByteArray = valStr != null ? valStr.getBytes() : null;
var.getFunc().setRessource(rName, valByteArray);
}
}
break;
case IMAGE_FILE: {
JFileChooser chooser = new ImageFileChooser(Tools.FILEEXT_PNG);
if (prefs.getInputImagePath() != null) {
try {
chooser.setCurrentDirectory(new File(prefs.getInputImagePath()));
}
catch (Exception ex) {
ex.printStackTrace();
}
}
if (chooser.showOpenDialog(centerPanel) == JFileChooser.APPROVE_OPTION) {
try {
File file = chooser.getSelectedFile();
byte[] imgData = Tools.readFile(file.getAbsolutePath());
var.getFunc().setRessource(rName, imgData);
}
catch (Exception ex) {
errorHandler.handleError(ex);
}
}
}
break;
case SVG_FILE: {
JFileChooser chooser = new SvgFileChooser(prefs);
if (prefs.getTinaSVGPath() != null) {
try {
chooser.setCurrentDirectory(new File(prefs.getTinaSVGPath()));
}
catch (Exception ex) {
ex.printStackTrace();
}
}
if (chooser.showOpenDialog(centerPanel) == JFileChooser.APPROVE_OPTION) {
try {
File file = chooser.getSelectedFile();
String svg = Tools.readUTF8Textfile(file.getAbsolutePath());
byte[] valByteArray = svg != null ? svg.getBytes() : null;
var.getFunc().setRessource(rName, valByteArray);
}
catch (Exception ex) {
errorHandler.handleError(ex);
}
}
}
break;
default: {
final RessourceDialog dlg = new RessourceDialog(SwingUtilities.getWindowAncestor(centerPanel), prefs, errorHandler);
dlg.setRessourceName(rName);
byte val[] = var.getFunc().getRessourceValues()[idx];
if (val != null) {
dlg.setRessourceValue(new String(val));
}
dlg.addValidation(new RessourceValidation() {
@Override
public void validate() {
String valStr = dlg.getRessourceValue();
byte[] valByteArray = valStr != null ? valStr.getBytes() : null;
byte[] oldValue = var.getFunc().getRessource(rName);
try {
var.getFunc().setRessource(rName, valByteArray);
var.getFunc().validate();
}
catch (Throwable ex) {
var.getFunc().setRessource(rName, oldValue);
throw new RuntimeException(ex);
}
}
});
dlg.setModal(true);
dlg.setVisible(true);
if (dlg.isConfirmed()) {
try {
String valStr = dlg.getRessourceValue();
byte[] valByteArray = valStr != null ? valStr.getBytes() : null;
var.getFunc().setRessource(rName, valByteArray);
}
catch (Throwable ex) {
errorHandler.handleError(ex);
}
}