Skip to content

Commit fccd969

Browse files
author
Paulsen
committed
v1.1.4
Added: - PInstance - PInstance to Demo Changed: - renamed PUIScrollPanel into PUIList - showFrame-parameter to PUIFrame-Constructors - Updated README Fixed: - SecurityException when interrupting Serial
1 parent 8d97971 commit fccd969

File tree

6 files changed

+119
-23
lines changed

6 files changed

+119
-23
lines changed

README.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,23 @@ The Library [jSerialComm](https://github.com/Fazecast/jSerialComm) is used for U
8787

8888
## Project status
8989

90-
The Library is ***fully functional*** and ![Maintenance](https://img.shields.io/maintenance/yes/2021)
90+
The Library is ***fully functional*** and ![Maintenance](https://img.shields.io/maintenance/yes/2022)
9191
by [`Paulsen`](https://github.com/realPaulsen)
9292

93-
### Planned Improvements / In Development
93+
### TODOs & In-Progress
94+
95+
- **BugFixes**
96+
- ...
97+
- **Changes**
98+
- Make `run()` of **PConsole** _semi-blocking_ instead of _full-blocking_. In case the Console/Terminal wants an input the run() would be blocked forever
99+
- Make `PUIElement` **abstract** and create `PUIButton` to replace the parent-class of the other Elements
100+
- **FixPoints** on PUISlider
101+
- **Free** movable PUIElements on PUIScrollPanel (without snapped locations)
102+
- **Additions**
103+
- **Graph plotter**: Plot multiple graphs and analyze them
104+
- **Built-In **File-Browser** based on PUIList
105+
- **Comments**/JavaDoc still missing😬
94106

95-
- **BugFixes:**
96-
- Make `run()` of **PConsole** _semi-blocking_ instead of _full-blocking_. In case the Console/Terminal wants an input the run() would be blocked forever
97-
- Make `PUIElement` **abstract** and create `PUIButton` to replace the parent-class of the other Elements
98-
- **FixPoints** on PUISlider
99-
- **Free** movable PUIElements on PUIScrollPanel (without snapped locations)
100-
- **Graph plotter**: Plot multiple graphs and analyze them
101-
- **Comments**/JavaDoc still missing😬
102107

103108

104109
## Build-Notes

src/ooo/paulsen/demo/Demo.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
import ooo.paulsen.ui.*;
66
import ooo.paulsen.ui.PUIElement.ElementAlignment;
77
import ooo.paulsen.ui.core.*;
8+
import ooo.paulsen.utils.PInstance;
89
import ooo.paulsen.utils.PSystem;
910

11+
import javax.swing.*;
1012
import java.awt.*;
1113
import java.awt.event.KeyEvent;
1214
import java.awt.event.KeyListener;
15+
import java.io.IOException;
16+
import java.net.BindException;
1317

1418
public class Demo {
1519

@@ -22,7 +26,7 @@ public static void main(String[] args) {
2226
PUICanvas canvas;
2327
PUIMatrix matrix;
2428
PUIText darkModeButton;
25-
PUIScrollPanel sp;
29+
PUIList sp;
2630
PUICheckBox cb;
2731
PUIRotaryControl rc;
2832
PUISlider slider;
@@ -34,6 +38,16 @@ public static void main(String[] args) {
3438

3539
public Demo() {
3640

41+
try {
42+
PInstance p = new PInstance(8123);
43+
} catch (BindException e) {
44+
System.out.println("Already runs");
45+
JOptionPane.showMessageDialog(null,"Port 6434 already taken by another Process","Instance already running",JOptionPane.INFORMATION_MESSAGE);
46+
System.exit(0);
47+
} catch (IOException e) {
48+
throw new RuntimeException(e);
49+
}
50+
3751
frameTitle = "JPL-Demo - " + PSystem.getUserName() + "'s " + PSystem.getOSType() + "-System from " + PSystem.getUserDisplayLocation();
3852

3953
// initialize frame before creating Elements
@@ -101,7 +115,7 @@ public void run(PUIElement that) {
101115
// if set to false: when pressed the eventchain doesnt stop => elements on layers behind this Button can be triggered as well
102116
darkModeButton.doBlockRaycast(false);
103117

104-
sp = new PUIScrollPanel(f);
118+
sp = new PUIList(f);
105119

106120
cb = new PUICheckBox(f);
107121
cb.addActionListener(new PUIAction() {

src/ooo/paulsen/io/serial/PSerialConnection.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,11 @@ public boolean connect() {
146146
public boolean disconnect() {
147147
if (isConnected) {
148148
port.closePort();
149-
listenerThread.interrupt();
149+
try {
150+
listenerThread.interrupt();
151+
} catch (SecurityException e) {
152+
System.err.println("[PSerialConnection] :: Couldn't interrupt thread");
153+
}
150154
listenerThread = null;
151155
isConnected = false;
152156
return true;
@@ -159,7 +163,7 @@ public boolean isConnected() {
159163
return isConnected;
160164
}
161165

162-
public void setDisconnectEvent(Runnable r){
166+
public void setDisconnectEvent(Runnable r) {
163167
disconnectEvent = r;
164168
}
165169

src/ooo/paulsen/ui/PUIScrollPanel.java renamed to src/ooo/paulsen/ui/PUIList.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import java.util.ConcurrentModificationException;
1313
import java.util.concurrent.CopyOnWriteArrayList;
1414

15-
public class PUIScrollPanel extends PUIElement {
15+
public class PUIList extends PUIElement {
1616

1717
private CopyOnWriteArrayList<PUIAction> valueUpdateAction = new CopyOnWriteArrayList<>();
1818

@@ -28,13 +28,13 @@ public class PUIScrollPanel extends PUIElement {
2828

2929
private PUISlider slider;
3030

31-
public PUIScrollPanel(PUIFrame f) {
31+
public PUIList(PUIFrame f) {
3232
super(f);
3333

3434
init();
3535
}
3636

37-
public PUIScrollPanel(PUIFrame f, int layer) {
37+
public PUIList(PUIFrame f, int layer) {
3838
super(f);
3939

4040
init();
@@ -65,7 +65,7 @@ public void mouseWheelMoved(MouseWheelEvent e) {
6565

6666
// Check if elements in list are hovered over
6767
for (PUIElement elem : elements)
68-
for (MouseMotionListener ml : elem.mouseMotionListeners)
68+
for (MouseMotionListener ml : elem.getMouseMotionListeners())
6969
ml.mouseMoved(e);
7070
}
7171
}

src/ooo/paulsen/ui/core/PUIFrame.java

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,57 @@ public class PUIFrame extends JFrame {
3737

3838
public PUIFrame() {
3939
super();
40-
constructorInit();
40+
constructorInit(true);
41+
}
42+
43+
public PUIFrame(boolean showFrame) {
44+
super();
45+
constructorInit(showFrame);
4146
}
4247

4348
public PUIFrame(String displayName) {
4449
super();
4550
this.displayName = displayName;
46-
constructorInit();
51+
constructorInit(true);
52+
}
53+
54+
public PUIFrame(String displayName, boolean showFrame) {
55+
super();
56+
this.displayName = displayName;
57+
constructorInit(showFrame);
4758
}
4859

4960
public PUIFrame(int width, int height) {
5061
super();
5162
this.w = width;
5263
this.h = height;
53-
constructorInit();
64+
constructorInit(true);
65+
}
66+
67+
public PUIFrame(int width, int height, boolean showFrame) {
68+
super();
69+
this.w = width;
70+
this.h = height;
71+
constructorInit(showFrame);
5472
}
5573

5674
public PUIFrame(String displayName, int width, int height) {
5775
super();
5876
this.displayName = displayName;
5977
this.w = width;
6078
this.h = height;
61-
constructorInit();
79+
constructorInit(true);
80+
}
81+
82+
public PUIFrame(String displayName, int width, int height, boolean showFrame){
83+
super();
84+
this.displayName = displayName;
85+
this.w = width;
86+
this.h = height;
87+
constructorInit(showFrame);
6288
}
6389

64-
private void constructorInit() {
90+
private void constructorInit(boolean showFrame) {
6591
setTitle(displayName);
6692
setSize(w, h);
6793
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
@@ -76,7 +102,7 @@ private void constructorInit() {
76102
hasInit = true;
77103

78104
repaint();
79-
setVisible(true);
105+
setVisible(showFrame);
80106
}
81107

82108
private void initWindowListener() {

src/ooo/paulsen/utils/PInstance.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package ooo.paulsen.utils;
2+
3+
import java.io.IOException;
4+
import java.net.BindException;
5+
import java.net.InetAddress;
6+
import java.net.ServerSocket;
7+
8+
/**
9+
* Used for singe-instance-Programms <br>
10+
* <a href="https://stackoverflow.com/questions/920386/how-to-allow-running-only-one-instance-of-a-java-program-at-a-time">Source</a>
11+
*/
12+
public class PInstance {
13+
14+
private static int PORT;
15+
private static ServerSocket socket;
16+
17+
/**
18+
* Creates/reserves a local Port to indicate that this Program is running.
19+
* @param port has to be in Range and not be allocated by <a href="https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml">another program</a>
20+
* @throws BindException if Another Instance already exists
21+
* <br>IOException if the socket failed
22+
*/
23+
public PInstance(int port) throws IOException, BindException {
24+
PORT = port;
25+
26+
//Bind to localhost adapter with a zero connection queue
27+
socket = new ServerSocket(PORT, 0, InetAddress.getByAddress(new byte[]{127, 0, 0, 1}));
28+
}
29+
30+
/**
31+
*
32+
* @param port
33+
* @return if a local Program, which reserves/uses a Port, is running.
34+
*/
35+
public static boolean isInstanceRunning(int port) {
36+
try {
37+
//Bind to localhost adapter with a zero connection queue
38+
socket = new ServerSocket(port, 0, InetAddress.getByAddress(new byte[]{127, 0, 0, 1}));
39+
} catch (BindException e) {
40+
return true;
41+
} catch (IOException e) {
42+
return false;
43+
}
44+
return false;
45+
}
46+
47+
}

0 commit comments

Comments
 (0)