/*
JWildfire - an image and animation processor written in Java
Copyright (C) 1995-2011 Andreas Maschke
This 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 software 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 software;
if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jwildfire.script;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import org.jwildfire.base.Tools;
import org.jwildfire.create.ImageCreator;
import org.jwildfire.loader.ImageLoader;
import org.jwildfire.swing.Buffer;
import org.jwildfire.swing.BufferList;
import org.jwildfire.transform.Transformer;
public class ActionList extends ArrayList<Action> {
private static final long serialVersionUID = 1L;
private static final String TOKEN_COMMENT = "//";
private static final String TOKEN_EOF = "EOF";
public void saveToFile(String pFilename) throws Exception {
String lineSeparator = System.getProperty("line.separator");
if ((lineSeparator == null) || (lineSeparator.length() == 0))
lineSeparator = "\n";
StringBuffer b = new StringBuffer();
b.append(TOKEN_COMMENT + " This script was generated by " + Tools.APP_TITLE + " "
+ Tools.APP_VERSION + " at " + Tools.DateTimeToString(Tools.now()) + lineSeparator);
for (Action action : this) {
action.saveToBuffer(b, lineSeparator);
}
Writer out = new OutputStreamWriter(new FileOutputStream(pFilename), Tools.FILE_ENCODING);
try {
out.write(b.toString());
}
finally {
out.close();
}
}
public void loadFromString(String pContent) throws Exception {
clear();
String rows[] = (pContent + "\r\n" + TOKEN_EOF).split("[\\r\\n]+");
parseRows(rows);
}
public void loadFromFile(String pFilename) throws Exception {
clear();
String rows[];
// read the file and split it's content in rows
String lineFeed = System.getProperty("line.separator");
String content = Tools.readUTF8Textfile(pFilename) + lineFeed + TOKEN_EOF;
rows = content.split("[\\r\\n]+");
//
parseRows(rows);
}
private void parseRows(String[] rows) {
// parse the rows
List<String> currLines = null;
for (String row : rows) {
// Ignore comments
if ((row.length() >= TOKEN_COMMENT.length()) && (row.indexOf(TOKEN_COMMENT) == 0))
continue;
// collect all lines belonging to one action
if (row.length() > 1) {
if (row.charAt(0) != ' ') {
if (currLines != null) {
add(Action.readFromLines(currLines));
}
if ((row.length() >= TOKEN_EOF.length()) && (row.indexOf(TOKEN_EOF) == 0))
break;
currLines = new ArrayList<String>();
currLines.add(row);
}
else {
if (currLines != null)
currLines.add(row);
else
throw new IllegalArgumentException(row);
}
}
}
}
private Action addAction() {
Action res = new Action();
add(res);
return res;
}
public Action addLoadImageAction(String pFilename) {
Action res = addAction();
res.setActionType(ActionType.LOAD_IMAGE);
res.setParameter(pFilename);
res.setOutputBuffer(new File(pFilename).getName());
return res;
}
public Action addExecuteTransformerAction(Transformer pTransformer, Buffer pInBuffer,
Buffer pOutBuffer, Buffer pOutHDRBuffer, Buffer pOutBuffer3D) {
Action res = addAction();
res.setActionType(ActionType.EXECUTE_TRANSFORMER);
res.setParameter(pTransformer.getName());
res.importProperties(pTransformer);
res.setInputBuffer(pInBuffer);
res.setOutputBuffer(pOutBuffer);
res.setOutputHDRBuffer(pOutHDRBuffer);
res.setOutputBuffer3D(pOutBuffer3D);
return res;
}
public Action addExecuteImageCreatorAction(ImageCreator pImageCreator, Buffer pOutBuffer,
int pWidth, int pHeight) {
Action res = addAction();
res.setActionType(ActionType.EXECUTE_CREATOR);
res.setParameter(pImageCreator.getName());
res.setWidth(pWidth);
res.setHeight(pHeight);
res.importProperties(pImageCreator);
res.setInputBuffer((Buffer) null);
res.setOutputBuffer(pOutBuffer);
res.setOutputBuffer3D((Buffer) null);
return res;
}
public Action addExecuteImageLoaderAction(ImageLoader pImageLoader, Buffer pOutBuffer) {
Action res = addAction();
res.setActionType(ActionType.EXECUTE_LOADER);
res.setParameter(pImageLoader.getName());
res.importProperties(pImageLoader);
res.setInputBuffer((Buffer) null);
res.setOutputBuffer(pOutBuffer);
res.setOutputBuffer3D((Buffer) null);
return res;
}
public void removeBuffer(Buffer pBuffer, BufferList pBufferList) {
if (pBuffer != null) {
int idx = 0;
while (idx < size()) {
Action action = get(idx);
if (((action.getOutputBuffer() != null) && (action.getOutputBuffer().equals(pBuffer
.getName())))
|| ((action.getOutputBuffer3D() != null) && (action.getOutputBuffer3D().equals(pBuffer
.getName()))))
{
remove(action);
}
else {
if ((action.getInputBuffer() != null)
&& (action.getInputBuffer().equals(pBuffer.getName())))
{
if (action.getOutputBuffer() != null) {
removeBuffer(pBufferList.bufferByName(action.getOutputBuffer()), pBufferList);
}
if (action.getOutputBuffer3D() != null) {
removeBuffer(pBufferList.bufferByName(action.getOutputBuffer3D()), pBufferList);
}
}
idx++;
}
}
}
}
public ActionList clone() {
ActionList res = new ActionList();
for (Action action : this) {
res.add(action.clone());
}
return res;
}
}