📄 localeconfiguration.java
字号:
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* Created on 2006/09/04
*
* @Author: Xiaojun Chen
* $Revision$ 1.0
*
*/
package eti.bi.alphaminer.Locale;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.border.CompoundBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import eti.bi.alphaminer.core.observer.Observer;
import eti.bi.alphaminer.ui.dialog.MessageDialog;
import eti.bi.common.Locale.Resource;
import eti.bi.common.Locale.locale;
public class LocaleConfiguration extends JDialog implements Observer{
/**
*
*/
private static final long serialVersionUID = 1L;
private LocaleListTree LocaleListTree;
private LocaleInformation LocaleInformation;
private int current;
private boolean isNew;
private boolean farFromTree;
public LocaleConfiguration() {
createUI();
}
private void createUI() {
LocaleListTree = new LocaleListTree(this);
LocaleInformation = new LocaleInformation(this);
LocaleInformation.setPreferredSize(new Dimension(300, 260));
JScrollPane list = new JScrollPane();
list.getViewport().add(LocaleListTree);
list.setPreferredSize(new Dimension(200, 260));
this.add(list, BorderLayout.CENTER);
this.add(LocaleInformation, BorderLayout.EAST);
this.setAlwaysOnTop(true);
this.setModal(true);
this.setResizable(false);
this.setSize(new Dimension(500, 260));
this.setTitle(Resource.srcStr("LocalesConfiguration"));
init();
}
private void init() {
Resource.registerInterest(this);
setCurrent(0);
}
public locale getCurrentLocale() {
return LocalesManager.getLocale(current);
}
public locale getLocale(int index) {
return LocalesManager.getLocale(index);
}
public int getCountofLocale() {
return LocalesManager.getCountofLocales();
}
public void setFarFromTree(boolean farFromTree) {
this.farFromTree = farFromTree;
}
/**
* return if set current Locale succeed
*/
public boolean setCurrent(int index) {
if (!askforNext()) {
return false;
}
current = index;
LocaleInformation.getContent();
return true;
}
/**
* set new Locale which hasn't been saved return if set current Locale
* succeed
*/
public boolean setCurrent(int index, locale Locale) {
current = index;
LocaleInformation.getContent(Locale);
return true;
}
public int getCurrentIndex() {
return current;
}
public boolean isNew() {
return isNew;
}
@SuppressWarnings("deprecation")
public boolean ImportLocale() {
if (!askforNext()) {
return false;
}
// set location of new Locale
int index = current;
if (farFromTree) {
index = LocalesManager.getCountofLocales();
}
// TODO add
locale Locale;
try {
Locale = LocalesManager.ImportLocale(LocaleInformation);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
if(Locale==null) {
return false;
}
if(LocalesManager.hasLocale(Locale)) {
MessageDialog.showMessageDialog(this, "This locale has been imported!", Resource.srcStr("Import_Locale_error"), MessageDialog.ERROR_MESSAGE);
return false;
}
LocaleListTree.addLocale(Locale.getName(), index);
isNew = true;
setCurrent(index, Locale);
return true;
}
public boolean deleteLocale() {
if(LocalesManager.getCountofLocales()==1) {
MessageDialog.showMessageDialog(this, "Only one locale left!\nCan't delete all!");
return false;
}
if (!LocalesManager.deleteLocale(current)) {
return false;
}
if (!LocaleListTree.removeLocale(current)) {
return false;
}
// set new current
if (current >= LocalesManager.getCountofLocales()) {
current--;
}
if (current >= 0) {
return setCurrent(current);
} else {
return LocaleInformation.clear();
}
}
public boolean saveLocale() {
locale Locale = LocaleInformation.getCurrentLocale();
if (!Locale.isvalid()) {
MessageDialog.showMessageDialog(this, Resource.srcStr("wrong_Locale"));
return false;
}
if(!isNew) {
return false;
}
if (!LocalesManager.addLocale(current, Locale,LocaleInformation)) {
return false;
}
if(!LocaleListTree.updateLocale(current, Locale.getName())) {
return false;
}
isNew = false;
return true;
}
/**
* @see java.awt.Window#processWindowEvent(WindowEvent)
*/
protected void processWindowEvent(WindowEvent e) {
if(e.getID()==WindowEvent.WINDOW_CLOSING){
close();
}
}
@SuppressWarnings("deprecation")
public void close() {
if (askforNext()) {
LocalesManager.configurationExit();
this.hide();
}
}
/**
* return if go to next Locale
*/
public boolean askforNext() {
int option;
if (isNew) {
option = MessageDialog.showConfirmDialog(this, Resource.srcStr("Locale_saveNew"));
} else {
return true;
}
if (option == MessageDialog.YES_OPTION) {
return saveLocale();
} else if (option == MessageDialog.NO_OPTION) {
return true;
} else if (option == MessageDialog.CANCEL_OPTION) {
return false;
} else {
return false;
}
}
// clear the Locale information
public void clear() {
LocaleInformation.clear();
}
public void sendNotify(String a_Message) {
if(a_Message.equals(Resource.CHANGE_LOCALE)) {
this.setTitle(Resource.srcStr("LocalesConfiguration"));
LocaleInformation.resetLocale();
LocaleListTree.resetLocale();
}
}
public void sendNotify(int a_Message) {
// TODO Auto-generated method stub
}
public void sendNotify(int a_Message, Object a_Object) {
// TODO Auto-generated method stub
}
}
class LocaleListTree extends JTree implements TreeSelectionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private LocaleConfiguration Localeconfiguration;
private DefaultMutableTreeNode m_RootNode;
private DefaultTreeModel m_Model;
private LocalePopupMenu Localepopupmenu;
LocaleListTree(LocaleConfiguration Localeconfiguration) {
this.Localeconfiguration = Localeconfiguration;
m_RootNode = new DefaultMutableTreeNode("Locale");
m_Model = new DefaultTreeModel(null);
this.setModel(m_Model);
this.setRootVisible(false);
this.setShowsRootHandles(true);
TreeSelectionModel selectionModel = this.getSelectionModel();
selectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
this.setSelectionModel(selectionModel);
this.addTreeSelectionListener(this);
buildTree();
addMouseListener(TreeMouseListener());
init();
Localepopupmenu = new LocalePopupMenu(Localeconfiguration);
}
public void resetLocale() {
Localepopupmenu.resetLocale();
}
public boolean updateLocale(int current, String name) {
LocaleTreeNode updateLocale = new LocaleTreeNode(name);
m_RootNode.remove(current);
m_RootNode.insert(updateLocale, current);
m_Model.reload();
return true;
}
public boolean removeLocale(int current) {
try {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -