Compare commits

...

3 commits

2 changed files with 373 additions and 329 deletions

View file

@ -1,314 +1,325 @@
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 (!arguments.getText().equals(history.getItemAt(0))
if (!arguments.getText().equals(history.getItemAt(0)) && !arguments.getText().equals(history.getSelectedItem())) {
&& !arguments.getText().equals(history.getSelectedItem())) { history.insertItemAt(arguments.getText(), 0);
history.insertItemAt(arguments.getText(), 0); }
}
if (command == null) {
if (command == null) { error("no Server connected");
error("no Server connected"); } else {
} else { String res = command.execute(args);
String res = command.execute(args);
if (graphicOn) {
if (graphicOn) { graphic.setFigure(res);
graphic.setFigure(res); } else {
} else { output.append(res);
output.append(res); }
} }
} }
}
private void setGraphicView() {
private void setGraphicView() { if (!graphicOn) {
if (!graphicOn) { remove(scrollPane);
remove(scrollPane); graphic = new GraphicPanel();
graphic = new GraphicPanel(); output.removeNotify();
output.removeNotify(); add(BorderLayout.CENTER, graphic);
add(BorderLayout.CENTER, graphic); graphicOn = true;
graphicOn = true; validate();
validate(); repaint();
repaint(); }
} }
}
private void setTextView() {
private void setTextView() { if (graphicOn) {
if (graphicOn) { remove(graphic);
remove(graphic); add(BorderLayout.CENTER, scrollPane);
add(BorderLayout.CENTER, scrollPane); graphicOn = false;
graphicOn = false; validate();
validate(); repaint();
repaint(); }
} }
}
private String openFileDialog(Path startDirectory, String pattern) {
private String openFileDialog(Path startDirectory, String pattern) { FileDialog fd = new FileDialog(this, "Open");
FileDialog fd = new FileDialog(this, "Open"); if (pattern != null)
if (pattern != null) fd.setFile(pattern);
fd.setFile(pattern); if (startDirectory != null)
if (startDirectory != null) fd.setDirectory(startDirectory.toString());
fd.setDirectory(startDirectory.toString()); fd.setVisible(true);
fd.setVisible(true); return fd.getDirectory() + fd.getFile();
return fd.getDirectory() + fd.getFile(); }
}
private void testCommand(boolean retest) throws Exception {
private void testCommand(boolean retest) throws Exception { if (!retest) {
if (!retest) { lastTestFile = openFileDialog(getPathCompiled(), "*test.class");
lastTestFile = openFileDialog(getPathCompiled(), "*test.class"); }
}
if (lastTestFile == null) {
if (lastTestFile == null) { output.append("ERROR no Test spezified\n");
output.append("ERROR no Test spezified\n"); } else if (unitTest != null) {
} else if (unitTest != null) { output.append(unitTest.execute(lastTestFile));
output.append(unitTest.execute(lastTestFile)); }
} }
}
private void connectCommand() throws Exception {
private void connectCommand() throws Exception { String name = openFileDialog(getPathCompiled(), "*Server.class");
String name = openFileDialog(getPathCompiled(), "*Server.class"); loadServer(name);
command = ServerFactory.createServer(name); lastServer = name;
lastServer = name; String fullClassName = command.getClass().getName();
String fullClassName = command.getClass().getName(); String simpleClassName = fullClassName.substring(fullClassName.lastIndexOf('.') + 1);
String simpleClassName = fullClassName.substring(fullClassName.lastIndexOf('.') + 1); setTitle("ExBox connected to " + simpleClassName);
setTitle("ExBox connected to " + simpleClassName);
}
}
private void resetCommand() throws Exception {
private void openFile() throws Exception { if (lastServer != null) {
String name = openFileDialog(null, null); loadServer(lastServer);
}
try (BufferedReader br = new BufferedReader( }
new InputStreamReader(new FileInputStream(name), STANDARDENCODING))) {
StringBuilder b = new StringBuilder(); private void loadServer(String name) throws Exception {
String line; command = ServerFactory.createServer(name);
}
while ((line = br.readLine()) != null) {
b.append(line); private void openFile() throws Exception {
b.append('\n'); String name = openFileDialog(null, null);
}
try (BufferedReader br = new BufferedReader(
execute(b.toString()); new InputStreamReader(new FileInputStream(name), STANDARDENCODING))) {
} StringBuilder b = new StringBuilder();
} String line;
public void itemStateChanged(ItemEvent e) { while ((line = br.readLine()) != null) {
try { b.append(line);
arguments.setText((String) e.getItem()); b.append('\n');
execute(arguments.getText()); }
} catch (Throwable ex) {
error(ex.toString()); execute(b.toString());
} }
} }
public void actionPerformed(ActionEvent e) { public void itemStateChanged(ItemEvent e) {
try { try {
if ((e.getSource() == arguments) || (e.getSource() == enter)) { arguments.setText((String) e.getItem());
execute(arguments.getText()); execute(arguments.getText());
} else if (e.getSource() == connect) { } catch (Throwable ex) {
connectCommand(); error(ex.toString());
} else if (e.getSource() == test) { }
testCommand(false); }
} else if (e.getSource() == retest) {
testCommand(true); public void actionPerformed(ActionEvent e) {
} else if (e.getSource() == open) { try {
openFile(); if ((e.getSource() == arguments) || (e.getSource() == enter)) {
} else if (e.getSource() == textView) { execute(arguments.getText());
setTextView(); } else if (e.getSource() == connect) {
} else if (e.getSource() == graphicView) { connectCommand();
setGraphicView(); } else if (e.getSource() == reset) {
} else if (e.getSource() == clear) { resetCommand();
output.setText(""); } else if (e.getSource() == test) {
} else if (e.getSource() == exit) { testCommand(false);
System.exit(0); } else if (e.getSource() == retest) {
} testCommand(true);
} catch (Throwable ex) { } else if (e.getSource() == open) {
ex.printStackTrace(); openFile();
error(ex.toString()); } 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());
}
}
}

View file

@ -6,37 +6,69 @@ import java.util.Set;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public class MyHashtable<K, V> implements Map<K, V> { public class MyHashtable<K, V> implements Map<K, V> {
private int capacity;
private int size;
private K[] keys = (K[]) new Object[10]; private K[] keys = (K[]) new Object[10];
private V[] values = (V[]) new Object[10]; private V[] values = (V[]) new Object[10];
public MyHashtable(int size) {
capacity = size;
clear();
}
private boolean hashEquals(Object x, Object y) {
if (x == null || y == null) {
return x == y;
} else {
return x.hashCode() == y.hashCode();
}
}
private int hash(Object k) { private int hash(Object k) {
int h = Math.abs(k.hashCode()); int h = Math.abs(k.hashCode());
return h % keys.length; return h % keys.length;
} }
public MyHashtable(int size) {
// to be done
}
// Removes all mappings from this map (optional operation). // Removes all mappings from this map (optional operation).
public void clear() { public void clear() {
// to be done keys = (K[]) new Object[capacity];
throw new UnsupportedOperationException(); values = (V[]) new Object[capacity];
} }
// Associates the specified value with the specified key in this map (optional operation). // Associates the specified value with the specified key in this map (optional
// operation).
public V put(K key, V value) { public V put(K key, V value) {
// to be done int hashCode = hash(key);
throw new UnsupportedOperationException(); int index = hashCode;
for (int i = 0; keys[index] != null && !hashEquals(keys[index], key); i++) {
index = hash(hashCode + (int) Math.pow(i, 2));
}
if (keys[index] == null) {
size++;
}
keys[index] = key;
values[index] = value;
return value;
} }
// Returns the value to which this map maps the specified key. // Returns the value to which this map maps the specified key.
public V get(Object key) { public V get(Object key) {
// to be done // to be done
throw new UnsupportedOperationException(); for (int i = 0; i < keys.length; i++) {
if (hashEquals(key, keys[i])) {
return values[i];
}
}
return null;
} }
// Removes the mapping for this key from this map if present (optional operation). // Removes the mapping for this key from this map if present (optional
// operation).
public V remove(Object key) { public V remove(Object key) {
// to be done (Aufgabe 3) // to be done (Aufgabe 3)
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
@ -44,11 +76,11 @@ public class MyHashtable<K, V> implements Map<K, V> {
// Returns the number of key-value mappings in this map. // Returns the number of key-value mappings in this map.
public int size() { public int size() {
// to be done return size;
throw new UnsupportedOperationException();
} }
// UnsupportedOperationException =================================================================== // UnsupportedOperationException
// ===================================================================
// Returns a collection view of the values contained in this map. // Returns a collection view of the values contained in this map.
public Collection<V> values() { public Collection<V> values() {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
@ -74,7 +106,8 @@ public class MyHashtable<K, V> implements Map<K, V> {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
// Copies all of the mappings from the specified map to this map (optional operation). // Copies all of the mappings from the specified map to this map (optional
// operation).
public void putAll(Map<? extends K, ? extends V> t) { public void putAll(Map<? extends K, ? extends V> t) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }