📄 preferencesdialog.java
字号:
/*
* Created on Apr 26, 2006
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package org.GTADS.client.preferences;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.border.Border;
import org.GTADS.client.DialogManager;
import org.GTADS.usermanager.TranslateManager;
import org.GTADS.client.swing.*;
;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class PreferencesDialog extends GTADSFrame implements ActionListener, KeyListener {
private static PreferencesDialog instance;
private GTADSPanel preferencesPanel = new GTADSPanel();
private GTADSPanel internationalPanel = new GTADSPanel();
private GTADSPanel chatroomPanel = new GTADSPanel();
private GTADSPanel proxyPanel = new GTADSPanel();
private GTADSPanel userPanel = new GTADSPanel();
private GTADSComboBox languageSettings = new GTADSComboBox();
private GTADSComboBox geoLocation = new GTADSComboBox();
private GTADSTextField defaultUserName = new GTADSTextField();
private GTADSPasswordField defaultPassword = new GTADSPasswordField();
private GTADSCheckBox isPasswordSaved = new GTADSCheckBox();
private GTADSCheckBox isTimeStamps = new GTADSCheckBox();
private GTADSTextField defaultChatroom = new GTADSTextField();
private GTADSTextField proxyPortField = new GTADSTextField();
private GTADSButton okButton = new GTADSButton();
private GTADSButton cancelButton = new GTADSButton();
private GTADSButton applyButton = new GTADSButton();
private boolean isChanged = false;
public static PreferencesDialog getInstance(){
if (instance == null){
instance = new PreferencesDialog();
}
return instance;
}
public static void clearInstance(){
if (instance != null){
instance = null;
}
}
PreferencesDialog(){
this.setTitle("Preferences");
createObjects();
createPanels();
this.setContentPane(preferencesPanel);
refreshSettings();
this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e){
cancel();
}
});
pack();
}
private void createObjects() {
languageSettings.addItem(PreferencesManager.getInstance().getValue("en"));
languageSettings.addItem(PreferencesManager.getInstance().getValue("es"));
languageSettings.addItem(PreferencesManager.getInstance().getValue("de"));
languageSettings.addItem(PreferencesManager.getInstance().getValue("fr"));
languageSettings.addActionListener(this);
geoLocation.addItem("US");
geoLocation.addActionListener(this);
defaultUserName.setColumns(10);
defaultPassword.setColumns(10);
defaultUserName.addKeyListener(this);
defaultPassword.addKeyListener(this);
isPasswordSaved.setText(TranslateManager.getInstance().printLocale("Save Password"));
isTimeStamps.setText(TranslateManager.getInstance().printLocale("TimeStamps"));
isPasswordSaved.addActionListener(this);
isTimeStamps.addActionListener(this);
proxyPortField.setColumns(5);
defaultChatroom.setColumns(8);
proxyPortField.addKeyListener(this);
defaultChatroom.addKeyListener(this);
okButton.setText(TranslateManager.getInstance().printLocale("Ok"));
applyButton.setText(TranslateManager.getInstance().printLocale("Apply"));
cancelButton.setText(TranslateManager.getInstance().printLocale("Cancel"));
okButton.addActionListener(this);
applyButton.addActionListener(this);
cancelButton.addActionListener(this);
}
private void createPanels(){
createInternationalPanel();
createUserPanel();
createChatroomPanel();
createProxyPanel();
GTADSPanel buttonPanel = new GTADSPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
buttonPanel.add(okButton);
buttonPanel.add(applyButton);
buttonPanel.add(cancelButton);
preferencesPanel.setLayout(new BoxLayout(preferencesPanel, BoxLayout.Y_AXIS));
preferencesPanel.add(internationalPanel);
preferencesPanel.add(userPanel);
preferencesPanel.add(chatroomPanel);
preferencesPanel.add(proxyPanel);
preferencesPanel.add(Box.createVerticalStrut(10));
preferencesPanel.add(buttonPanel);
}
private void createInternationalPanel(){
internationalPanel.setLayout(new BoxLayout(internationalPanel,BoxLayout.Y_AXIS));
Border b = BorderFactory.createTitledBorder(BorderFactory.createBevelBorder(1, Color.WHITE, Color.WHITE),
TranslateManager.getInstance().printLocale("International Settings"),0,0,this.getFont(),Color.WHITE);
internationalPanel.setBorder(b);
GTADSPanel iPanel1 = new GTADSPanel();
iPanel1.setLayout(new FlowLayout(FlowLayout.LEFT));
iPanel1.add(new GTADSLabel(TranslateManager.getInstance().printLocale("Language") + ": "));
iPanel1.add(languageSettings);
GTADSPanel iPanel2 = new GTADSPanel();
iPanel2.setLayout(new FlowLayout(FlowLayout.LEFT));
iPanel2.add(new GTADSLabel(TranslateManager.getInstance().printLocale("Country") + ": "));
iPanel2.add(geoLocation);
internationalPanel.add(iPanel1);
internationalPanel.add(iPanel2);
}
private void createUserPanel(){
userPanel.setLayout(new BoxLayout(userPanel, BoxLayout.Y_AXIS));
Border b = BorderFactory.createTitledBorder(BorderFactory.createBevelBorder(1, Color.WHITE, Color.WHITE),
TranslateManager.getInstance().printLocale("User Settings"),0,0,this.getFont(),Color.WHITE);
userPanel.setBorder(b);
GTADSPanel uPanel1 = new GTADSPanel();
GTADSPanel uPanel2 = new GTADSPanel();
String passwordString = TranslateManager.getInstance().printLocale("Password") + ": ";
String usernameString = TranslateManager.getInstance().printLocale("Username") + ": ";
int largerWord = usernameString.length() > passwordString.length() ? usernameString.length() : passwordString.length();
int usernameLen = usernameString.length();
int passwordLen = passwordString.length();
String fillerString = new String();
for (int i = 0; i < largerWord - usernameLen; i++){
fillerString += " ";
}
uPanel1.setLayout(new FlowLayout(FlowLayout.LEFT));
uPanel1.add(new GTADSLabel(usernameString + fillerString));
uPanel1.add(defaultUserName);
fillerString = "";
for (int i = 0; i < largerWord - passwordLen; i++){
fillerString += " ";
}
uPanel2.setLayout(new FlowLayout(FlowLayout.LEFT));
uPanel2.add(new GTADSLabel(passwordString + fillerString));
uPanel2.add(defaultPassword);
uPanel2.add(Box.createHorizontalStrut(10));
uPanel2.add(isPasswordSaved);
userPanel.add(uPanel1);
userPanel.add(uPanel2);
}
private void createChatroomPanel(){
chatroomPanel.setLayout(new BoxLayout(chatroomPanel, BoxLayout.Y_AXIS));
Border b = BorderFactory.createTitledBorder(BorderFactory.createBevelBorder(1, Color.WHITE, Color.WHITE),
TranslateManager.getInstance().printLocale("Chatroom Settings"),0,0,this.getFont(),Color.WHITE);
chatroomPanel.setBorder(b);
GTADSPanel cPanel1 = new GTADSPanel();
GTADSPanel cPanel2 = new GTADSPanel();
cPanel1.setLayout(new FlowLayout(FlowLayout.LEFT));
cPanel1.add(isTimeStamps);
cPanel2.setLayout(new FlowLayout(FlowLayout.LEFT));
cPanel2.add(new GTADSLabel(TranslateManager.getInstance().printLocale("Default Chatroom") + ": "));
cPanel2.add(defaultChatroom);
chatroomPanel.add(cPanel1);
chatroomPanel.add(cPanel2);
}
private void createProxyPanel() {
proxyPanel.setLayout(new BoxLayout(proxyPanel, BoxLayout.Y_AXIS));
Border b = BorderFactory.createTitledBorder(BorderFactory.createBevelBorder(1, Color.WHITE, Color.WHITE),
TranslateManager.getInstance().printLocale("Proxy Settings"),0,0,this.getFont(),Color.WHITE);
proxyPanel.setBorder(b);
GTADSPanel pPanel1 = new GTADSPanel();
pPanel1.setLayout(new FlowLayout(FlowLayout.LEFT));
pPanel1.add(new GTADSLabel(TranslateManager.getInstance().printLocale("Proxy Port: ")));
pPanel1.add(proxyPortField);
proxyPanel.add(pPanel1);
}
public void refreshSettings(){
// Refresh settings to respected components
// May need to be triggered from outside classes
// Language
// TODO: Make a less hardcoded solution to the following:
String languageSetting = PreferencesManager.getInstance().getValue(PreferencesManager.getInstance().getLocale());
if (!languageSetting.equals("")){
this.languageSettings.setSelectedItem(languageSetting);
}
// TODO: Set up country locations
this.defaultUserName.setText(PreferencesManager.getInstance().getDefaultUsername());
if (PreferencesManager.getInstance().getPasswordSaved()){
this.isPasswordSaved.setSelected(true);
this.defaultPassword.setText(PreferencesManager.getInstance().getDefaultPassword());
}
else {
this.isPasswordSaved.setSelected(false);
}
this.isTimeStamps.setSelected(PreferencesManager.getInstance().getTimeStamps());
this.defaultChatroom.setText(PreferencesManager.getInstance().getDefaultChatroom());
this.proxyPortField.setText(PreferencesManager.getInstance().getProxyPort() + "");
}
public boolean save() {
// Steps here:
// Check if anything is modified
// Validate settings
// Write to file
// Refresh
if (isChanged){
if (!validateData()){
return false;
}
else {
// Intl Settings
PreferencesManager.getInstance().setValue("language", PreferencesManager.getInstance().getKey(this.languageSettings.getSelectedItem().toString()));
PreferencesManager.getInstance().setValue("country", this.geoLocation.getSelectedItem().toString());
// User Settings
PreferencesManager.getInstance().setValue("username", this.defaultUserName.getText());
PreferencesManager.getInstance().setValue("password", this.defaultPassword.getText());
PreferencesManager.getInstance().setValue("save password", this.isPasswordSaved.isSelected() ? "yes" : "no");
// Chatroom Settings
PreferencesManager.getInstance().setValue("timestamps", this.isTimeStamps.isSelected() ? "yes" : "no");
PreferencesManager.getInstance().setValue("Default room", this.defaultChatroom.getText());
// Proxy Settings
PreferencesManager.getInstance().setValue("proxy port", this.proxyPortField.getText());
// Write it to file
if (!PreferencesManager.getInstance().writeOutPreferences()){
DialogManager.ErrorDialog("Error writing to config file");
}
refreshSettings();
isChanged = false;
}
}
return true;
}
private boolean validateData() {
boolean isValid = true;
if (this.isPasswordSaved.isSelected()){
if (this.defaultPassword.getText().equals("")){
// Error here: No Password
DialogManager.ErrorDialog("Cannot save blank password");
isValid = false;
}
}
if (!this.defaultPassword.getText().equals("") && this.defaultUserName.getText().equals("")){
// Error here: No username with password
DialogManager.ErrorDialog("No username specified for entered password");
isValid = false;
}
if (!this.defaultChatroom.getText().startsWith("#")){
// Error here: Invalid Default Chatroom
DialogManager.ErrorDialog("Invalid chatroom entry.\n All chatrooms must start with #");
isValid = false;
}
if (!this.proxyPortField.getText().equals("")){
try {
Integer proxyRange = Integer.valueOf(proxyPortField.getText());
if (!(proxyRange.intValue() > 0 && proxyRange.intValue() <= 65535)){
// Error here: Proxy Port must be between 1 and 65535
DialogManager.ErrorDialog("Proxy port must be between 1 and 65535");
isValid = false;
}
} catch(NumberFormatException nfe){
// Error here: Invalid Entry
DialogManager.ErrorDialog("Invalid Entry: Proxy port must be a numeric value");
isValid = false;
}
}
if (this.proxyPortField.equals("")){
// Error here: Port number blank
DialogManager.ErrorDialog("Proxy Port not set");
isValid = false;
}
return isValid;
}
public void cancel(){
if (isChanged){
int option = JOptionPane.showConfirmDialog(null,
"Save Changes", "Would you like to save changes made?", JOptionPane.YES_NO_OPTION);
if (option == JOptionPane.YES_OPTION){
if (!save()){
return;
}
}
}
refreshSettings();
hide();
applyButton.setEnabled(false);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(this.okButton) || e.getSource().equals(this.applyButton)
|| e.getSource().equals(this.cancelButton)){
if (e.getSource().equals(cancelButton)){
cancel();
}
else {
if (!save()){
return;
}
if (e.getSource().equals(this.okButton)){
this.hide();
}
applyButton.setEnabled(false);
}
}
else {
isChanged = true;
this.applyButton.setEnabled(true);
}
}
public void keyPressed(KeyEvent arg0) {
isChanged = true;
this.applyButton.setEnabled(true);
}
public void keyReleased(KeyEvent arg0) {
isChanged = true;
this.applyButton.setEnabled(true);
}
public void keyTyped(KeyEvent arg0) {
isChanged = true;
this.applyButton.setEnabled(true);
}
public static void main(String args[]){
PreferencesDialog.getInstance().show();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -