📄 generalizedtimeeditor.java
字号:
// Set the position of the day combo box...
dayCombo.setSelectedIndex(index);
}
catch (Exception e)
{
int items = dayCombo.getItemCount();
dayCombo.setSelectedIndex(items - 1);
}
}
/**
* Determines if given year is a leap year.
* @param year = given year after 1582 (start of the Gregorian calendar).
* @return TRUE if given year is leap year, FALSE if not.
*/
public boolean isLeapYear(int year)
{
// If multiple of 100, leap year if multiple of 400...
if ((year % 100) == 0)
return ((year % 400) == 0);
// Otherwise leap year iff multiple of 4...
return ((year % 4) == 0);
}
/**
* Returns a string representation of the year as entered in the year field.
* The year can only have four characters therefore any characters after the
* first for are ignored. If there is less than four characters the space(s)
* to the left are filled with a zero (0).
* @return the year as a string for example 2001.
*/
protected String getSelectedYear()
{
String year = yearTextField.getText();
int len = year.length();
switch (len)
{
case 0:
year = "0000";
break;
case 1:
year = "000" + year;
break;
case 2:
year = "00" + year;
break;
case 3:
year = "0" + year;
break;
case 4:
break; // year = year;
default:
year = year.substring(0, 4);
}
return year;
}
/**
* Returns a string representation of the month as entered in the month combo.
* @return the month as a string for example 01 (January).
*/
protected String getSelectedMonth()
{
int selection = monthCombo.getSelectedIndex();
String month = Integer.toString(selection);
if (selection < 10)
month = "0" + month;
return month;
}
/**
* Returns a string representation of the day as entered in the day combo.
* @return the day as a string for example 31.
*/
protected String getSelectedDay()
{
int selection = dayCombo.getSelectedIndex();
String day = Integer.toString(selection);
if (selection < 10)
day = "0" + day;
return day;
}
/**
* Returns a string representation of the hour as entered in the hour combo.
* @return the hour as a string for example 23.
*/
protected String getSelectedHour()
{
int selection = hourCombo.getSelectedIndex();
String hour = Integer.toString(selection);
if (selection < 10)
hour = "0" + hour;
return hour;
}
/**
* Returns a string representation of the minute as entered in the minute combo.
* @return the minute as a string for example 59.
*/
protected String getSelectedMinute()
{
int selection = minuteCombo.getSelectedIndex();
String minute = Integer.toString(selection);
if (selection < 10)
minute = "0" + minute;
return minute;
}
/**
* Returns a string representation of the second as entered in the second combo.
* @return the second as a string for example 59.
*/
protected String getSelectedSecond()
{
int selection = secondCombo.getSelectedIndex();
String second = Integer.toString(selection);
if (selection < 10)
second = "0" + second;
return second;
}
/**
* Returns a string representation of the milliSecond as entered in the milliSecond combo.
* @return the milliSecond as a string for example '.999', or an empty string if milliseconds equals 0.
*/
protected String getSelectedMilliSecond()
{
int selection = milliSecondCombo.getSelectedIndex();
// special handling for '0' milliseconds - some directories do not like milliseconds
// DDDDDDDDDD.mmm format, and cannot handle the decimal '.mmm' bit - to allow users to still
// get by, we'll leave it out altogether if milliseconds are unspecified.
if (selection == 0)
return "";
String milliSecond = Integer.toString(selection);
if (selection < 10)
milliSecond = "00" + milliSecond;
if (selection < 100 && selection > 9)
milliSecond = "0" + milliSecond;
return "." + milliSecond;
}
/**
* Sets the EditableString of this class to the supplied EditableString.
* @param editString
*/
public void setStringValue(editablestring editString)
{
editableString = editString;
}
/**
* Sets the changed (or unchanged) attribute value from the attribute editor in the table.
* Disposes of the dialog.
*/
public void doOK()
{
String date = getSelectedYear() + getSelectedMonth() +
getSelectedDay() + getSelectedHour() + getSelectedMinute() +
getSelectedSecond() + getSelectedMilliSecond();
// Add the Z if check box is selected...
if (checkBox.isSelected())
date = date + "Z";
if (isTableEditor)
// Sets the attribute value in the table editor to reflect the changes made...
editableString.setStringValue(date);
else
time = date;
setVisible(false);
dispose();
}
/**
* When the user hits 'cancel', the window is shut down.
* Sets the 'time' (accessable via getTime()) to the initial 'value'.
*/
public void doCancel()
{
if (!isTableEditor)
time = value;
super.doCancel();
}
/**
* Returns the time set in the GUI. This is a hack so that the Search dialog can
* use this editor for time attributes. Instead of setting the value
* via editableString.setStringValue(), a global variable should have
* been set with the date/time.
* @return the date/time set by the user.
*/
public String getTime()
{
return time;
}
/**
* Listener that monitors any insert or removal updates of a document.
* This is used on the year text field to spawn a leap year check if
* february is selected in the month combo.
* @author Trudi.
*/
class MyDocumentListener implements DocumentListener
{
/**
* Does a date check if the user inserts a value in to the document (year field).
*/
public void insertUpdate(DocumentEvent e)
{
checkDate(e);
}
/**
* Does a date check if the user removes a value from the document (year field).
*/
public void removeUpdate(DocumentEvent e)
{
checkDate(e);
}
public void changedUpdate(DocumentEvent e)
{
/* we won't ever get this with PlainDocument */
}
/**
* Triggers an update in the day combo if the month selected in the month combo is february.
* Basically it is checking that the correct days are displayed if the year is a leap or not.
*/
private void checkDate(DocumentEvent e)
{
try
{
int index = monthCombo.getSelectedIndex();
// If the month that is selected is feb, make sure the number of days
// in the day combo is correct (leap year)...
if (index == 2)
updateDayCombo();
}
catch (Exception ee)
{
log.log(Level.WARNING, "Problem getting the selected month from the drop down box in the Generalized Time editor. ", e);
}
}
}
/**
* This class extends JTextField to only allow whole
* numbers to be entered into the text field.
*/
class WholeNumberField extends JTextField
{
private NumberFormat integerFormatter;
public WholeNumberField(int value, int columns)
{
super(columns);
integerFormatter = NumberFormat.getNumberInstance(Locale.getDefault());
integerFormatter.setParseIntegerOnly(true);
integerFormatter.setGroupingUsed(false); // prevents error where formatter turns 2007 into '2,007'
setValue(value);
}
public int getValue()
{
int retVal = 0;
try
{
retVal = integerFormatter.parse(getText()).intValue();
}
catch (ParseException e)
{
// This should never happen because insertString allows
// only properly formatted data to get in the field.
}
return retVal;
}
public void setValue(int value)
{
setText(integerFormatter.format(value));
}
protected Document createDefaultModel()
{
return new WholeNumberDocument();
}
protected class WholeNumberDocument extends PlainDocument
{
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException
{
char[] source = str.toCharArray();
char[] result = new char[source.length];
int j = 0;
for (int i = 0; i < result.length; i++)
{
if (Character.isDigit(source[i]))
result[j++] = source[i];
else
log.warning("Invalid data, you can't enter '" + source[i] + "' (from " + str + ") into a year field.");
}
super.insertString(offs, new String(result, 0, j), a);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -