Fix scaling for linux devices

This commit is contained in:
Manuel Thalmann 2022-11-15 16:30:05 +01:00
parent ff0c3f0b98
commit 25068543ad

View file

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