Created
July 3, 2018 14:12
-
-
Save crevier/a372695cb342d74e22fdbe78dd179726 to your computer and use it in GitHub Desktop.
Simple swing timer in groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.JDialog | |
import javax.swing.JPanel | |
import java.awt.* | |
import java.awt.image.BufferedImage | |
import static java.lang.System.exit | |
import static javax.swing.JOptionPane.* | |
class TimerPanel extends JPanel { | |
int progress = 0 | |
TimerPanel() { | |
setDoubleBuffered(true) | |
} | |
void setProgress(int p) { | |
this.progress = p | |
this.repaint() | |
} | |
@Override | |
protected void paintComponent(Graphics g) { | |
super.paintComponent(g) | |
def insets = getInsets() | |
g.setColor(Color.GREEN) | |
int progressWith = width * progress / 100 | |
g.fillRect(insets.left, insets.top, progressWith, height - insets.bottom) | |
g.setColor(Color.BLACK) | |
g.drawRect(insets.left, insets.top, width - insets.right - 1, height - insets.bottom - 1) | |
g.setFont(new Font("TimesRoman", Font.PLAIN, 15)); | |
g.drawString("Progress : " + progress + "%", 5, height / 2 + 5) | |
} | |
} | |
class TimerDialogBuilder { | |
private final String title | |
private final int width | |
private final int height | |
private final TimerPanel panel | |
TimerDialogBuilder(String title, int width, int height, TimerPanel panel) { | |
this.panel = panel | |
this.height = height | |
this.width = width | |
this.title = title | |
} | |
JDialog jDialog() { | |
JDialog jDialog = new JDialog() | |
jDialog.setTitle(title) | |
jDialog.setSize(width, height) | |
jDialog.setResizable(false) | |
jDialog.setIconImage(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB_PRE)); | |
centerDialog(jDialog) | |
jDialog.add(panel) | |
jDialog | |
} | |
private void centerDialog(JDialog dialog) { | |
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize(); | |
int x = (int) ((dimension.getWidth() - dialog.getWidth()) / 2); | |
int y = (int) ((dimension.getHeight() - dialog.getHeight()) / 2); | |
dialog.setLocation(x, y); | |
} | |
} | |
class CenteredFrame extends JDialog { | |
CenteredFrame(String title, int width, int height) { | |
setTitle(title) | |
setSize(width, height) | |
setResizable(false) | |
centerFrame() | |
Image icon = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB_PRE); | |
setIconImage(icon); | |
} | |
void centerFrame() { | |
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize(); | |
int x = (int) ((dimension.getWidth() - getWidth()) / 2); | |
int y = (int) ((dimension.getHeight() - getHeight()) / 2); | |
setLocation(x, y); | |
} | |
} | |
def startRepeatableTimer(String taskName, int durationInSeconds) { | |
def f = new TimerPanel() | |
def frame = new TimerDialogBuilder(taskName, 200, 60, f).jDialog() | |
frame.setVisible(true) | |
new Timer().runAfter(durationInSeconds * 1000) { | |
if (OK_OPTION == showConfirmDialog(null, "Task '$taskName' done.\nStart again ?", taskName, YES_NO_OPTION)) { | |
frame.dispose() | |
startRepeatableTimer(taskName, durationInSeconds) | |
} else { | |
exit(0) | |
} | |
} | |
Thread.start { | |
(1..durationInSeconds * 100).each { | |
int progress = it / durationInSeconds | |
f.setProgress(progress) | |
sleep(10) | |
} | |
} | |
} | |
if(args && args.length ==2) { | |
def durationInSeconds = args[1].toInteger() | |
def taskName = args[0] | |
startRepeatableTimer(taskName, durationInSeconds) | |
} else { | |
println("""Please enter task name and duration in sec for the timer""") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment