public GeneralPanel(PreferencesDialog parent) {
super(parent, Translator.get("prefs_dialog.general_tab"));
setLayout(new BorderLayout());
YBoxPanel mainPanel = new YBoxPanel();
JPanel tempPanel;
// Language
JPanel languagePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
languagePanel.setBorder(BorderFactory.createTitledBorder(Translator.get("prefs_dialog.language")));
this.languages = Translator.getAvailableLanguages();
String currentLang = MuConfigurations.getPreferences().getVariable(MuPreference.LANGUAGE);
String lang;
languageComboBox = new PrefComboBox() {
public boolean hasChanged() {
return !languages[getSelectedIndex()].equals(MuConfigurations.getPreferences().getVariable(MuPreference.LANGUAGE));
}
};
// Use a custom combo box renderer to display language icons
class LanguageComboBoxRenderer extends BasicComboBoxRenderer {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
JLabel label = (JLabel)super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
String language = (String)value;
label.setText(Translator.get("language."+language));
label.setIcon(IconManager.getIcon(IconManager.LANGUAGE_ICON_SET, language+".png"));
return label;
}
}
languageComboBox.setRenderer(new LanguageComboBoxRenderer());
// Add combo items and select current language (defaults to EN if current language can't be found)
int languageIndex = -1;
for(int i=0; i<languages.length; i++) {
lang = languages[i];
languageComboBox.addItem(lang);
if(lang.equalsIgnoreCase(currentLang))
languageIndex = i;
else if(languageIndex==-1 && lang.equalsIgnoreCase("en"))
languageIndex = i;
}
languageComboBox.setSelectedIndex(languageIndex);
languagePanel.add(languageComboBox);
mainPanel.add(languagePanel);
mainPanel.addSpace(10);
// Date & time format panel
YBoxPanel dateTimeFormatPanel = new YBoxPanel();
dateTimeFormatPanel.setBorder(BorderFactory.createTitledBorder(Translator.get("prefs_dialog.date_time")));
JPanel gridPanel = new JPanel(new GridLayout(1, 2));
YBoxPanel dateFormatPanel = new YBoxPanel();
dateFormatPanel.setBorder(BorderFactory.createTitledBorder(Translator.get("prefs_dialog.date")));
// Date format combo
dateFormatComboBox = new PrefComboBox() {
public boolean hasChanged() {
return !getDateFormatString().equals(MuConfigurations.getPreferences().getVariable(MuPreference.DATE_FORMAT));
}
};
String dateFormat = MuConfigurations.getPreferences().getVariable(MuPreference.DATE_FORMAT);
String separator = MuConfigurations.getPreferences().getVariable(MuPreference.DATE_SEPARATOR, MuPreferences.DEFAULT_DATE_SEPARATOR);
int dateFormatIndex = 0;
String buffer = dateFormat.replace(separator.charAt(0), '/');
for(int i=0; i<DATE_FORMATS.length; i++) {
dateFormatComboBox.addItem(DATE_FORMAT_LABELS[i]);
if(buffer.equals(DATE_FORMATS[i]) || buffer.equals(DATE_FORMATS_WITH_CENTURY[i]))
dateFormatIndex = i;
}
dateFormatComboBox.setSelectedIndex(dateFormatIndex);
dateFormatComboBox.addItemListener(this);
tempPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
tempPanel.add(dateFormatComboBox);
tempPanel.add(Box.createHorizontalGlue());
dateFormatPanel.add(tempPanel);
// Date separator field
tempPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
tempPanel.add(new JLabel(Translator.get("prefs_dialog.date_separator")+": "));
dateSeparatorField = new PrefTextField(1) {
public boolean hasChanged() {
return !getText().equals(MuConfigurations.getPreferences().getVariable(MuPreference.DATE_SEPARATOR));
}
};
// Limit the number of characters in the text field to 1 and enforces only non-alphanumerical characters
PlainDocument doc = new PlainDocument() {
@Override
public void insertString(int param, String str, javax.swing.text.AttributeSet attributeSet) throws javax.swing.text.BadLocationException {
// Limit field to 1 character max
if (str != null && this.getLength() + str.length() > 1)
return;
// Reject letters and digits, as they don't make much sense,
// plus letters would be misinterpreted by SimpleDateFormat
if (str != null) {
int len = str.length();
for(int i=0; i<len; i++)
if(Character.isLetterOrDigit(str.charAt(i)))
return;
}
super.insertString(param, str, attributeSet);
}
};
dateSeparatorField.setDocument(doc);
dateSeparatorField.setText(separator);
doc.addDocumentListener(this);
tempPanel.add(dateSeparatorField);
tempPanel.add(Box.createHorizontalGlue());
dateFormatPanel.add(tempPanel);
showCenturyCheckBox = new PrefCheckBox(Translator.get("prefs_dialog.show_century")) {
public boolean hasChanged() {
return isSelected() != (MuConfigurations.getPreferences().getVariable(MuPreference.DATE_FORMAT).indexOf("yyyy")!=-1);
}
};
showCenturyCheckBox.setSelected(dateFormat.indexOf("yyyy")!=-1);
showCenturyCheckBox.addItemListener(this);
dateFormatPanel.add(showCenturyCheckBox);
dateFormatPanel.addSpace(10);
gridPanel.add(dateFormatPanel);
// Time format
YBoxPanel timeFormatPanel = new YBoxPanel();
timeFormatPanel.setBorder(BorderFactory.createTitledBorder(Translator.get("prefs_dialog.time")));
time12RadioButton = new PrefRadioButton(Translator.get("prefs_dialog.time_12_hour")) {
public boolean hasChanged() {
String timeFormat = MuConfigurations.getPreferences().getVariable(MuPreference.TIME_FORMAT);
return isSelected() != (timeFormat.equals(HOUR_12_TIME_FORMAT) || timeFormat.equals(HOUR_12_TIME_FORMAT_WITH_SECONDS));
}
};
time12RadioButton.addActionListener(this);
PrefRadioButton time24RadioButton = new PrefRadioButton(Translator.get("prefs_dialog.time_24_hour")) {
public boolean hasChanged() {
String timeFormat = MuConfigurations.getPreferences().getVariable(MuPreference.TIME_FORMAT);
return isSelected() != (timeFormat.equals(HOUR_24_TIME_FORMAT) || timeFormat.equals(HOUR_24_TIME_FORMAT_WITH_SECONDS));
}
};
time24RadioButton.addActionListener(this);
String timeFormat = MuConfigurations.getPreferences().getVariable(MuPreference.TIME_FORMAT);
if(timeFormat.equals(HOUR_12_TIME_FORMAT) || timeFormat.equals(HOUR_12_TIME_FORMAT_WITH_SECONDS))
time12RadioButton.setSelected(true);
else
time24RadioButton.setSelected(true);
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(time12RadioButton);
buttonGroup.add(time24RadioButton);
timeFormatPanel.add(time12RadioButton);
timeFormatPanel.add(time24RadioButton);
timeFormatPanel.addSpace(10);
showSecondsCheckBox = new PrefCheckBox(Translator.get("prefs_dialog.show_seconds")) {
public boolean hasChanged() {
return isSelected() != (MuConfigurations.getPreferences().getVariable(MuPreference.TIME_FORMAT).indexOf(":ss")!=-1);
}
};
showSecondsCheckBox.setSelected(timeFormat.indexOf(":ss")!=-1);
showSecondsCheckBox.addItemListener(this);
timeFormatPanel.add(showSecondsCheckBox);
timeFormatPanel.addSpace(10);
gridPanel.add(timeFormatPanel);
dateTimeFormatPanel.add(gridPanel);
// Date/time preview