diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/net/jesterpm/utilaclock/UtilaClockManager.java | 273 | ||||
| -rw-r--r-- | src/net/jesterpm/utilaclock/UtilaclockClockDisplay.java | 234 | 
2 files changed, 507 insertions, 0 deletions
diff --git a/src/net/jesterpm/utilaclock/UtilaClockManager.java b/src/net/jesterpm/utilaclock/UtilaClockManager.java new file mode 100644 index 0000000..ef706d7 --- /dev/null +++ b/src/net/jesterpm/utilaclock/UtilaClockManager.java @@ -0,0 +1,273 @@ +/* + * Utilaclock -- Simple fullscreen clock app Jesse Morgan <jesse@jesterpm.net> + */ + +package net.jesterpm.utilaclock; + +import java.awt.FlowLayout; +import java.awt.Frame; +import java.awt.GridLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.text.DateFormat; +import java.text.ParseException; +import java.util.Date; +import java.util.Observer; + +import javax.swing.BorderFactory; +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JTextField; + +/** + * Management GUI. + *  + * @author Jesse Morgan <jesse@jesterpm.net> + * @version 25 Oct 2009 + */ +public class UtilaClockManager extends JFrame { +  /** +   * Serial UID. +   */ +  private static final long serialVersionUID = 8838459061020873829L; + +  /** +   * Font size text field. +   */ +  private JTextField my_font_size; + +  /** +   * End time for countdown. +   */ +  private JTextField my_end_time; +   +  /** +   * Amount of time for the timer. +   */ +  private JTextField my_timer_time; +   +  private final UtilaclockClockDisplay my_timer_window; + +  /** +   * Clock Manager Constructor. +   */ +  public UtilaClockManager() { +    setDefaultCloseOperation(EXIT_ON_CLOSE); +    setTitle("Utilaclock"); +    setResizable(false); +    setLayout(new GridLayout(0, 1)); + +    addWindowListener(new WindowAdapter() { +      public void windowClosed(final WindowEvent the_event) { +        for (Frame f : getFrames()) { +          f.dispose(); +        } +      } +    }); +     +    my_timer_window = new UtilaclockClockDisplay(); +     +    setupButtons(); + +    add(getCountdownUI()); + +    add(getOptionsUI()); + +    pack(); +  } +   +  /** +   * Creates the row of buttons and their listeners. +   *  +   * @return JPanel with buttons. +   */ +  public void setupButtons() { +    final JPanel buttons = new JPanel(new FlowLayout()); + +    final JButton clockButton = new JButton("Create Clock"); +    clockButton.addActionListener(new ActionListener() { +      public void actionPerformed(final ActionEvent the_event) { +        try { +          final float size = Float.parseFloat(my_font_size.getText()); +           +          final UtilaclockClockDisplay clock = new UtilaclockClockDisplay(); +          clock.setSize(size); +          clock.setVisible(true); +          clock.start(); +         +        } catch (final NumberFormatException nf_exception) { +          JOptionPane.showMessageDialog(null, "Invalid Font Size."); +        } +      } +    }); + +    buttons.add(clockButton); + +    final JButton countup_button = new JButton("Create Timer"); +    countup_button.addActionListener(new ActionListener() { +      public void actionPerformed(final ActionEvent the_event) { +        try { +          final float size = Float.parseFloat(my_font_size.getText()); +           +          final UtilaclockClockDisplay clock = new UtilaclockClockDisplay(new Date()); +          clock.setSize(size); +          clock.setVisible(true); +          clock.start(); +           +        } catch (final NumberFormatException nf_exception) { +          JOptionPane.showMessageDialog(null, "Invalid Font Size."); +        } +      } +    }); + +    final JButton countdown_button = new JButton("Create Countdown"); +    countdown_button.addActionListener(new ActionListener() { +      public void actionPerformed(final ActionEvent the_event) { +        final DateFormat df = DateFormat.getDateTimeInstance(); + +        try { +          final Date d = df.parse(my_end_time.getText()); +          final float size = Float.parseFloat(my_font_size.getText()); +           +          final UtilaclockClockDisplay clock = new UtilaclockClockDisplay(d); +          clock.setSize(size); +          clock.setVisible(true); +          clock.start(); + +        } catch (final ParseException exception) { +          JOptionPane.showMessageDialog(null, "Invalid Date."); +         +        } catch (final NumberFormatException nf_exception) { +          JOptionPane.showMessageDialog(null, "Invalid Font Size."); +        } +      } +    }); +     +    final JButton about = new JButton("About"); +    about.addActionListener(new ActionListener() { +      public void actionPerformed(final ActionEvent the_event) { +        JOptionPane.showMessageDialog(UtilaClockManager.this, "Created by Jesse Morgan <jesse@jesterpm.net>.\n Version: $Id: UtilaClockManager.java 57 2010-10-17 05:40:34Z jesse $"); +      } +    }); + +     +     +    buttons.add(countdown_button); + +    buttons.add(countup_button); +     +    buttons.add(about); + +    add(buttons); +     +    final JPanel row2 = new JPanel(new FlowLayout(FlowLayout.LEFT)); +    row2.add(new JLabel("Time Time (seconds): ")); +     +    my_timer_time = new JTextField("90"); +    row2.add(my_timer_time); +     +    final JCheckBox cbox = new JCheckBox("Enable Timer"); +    cbox.addActionListener(new ActionListener() { +       +      public void actionPerformed(final ActionEvent the_event) { +        if (((JCheckBox) the_event.getSource()).isSelected()) { +          try { +            final int time = Integer.parseInt(my_timer_time.getText()); +             +            Date d = new Date(System.currentTimeMillis() + 1000 * time); +             +            my_timer_window.setFinalTime(d); +            my_timer_window.start(); +            my_timer_window.setVisible(true); +           +          } catch (final NumberFormatException the_e) { +            JOptionPane.showMessageDialog(null, "You must enter an integer."); +          } +           +        } else { +          my_timer_window.stop(); +        } +         +      } +    }); +     +    row2.add(cbox); +     +    final JButton timer_button = new JButton("Show Timer"); +    timer_button.addActionListener(new ActionListener() { +      public void actionPerformed(final ActionEvent the_event) { +        try { +          final int time = Integer.parseInt(my_timer_time.getText()); +          my_timer_window.setFinalTime(new Date(System.currentTimeMillis() + 1000 * time)); +          my_timer_window.setVisible(true); +         +        } catch (final NumberFormatException the_e) { +          JOptionPane.showMessageDialog(null, "You must enter an integer."); +        } +      } +    }); +     +    row2.add(timer_button); +     +    add(row2); +  } + +  /** +   * Creates the Countdown Configuration UI. +   *  +   * @return JPanel with Countdown configuration UI +   */ +  private JPanel getCountdownUI() { +    final JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT)); +    panel.setBorder(BorderFactory.createTitledBorder("Count Down")); + +    final DateFormat df = DateFormat.getDateTimeInstance(); + +    panel.add(new JLabel("Count down ends at:")); + +    my_end_time = new JTextField(); +    my_end_time.setText(df.format(new Date())); +    panel.add(my_end_time); + +    return panel; +  } + +  /** +   * Creates the general options UI. +   *  +   * @return JPanel with general options. +   */ +  private JPanel getOptionsUI() { +    final JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT)); +    panel.setBorder(BorderFactory.createTitledBorder("Options")); + +    panel.add(new JLabel("Font size:")); + +    my_font_size = new JTextField(); +    my_font_size.setText("200"); +    panel.add(my_font_size); + +    panel.add(new JLabel("pt")); + +    return panel; +  } +   +   +   +  /** +   * Program Entry-point. +   *  +   * @param the_args Command line arguments +   */ +  public static void main(final String[] the_args) { +    final UtilaClockManager manager = new UtilaClockManager(); + +    manager.setVisible(true); +  } +} diff --git a/src/net/jesterpm/utilaclock/UtilaclockClockDisplay.java b/src/net/jesterpm/utilaclock/UtilaclockClockDisplay.java new file mode 100644 index 0000000..7e59bf8 --- /dev/null +++ b/src/net/jesterpm/utilaclock/UtilaclockClockDisplay.java @@ -0,0 +1,234 @@ +/* + * Utilaclock -- Simple fullscreen clock app Jesse Morgan <jesse@jesterpm.net> + */ + +package net.jesterpm.utilaclock; + +import java.awt.Color; +import java.awt.FlowLayout; +import java.awt.Font; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.text.DateFormatSymbols; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.TimeZone; + +import javax.swing.ImageIcon; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.SwingConstants; +import javax.swing.Timer; + +/** + * Clock display. + *  + * @author jesse + * @version 25 Oct 2009 + */ +public class UtilaclockClockDisplay extends JFrame { +  // Private Constants +  /** +   * Serial UID. +   */ +  private static final long serialVersionUID = -2788602417853170710L; + +  /** +   * How often do we update the clock. +   */ +  private static final int UPDATE_FREQ = 500; +   +  /** +   * Clock Color. +   */ +  private static final int CLOCK_COLOR = 0xFFFFFFFF; +   +  /** +   * Countdown Color. +   */ +  private static final int COUNT_DOWN_COLOR = 0xFF00FF00; +   +  /** +   * Count up Color. +   */ +  private static final int COUNT_UP_COLOR = 0xFF0000FF; +   +  /** +   * String representing GMT. +   */ +  private static final String TIMEZONE_GMT = "GMT"; +   +  // Instance fields +  /** +   * The label containing the time. +   */ +  private JLabel my_label; +   +  /** +   * Snowman. +   */ +  private JLabel my_snowman; + +  /** +   * End time for countdowns. +   */ +  private Date my_final_time; + +  /** +   * Our timer. +   */ +  private Timer my_timer; + +  /** +   * Constructor to setup a clock. +   */ +  public UtilaclockClockDisplay() { +    my_final_time = null; +    setupClock(); +  } + +  /** +   * Constructor to setup a countdown clock. +   *  +   * @param the_final_time The end time for this countdown. +   */ +  public UtilaclockClockDisplay(final Date the_final_time) { +    my_final_time = the_final_time; +    setupClock(); +  } +   +  /** +   * Change the final countdown time. +   *  +   * @param the_final_time The end time. +   */ +  public void setFinalTime(final Date the_final_time) { +    my_final_time = the_final_time; +    updateClock(); +  } +   +  /** +   * Enable the countdown. +   */ +  public void start() { +    my_timer.start(); +  } +   +  /** +   * Disable the countdown. +   */ +  public void stop() { +    my_timer.stop();  +  } +   +  /** +   * Set the size of the font on the clock. +   * @param the_size The font size. +   */ +  public void setSize(final float the_size) { +    final Font font = my_label.getFont().deriveFont(the_size); +    my_label.setFont(font); +    pack(); +  } + +  /** +   * Utility to setup the clock window and start it going. +   */ +  private void setupClock() { +    // Default close opperation and background color +    setDefaultCloseOperation(DISPOSE_ON_CLOSE); +    setBackground(new Color(0)); +    setLayout(new FlowLayout()); + +    addWindowListener(new WindowAdapter() { +      public void windowClosed(final WindowEvent the_event) { +        my_timer.stop(); +      } +    }); +     +    addMouseListener(new MouseAdapter() { +      public void mouseClicked(final MouseEvent the_event) { +        if (the_event.getClickCount() == 2) { +          my_snowman.setVisible(!my_snowman.isVisible()); +        } +      } +    }); + +    // Create our label +    my_label = new JLabel(); + +    // Set the font and color and such +    final Font font = new Font("Arial Black", Font.PLAIN, 200); +    my_label.setForeground(new Color(CLOCK_COLOR)); +    my_label.setFont(font); +    my_label.setHorizontalAlignment(SwingConstants.CENTER); +    my_label.setVerticalAlignment(SwingConstants.CENTER); + +    // Add the label to the window +    add(my_label); +     +    // Snowman? +    my_snowman = new JLabel( +              new ImageIcon(UtilaclockClockDisplay.class.getResource("/resources/snowman.gif"))); +     +    add(my_snowman); +     +    my_snowman.setVisible(false); + +    // Update the clock before packing +    updateClock(); + +    // Schedule Updates +    my_timer = new Timer(UPDATE_FREQ, new ActionListener() { +      public void actionPerformed(final ActionEvent the_event) { +        updateClock(); +      } +    }); + +    // and pack. +    pack(); +  } + +  /** +   * Update the clock to the correct time. +   */ +  private void updateClock() { +    final Calendar cal = Calendar.getInstance(); +    SimpleDateFormat df = new SimpleDateFormat("hh:mm:ss"); + +    // If this is a count down, we do some extra work +    if (my_final_time != null) { +      df = new SimpleDateFormat("HH:mm:ss"); +       +      // First off, we switch to GMT to solve timezone problems. +      df.setTimeZone(TimeZone.getTimeZone(TIMEZONE_GMT)); +      cal.setTimeZone(TimeZone.getTimeZone(TIMEZONE_GMT)); + +      // Get the difference between the final time and now +      final long timediff = my_final_time.getTime() - cal.getTimeInMillis(); + +      if (timediff > 0) { +        // We're counting down, set the time +        cal.setTimeInMillis(timediff); +        // and make the color green +        my_label.setForeground(new Color(COUNT_DOWN_COLOR)); + +      } else { +        // We're counting up, so set the time to the negative of the time +        // difference +        cal.setTimeInMillis(-timediff); +        // And color it blue +        my_label.setForeground(new Color(COUNT_UP_COLOR)); +      } +    } + +    // Set the label as needed +    my_label.setText(df.format(cal.getTime())); +  } +}  | 
