diff options
| author | Paul Buetow <paul@buetow.org> | 2025-06-06 08:02:52 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-06-06 08:02:52 +0300 |
| commit | 1d99762c7965d351510cfb5e08eac25e48d96038 (patch) | |
| tree | f469493e911878ab9055ccf0494211bf9015922d /src/main/java/utils | |
| parent | 4d35597bd92607c4d194686e20b125044506c79a (diff) | |
Modernize project structure, update Maven config, move sources, add logging config, update README and .gitignore
Diffstat (limited to 'src/main/java/utils')
| -rw-r--r-- | src/main/java/utils/VS3Tupel.java | 58 | ||||
| -rw-r--r-- | src/main/java/utils/VSAboutFrame.java | 87 | ||||
| -rw-r--r-- | src/main/java/utils/VSClassLoader.java | 29 | ||||
| -rw-r--r-- | src/main/java/utils/VSFrame.java | 118 | ||||
| -rw-r--r-- | src/main/java/utils/VSInfoArea.java | 49 | ||||
| -rw-r--r-- | src/main/java/utils/VSPriorityQueue.java | 32 | ||||
| -rw-r--r-- | src/main/java/utils/VSRandom.java | 37 | ||||
| -rw-r--r-- | src/main/java/utils/VSTools.java | 92 |
8 files changed, 502 insertions, 0 deletions
diff --git a/src/main/java/utils/VS3Tupel.java b/src/main/java/utils/VS3Tupel.java new file mode 100644 index 0000000..ac7ffba --- /dev/null +++ b/src/main/java/utils/VS3Tupel.java @@ -0,0 +1,58 @@ +package utils; + +/** + * The class VS3Tupel, an object of this class represents a 3-Tupel of objects. + * Each object can have its own type. + * + * @author Paul C. Buetow + */ +public final class VS3Tupel<A,B,C> { + /** The a. */ + private A a; + + /** The b. */ + private B b; + + /** The c. */ + private C c; + + /** + * Instantiates a new tupel. + * + * @param a the a + * @param b the b + * @param c the c + */ + public VS3Tupel(A a, B b, C c) { + this.a = a; + this.b = b; + this.c = c; + } + + /** + * Gets the a. + * + * @return the a + */ + public A getA() { + return a; + } + + /** + * Gets the b. + * + * @return the b + */ + public B getB() { + return b; + } + + /** + * Gets the c. + * + * @return the c + */ + public C getC() { + return c; + } +} diff --git a/src/main/java/utils/VSAboutFrame.java b/src/main/java/utils/VSAboutFrame.java new file mode 100644 index 0000000..e07289f --- /dev/null +++ b/src/main/java/utils/VSAboutFrame.java @@ -0,0 +1,87 @@ +package utils; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Component; +import java.awt.Container; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JButton; +import javax.swing.JPanel; +import javax.swing.JScrollPane; + +//import utils.*; +import prefs.VSPrefs; + +/** + * The class VSAboutFrame. This class is only for the about window which + * shows up if selected in the GUI. + * + * @author Paul C. Buetow + */ +public class VSAboutFrame extends VSFrame { + /** The prefs. */ + private VSPrefs prefs; + + /** + * Instantiates a new VSAboutFrame object. + * + * @param prefs the prefs + * @param relativeTo the component to open the about window relative to + */ + public VSAboutFrame(VSPrefs prefs, Component relativeTo) { + super(prefs.getString("lang.name") + " - " + + prefs.getString("lang.about"), relativeTo); + this.prefs = prefs; + + disposeWithParent(); + setContentPane(createContentPane()); + setSize(350, 250); + setResizable(false); + setVisible(true); + } + + /** + * Creates the content pane. + * + * @return the container + */ + public Container createContentPane() { + Container contentPane = getContentPane(); + + VSInfoArea infoArea = new VSInfoArea( + prefs.getString("lang.about.info")); + JPanel buttonPane = createButtonPanel(); + JScrollPane scrollPane = new JScrollPane(infoArea); + + contentPane.add(scrollPane, BorderLayout.CENTER); + contentPane.add(buttonPane, BorderLayout.SOUTH); + + return contentPane; + } + + /** + * Creates the button panel. + * + * @return the panel + */ + public JPanel createButtonPanel() { + JPanel buttonPane = new JPanel(); + buttonPane.setBackground(Color.WHITE); + + JButton closeButton = new JButton( + prefs.getString("lang.close")); + closeButton.setMnemonic(prefs.getInteger("keyevent.close")); + closeButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + String actionCommand = e.getActionCommand(); + if (actionCommand.equals(prefs.getString("lang.close"))) + dispose(); + } + }); + buttonPane.add(closeButton); + + return buttonPane; + } +} diff --git a/src/main/java/utils/VSClassLoader.java b/src/main/java/utils/VSClassLoader.java new file mode 100644 index 0000000..37f4027 --- /dev/null +++ b/src/main/java/utils/VSClassLoader.java @@ -0,0 +1,29 @@ +package utils; + +/** + * The class VSClassLoader. This class is used in order to create new objects + * by its classnames. + * + * @author Paul C. Buetow + */ +public class VSClassLoader extends ClassLoader { + /** + * Creates a new instance of the given classname. + * + * @param classname the classname + * + * @return the object + */ + public Object newInstance(String classname) { + Object object = null; + + try { + object = super.loadClass(classname, true).getDeclaredConstructor().newInstance(); + + } catch (Exception e) { + System.out.println(e + "; Classname " + classname); + } + + return object; + } +} diff --git a/src/main/java/utils/VSFrame.java b/src/main/java/utils/VSFrame.java new file mode 100644 index 0000000..818a72d --- /dev/null +++ b/src/main/java/utils/VSFrame.java @@ -0,0 +1,118 @@ +package utils; + +import java.awt.Component; +import java.awt.Dimension; +import java.awt.Point; +import java.awt.Toolkit; +import java.awt.Window; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; + +import javax.swing.JFrame; + +/** + * The class VSFrame. All frames of the simulator extend this VSFrame class. + * This class makes sure that all 'subwindows' get closed if its parent gets + * closed. And it also makes sure to open new windows relative to its parent. + * + * @author Paul C. Buetow + */ +public class VSFrame extends JFrame { + /** The serial version uid */ + private static final long serialVersionUID = 1L; + + /** The Constant X_LOCATION_OFFSET. */ + private final static int X_LOCATION_OFFSET = 40; + + /** The Constant Y_LOCATION_OFFSET. */ + private final static int Y_LOCATION_OFFSET = 80; + + /** The parent window/component. */ + private Component parent; + + /** True, if the current window will get disposed with its parent. */ + private boolean dispose; + + /** + * Instantiates a VSFrame object. + * + * @param title the title + * @param parent the parent + */ + public VSFrame(String title, Component parent) { + super(title); + init(parent); + } + + /** + * Instantiates a new VSFrame object. + * + * @param title the title + */ + public VSFrame(String title) { + super(title); + init(null); + } + + /** + * Inits the VSFrame. + * + * @param parent the parent + */ + private void init(Component parent) { + this.parent = parent; + this.dispose = false; + } + + /** + * Dispose with its parent. + */ + public void disposeWithParent() { + if (!dispose && parent != null && parent instanceof Window) { + Window window = (Window) parent; + window.addWindowListener(new WindowAdapter() { + public void windowClosed(WindowEvent we) { + VSFrame.this.dispose(); + } + }); + } + dispose = true; + } + + /** + * Sets the correct location of the window. + */ + private void setCorrectLocation() { + int x = 0, y = 0; + final Dimension screenSize = + Toolkit.getDefaultToolkit().getScreenSize(); + + if (parent == null) { + x = (int) (screenSize.width - getWidth()) / 2; + y = 50;//(int) (screenSize.height - getHeight()) / 2; + + } else { + final Point location = parent.getLocation(); + x = (int) location.getX() + X_LOCATION_OFFSET; + y = (int) location.getY() + Y_LOCATION_OFFSET; + } + + if (x + super.getWidth() >= screenSize.width) + x = screenSize.width - super.getWidth(); + else if (x < 0) + x = 0; + + if (y + super.getHeight() >= screenSize.height) + y = screenSize.height - super.getHeight(); + + super.setLocation(x, y); + } + + /* (non-Javadoc) + * @see java.awt.Window#setSize(int, int) + */ + public void setSize(int width, int height) { + super.setSize(width, height); + setCorrectLocation(); + } +} diff --git a/src/main/java/utils/VSInfoArea.java b/src/main/java/utils/VSInfoArea.java new file mode 100644 index 0000000..645afb1 --- /dev/null +++ b/src/main/java/utils/VSInfoArea.java @@ -0,0 +1,49 @@ +package utils; + +import java.awt.Color; + +import javax.swing.JTextPane; +import javax.swing.border.CompoundBorder; +import javax.swing.border.EmptyBorder; +import javax.swing.border.LineBorder; + +/** + * The class VSInfoArea, an object of this class is used for some information + * areas. E.g. in the VSAboutFrame class. + * + * @author Paul C. Buetow + */ +public class VSInfoArea extends JTextPane { + /** The serial version uid */ + private static final long serialVersionUID = 1L; + + /** + * Instantiates a new VSInfoArea. + */ + public VSInfoArea() { + init(); + } + + /** + * Instantiates a new VSInfoArea. + * + * @param text the text to display + */ + public VSInfoArea(String text) { + setText(text); + init(); + } + + /** + * Inits the info area. + */ + private void init() { + setOpaque(false); + setBorder(null); + setFocusable(false); + setBorder(new CompoundBorder( + new LineBorder(Color.BLACK), + new EmptyBorder(15, 15, 15, 15))); + setBackground(Color.WHITE); + } +} diff --git a/src/main/java/utils/VSPriorityQueue.java b/src/main/java/utils/VSPriorityQueue.java new file mode 100644 index 0000000..d706352 --- /dev/null +++ b/src/main/java/utils/VSPriorityQueue.java @@ -0,0 +1,32 @@ +package utils; + +import java.util.PriorityQueue; + +/** + * The class VSPriorityQueue. This class is the same like the standard + * VSPriorityQueue of the Java API. It only overrides the get(int) method. + * + * @author Paul C. Buetow + */ +public final class VSPriorityQueue<T> extends PriorityQueue<T> { + /** The serial version uid */ + private static final long serialVersionUID = 1L; + + /** + * Gets the specific element. If the index is out of bounds, it will return + * null. + * + * @param index the index + * + * @return the element, or null, if out of bounds + */ + public T get(int index) { + int i = 0; + + for (T t : this) + if (i++ == index) + return t; + + return null; + } +} diff --git a/src/main/java/utils/VSRandom.java b/src/main/java/utils/VSRandom.java new file mode 100644 index 0000000..6c1de90 --- /dev/null +++ b/src/main/java/utils/VSRandom.java @@ -0,0 +1,37 @@ +package utils; + +import java.util.Random; + +/** + * The class VSRandom. Some customization of the standard Random class of Java. + * + * @author Paul C. Buetow + */ +public final class VSRandom extends Random { + /** + * Instantiates a new VSrandom object. + * + * @param seedAdd the seed to add. + */ + public VSRandom(long seedAdd) { + super(seedAdd*System.currentTimeMillis()+seedAdd); + } + + /* (non-Javadoc) + * @see java.util.Random#nextInt() + */ + public int nextInt() { + return Math.abs(super.nextInt()); + } + + /** + * Next long. + * + * @param mod the mod + * + * @return the random long + */ + public long nextLong(long mod) { + return Math.abs((super.nextLong() + System.currentTimeMillis()) % mod); + } +} diff --git a/src/main/java/utils/VSTools.java b/src/main/java/utils/VSTools.java new file mode 100644 index 0000000..ceb5760 --- /dev/null +++ b/src/main/java/utils/VSTools.java @@ -0,0 +1,92 @@ +package utils; + +import java.util.Vector; + +/** + * The class VSTools. This class contains only static methods. Those methods + * are for general usage and don't fit into other classes. + * + * @author Paul C. Buetow + */ +public final class VSTools { + /** + * Gets the time string. + * + * @param time the time + * + * @return the time string + */ + public static String getTimeString(long time) { + String ret = ""+time; + + while (ret.length() < 6) + ret = "0" + ret; + + return ret + "ms"; + } + + /** + * Gets the string time. + * + * @param string the string + * + * @return the string time + */ + public static long getStringTime(String string) { + try { + /* Ignore the "ms" postfix */ + Long longValue = Long.valueOf( + string.substring(0, string.length()-2)); + return longValue.longValue(); + } catch (NumberFormatException e) { + } + + return 0; + } + + /** + * Gets the integer vector represented by a comma separated string. + * + * @param string the string + * + * @return the parsed vector + */ + public static Vector<Integer> parseIntegerVector(String string) + throws exceptions.VSParseIntegerVectorException { + Vector<Integer> vec = new Vector<Integer>(); + + int index = string.indexOf('['); + if (index == -1) + throw new exceptions.VSParseIntegerVectorException(); + + string = string.substring(index+1); + + index = string.indexOf(']'); + if (index == -1) + throw new exceptions.VSParseIntegerVectorException(); + + string = string.substring(0, index); + + try { + while ( (index = string.indexOf(',')) != -1 ) { + String substring = string.substring(0, index); + + /* Remove leading whitespaces */ + while (substring.charAt(0) == ' ') + substring = substring.substring(1); + + vec.add(Integer.parseInt(substring)); + string = string.substring(index+1); + } + + /* Remove leading whitespaces */ + while (string.charAt(0) == ' ') + string = string.substring(1); + vec.add(Integer.parseInt(string)); + + } catch (StringIndexOutOfBoundsException e) { + } catch (NumberFormatException e) { + } + return vec; + } +} |
