Add a method for manually reloading a server

This commit is contained in:
Manuel Thalmann 2022-11-16 21:29:27 +01:00
parent 5e93294c38
commit ce65993bee

View file

@ -1,314 +1,329 @@
package ch.nuth.zhaw.exbox; package ch.nuth.zhaw.exbox;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.ItemEvent; import java.awt.event.ItemEvent;
import java.awt.event.ItemListener; import java.awt.event.ItemListener;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Set; import java.util.Set;
/** /**
* @(#)ExBoxFrame.java * @(#)ExBoxFrame.java
* *
* JFC ExBox application * JFC ExBox application
* *
* @author K.Rege * @author K.Rege
* @version 1.00 2014/2/3 * @version 1.00 2014/2/3
* @version 1.01 2016/8/2 * @version 1.01 2016/8/2
* @version 2.00 2017/8/30 Test * @version 2.00 2017/8/30 Test
* @version 2.01 2018/2/5 AutoscaleFaktor * @version 2.01 2018/2/5 AutoscaleFaktor
* @version 2.02 2018/3/12 Reconnect (inspired by S. Kunz) * @version 2.02 2018/3/12 Reconnect (inspired by S. Kunz)
* @version 2.03 2021/7/24 Test (repeat) * @version 2.03 2021/7/24 Test (repeat)
* @version 2.04 2021/9/11 Test as plugin * @version 2.04 2021/9/11 Test as plugin
*/ */
public class ExBoxFrame extends JFrame implements ActionListener, ItemListener { public class ExBoxFrame extends JFrame implements ActionListener, ItemListener {
private final Dimension UHDTHRESHOLD = new Dimension(2000, 1500); private final Dimension UHDTHRESHOLD = new Dimension(2000, 1500);
private final String STANDARDENCODING = "UTF-8"; private final String STANDARDENCODING = "UTF-8";
private JMenuItem connect, exit, open, test, retest, textView, graphicView, clear; private JMenuItem connect, reset, exit, open, test, retest, textView, graphicView, clear;
private JMenu menuServer; private JMenu menuServer;
private JButton enter; private JButton enter;
private JTextField arguments; private JTextField arguments;
private JComboBox<String> history; private JComboBox<String> history;
private JTextArea output; private JTextArea output;
private JScrollPane scrollPane; private JScrollPane scrollPane;
private CommandExecutor command; private CommandExecutor command;
private CommandExecutor unitTest; private CommandExecutor unitTest;
private boolean graphicOn; private boolean graphicOn;
private GraphicPanel graphic; private GraphicPanel graphic;
private String lastServer; private String lastServer;
private String lastTestFile; private String lastTestFile;
public void setFontSize(int size) { public void setFontSize(int size) {
Set<Object> keySet = UIManager.getLookAndFeelDefaults().keySet(); Set<Object> keySet = UIManager.getLookAndFeelDefaults().keySet();
for (Object key : keySet) { for (Object key : keySet) {
if (key != null && key.toString().toLowerCase().contains("font")) { if (key != null && key.toString().toLowerCase().contains("font")) {
Font font = UIManager.getDefaults().getFont(key); Font font = UIManager.getDefaults().getFont(key);
if (font != null) { if (font != null) {
font = font.deriveFont((float) size); font = font.deriveFont((float) size);
UIManager.put(key, font); UIManager.put(key, font);
} }
} }
} }
} }
private void initMenu() { private void initMenu() {
JMenuBar menuBar = new JMenuBar(); JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar); setJMenuBar(menuBar);
JMenu menuFile = new JMenu("File"); JMenu menuFile = new JMenu("File");
menuBar.add(menuFile); menuBar.add(menuFile);
open = new JMenuItem("Open..."); open = new JMenuItem("Open...");
open.addActionListener(this); open.addActionListener(this);
menuFile.add(open); menuFile.add(open);
exit = new JMenuItem(); exit = new JMenuItem();
exit.setText("Exit"); exit.setText("Exit");
exit.addActionListener(this); exit.addActionListener(this);
menuFile.add(exit); menuFile.add(exit);
menuServer = new JMenu("Server"); menuServer = new JMenu("Server");
menuBar.add(menuServer); menuBar.add(menuServer);
connect = new JMenuItem("Connect ..."); connect = new JMenuItem("Connect ...");
connect.addActionListener(this); connect.addActionListener(this);
menuServer.add(connect); menuServer.add(connect);
reset = new JMenuItem("Reset Server...");
JMenu menuView = new JMenu("View"); reset.addActionListener(this);
menuBar.add(menuView); menuServer.add(reset);
clear = new JMenuItem("Clear");
clear.addActionListener(this); JMenu menuView = new JMenu("View");
menuView.add(clear); menuBar.add(menuView);
textView = new JMenuItem("Text"); clear = new JMenuItem("Clear");
textView.addActionListener(this); clear.addActionListener(this);
menuView.add(textView); menuView.add(clear);
graphicView = new JMenuItem("Graphic"); textView = new JMenuItem("Text");
graphicView.addActionListener(this); textView.addActionListener(this);
menuView.add(graphicView); menuView.add(textView);
} graphicView = new JMenuItem("Graphic");
graphicView.addActionListener(this);
private void initJUnit() { menuView.add(graphicView);
try { }
unitTest = ServerFactory.createServer(getPathCompiled().resolve("ExBoxJUnit.class").toString());
test = new JMenuItem("Test ..."); private void initJUnit() {
test.addActionListener(this); try {
menuServer.add(test); unitTest = ServerFactory.createServer(getPathCompiled().resolve("ExBoxJUnit.class").toString());
retest = new JMenuItem("Test"); test = new JMenuItem("Test ...");
retest.addActionListener(this); test.addActionListener(this);
menuServer.add(retest); menuServer.add(test);
} catch (Exception e) { retest = new JMenuItem("Test");
warning("Test Plugin not found\n"); retest.addActionListener(this);
} menuServer.add(retest);
} } catch (Exception e) {
warning("Test Plugin not found\n");
private void initComponents() { }
setLayout(new BorderLayout()); }
output = new JTextArea();
scrollPane = new JScrollPane(output); private void initComponents() {
add(BorderLayout.CENTER, scrollPane); setLayout(new BorderLayout());
output = new JTextArea();
JPanel panel = new JPanel(new BorderLayout()); scrollPane = new JScrollPane(output);
panel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4)); add(BorderLayout.CENTER, scrollPane);
arguments = new JTextField();
arguments.addActionListener(this); JPanel panel = new JPanel(new BorderLayout());
panel.add(BorderLayout.CENTER, arguments); panel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
enter = new JButton("enter"); arguments = new JTextField();
enter.addActionListener(this); arguments.addActionListener(this);
panel.add(BorderLayout.EAST, enter); panel.add(BorderLayout.CENTER, arguments);
history = new JComboBox<>(); enter = new JButton("enter");
history.addItemListener(this); enter.addActionListener(this);
panel.add(BorderLayout.SOUTH, history); panel.add(BorderLayout.EAST, enter);
add(BorderLayout.SOUTH, panel); history = new JComboBox<>();
} history.addItemListener(this);
panel.add(BorderLayout.SOUTH, history);
/** add(BorderLayout.SOUTH, panel);
* get default path for file open dialog }
*/
private Path getPathCompiled() { /**
try { * get default path for file open dialog
Path path = Paths.get(getClass().getProtectionDomain().getCodeSource().getLocation().toURI()); */
private Path getPathCompiled() {
for (String part : getClass().getPackage().getName().split("\\.")) { try {
path = path.resolve(part); Path path = Paths.get(getClass().getProtectionDomain().getCodeSource().getLocation().toURI());
}
for (String part : getClass().getPackage().getName().split("\\.")) {
return path; path = path.resolve(part);
} catch (URISyntaxException e) { }
throw new RuntimeException(e);
} return path;
} } catch (URISyntaxException e) {
throw new RuntimeException(e);
/** }
* The constructor }
*/
public ExBoxFrame() { /**
try { * The constructor
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); */
} catch (Exception ex) { public ExBoxFrame() {
ex.printStackTrace(); try {
} UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); ex.printStackTrace();
}
double scale = (
screenSize.getWidth() > UHDTHRESHOLD.getWidth() && Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
screenSize.getHeight() > UHDTHRESHOLD.getHeight()) ? 2 : 1;
double scale = (
scale = Math.max(scale, Toolkit.getDefaultToolkit().getScreenResolution() / 96.0); screenSize.getWidth() > UHDTHRESHOLD.getWidth() &&
setFontSize((int) (11 * scale)); screenSize.getHeight() > UHDTHRESHOLD.getHeight()) ? 2 : 1;
setSize( scale = Math.max(scale, Toolkit.getDefaultToolkit().getScreenResolution() / 96.0);
new Dimension((int)(400 * scale), (int)(400 * scale))); setFontSize((int) (11 * scale));
setTitle("ExBox"); setSize(
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); new Dimension((int)(400 * scale), (int)(400 * scale)));
initComponents();
initMenu(); setTitle("ExBox");
initJUnit(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} initComponents();
initMenu();
private void warning(String s) { initJUnit();
System.err.println("\nWARNING: " + s + "\n"); }
}
private void warning(String s) {
private void error(String s) { System.err.println("\nWARNING: " + s + "\n");
output.append("\nERROR: " + s + "\n"); }
}
private void error(String s) {
private void execute(String args) throws Exception { output.append("\nERROR: " + s + "\n");
if (lastServer != null) { }
command = ServerFactory.createServer(lastServer);
} private void execute(String args) throws Exception {
if (lastServer != null) {
if (!arguments.getText().equals(history.getItemAt(0)) command = ServerFactory.createServer(lastServer);
&& !arguments.getText().equals(history.getSelectedItem())) { }
history.insertItemAt(arguments.getText(), 0);
} if (!arguments.getText().equals(history.getItemAt(0))
&& !arguments.getText().equals(history.getSelectedItem())) {
if (command == null) { history.insertItemAt(arguments.getText(), 0);
error("no Server connected"); }
} else {
String res = command.execute(args); if (command == null) {
error("no Server connected");
if (graphicOn) { } else {
graphic.setFigure(res); String res = command.execute(args);
} else {
output.append(res); if (graphicOn) {
} graphic.setFigure(res);
} } else {
} output.append(res);
}
private void setGraphicView() { }
if (!graphicOn) { }
remove(scrollPane);
graphic = new GraphicPanel(); private void setGraphicView() {
output.removeNotify(); if (!graphicOn) {
add(BorderLayout.CENTER, graphic); remove(scrollPane);
graphicOn = true; graphic = new GraphicPanel();
validate(); output.removeNotify();
repaint(); add(BorderLayout.CENTER, graphic);
} graphicOn = true;
} validate();
repaint();
private void setTextView() { }
if (graphicOn) { }
remove(graphic);
add(BorderLayout.CENTER, scrollPane); private void setTextView() {
graphicOn = false; if (graphicOn) {
validate(); remove(graphic);
repaint(); add(BorderLayout.CENTER, scrollPane);
} graphicOn = false;
} validate();
repaint();
private String openFileDialog(Path startDirectory, String pattern) { }
FileDialog fd = new FileDialog(this, "Open"); }
if (pattern != null)
fd.setFile(pattern); private String openFileDialog(Path startDirectory, String pattern) {
if (startDirectory != null) FileDialog fd = new FileDialog(this, "Open");
fd.setDirectory(startDirectory.toString()); if (pattern != null)
fd.setVisible(true); fd.setFile(pattern);
return fd.getDirectory() + fd.getFile(); if (startDirectory != null)
} fd.setDirectory(startDirectory.toString());
fd.setVisible(true);
private void testCommand(boolean retest) throws Exception { return fd.getDirectory() + fd.getFile();
if (!retest) { }
lastTestFile = openFileDialog(getPathCompiled(), "*test.class");
} private void testCommand(boolean retest) throws Exception {
if (!retest) {
if (lastTestFile == null) { lastTestFile = openFileDialog(getPathCompiled(), "*test.class");
output.append("ERROR no Test spezified\n"); }
} else if (unitTest != null) {
output.append(unitTest.execute(lastTestFile)); if (lastTestFile == null) {
} output.append("ERROR no Test spezified\n");
} } else if (unitTest != null) {
output.append(unitTest.execute(lastTestFile));
private void connectCommand() throws Exception { }
String name = openFileDialog(getPathCompiled(), "*Server.class"); }
command = ServerFactory.createServer(name);
lastServer = name; private void connectCommand() throws Exception {
String fullClassName = command.getClass().getName(); String name = openFileDialog(getPathCompiled(), "*Server.class");
String simpleClassName = fullClassName.substring(fullClassName.lastIndexOf('.') + 1); loadServer(name);
setTitle("ExBox connected to " + simpleClassName); lastServer = name;
String fullClassName = command.getClass().getName();
} String simpleClassName = fullClassName.substring(fullClassName.lastIndexOf('.') + 1);
setTitle("ExBox connected to " + simpleClassName);
private void openFile() throws Exception {
String name = openFileDialog(null, null); }
try (BufferedReader br = new BufferedReader( private void resetCommand() throws Exception {
new InputStreamReader(new FileInputStream(name), STANDARDENCODING))) { if (lastServer != null) {
StringBuilder b = new StringBuilder(); loadServer(lastServer);
String line; }
}
while ((line = br.readLine()) != null) {
b.append(line); private void loadServer(String name) throws Exception {
b.append('\n'); command = ServerFactory.createServer(name);
} }
execute(b.toString()); private void openFile() throws Exception {
} String name = openFileDialog(null, null);
}
try (BufferedReader br = new BufferedReader(
public void itemStateChanged(ItemEvent e) { new InputStreamReader(new FileInputStream(name), STANDARDENCODING))) {
try { StringBuilder b = new StringBuilder();
arguments.setText((String) e.getItem()); String line;
execute(arguments.getText());
} catch (Throwable ex) { while ((line = br.readLine()) != null) {
error(ex.toString()); b.append(line);
} b.append('\n');
} }
public void actionPerformed(ActionEvent e) { execute(b.toString());
try { }
if ((e.getSource() == arguments) || (e.getSource() == enter)) { }
execute(arguments.getText());
} else if (e.getSource() == connect) { public void itemStateChanged(ItemEvent e) {
connectCommand(); try {
} else if (e.getSource() == test) { arguments.setText((String) e.getItem());
testCommand(false); execute(arguments.getText());
} else if (e.getSource() == retest) { } catch (Throwable ex) {
testCommand(true); error(ex.toString());
} else if (e.getSource() == open) { }
openFile(); }
} else if (e.getSource() == textView) {
setTextView(); public void actionPerformed(ActionEvent e) {
} else if (e.getSource() == graphicView) { try {
setGraphicView(); if ((e.getSource() == arguments) || (e.getSource() == enter)) {
} else if (e.getSource() == clear) { execute(arguments.getText());
output.setText(""); } else if (e.getSource() == connect) {
} else if (e.getSource() == exit) { connectCommand();
System.exit(0); } else if (e.getSource() == reset) {
} resetCommand();
} catch (Throwable ex) { } else if (e.getSource() == test) {
ex.printStackTrace(); testCommand(false);
error(ex.toString()); } else if (e.getSource() == retest) {
} testCommand(true);
} } else if (e.getSource() == open) {
} openFile();
} else if (e.getSource() == textView) {
setTextView();
} else if (e.getSource() == graphicView) {
setGraphicView();
} else if (e.getSource() == clear) {
output.setText("");
} else if (e.getSource() == exit) {
System.exit(0);
}
} catch (Throwable ex) {
ex.printStackTrace();
error(ex.toString());
}
}
}