Commit cbe641d0 authored by Fabian Becker's avatar Fabian Becker

Lots of refactoring

- Introduces new WindowManager and subclasses for window management
- Breaks a lot of stuff :/
parent 3e22ffa4
Pipeline #54 passed with stage
......@@ -16,21 +16,16 @@ import java.io.Serializable;
/**
*
*/
public class JTextoutputFrame implements JTextoutputFrameInterface, ActionListener, Serializable {
public class JTextoutputFrame extends JPanel implements JTextoutputFrameInterface, ActionListener, Serializable {
private JMenuItem clearItem, saveItem;
protected String frameTitle = "undefined";
private transient JTextArea textArea = null;
private final JInternalFrame frame;
private JPopupMenu popup;
/**
*
*/
public JTextoutputFrame(String title) {
frameTitle = title;
frame = new JEFrame(frameTitle);
frame.setClosable(false);
public JTextoutputFrame() {
textArea = null;
}
......@@ -51,23 +46,6 @@ public class JTextoutputFrame implements JTextoutputFrameInterface, ActionListen
print(txt + '\n');
}
@Override
public void setShow(boolean bShow) {
if (frame.isVisible() != bShow) {
if (frame.isVisible()) {
frame.dispose();
textArea.setText(null);
} else {
if (textArea == null) {
createFrame();
} else {
frame.setVisible(true);
}
frame.setEnabled(true);
}
}
}
/**
*
*/
......@@ -84,9 +62,9 @@ public class JTextoutputFrame implements JTextoutputFrameInterface, ActionListen
((AbstractDocument)textArea.getDocument()).setDocumentFilter(new LineBufferDocumentFilter(textArea, 2500));
frame.getContentPane().setLayout(new BorderLayout());
setLayout(new BorderLayout());
final JScrollPane scrollpane = new JScrollPane(textArea);
frame.getContentPane().add(scrollpane, BorderLayout.CENTER);
add(scrollpane, BorderLayout.CENTER);
scrollpane.getViewport().addChangeListener(new ChangeListener() {
private int lastHeight;
......@@ -104,9 +82,8 @@ public class JTextoutputFrame implements JTextoutputFrameInterface, ActionListen
}
});
makePopupMenu();
frame.pack();
frame.setSize(800, 400);
frame.setVisible(true);
setSize(800, 400);
setVisible(true);
}
void makePopupMenu() {
......@@ -130,11 +107,17 @@ public class JTextoutputFrame implements JTextoutputFrameInterface, ActionListen
if (src == clearItem) {
textArea.setText(null);
} else if (src == saveItem) {
FileTools.saveObjectWithFileChooser(frame, textArea.getText(), null);
FileTools.saveObjectWithFileChooser(this, textArea.getText(), null);
} else {
System.err.println("Unknown popup component (JTextoutputFrame)!");
}
}
@Override
public void setShow(boolean bShow) {
// Nothing to do
// ToDo remove interface
}
}
/**
......
package eva2.gui;
import eva2.EvAInfo;
import eva2.gui.wm.WindowManager;
import eva2.optimization.InterfaceOptimizationParameters;
import eva2.optimization.OptimizationParameters;
import eva2.optimization.OptimizationStateListener;
......@@ -355,7 +356,7 @@ public class MainFrame extends JFrame implements OptimizationStateListener {
setMinimumSize(new Dimension(1024, 800));
/* Creates the desktopPane for Plot/Text Output */
desktopPane = new JExtDesktopPane();
desktopPane = WindowManager.getInstance().getDesktopPane();
JEFrameRegister.getInstance().setDesktopPane(desktopPane);
/* Creates desktopPane ToolBar to show tiling buttons */
JExtDesktopPaneToolBar desktopToolBar = new JExtDesktopPaneToolBar((JExtDesktopPane) desktopPane);
......
package eva2.gui.plot;
import eva2.gui.wm.WindowManager;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
......@@ -55,6 +57,7 @@ public class GraphWindow {
private GraphWindow(String plotName, String strx, String stry) {
name = plotName;
plotter = new Plot(plotName, strx, stry, true);
WindowManager.getInstance().addWindow(plotName, (Plot)plotter);
}
/**
......
......@@ -24,7 +24,7 @@ import java.util.Locale;
/**
* ToDo: Rename to PlotWindow
*/
public class Plot implements PlotInterface, Serializable {
public class Plot extends JPanel implements PlotInterface, Serializable {
/**
* Generated serial version identifier.
......@@ -36,7 +36,6 @@ public class Plot implements PlotInterface, Serializable {
private String xAxisText;
private String yAxisText;
protected FunctionArea plotArea;
protected JInternalFrame internalFrame;
/**
* You might want to try to assign the x-range as x and y-range as y array
......@@ -129,7 +128,7 @@ public class Plot implements PlotInterface, Serializable {
// Let user choose output path and filename
JFileChooser fc = new JFileChooser();
if (fc.showSaveDialog(internalFrame) != JFileChooser.APPROVE_OPTION) {
if (fc.showSaveDialog(this) != JFileChooser.APPROVE_OPTION) {
return;
}
try {
......@@ -152,7 +151,6 @@ public class Plot implements PlotInterface, Serializable {
*/
@Override
public void init() {
internalFrame = new JEFrame("Plot: " + plotName);
BasicResourceLoader loader = BasicResourceLoader.getInstance();
byte[] bytes = loader.getBytesFromResourceLocation(EvAInfo.iconLocation, true);
......@@ -162,20 +160,10 @@ public class Plot implements PlotInterface, Serializable {
installButtons(buttonPanel);
internalFrame.add(buttonPanel, BorderLayout.PAGE_END);
internalFrame.add(plotArea, BorderLayout.CENTER); // north was not so nice
internalFrame.addInternalFrameListener(new InternalFrameAdapter() {
add(buttonPanel, BorderLayout.PAGE_END);
add(plotArea, BorderLayout.CENTER); // north was not so nice
@Override
public void internalFrameClosing(InternalFrameEvent e) {
super.internalFrameClosing(e);
plotArea.clearAll(); // this was a memory leak
plotArea = null;
internalFrame.dispose();
}
});
internalFrame.pack();
internalFrame.setVisible(true);
setVisible(true);
}
/**
......@@ -239,13 +227,6 @@ public class Plot implements PlotInterface, Serializable {
getFunctionArea().drawIcon(iconType, sb.toString(), indy.getDoublePosition(), graphID);
}
public void setPreferredSize(Dimension prefSize) {
if (internalFrame != null) {
internalFrame.setPreferredSize(prefSize);
internalFrame.pack();
}
}
/**
* Return true if the Plot object is valid.
*
......@@ -253,7 +234,7 @@ public class Plot implements PlotInterface, Serializable {
*/
@Override
public boolean isValid() {
return (internalFrame != null) && (plotArea != null);
return plotArea != null;
}
/**
......@@ -293,7 +274,7 @@ public class Plot implements PlotInterface, Serializable {
plotArea.clearAll();
plotArea.removeAllDElements();
plotArea.clearLegend();
internalFrame.repaint();
repaint();
}
/**
......@@ -326,7 +307,7 @@ public class Plot implements PlotInterface, Serializable {
if (fileChooser == null) {
createFileChooser();
}
int returnVal = fileChooser.showOpenDialog(internalFrame);
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File selected = fileChooser.getSelectedFile();
try {
......@@ -341,7 +322,7 @@ public class Plot implements PlotInterface, Serializable {
}
return obj;
} catch (Exception ex) {
JOptionPane.showMessageDialog(internalFrame, "Couldn't read object: "
JOptionPane.showMessageDialog(this, "Couldn't read object: "
+ selected.getName() + "\n" + ex.getMessage(),
"Open object file", JOptionPane.ERROR_MESSAGE);
}
......@@ -363,18 +344,18 @@ public class Plot implements PlotInterface, Serializable {
if (fileChooser == null) {
createFileChooser();
}
int returnVal = fileChooser.showSaveDialog(internalFrame);
int returnVal = fileChooser.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File sFile = fileChooser.getSelectedFile();
if (sFile.exists()) {
returnVal = JOptionPane.showConfirmDialog(internalFrame, "The file "
returnVal = JOptionPane.showConfirmDialog(this, "The file "
+ sFile.getName() + " already exists. Overwrite?");
if (returnVal != JOptionPane.YES_OPTION) {
return;
}
}
if (!(plotArea.exportToAscii(sFile))) {
JOptionPane.showMessageDialog(internalFrame,
JOptionPane.showMessageDialog(this,
"Couldn't write to file: " + sFile.getName(),
"Export error", JOptionPane.ERROR_MESSAGE);
}
......@@ -388,7 +369,7 @@ public class Plot implements PlotInterface, Serializable {
if (fileChooser == null) {
createFileChooser();
}
int returnVal = fileChooser.showSaveDialog(internalFrame);
int returnVal = fileChooser.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File sFile = fileChooser.getSelectedFile();
try {
......@@ -397,7 +378,7 @@ public class Plot implements PlotInterface, Serializable {
oo.writeObject(object);
oo.close();
} catch (IOException ex) {
JOptionPane.showMessageDialog(internalFrame,
JOptionPane.showMessageDialog(this,
"Couldn't write to file: " + sFile.getName() + "\n"
+ ex.getMessage(), "Save object",
JOptionPane.ERROR_MESSAGE);
......@@ -432,8 +413,8 @@ public class Plot implements PlotInterface, Serializable {
*
*/
public void dispose() {
internalFrame.dispose();
internalFrame = null;
plotArea.clearAll(); // this was a memory leak
plotArea = null;
}
/**
......
......@@ -39,11 +39,11 @@ public class TopoPlot extends Plot {
* @param color_scale the topologies color coding. Values (0-3) are valid. @See ColorBarCalculator.
*/
public void setParams(int gridX, int gridY, int color_scale) {
if (gridX > internalFrame.getWidth()) {
gridX = internalFrame.getWidth();
if (gridX > getWidth()) {
gridX = getWidth();
}
if (gridY > internalFrame.getHeight()) {
gridY = internalFrame.getHeight();
if (gridY > getHeight()) {
gridY = getHeight();
}
gridx = gridX;
gridy = gridY;
......@@ -120,7 +120,7 @@ public class TopoPlot extends Plot {
fitRange = java.lang.Math.abs(max - min);
ColorBarCalculator colorBar = new ColorBarCalculator(colorScale);
internalFrame.setVisible(false);
setVisible(false);
for (int x = 0; x < gridx; x++) {
for (int y = 0; y < gridy; y++) {
pos[0] = border[0][0] + x * deltaX;
......@@ -147,6 +147,6 @@ public class TopoPlot extends Plot {
} // for y
} // for x
}
internalFrame.setVisible(true);
setVisible(true);
} // setTopology
} // class
package eva2.gui.wm;
import eva2.gui.JEFrameRegister;
import javax.swing.*;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
/**
* Created by halfdan on 16/12/15.
*/
public class InternalWindow extends JInternalFrame implements WindowInterface {
private boolean closeAllOnClose = false;
public InternalWindow() {
super();
init();
}
public InternalWindow(String name) {
super(name);
init();
}
/**
* Set to true if all registered JEFrames should be closed if this frame is closed.
*
* @param c
*/
public void setCloseAllOnClosed(boolean c) {
closeAllOnClose = c;
}
@Override
public void addInternalFrameListener(InternalFrameListener l) {
super.addInternalFrameListener(l);
}
private void init() {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.addInternalFrameListener(new InternalFrameAdapter() {
@Override
public void internalFrameClosed(InternalFrameEvent e) {
super.internalFrameClosed(e);
WindowManager.getInstance().removeWindow(e.getInternalFrame().getTitle());
if (closeAllOnClose) {
JEFrameRegister.getInstance().closeAll();
}
}
@Override
public void internalFrameOpened(InternalFrameEvent e) {
super.internalFrameOpened(e);
//JEFrameRegister.getInstance().register((JEFrame) e.getInternalFrame());
}
@Override
public void internalFrameActivated(InternalFrameEvent e) {
super.internalFrameActivated(e);
}
});
this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_MASK),
"ctrlFpressed"
);
this.getRootPane().getActionMap().put(
"ctrlFpressed",
new AbstractAction("ctrlFpressed") {
@Override
public void actionPerformed(ActionEvent actionEvent) {
//JEFrameRegister.getInstance().getFrameList().get(0).toFront();
}
}
);
this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK),
"ctrlOpressed"
);
this.getRootPane().getActionMap().put(
"ctrlOpressed",
new AbstractAction("ctrlOpressed") {
@Override
public void actionPerformed(ActionEvent actionEvent) {
// all to front?
}
}
);
this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_LESS, InputEvent.CTRL_MASK),
"ctrlSmallerpressed"
);
final InternalWindow self = this;
this.getRootPane().getActionMap().put(
"ctrlSmallerpressed",
new AbstractAction("ctrlSmallerpressed") {
@Override
public void actionPerformed(ActionEvent actionEvent) {
//JEFrameRegister.getInstance().setFocusToNext(self);
}
}
);
this.setMaximizable(true);
this.setResizable(true);
this.setIconifiable(false);
this.setClosable(true);
}
}
package eva2.gui.wm;
import javax.swing.*;
/**
* Created by halfdan on 16/12/15.
*/
public class Window extends JFrame implements WindowInterface {
@Override
public void setMaximizable(boolean flag) {
// Not exactly the same but good for now
setResizable(flag);
}
}
package eva2.gui.wm;
import java.awt.*;
/**
* Created by halfdan on 16/12/15.
*/
public interface WindowInterface {
void setVisible(boolean flag);
void setMaximizable(boolean flag);
void setTitle(String title);
void pack();
void add(Component component, Object constraints);
}
package eva2.gui;
package eva2.gui.wm;
import eva2.gui.JExtDesktopPane;
import eva2.tools.Pair;
import javax.swing.*;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;
......@@ -16,11 +20,22 @@ public class WindowManager {
/**
* List of all frames maintained.
*/
private Map<String, JComponent> windowList;
private Map<String, Pair<JComponent, WindowInterface>> windowList;
/**
* Two settings:
*
* - Single Window Mode: Panels are rendered in InternalWindows inside
* a JDesktopPane
* - Multi Window Mode: Panels are opened in new JFrames.
*/
private boolean isSingleWindowMode = true;
private JExtDesktopPane desktopPane;
private WindowManager() {
this.windowList = new HashMap<>();
this.desktopPane = new JExtDesktopPane();
}
public static WindowManager getInstance() {
......@@ -37,11 +52,52 @@ public class WindowManager {
* @param contentPane Main content panel
*/
public void addWindow(String title, JComponent contentPane) {
windowList.put(title, contentPane);
JFrame frame = new JFrame(title);
frame.add(contentPane);
frame.setVisible(true);
frame.pack();
if (windowList.containsKey(title)) {
windowList.get(title).getTail().setVisible(true);
} else {
WindowInterface frame;
if (isSingleWindowMode) {
frame = new InternalWindow();
desktopPane.add((InternalWindow) frame);
} else {
frame = new Window();
}
frame.setTitle(title);
frame.add(contentPane, BorderLayout.CENTER);
frame.setVisible(true);
frame.pack();
windowList.put(title, new Pair<>(contentPane, frame));
}
}
/**
* Remove window from registry
*
* @param title
*/
public void removeWindow(String title) {
windowList.remove(title);
}
/**
*
* @param title
*/
public void showWindow(String title) {
WindowInterface compo = windowList.get(title).getTail();
compo.pack();
compo.setVisible(true);
}
/**
*
*/
public JDesktopPane getDesktopPane() {
return this.desktopPane;
}
public void toggleMode() {
}
}
......@@ -7,6 +7,7 @@ import eva2.gui.plot.Graph;
import eva2.gui.plot.GraphWindow;
import eva2.gui.plot.Plot;
import eva2.gui.plot.PlotInterface;
import eva2.gui.wm.WindowManager;
import eva2.optimization.InterfaceOptimizationParameters;
import eva2.optimization.population.PopulationInterface;
import eva2.problems.InterfaceAdditionalPopulationInformer;
......@@ -32,7 +33,7 @@ public class StatisticsWithGUI extends AbstractStatistics implements Serializabl
private Graph[][] fitnessGraph;
private Graph[][] statGraph;
private String graphInfoString;
private JTextoutputFrameInterface proxyPrinter;
private JTextoutputFrame proxyPrinter;
/*
* List of descriptor strings and optional indices. strictly its redundant
* since super.lastGraphSelection is always available. However it spares
......@@ -45,8 +46,9 @@ public class StatisticsWithGUI extends AbstractStatistics implements Serializabl
*/
public StatisticsWithGUI() {
statisticsParameter = StatisticsParameters.getInstance(true);
proxyPrinter = new JTextoutputFrame("Optimization Log");
proxyPrinter = new JTextoutputFrame();
addTextListener(proxyPrinter);
WindowManager.getInstance().addWindow("Optimization Log", proxyPrinter);
}
/**
......@@ -106,8 +108,8 @@ public class StatisticsWithGUI extends AbstractStatistics implements Serializabl
}
public void maybeShowProxyPrinter() {
if (proxyPrinter != null) {
proxyPrinter.setShow(statisticsParameter.isShowTextOutput());
if (proxyPrinter != null && statisticsParameter.isShowTextOutput()) {
WindowManager.getInstance().showWindow("Optimization Log");
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment