From 015f39e6d29404e3fa5f182f7a924b043b9d9ca0 Mon Sep 17 00:00:00 2001 From: Patrick Favre-Bulle Date: Sun, 6 Aug 2023 14:07:33 +0200 Subject: [PATCH] Support other operation systems than Windows by disabling Windows specific features --- pom.xml | 9 +- .../tools/dconvert/ui/GUIController.java | 8 +- .../tools/dconvert/ui/TaskbarProgress.java | 135 ++++++++++++++++++ .../tools/dconvert/ui/WinTaskbarProgress.java | 95 ------------ 4 files changed, 147 insertions(+), 100 deletions(-) create mode 100644 src/main/java/at/favre/tools/dconvert/ui/TaskbarProgress.java delete mode 100644 src/main/java/at/favre/tools/dconvert/ui/WinTaskbarProgress.java diff --git a/pom.xml b/pom.xml index 1d6c367..e5b58f8 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ at.favre.tools dconvert - ${raw.version}-alpha7 + ${raw.version}-alpha9 @@ -213,6 +213,13 @@ 1.5.0 + + + org.apache.commons + commons-lang3 + 3.13.0 + + org.imgscalr diff --git a/src/main/java/at/favre/tools/dconvert/ui/GUIController.java b/src/main/java/at/favre/tools/dconvert/ui/GUIController.java index eb40555..a35fa76 100644 --- a/src/main/java/at/favre/tools/dconvert/ui/GUIController.java +++ b/src/main/java/at/favre/tools/dconvert/ui/GUIController.java @@ -155,7 +155,7 @@ public void onCreate(final Stage stage, IPreferenceStore store, ResourceBundle b btnSrcFolder.setOnAction(new FolderPicker(srcDirectoryChooser, textFieldSrcPath, textFieldDstPath, bundle)); btnDstFolder.setOnAction(new FolderPicker(srcDirectoryChooser, textFieldDstPath, null, bundle)); btnConvert.setOnAction(event -> { - WinTaskbarProgress winTaskbarProgress = new WinTaskbarProgress(); + TaskbarProgress taskbarProgress = TaskbarProgress.create(); try { Arguments arg = getFromUI(false); saveToPrefs(arg); @@ -171,7 +171,7 @@ public void onCreate(final Stage stage, IPreferenceStore store, ResourceBundle b public void onProgress(float progress) { Platform.runLater(() -> { progressBar.setProgress(progress); - winTaskbarProgress.updateProgress(progress); + taskbarProgress.updateProgress(progress); }); } @@ -185,14 +185,14 @@ public void onFinished(int finishedJobs, List exceptions, long time, textFieldConsole.appendText(""); if (!exceptions.isEmpty()) { - winTaskbarProgress.error(); + taskbarProgress.error(); Alert alert = new Alert(Alert.AlertType.WARNING); alert.setTitle(bundle.getString("main.alert.title")); alert.setHeaderText(null); alert.setContentText(MessageFormat.format(bundle.getString("main.alert.content"), exceptions.size())); alert.showAndWait(); } else { - winTaskbarProgress.finish(); + taskbarProgress.finish(); } }); diff --git a/src/main/java/at/favre/tools/dconvert/ui/TaskbarProgress.java b/src/main/java/at/favre/tools/dconvert/ui/TaskbarProgress.java new file mode 100644 index 0000000..0ca87a1 --- /dev/null +++ b/src/main/java/at/favre/tools/dconvert/ui/TaskbarProgress.java @@ -0,0 +1,135 @@ +/* + * Copyright 2016 Patrick Favre-Bulle + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package at.favre.tools.dconvert.ui; + +import org.apache.commons.lang3.SystemUtils; +import org.bridj.Pointer; +import org.bridj.cpp.com.COMRuntime; +import org.bridj.cpp.com.shell.ITaskbarList3; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +/** + * Wrapper to use native windows taskbar features like progress, error etc. + */ +public interface TaskbarProgress { + + void updateProgress(double progress); + + void finish(); + + void error(); + + static TaskbarProgress create() { + if (SystemUtils.IS_OS_WINDOWS) { + return new WinTaskbarProgress(); + } else { + return new NoopTaskbarProgress(); + } + } + + /** + * No-Op for non Windows OSes + */ + class NoopTaskbarProgress implements TaskbarProgress { + + @Override + public void updateProgress(double progress) { + } + + @Override + public void finish() { + } + + @Override + public void error() { + } + } + + /** + * Implementation for Windows OS + */ + final class WinTaskbarProgress implements TaskbarProgress { + + private ExecutorService executor; + private static final long MAX = 10000; + private Pointer hwnd; + private ITaskbarList3 list; + private boolean enabled = true; + + public WinTaskbarProgress() { + try { + hwnd = Pointer.pointerToAddress(com.sun.glass.ui.Window.getWindows().get(0).getNativeWindow()); + } catch (Exception e) { + enabled = false; + return; + } + + executor = Executors.newSingleThreadExecutor(r -> { + Thread t = new Thread(r); + t.setDaemon(true); + return t; + }); + + executor.execute(() -> { + try { + list = COMRuntime.newInstance(ITaskbarList3.class); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + }); + hwnd = Pointer.pointerToAddress(com.sun.glass.ui.Window.getWindows().get(0).getNativeWindow()); + } + + public void updateProgress(double progress) { + if (!enabled) return; + executor.execute(() -> list.SetProgressValue((Pointer) hwnd, Math.round(progress * (double) MAX), MAX)); + } + + public void finish() { + if (!enabled) return; + executor.execute(() -> { + list.SetProgressValue((Pointer) hwnd, MAX, MAX); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + list.SetProgressState((Pointer) hwnd, ITaskbarList3.TbpFlag.TBPF_NOPROGRESS); + list.SetProgressValue((Pointer) hwnd, 0, MAX); + list.Release(); + }); + } + + public void error() { + if (!enabled) return; + executor.execute(() -> { + list.SetProgressState((Pointer) hwnd, ITaskbarList3.TbpFlag.TBPF_ERROR); + list.SetProgressValue((Pointer) hwnd, MAX, MAX); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + list.SetProgressState((Pointer) hwnd, ITaskbarList3.TbpFlag.TBPF_NOPROGRESS); + list.SetProgressValue((Pointer) hwnd, 0, MAX); + list.Release(); + }); + } + } +} diff --git a/src/main/java/at/favre/tools/dconvert/ui/WinTaskbarProgress.java b/src/main/java/at/favre/tools/dconvert/ui/WinTaskbarProgress.java deleted file mode 100644 index e87fe7d..0000000 --- a/src/main/java/at/favre/tools/dconvert/ui/WinTaskbarProgress.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright 2016 Patrick Favre-Bulle - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package at.favre.tools.dconvert.ui; - -import org.bridj.Pointer; -import org.bridj.cpp.com.COMRuntime; -import org.bridj.cpp.com.shell.ITaskbarList3; - -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -/** - * Wrapper to use native windows taskbar features like progress, error etc. - */ -public class WinTaskbarProgress { - private ExecutorService executor; - private static final long MAX = 10000; - private Pointer hwnd; - private ITaskbarList3 list; - private boolean enabled = true; - - public WinTaskbarProgress() { - try { - hwnd = Pointer.pointerToAddress(com.sun.glass.ui.Window.getWindows().get(0).getNativeWindow()); - } catch (Exception e) { - enabled = false; - return; - } - - executor = Executors.newSingleThreadExecutor(r -> { - Thread t = new Thread(r); - t.setDaemon(true); - return t; - }); - - executor.execute(() -> { - try { - list = COMRuntime.newInstance(ITaskbarList3.class); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } - }); - hwnd = Pointer.pointerToAddress(com.sun.glass.ui.Window.getWindows().get(0).getNativeWindow()); - } - - public void updateProgress(double progress) { - if (!enabled) return; - executor.execute(() -> list.SetProgressValue((Pointer) hwnd, Math.round(progress * (double) MAX), MAX)); - } - - public void finish() { - if (!enabled) return; - executor.execute(() -> { - list.SetProgressValue((Pointer) hwnd, MAX, MAX); - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - list.SetProgressState((Pointer) hwnd, ITaskbarList3.TbpFlag.TBPF_NOPROGRESS); - list.SetProgressValue((Pointer) hwnd, 0, MAX); - list.Release(); - }); - } - - public void error() { - if (!enabled) return; - executor.execute(() -> { - list.SetProgressState((Pointer) hwnd, ITaskbarList3.TbpFlag.TBPF_ERROR); - list.SetProgressValue((Pointer) hwnd, MAX, MAX); - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - list.SetProgressState((Pointer) hwnd, ITaskbarList3.TbpFlag.TBPF_NOPROGRESS); - list.SetProgressValue((Pointer) hwnd, 0, MAX); - list.Release(); - }); - } -}