Skip to content

Commit 9b45677

Browse files
author
realPaulsen
committed
v1.1.3c
Added: - Serial-Disconnect-Event Changes: - clearElements() on ScrollPanel also removes Elements from frame Fixes: - Error-Proofed PDataStorage - Added release()-overrides to ScrollPanel & Slider
1 parent 8159f1e commit 9b45677

File tree

5 files changed

+84
-37
lines changed

5 files changed

+84
-37
lines changed

src/ooo/paulsen/io/PDataStorage.java

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,25 @@ public String toString() {
8585

8686
private ArrayList<DataVariable> variables = new ArrayList<>();
8787

88-
// public static void main(String[] args) {
89-
// PDataStorage ds = new PDataStorage();
90-
91-
// ds.add("teststring", "asdfe4[]");
92-
// ds.save("testfile.paulsen");
93-
94-
// ds.read("testfile.paulsen");
95-
// String s = ds.getString("teststring");
96-
// System.out.println(s);
97-
// }
88+
// public static void main(String[] args) {
89+
//
90+
// PDataStorage ds = new PDataStorage();
91+
//
92+
// ds.add("string", "asdöfljk");
93+
// ds.add("int", 0);
94+
// ds.add("long", 0L);
95+
// ds.add("float", 0.0f);
96+
// ds.add("bool", false);
97+
// ds.save("testfile.paulsen");
98+
//
99+
// ds = new PDataStorage();
100+
// ds.read("testfile.paulsen");
101+
// System.out.println(ds.getString("string"));
102+
// System.out.println(ds.getInteger("int"));
103+
// System.out.println(ds.getLong("long"));
104+
// System.out.println(ds.getFloat("float"));
105+
// System.out.println(ds.getBoolean("bool"));
106+
// }
98107

99108
public void save(String path) {
100109
PFile file = new PFile(path);
@@ -107,11 +116,14 @@ public void save(String path) {
107116
}
108117

109118
/**
110-
* Reads dataStorage-file and stores all Data in this object
119+
* Reads dataStorage-file and stores all Data in this object.
120+
* <br>
121+
* Ignores Variables with no valid value
111122
*
112123
* @param path
124+
* @throws IllegalArgumentException when file does not meet the expectations of this format
113125
*/
114-
public void read(String path) {
126+
public void read(String path) throws IllegalArgumentException {
115127
if (PFolder.isFolder(path))
116128
return;
117129

@@ -123,7 +135,6 @@ public void read(String path) {
123135
String variableName = "";
124136
String data = "";
125137

126-
loop:
127138
for (int i = 0; i < file.length(); i++) {
128139
char c = file.charAt(i);
129140

@@ -145,8 +156,7 @@ public void read(String path) {
145156
currentDataType = DataType.BOOLEAN;
146157
break;
147158
default:
148-
System.err.println("[DataStorage]::could not identify datatype '" + c + "' at pos " + i);
149-
break loop;
159+
throw new IllegalArgumentException("could not identify datatype '\" + c + \"' at pos " + i);
150160
}
151161
hasBeenDataType = true;
152162
i++; // jump over [
@@ -160,8 +170,13 @@ public void read(String path) {
160170
if (c != ']') {
161171
data += c;
162172
} else {
163-
addData(currentDataType, variableName.replace("^<<^", "[").replace("^>>^", "]"),
164-
data.replace("^<<^", "[").replace("^>>^", "]"));
173+
try {
174+
addData(currentDataType, variableName.replace("^<<^", "[").replace("^>>^", "]"),
175+
data.replace("^<<^", "[").replace("^>>^", "]"));
176+
} catch (IllegalArgumentException e) {
177+
} catch (Exception e) {
178+
throw e;
179+
}
165180
variableName = "";
166181
data = "";
167182
hasBeenDataType = false;
@@ -172,12 +187,19 @@ public void read(String path) {
172187
}
173188
}
174189

175-
public boolean addData(DataType type, String name, String data) {
190+
public boolean addData(DataType type, String name, String data) throws IllegalArgumentException {
176191
try {
177192
switch (type) {
178193
case BOOLEAN:
179-
add(name, Boolean.valueOf(data));
180-
break;
194+
if (data != null) {
195+
if (data.toLowerCase().equals("true"))
196+
add(name, true);
197+
else if (data.toLowerCase().equals("false"))
198+
add(name, false);
199+
else
200+
throw new IllegalArgumentException("Could not find \"" + type + " " + name + "\"");
201+
break;
202+
}
181203
case FLOAT:
182204
add(name, Float.valueOf(data));
183205
break;
@@ -188,15 +210,14 @@ public boolean addData(DataType type, String name, String data) {
188210
add(name, Long.valueOf(data));
189211
break;
190212
case STRING:
191-
add(name, String.valueOf(data));
213+
add(name, data == null ? "" : String.valueOf(data));
192214
break;
193215
default:
194216
System.err.println("[DataStorage]::UNKNOWN datatype!");
195217
return false;
196218
}
197219
} catch (Exception e) {
198-
System.err.println("[DataStorage]::Some error occured when adding data!");
199-
e.printStackTrace();
220+
throw new IllegalArgumentException("Can not parse the data into this DataType!");
200221
}
201222
return true;
202223
}
@@ -221,31 +242,31 @@ public void add(String name, boolean data) {
221242
variables.add(new DataVariable(name, data));
222243
}
223244

224-
public Object get(DataType type, String name) {
245+
public Object get(DataType type, String name) throws IllegalArgumentException {
225246
for (DataVariable v : variables) {
226247
if (v.dt == type && v.name.equals(name))
227248
return v.obj;
228249
}
229-
return null;
250+
throw new IllegalArgumentException("Could not find \"" + type + " " + name + "\"");
230251
}
231252

232-
public String getString(String name) {
253+
public String getString(String name) throws IllegalArgumentException {
233254
return (String) get(DataType.STRING, name);
234255
}
235256

236-
public long getLong(String name) {
257+
public long getLong(String name) throws IllegalArgumentException {
237258
return (long) get(DataType.LONG, name);
238259
}
239260

240-
public int getInteger(String name) {
261+
public int getInteger(String name) throws IllegalArgumentException {
241262
return (int) get(DataType.INTEGER, name);
242263
}
243264

244-
public float getFloat(String name) {
265+
public float getFloat(String name) throws IllegalArgumentException {
245266
return (float) get(DataType.FLOAT, name);
246267
}
247268

248-
public boolean getBoolean(String name) {
269+
public boolean getBoolean(String name) throws IllegalArgumentException {
249270
return (boolean) get(DataType.BOOLEAN, name);
250271
}
251272

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public PSerialConnection(String systemPortName) throws SerialPortInvalidPortExce
4545
private OutputStream out;
4646

4747
private Thread listenerThread;
48+
private Runnable disconnectEvent;
4849
private boolean isConnected = false;
4950

5051
private void initSerial(SerialPort port) {
@@ -60,8 +61,11 @@ public int getListeningEvents() {
6061

6162
@Override
6263
public void serialEvent(SerialPortEvent serialPortEvent) {
63-
if (serialPortEvent.getEventType() == SerialPort.LISTENING_EVENT_PORT_DISCONNECTED)
64+
if (serialPortEvent.getEventType() == SerialPort.LISTENING_EVENT_PORT_DISCONNECTED) {
6465
disconnect();
66+
if (disconnectEvent != null)
67+
disconnectEvent.run();
68+
}
6569
}
6670
});
6771

@@ -92,6 +96,11 @@ public void run() {
9296
} catch (Exception e) {
9397
}
9498
}
99+
try {
100+
Thread.sleep(1);
101+
} catch (InterruptedException e) {
102+
return;
103+
}
95104
}
96105

97106
if (!port.isOpen()) {
@@ -128,9 +137,9 @@ public boolean connect() {
128137
isConnected = true;
129138
return true;
130139
} else
131-
System.err.println("--port could not be opened");
140+
System.err.println("[PSerialConnection] :: port could not be opened");
132141
} else
133-
System.err.println("--already connected to something");
142+
System.err.println("[PSerialConnection] :: already connected to something");
134143
return false;
135144
}
136145

@@ -150,6 +159,10 @@ public boolean isConnected() {
150159
return isConnected;
151160
}
152161

162+
public void setDisconnectEvent(Runnable r){
163+
disconnectEvent = r;
164+
}
165+
153166
public synchronized boolean addListener(PSerialListener listener) {
154167
if (listener != null)
155168
return listeners.add(listener);

src/ooo/paulsen/ui/PUIScrollPanel.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,13 @@ public void swapElements(int e1, int e2) {
237237
}
238238

239239
/**
240-
* removes all Elements from Frame and clears its own list
240+
* removes all Elements from Core, Frame and clears its own List
241241
*/
242242
public void clearElements() {
243-
for (PUIElement e : elements)
243+
for (PUIElement e : elements) {
244244
e.release();
245+
frame.remove(e);
246+
}
245247
elements.clear();
246248
updateElements();
247249
}
@@ -304,6 +306,13 @@ public void setBounds(int w, int h) {
304306
setBounds(x, y, w, h);
305307
}
306308

309+
@Override
310+
public void release() {
311+
super.release();
312+
clearElements();
313+
frame.remove(slider);
314+
}
315+
307316
public void runAllValueUpdateActions() {
308317
if (valueUpdateAction != null)
309318
for (PUIAction r : valueUpdateAction)

src/ooo/paulsen/ui/PUISlider.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ public void doPaintOverOnHover(boolean paintOverOnHover) {
162162
sliderB.doPaintOverOnHover(paintOverOnHover);
163163
}
164164

165+
@Override
166+
public void release() {
167+
super.release();
168+
frame.remove(sliderB);
169+
}
170+
165171
public float getValue() {
166172
return sliderValue;
167173
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ public void run() {
8686
}
8787

8888
private void init() {
89-
System.out.println("init PUICore");
90-
9189
f.c().addMouseListener(new MouseListener() {
9290
@Override
9391
public void mouseClicked(MouseEvent e) {

0 commit comments

Comments
 (0)