java-org.hwo/src/org/hwo/datetime/JTimeOfDay.java

163 lines
4.1 KiB
Java

package org.hwo.datetime;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JSpinner;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.util.LinkedList;
import javax.swing.JLabel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class JTimeOfDay extends JPanel {
private TimeOfDay timeOfDay;
private LinkedList<ChangeListener> changeListeners;
private JSpinner spHours;
private JSpinner spMinutes;
private JSpinner spSeconds;
private JLabel lColon2;
private boolean hideSeconds;
public JTimeOfDay() {
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0};
gridBagLayout.rowHeights = new int[]{0, 0};
gridBagLayout.columnWeights = new double[]{1.0, 0.0, 1.0, 0.0, 1.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
setLayout(gridBagLayout);
spHours = new JSpinner();
spHours.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
timeOfDay.setHours((Integer)spHours.getValue());
fireChange();
}
});
spHours.setModel(new SpinnerNumberModel(0, 0, 23, 1));
GridBagConstraints gbc_spHours = new GridBagConstraints();
gbc_spHours.fill = GridBagConstraints.HORIZONTAL;
gbc_spHours.insets = new Insets(0, 0, 0, 5);
gbc_spHours.gridx = 0;
gbc_spHours.gridy = 0;
add(spHours, gbc_spHours);
JLabel lblNewLabel = new JLabel(":");
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.insets = new Insets(0, 0, 0, 5);
gbc_lblNewLabel.gridx = 1;
gbc_lblNewLabel.gridy = 0;
add(lblNewLabel, gbc_lblNewLabel);
spMinutes = new JSpinner();
spMinutes.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
timeOfDay.setMinutes((Integer)spMinutes.getValue());
fireChange();
}
});
spMinutes.setModel(new SpinnerNumberModel(0, 0, 59, 1));
GridBagConstraints gbc_spMinutes = new GridBagConstraints();
gbc_spMinutes.fill = GridBagConstraints.HORIZONTAL;
gbc_spMinutes.insets = new Insets(0, 0, 0, 5);
gbc_spMinutes.gridx = 2;
gbc_spMinutes.gridy = 0;
add(spMinutes, gbc_spMinutes);
lColon2 = new JLabel(":");
GridBagConstraints gbc_lColon2 = new GridBagConstraints();
gbc_lColon2.insets = new Insets(0, 0, 0, 5);
gbc_lColon2.gridx = 3;
gbc_lColon2.gridy = 0;
add(lColon2, gbc_lColon2);
spSeconds = new JSpinner();
spSeconds.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
timeOfDay.setSeconds((Integer)spSeconds.getValue());
fireChange();
}
});
spSeconds.setModel(new SpinnerNumberModel(0, 0, 59, 1));
GridBagConstraints gbc_spSeconds = new GridBagConstraints();
gbc_spSeconds.fill = GridBagConstraints.HORIZONTAL;
gbc_spSeconds.gridx = 4;
gbc_spSeconds.gridy = 0;
add(spSeconds, gbc_spSeconds);
initialize();
}
void initialize()
{
setSecondsHidden(false);
changeListeners = new LinkedList<ChangeListener>();
timeOfDay = new TimeOfDay();
}
public void addChangeListener(ChangeListener changeListener)
{
changeListeners.add(changeListener);
}
public void removeChangeListener(ChangeListener changeListener)
{
changeListeners.remove(changeListener);
}
public Boolean getSecondsHidden()
{
return this.hideSeconds;
}
public void setSecondsHidden(Boolean hideSeconds)
{
this.hideSeconds = hideSeconds;
if (hideSeconds)
{
lColon2.setVisible(false);
spSeconds.setVisible(false);
spSeconds.setValue(0);
} else
{
lColon2.setVisible(true);
spSeconds.setVisible(true);
}
this.validate();
}
void fireChange()
{
for (ChangeListener l:changeListeners)
l.stateChanged(new ChangeEvent(this));
}
void updateView()
{
spHours.setValue(timeOfDay.getHours());
spMinutes.setValue(timeOfDay.getMinutes());
spSeconds.setValue(timeOfDay.getSeonds());
}
public TimeOfDay getTimeOfDay()
{
return timeOfDay;
}
public void setTimeOfDay(TimeOfDay timeOfDay)
{
this.timeOfDay = timeOfDay;
updateView();
}
}