cleanup Flashing plugin

This commit is contained in:
Sergey Ponomarev
2026-07-23 22:06:54 +03:00
parent 400e574f06
commit df7a0a1b12
9 changed files with 106 additions and 127 deletions

View File

@ -15,113 +15,88 @@
*/
package org.jivesoftware.spark.plugin.flashing;
import java.awt.FlowLayout;
import java.awt.Window;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import javax.swing.JButton;
import javax.swing.JFrame;
import org.jivesoftware.spark.PluginManager;
import org.jivesoftware.spark.util.log.Log;
public class FlashWindow {
private final HashMap<Window, Thread> flashings = new HashMap<>();
private final HashMap<Window, Thread> flashings = new HashMap<>();
static {
boolean is64bit = System.getProperty("sun.arch.data.model").equals("64");
String arch = "";
if (is64bit) {
arch = "64";
}
static {
boolean is64bit = System.getProperty("sun.arch.data.model").equals("64");
String arch = "";
if (is64bit) {
arch = "64";
}
try {
System.load(PluginManager.PLUGINS_DIRECTORY.getCanonicalPath() +
File.separator + "flashing" + File.separator + "native" + File.separator + "FlashWindow" + arch + ".dll");
} catch (UnsatisfiedLinkError | IOException e) {
Log.error(e);
}
}
public native void flash(String name, boolean bool);
/*
* @param frame The JFrame to be flashed
* @param intratime The amount of time between the on and off states of a
* single flash
* @param intertime The amount of time between different flashes
* @param count The number of times to flash the window
*/
public void flash(final Window window, final int intratime,
final int count) {
new Thread( () -> {
try {
if (window instanceof JFrame)
{
// flash on and off each time
for (int i = 0; i < count; i++) {
flash(((JFrame) window).getTitle(), true);
Thread.sleep(intratime);
}
// turn the flash off
flash(((JFrame) window).getTitle(), false);
}
} catch (Exception ex) {
// System.out.println(ex.getMessage());
}
} ).start();
}
public void startFlashing(final Window window) {
if (flashings.get(window) == null) {
Thread t = new Thread(() -> {
while (true) {
try {
Thread.sleep(1500);
if (window instanceof JFrame)
flash(((JFrame) window).getTitle(), true);
} catch (Exception ex) {
flash(((JFrame) window).getTitle(), false);
break;
}
}
});
t.start();
flashings.put(window, t);
}
}
public void stopFlashing(final Window window) {
if (flashings.get(window) != null) {
flashings.get(window).interrupt();
flashings.remove(window);
}
}
public native void flash(String name, boolean bool);
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setTitle("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new FlowLayout());
JButton button = new JButton("Temp Flashing");
frame.getContentPane().add(button);
final FlashWindow winutil = new FlashWindow();
button.addActionListener( evt -> winutil.flash(frame, 750, 5) );
/*
* @param frame The JFrame to be flashed
* @param intratime The amount of time between the on and off states of a
* single flash
* @param intertime The amount of time between different flashes
* @param count The number of times to flash the window
*/
public void flash(Window window, int intratime, int count) {
if (!(window instanceof JFrame)) {
return;
}
JFrame jFrame = (JFrame) window;
new Thread(() -> {
try {
// flash on and off each time
for (int i = 0; i < count; i++) {
flash(jFrame.getTitle(), true);
Thread.sleep(intratime);
}
// turn the flash off
flash(jFrame.getTitle(), false);
} catch (Exception ignored) {
}
}).start();
}
JButton startButton = new JButton("Start Flashing");
frame.getContentPane().add(startButton);
startButton.addActionListener( evt -> winutil.startFlashing(frame) );
JButton stopButton = new JButton("Stop Flashing");
frame.getContentPane().add(stopButton);
stopButton.addActionListener( evt -> {
// winutil.flash(frame,750,1500,5);
winutil.stopFlashing(frame);
} );
frame.pack();
frame.setVisible(true);
}
public void startFlashing(final Window window) {
if (flashings.get(window) != null) {
return;
}
if (!(window instanceof JFrame)) {
return;
}
JFrame jFrame = (JFrame) window;
Thread t = new Thread(() -> {
while (true) {
try {
Thread.sleep(1500);
flash(jFrame.getTitle(), true);
} catch (Exception ex) {
flash(jFrame.getTitle(), false);
break;
}
}
});
t.start();
flashings.put(window, t);
}
public void stopFlashing(final Window window) {
Thread windowFlashingThread = flashings.remove(window);
if (windowFlashingThread != null) {
windowFlashingThread.interrupt();
}
}
}

View File

@ -21,11 +21,7 @@ import org.jivesoftware.spark.NativeHandler;
import org.jivesoftware.spark.SparkManager;
public class FlashingHandler implements NativeHandler {
private final FlashWindow flasher;
public FlashingHandler() {
flasher = new FlashWindow();
}
private final FlashWindow flasher = new FlashWindow();
@Override
public void flashWindow(Window window) {

View File

@ -19,8 +19,9 @@ import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.plugin.Plugin;
public class FlashingPlugin implements Plugin {
@Override
private FlashingHandler nativeHandler;
@Override
public boolean canShutDown() {
return true;
}
@ -29,11 +30,14 @@ public class FlashingPlugin implements Plugin {
public void initialize() {
FlashingPreference preference = new FlashingPreference();
SparkManager.getPreferenceManager().addPreference(preference);
SparkManager.getNativeManager().addNativeHandler(new FlashingHandler());
nativeHandler = new FlashingHandler();
SparkManager.getNativeManager().addNativeHandler(nativeHandler);
}
@Override
public void shutdown() {
SparkManager.getNativeManager().removeNativeHandler(nativeHandler);
nativeHandler = null;
}
@Override

View File

@ -51,7 +51,6 @@ public class FlashingPreferenceDialog extends JPanel {
flashingPanel.setBorder(BorderFactory.createTitledBorder(FlashingResources.getString("title.flashing")));
// Setup MNEMORICS
ResourceUtils.resButton(flashingEnabled, FlashingResources.getString("flashing.enable"));
ResourceUtils.resLabel(lTyps, flashingType, FlashingResources.getString("flashing.type"));
@ -73,25 +72,27 @@ public class FlashingPreferenceDialog extends JPanel {
}
public void setFlashingType(String type) {
if (FlashingPreferences.TYPE_CONTINUOUS.equals(type)) {
flashingType.setSelectedIndex(0);
}
else if (FlashingPreferences.TYPE_TEMPORARY.equals(type)) {
flashingType.setSelectedIndex(1);
}
else {
flashingType.setSelectedIndex(0);
}
switch (type) {
case FlashingPreferences.TYPE_CONTINUOUS:
flashingType.setSelectedIndex(0);
break;
case FlashingPreferences.TYPE_TEMPORARY:
flashingType.setSelectedIndex(1);
break;
default:
flashingType.setSelectedIndex(0);
break;
}
}
public String getFlashingType() {
if (flashingType.getSelectedIndex() == 0) {
return FlashingPreferences.TYPE_CONTINUOUS;
}
else if (flashingType.getSelectedIndex() == 1) {
return FlashingPreferences.TYPE_TEMPORARY;
}
return "continuous";
switch (flashingType.getSelectedIndex()) {
case 0:
return FlashingPreferences.TYPE_CONTINUOUS;
case 1:
return FlashingPreferences.TYPE_TEMPORARY;
default:
return FlashingPreferences.TYPE_CONTINUOUS;
}
}
}

View File

@ -34,20 +34,17 @@ public class FlashingPreferences {
public FlashingPreferences() {
this.props = new Properties();
try {
props.load(new FileInputStream(getConfigFile()));
} catch (IOException e) {
// Can't load ConfigFile
}
}
public File getConfigFile() {
if (configFile == null)
configFile = new File(Spark.getSparkUserHome(),
"flashing.properties");
if (configFile == null) {
configFile = new File(Spark.getSparkUserHome(), "flashing.properties");
}
return configFile;
}
@ -76,11 +73,11 @@ public class FlashingPreferences {
}
private boolean getBoolean(String property, boolean defaultValue) {
return Boolean.parseBoolean(props.getProperty(property, Boolean
.toString(defaultValue)));
String propertyVal = props.getProperty(property);
return propertyVal != null ? Boolean.parseBoolean(propertyVal) : defaultValue;
}
private void setBoolean(String property, boolean value) {
props.setProperty(property, Boolean.toString(value));
props.setProperty(property, String.valueOf(value));
}
}

View File

@ -27,7 +27,7 @@ public class FlashingResources {
private static ClassLoader cl = FlashingResources.class.getClassLoader();
static final ImageIcon LIGHTING_BOLT_IMAGE = getImageIcon("lightning.png");
public static String getString(String propertyName) {
static String getString(String propertyName) {
try {
return prb.getString(propertyName);
} catch (Exception e) {
@ -36,7 +36,7 @@ public class FlashingResources {
}
}
static ImageIcon getImageIcon(String icon) {
private static ImageIcon getImageIcon(String icon) {
return new ImageIcon(cl.getResource(icon));
}
}

View File

@ -3,4 +3,4 @@ title.flashing=Taskbar Flashing
flashing.enable=&Enable Taskbar flashing
flashing.type=&Type
flashing.type.continuous=Continuous
flashing.type.temporary=Temporary
flashing.type.temporary=Temporary

View File

@ -0,0 +1,6 @@
title.flashing=Barra de tarefas piscando
flashing.enable=Ativar flash da barra de tarefas
flashing.type=Tipo
flashing.type.continuous=Contínuo
flashing.type.temporary=Temporário

View File

@ -1,6 +1,6 @@
title.flashing=Мигание в панели задач
flashing.enable=&Включить мигание в панели задач
flashing.type=&Тип
flashing.enable=Включить мигание в панели задач
flashing.type=Тип
flashing.type.continuous=Непрерывно
flashing.type.temporary=Временно
flashing.type.temporary=Временно