Skip to content

Commit

Permalink
Merge branch 'feature/gui-update'
Browse files Browse the repository at this point in the history
  • Loading branch information
ZenHarbinger committed Dec 22, 2016
2 parents a80cdcd + 4df2e8c commit 8b2447d
Show file tree
Hide file tree
Showing 13 changed files with 709 additions and 67 deletions.
6 changes: 5 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,11 @@
<artifactId>batik-anim</artifactId>
<version>1.8</version>
</dependency>

<dependency>
<groupId>org.dockingframes</groupId>
<artifactId>docking-frames-common</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
158 changes: 135 additions & 23 deletions src/main/java/org/tros/torgo/ControllerBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,31 @@
*/
package org.tros.torgo;

import bibliothek.gui.dock.DefaultDockable;
import bibliothek.gui.dock.common.CControl;
import bibliothek.gui.dock.common.CGrid;
import bibliothek.gui.dock.common.CLocation;
import bibliothek.gui.dock.common.DefaultSingleCDockable;
import bibliothek.gui.dock.common.mode.ExtendedMode;
import bibliothek.gui.dock.common.SingleCDockable;
import bibliothek.gui.dock.common.SingleCDockableFactory;
import bibliothek.util.xml.XElement;
import bibliothek.util.xml.XIO;
import org.tros.torgo.interpreter.CodeBlock;
import org.tros.torgo.interpreter.InterpreterListener;
import org.tros.torgo.interpreter.InterpreterThread;
import org.tros.torgo.interpreter.Scope;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;
Expand All @@ -48,17 +59,18 @@
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JSplitPane;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.event.EventListenerSupport;
import org.apache.commons.lang3.tuple.ImmutablePair;
import static org.tros.torgo.Main.IMAGE_ICON_CLASS_PATH;
import org.tros.torgo.swing.AboutWindow;
import org.tros.torgo.swing.Localization;
import org.tros.torgo.swing.TorgoMenuBar;
import org.tros.utils.swing.NamedWindow;
import org.tros.utils.AutoResetEvent;
import org.tros.utils.PathUtils;

/**
* The main application. Controls GUI and interpreting process.
Expand All @@ -74,6 +86,7 @@ public abstract class ControllerBase implements Controller {
private String filename;
protected final AutoResetEvent step;
protected final AtomicBoolean isStepping;
private CControl dockControl;

private final ArrayList<JCheckBoxMenuItem> viz = new ArrayList<>();

Expand Down Expand Up @@ -157,6 +170,55 @@ protected ControllerBase() {
*/
protected abstract InterpreterThread createInterpreterThread(String source);

public static class TorgoSingleDockable extends DefaultSingleCDockable {

public TorgoSingleDockable(String title, final Component panel) {
super(title);
super.setTitleText(title);
super.add(panel);
}
}

public static class TorgoDockable extends DefaultDockable {

public TorgoDockable(String title, final Component panel) {
super(title);
super.setTitleText(title);
super.add(panel);
}
}

/* This method simulates the creation of a layout */
private static XElement createLayout(Component display, ArrayList<ImmutablePair<String, Component>> input) {
/* This method simulates the creation of a layout */
CControl control = new CControl();
control.getContentArea();

CGrid grid = new CGrid(control);

DefaultSingleCDockable displayDock = display != null ? new TorgoSingleDockable("Display", display) : null;
if (displayDock != null) {
grid.add(0, 0, 10, 10, displayDock);
displayDock.setLocation(CLocation.base().minimalWest());
displayDock.setExtendedMode(ExtendedMode.NORMALIZED);
}

int count = 1;
for (ImmutablePair<String, Component> key : input) {
DefaultSingleCDockable dock = new TorgoSingleDockable(key.left, key.right);
grid.add(10, 0, 6, count, dock);
dock.setExtendedMode(ExtendedMode.NORMALIZED);
count += 1;
}

control.getContentArea().deploy(grid);

XElement root = new XElement("root");
control.writeXML(root);
control.destroy();
return root;
}

/**
* Initialize the window. This is called here from run() and not the
* constructor so that the Service Provider doesn't load up all of the
Expand All @@ -173,22 +235,72 @@ private void initSwing() {
contentPane.add(tb, BorderLayout.NORTH);
}

final java.util.prefs.Preferences prefs = java.util.prefs.Preferences.userNodeForPackage(NamedWindow.class);
dockControl = new CControl(window);
window.add(dockControl.getContentArea(), BorderLayout.CENTER);
final ArrayList<String> presetFilter = new ArrayList<>();
if (torgoCanvas != null) {
final JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, torgoCanvas.getComponent(), torgoPanel.getComponent());
int dividerLocation = prefs.getInt(this.getClass().getName() + "divider-location", window.getWidth() - 300);
splitPane.setDividerLocation(dividerLocation);
splitPane.addPropertyChangeListener(new PropertyChangeListener() {

@Override
public void propertyChange(PropertyChangeEvent pce) {
prefs.putInt(this.getClass().getName() + "divider-location", splitPane.getDividerLocation());
presetFilter.add("Display");
}
for (ImmutablePair<String, Component> pair : torgoPanel.getTorgoComponents()) {
presetFilter.add(pair.left);
}
bibliothek.util.Filter<String> filter = new bibliothek.util.Filter<String>() {
@Override
public boolean includes(String item) {
return presetFilter.contains(item);
}
};
dockControl.addSingleDockableFactory(filter, new SingleCDockableFactory() {
@Override
public SingleCDockable createBackup(String id) {
TorgoSingleDockable ret = null;
if ("Display".equals(id)) {
ret = new TorgoSingleDockable(id, torgoCanvas.getComponent());
} else {
for (ImmutablePair<String, Component> pair : torgoPanel.getTorgoComponents()) {
if (pair.left.equals(id)) {
ret = new TorgoSingleDockable(pair.left, pair.right);
}
}
}
if (ret != null) {
ImageIcon icon = Main.getIcon("layouts/" + ret.getTitleText().toLowerCase() + "-24x24.png");
ret.setTitleIcon(icon);
}
});
return ret;
}
});

contentPane.add(splitPane);
} else {
contentPane.add(torgoPanel.getComponent());
// Try to load a saved layout.
// If no layout exists or it fails, load from CLASSPATH/resources.
// If that fails, dynamically generate something.
String layoutFileName = PathUtils.getApplicationConfigDirectory(TorgoInfo.INSTANCE) + java.io.File.separatorChar + getLang() + "-layout.xml";
File layoutFile = new File(layoutFileName);
boolean loaded = false;
if (layoutFile.exists()) {
try {
XElement elem = XIO.readUTF(new FileInputStream(layoutFile));
dockControl.readXML(elem);
loaded = true;
} catch (FileNotFoundException ex) {
Logger.getLogger(ControllerBase.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(ControllerBase.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (!loaded) {
try {
java.util.Enumeration<URL> resources = ClassLoader.getSystemClassLoader().getResources("layouts/" + this.getLang() + "-layout.xml");
XElement elem = XIO.readUTF(resources.nextElement().openStream());
dockControl.readXML(elem);
loaded = true;
} catch (IOException | java.util.NoSuchElementException ex) {
Logger.getLogger(ControllerBase.class.getName()).log(Level.WARNING, "Layout Error: Auto-generating: {0}", ex.getMessage());
}
}
if (!loaded) {
XElement elem = createLayout(torgoCanvas != null ? torgoCanvas.getComponent() : null, torgoPanel.getTorgoComponents());
dockControl.readXML(elem);
}

JMenuBar mb = createMenuBar();
Expand Down Expand Up @@ -291,13 +403,19 @@ public void windowOpened(WindowEvent e) {

/**
* We only care if the window is closing so we can kill the
* interpreter thread.
* interpreter thread and save the layout.
*
* @param e
*/
@Override
public void windowClosing(WindowEvent e) {
stopInterpreter();
try {
String layoutFile = PathUtils.getApplicationConfigDirectory(TorgoInfo.INSTANCE) + java.io.File.separatorChar + getLang() + "-layout.xml";
dockControl.writeXML(new java.io.File(layoutFile));
} catch (IOException ex) {
Logger.getLogger(ControllerBase.class.getName()).log(Level.SEVERE, null, ex);
}
}

@Override
Expand Down Expand Up @@ -517,9 +635,6 @@ public void startInterpreter() {
TorgoToolkit.getVisualization(item.getText()).create().watch(this.getLang(), this, interp);
}
}
// viz.stream().filter((item) -> (item.getState())).map((item) -> TorgoToolkit.getVisualization(item.getText()).create()).forEach((visualization) -> {
// visualization.watch(this.getLang(), this, interp);
// });

for (InterpreterListener l : listeners.getListeners()) {
interp.addInterpreterListener(l);
Expand Down Expand Up @@ -568,9 +683,6 @@ public void debugInterpreter() {
TorgoToolkit.getVisualization(item.getText()).create().watch(this.getLang(), this, interp);
}
}
// viz.stream().filter((item) -> (item.getState())).map((item) -> TorgoToolkit.getVisualization(item.getText()).create()).forEach((visualization) -> {
// visualization.watch(this.getLang(), this, interp);
// });

for (InterpreterListener l : listeners.getListeners()) {
interp.addInterpreterListener(l);
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/org/tros/torgo/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,21 @@ public void run() {
public static final String IMAGE_ICON_CLASS_PATH = "torgo-48x48.png";

public static void loadIcon(Window frame) {
frame.setIconImage(getIcon().getImage());
}

public static ImageIcon getIcon() {
return getIcon(IMAGE_ICON_CLASS_PATH);
}

public static ImageIcon getIcon(String path) {
try {
java.util.Enumeration<URL> resources = ClassLoader.getSystemClassLoader().getResources(IMAGE_ICON_CLASS_PATH);
java.util.Enumeration<URL> resources = ClassLoader.getSystemClassLoader().getResources(path);
ImageIcon ico = new ImageIcon(resources.nextElement());
frame.setIconImage(ico.getImage());
return ico;
} catch (IOException ex) {
org.tros.utils.logging.Logging.getLogFactory().getLogger(Main.class).warn(null, ex);
}
return null;
}
}
5 changes: 4 additions & 1 deletion src/main/java/org/tros/torgo/TorgoTextConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
package org.tros.torgo;

import java.awt.Component;
import java.util.ArrayList;
import java.util.Map;
import org.apache.commons.lang3.tuple.ImmutablePair;

/**
* I/O interface. Get source and print messages.
Expand Down Expand Up @@ -98,5 +101,5 @@ public interface TorgoTextConsole {
*
* @return
*/
Component getComponent();
ArrayList<ImmutablePair<String, Component>> getTorgoComponents();
}
Loading

0 comments on commit 8b2447d

Please sign in to comment.