Skip to content

Commit

Permalink
Enable YAML loading, fix bug with graphic zoom initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
DanySK committed Apr 20, 2016
1 parent 9cef9c9 commit c293097
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import org.apache.commons.math3.random.RandomGenerator;
import it.unibo.alchemist.language.EnvironmentBuilder;
import it.unibo.alchemist.language.EnvironmentBuilder.Result;
import it.unibo.alchemist.loader.Loader;
import it.unibo.alchemist.loader.YamlLoader;
import it.unibo.alchemist.model.implementations.times.DoubleTime;
import it.unibo.alchemist.model.interfaces.Environment;

Expand All @@ -52,7 +54,7 @@
public class Perspective<T> extends JPanel implements ChangeListener, ActionListener {

private static final long serialVersionUID = -6074331788924400019L;
private static final FileFilter XML_FILTER = new FileNameExtensionFilter(getString("alchemist_xml"), "xml");
private static final FileFilter FILE_FILTER = new FileNameExtensionFilter(getString("alchemist_xml"), "xml", "yml", "yaml", "YML", "YAML");
private static final Logger L = LoggerFactory.getLogger(Perspective.class);
private static final String FILE_NOT_VALID = getString("file_not_valid");
private static final String RANDOM_REINIT_SUCCESS = getString("random_reinit_success");
Expand All @@ -70,7 +72,7 @@ public class Perspective<T> extends JPanel implements ChangeListener, ActionList
private JEffectsTab<T> effectsTab;
private transient Simulation<T> sim;
private final StatusBar status;
private File xml;
private File fileToLoad;


/**
Expand Down Expand Up @@ -117,7 +119,7 @@ private void dispose() {
@Override
public void actionPerformed(final ActionEvent e) {
if (Commands.OPEN.equalsToString(e.getActionCommand())) {
openXML();
openFile();
} else if (Commands.PARALLEL.equalsToString(e.getActionCommand())) {
process(true);
} else if (Commands.PROCESS.equalsToString(e.getActionCommand())) {
Expand Down Expand Up @@ -163,24 +165,24 @@ private void createMonitor() {
setMainDisplay(display);
}

private void openXML() {
private void openFile() {
final JFileChooser fc = new JFileChooser();
fc.setMultiSelectionEnabled(false);
fc.setFileFilter(XML_FILTER);
fc.setFileFilter(FILE_FILTER);
fc.setCurrentDirectory(currentDirectory);
final int returnVal = fc.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
xml = fc.getSelectedFile();
fileToLoad = fc.getSelectedFile();
currentDirectory = fc.getSelectedFile().getParentFile();
if (xml.exists() && xml.getName().endsWith("xml")) {
status.setText(getString("ready_to_process") + " " + xml.getAbsolutePath());
if (fileToLoad.exists()) {
status.setText(getString("ready_to_process") + " " + fileToLoad.getAbsolutePath());
status.setOK();
if (sim != null) {
sim.addCommand(new Engine.StateCommand<T>().stop().build());
}
bar.setFileOK(true);
} else {
status.setText(FILE_NOT_VALID + " " + xml.getAbsolutePath());
status.setText(FILE_NOT_VALID + " " + fileToLoad.getAbsolutePath());
status.setNo();
bar.setFileOK(false);
}
Expand All @@ -194,9 +196,16 @@ private void process(final boolean parallel) {
}
try {
sim = null;
final Future<Result<T>> fenv = EnvironmentBuilder.build(new FileInputStream(xml));
final Environment<T> env = fenv.get().getEnvironment();
rand = fenv.get().getRandomGenerator();
final Environment<T> env;
if (fileToLoad.getName().toLowerCase().endsWith("xml")) {
final Future<Result<T>> fenv = EnvironmentBuilder.build(new FileInputStream(fileToLoad));
env = fenv.get().getEnvironment();
rand = fenv.get().getRandomGenerator();
} else {
rand = null;
final Loader loader = new YamlLoader(new FileInputStream(fileToLoad));
env = loader.getDefault();
}
sim = new Engine<>(env, new DoubleTime(Double.POSITIVE_INFINITY), parallel);
bar.setSimulation(sim);
scp.setSimulation(sim);
Expand All @@ -209,7 +218,7 @@ private void process(final boolean parallel) {
bar.setProcessOK(true);
effectsTab.setEnabled(true);
status.setOK();
status.setText(getString("file_processed") + ": " + xml.getAbsolutePath());
status.setText(getString("file_processed") + ": " + fileToLoad.getAbsolutePath());
} catch (Exception e) {
processError(e);
}
Expand All @@ -219,26 +228,34 @@ private void processError(final Throwable e) {
SwingUtilities.invokeLater(() -> {
bar.setFileOK(false);
bar.setProcessOK(false);
status.setText(FILE_NOT_VALID + " " + xml.getAbsolutePath());
status.setText(FILE_NOT_VALID + " " + fileToLoad.getAbsolutePath());
status.setNo();
L.error("Process error", e);
});
}

private void setMainDisplay(final GraphicalOutputMonitor<T> gom) {
if (!(gom instanceof Component)) {
throw new IllegalArgumentException("Java Swing can not load a " + gom);
}
final Component gomComp = (Component) gom;
if (main != null) {
sim.removeOutputMonitor(main);
gom.setStep(main.getStep());
gom.setRealTime(main.isRealTime());
remove((Component) main);
final Component mainComp = (Component) main;
gomComp.setSize(mainComp.getSize());
remove(mainComp);
} else {
gomComp.setSize(getSize());
}
main = gom;
add(gomComp, BorderLayout.CENTER);
revalidate();
makeEffects();
if (sim != null) {
new Thread(() -> sim.addOutputMonitor(main)).start();
}
add((Component) main, BorderLayout.CENTER);
makeEffects();
revalidate();
}

private void setRandom() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
add_effect = Add effect
alchemist = Alchemist
alchemist_xml = Alchemist XML
alchemist_xml = Alchemist YAML or Alchemist XML
alchemist_effect_builder = Alchemist effect builder
attach_monitor = Attach monitor
cannot_access_object = Cannot access the object
Expand Down

0 comments on commit c293097

Please sign in to comment.