FileDocCategorySizeDatePackage
BreakEnd.javaAPI DocExample7614Sun Feb 08 21:34:06 GMT 2004None

BreakEnd

public class BreakEnd extends JFrame implements Runnable
BreakEnd - Tool to display current time and time that class starts/resumes. This started as a quick hack when I saw the Windows guys with a tool like this, but it has grown somewhat since then. TODO
  • add another Thread to flash the screen if the resume time is passed.
author
Ian Darwin
version
$Id: BreakEnd.java,v 1.12 2004/02/09 03:34:06 ian Exp $

Fields Summary
protected JLabel
nowLabel
Label for the current time.
protected JLabel
endsLabel
Label for when break ends.
protected JSlider
fontSize
JSlider for font sizes
protected Thread
ticker
A thread to run the clock ticker
protected Font
f
The font for the large text
protected static final int
FONT_SIZE_MIN
The minimum allowable font size
protected static final int
FONT_SIZE_MAX
The maximum sensible font size given current monitors.
protected static final int
FONT_SIZE_DEFAULT
Default point size
protected NumberFormat
form
NumberFormat to format a non-localized two-digit number
Constructors Summary
public BreakEnd(String s)
Constructor: set a font, initialize UI components.

		super("Java Breaker: " + s);
		setBackground(Color.white);
		setForeground(Color.red);

		Container cp;
		cp = getContentPane();
		// cp = this;
		cp.setLayout(new BorderLayout());

		fontSize = new JSlider(JSlider.HORIZONTAL,
			FONT_SIZE_MIN, FONT_SIZE_MAX, FONT_SIZE_DEFAULT);
		fontSize.setMajorTickSpacing(10);
		fontSize.setMinorTickSpacing(5);
		fontSize.setSnapToTicks(true);
		fontSize.setPaintTicks(true);
		fontSize.setPaintLabels(true);
		fontSize.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));


		fontSize.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				JSlider source = (JSlider)e.getSource();
				if (!source.getValueIsAdjusting()) {
					int points = (int)source.getValue();
					setFontSize(points);
				}
			}
		});
		//add the slider to the content pane
		cp.add(BorderLayout.NORTH, fontSize);

		// Make a panel for CENTER to hold two labels above each other
		JPanel timesPanel = new JPanel();
		timesPanel.setLayout(new GridLayout(2,1));

		cp.add(BorderLayout.CENTER, timesPanel);
		timesPanel.add(
			nowLabel =  new JLabel("Time now is: 00:00:00", JLabel.CENTER));
	
		String mesg = null;
		if (s.startsWith("+")) {	// "This will be HARDer..."
			Calendar d = new GregorianCalendar();
			int newMinutes = d.get(Calendar.MINUTE)+
				Integer.parseInt(s.substring(1))+1;
			d.set(Calendar.MINUTE, newMinutes);
			mesg = "We start at " + toHHMM_String(d);
		} else {
			mesg = "We start at " + s + " ";
		}
		timesPanel.add(BorderLayout.CENTER,
			endsLabel = new JLabel(mesg, JLabel.CENTER));
		setFontSize(FONT_SIZE_DEFAULT);
		JButton b;
		cp.add(BorderLayout.SOUTH, b = new JButton("Done"));
		b.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});

		// Pack should be done here so the ComponentAdapter's
		// componentResized doesn't fire and shrink the size
		pack();

		// Start the timer thread now. Tick, tick, tick.
		(ticker = new Thread(this)).start();

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		addComponentListener(new ComponentAdapter() {
			public void componentResized(ComponentEvent e) {
				Dimension d = getSize();
				if (d.height < 100)
					setFontSize(15);
				else
					setFontSize(d.height/6);
			}
		});
	
Methods Summary
public static voidmain(java.lang.String[] av)
Main method to start me up.


	       
	     
		for (int i=0; i<av.length; i++) {
			// parse options...
		}

		if (av.length != 1) {
			System.err.println("Usage: javaBreak time");
			return;
		}
		BreakEnd b = new BreakEnd(av[0]);
		UtilGUI.centre(b);

		b.setVisible(true);
	
public voidrun()

		Calendar d /* = new GregorianCalendar() */; // For 1.4
		while (ticker.isAlive()) {
			//d.setTimeInMillis(System.currentTimeMillis()); // For 1.4
			d = new GregorianCalendar();
			nowLabel.setText("Time is now " + toHHMM_String(d));
			try {
				Thread.sleep(1000);
			} catch (Exception e) {
				return;
			}
		}
	
protected voidsetFontSize(int sz)
Set the font to the given size

		if (sz > FONT_SIZE_MAX)
			sz = FONT_SIZE_MAX;
		//System.out.println("Setting font size to " + sz);
		Font f = new Font("Helvetica", Font.PLAIN, sz);
		nowLabel.setFont(f);
		endsLabel.setFont(f);
		fontSize.setValue(sz);
	
private java.lang.StringtoHHMM_String(java.util.Calendar d)
Get the Calendar into HHMM format


	       
	    
		String result = null;
		int h = d.get(Calendar.HOUR_OF_DAY);
		int m = d.get(Calendar.MINUTE);
		int s = d.get(Calendar.SECOND);
		try {
			result = form.format(h) + ":" +
				form.format(m) + ":" +
				form.format(s);
			// Why not just use a DateFormat for that?
		} catch (IllegalArgumentException iae) {
			JOptionPane.showMessageDialog(this,
				"Formatting Error!" + iae,	// message
				"Formatting Error!",					// titlebar
				JOptionPane.ERROR_MESSAGE);	// icon
		}
		return result;