Skip to content

Commit 79b95a1

Browse files
committed
More explicit validation with exceptions
1 parent 332c607 commit 79b95a1

3 files changed

Lines changed: 42 additions & 0 deletions

File tree

src/main/java/com/nativejavafx/taskbar/TaskbarProgressbarImpl.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
package com.nativejavafx.taskbar;
1616

17+
import com.nativejavafx.taskbar.exception.StageNotShownException;
18+
import com.nativejavafx.taskbar.exception.UnsupportedSystemException;
1719
import com.nativejavafx.taskbar.strategy.HWNDStrategy;
1820
import javafx.stage.Stage;
1921
import org.bridj.Pointer;
@@ -82,20 +84,23 @@ private void setProgressState(Stage stage, Type type) {
8284

8385
@Override
8486
public void stopProgress() {
87+
validate(stage.get());
8588
if (this.stage.get() != null) {
8689
setProgressState(stage.get(), Type.NO_PROGRESS);
8790
}
8891
}
8992

9093
@Override
9194
public void showIndeterminateProgress() {
95+
validate(stage.get());
9296
if (this.stage.get() != null) {
9397
setProgressState(stage.get(), Type.INDETERMINATE);
9498
}
9599
}
96100

97101
@Override
98102
public void showCustomProgress(long startValue, long endValue, @NotNull Type type) {
103+
validate(stage.get());
99104
if (this.stage.get() != null) {
100105
final Pointer<Integer> pointer = getPointer(stage.get());
101106
executor.execute(() -> {
@@ -107,6 +112,7 @@ public void showCustomProgress(long startValue, long endValue, @NotNull Type typ
107112

108113
@Override
109114
public void setProgressType(@NotNull Type type) {
115+
validate(stage.get());
110116
setProgressState(stage.get(), type);
111117
}
112118

@@ -115,6 +121,28 @@ public void closeOperations() {
115121
executor.submit(() -> iTaskbarList3.get().Release());
116122
}
117123

124+
private void validate(Stage stage) {
125+
checkSystemSupported();
126+
checkStageShown(stage);
127+
}
128+
129+
private void checkStageShown(Stage stage) {
130+
if (!stage.isShowing()) throw new StageNotShownException(
131+
"The given Stage is not showing and therefore taskbar-progressbar can't be created"
132+
);
133+
}
134+
135+
private void checkSystemSupported() {
136+
if (isNotSupported()) throw new UnsupportedSystemException(
137+
String.format(
138+
"%s (system: %s, version: %s)",
139+
"Your system does not support taskbar-progressbar!",
140+
System.getProperty("os.name"),
141+
System.getProperty("os.version")
142+
)
143+
);
144+
}
145+
118146
private static final class DaemonThread extends Thread {
119147
DaemonThread(Runnable runnable) {
120148
super(runnable);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.nativejavafx.taskbar.exception;
2+
3+
public class StageNotShownException extends RuntimeException {
4+
public StageNotShownException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.nativejavafx.taskbar.exception;
2+
3+
public class UnsupportedSystemException extends RuntimeException {
4+
public UnsupportedSystemException(String message) {
5+
super(message);
6+
}
7+
}

0 commit comments

Comments
 (0)