Skip to content

Commit

Permalink
swing/HoldFillButton: Added tick icon when button action is actually …
Browse files Browse the repository at this point in the history
…fired.
  • Loading branch information
miguel-paz committed Jan 17, 2025
1 parent d3b5eb0 commit 5986808
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/java/pt/lsts/neptus/gui/swing/HoldFillButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,23 @@
public class HoldFillButton extends JButton {
private Timer timer;
private int progress = 0;
private int holdDurationMs = 2000; // 2 seconds
private int holdDurationMillis = 2000;
private final Color fillColor = new Color(0, 128, 255, 100);
private final int tempButtonDurationMillis = 1000;
private final List<ActionListener> actionListeners = new ArrayList<>();
private final ImageIcon LOCK_CLOCK = new ImageIcon(ImageUtils.getScaledImage("images/buttons/lock_clock.png", 15, 15));
private final ImageIcon TICK = new ImageIcon(ImageUtils.getScaledImage("images/buttons/tick.png", 15, 15));

public HoldFillButton(String text) {
super(text);
setIcon(LOCK_CLOCK);
setupButton();
}

public HoldFillButton(String text, int holdDurationMs) {
public HoldFillButton(String text, int holdDurationMillis) {
super(text);
setIcon(LOCK_CLOCK);
this.holdDurationMs = holdDurationMs;
this.holdDurationMillis = holdDurationMillis;
setupButton();
}

Expand All @@ -81,13 +84,14 @@ public void mousePressed(MouseEvent e) {

timer = new Timer(10, event -> {
long elapsed = System.currentTimeMillis() - pressStartTime;
progress = (int) (elapsed * 100 / holdDurationMs);
progress = (int) (elapsed * 100 / holdDurationMillis);
repaint();

if (elapsed >= holdDurationMs) {
if (elapsed >= holdDurationMillis) {
timer.stop();
triggerAction();
progress = 0;
repaint();
}
});
timer.start();
Expand All @@ -104,6 +108,8 @@ public void mouseReleased(MouseEvent e) {
}

private void triggerAction() {
progress = 0;
changeButtonTemporarily();
fireCustomActionPerformed();
}
});
Expand All @@ -120,6 +126,15 @@ this, ActionEvent.ACTION_PERFORMED, getActionCommand()
}
}

private void changeButtonTemporarily() {
setIcon(TICK);
Timer timer = new Timer(tempButtonDurationMillis, (ActionEvent event) -> {
setIcon(LOCK_CLOCK);
});
timer.setRepeats(false); // Run only once
timer.start();
}

@Override
public void addActionListener(ActionListener l) {
if (l == null) {
Expand All @@ -138,8 +153,6 @@ protected void paintComponent(Graphics g) {

int fillWidth = (int) (getWidth() * (progress / 100.0));

Color fillColor = new Color(0, 128, 255, 100);

g2d.setColor(fillColor);
g2d.fillRect(0, 0, fillWidth, getHeight());
}
Expand Down
Binary file added src/resources/images/buttons/tick.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5986808

Please sign in to comment.