📄 workspaceconfiguration.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/7/11
*
* @Author: Xiaojun Chen
* $Revision$ 1.0
*
*/
package eti.bi.alphaminer.core.workspace;
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.File;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.border.CompoundBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
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.Constants;
import eti.bi.common.Locale.Resource;
import eti.bi.common.System.SysConfig;
public class WorkspaceConfiguration extends JDialog implements Observer{
/**
*
*/
private static final long serialVersionUID = 1L;
private WorkspaceListTree workspaceListTree;
private WorkspaceInformation workspaceInformation;
private int current;
private boolean isNew;
private boolean farFromTree;
public WorkspaceConfiguration() {
createUI();
}
private void createUI() {
workspaceListTree = new WorkspaceListTree(this);
workspaceInformation = new WorkspaceInformation(this);
workspaceInformation.setPreferredSize(new Dimension(300, 260));
JScrollPane list = new JScrollPane();
list.getViewport().add(workspaceListTree);
list.setPreferredSize(new Dimension(200, 260));
this.add(list, BorderLayout.CENTER);
this.add(workspaceInformation, BorderLayout.EAST);
this.setAlwaysOnTop(true);
this.setModal(true);
this.setResizable(false);
this.setSize(new Dimension(500, 260));
this.setTitle(Resource.srcStr("WorkspacesConfiguration"));
init();
}
private void init() {
Resource.registerInterest(this);
setCurrent(0);
}
public Workspace getCurrentWorkspace() {
return WorkspacesManager.getWorkspace(current);
}
public Workspace getWorkspace(int index) {
return WorkspacesManager.getWorkspace(index);
}
public int getCountofWorkspace() {
return WorkspacesManager.getCountofWorkspaces();
}
public void setFarFromTree(boolean farFromTree) {
this.farFromTree = farFromTree;
}
/**
* return if set current workspace succeed
*/
public boolean setCurrent(int index) {
if (!askforNext()) {
return false;
}
current = index;
workspaceInformation.getContent();
return true;
}
/**
* set new workspace which hasn't been saved return if set current workspace
* succeed
*/
public boolean setCurrent(int index, Workspace workspace) {
current = index;
workspaceInformation.getContent(workspace);
return true;
}
public int getCurrentIndex() {
return current;
}
public boolean isNew() {
return isNew;
}
public boolean addWorkspace() {
if (!askforNext()) {
return false;
}
// set location of new workspace
int index = current;
if (farFromTree) {
index = WorkspacesManager.getCountofWorkspaces();
}
// TODO add
Workspace workspace = new Workspace(Resource.srcStr("default_workspaceName") + (WorkspacesManager.getCountofWorkspaces() + 1), SysConfig
.getProperty(Constants.BIML_REPOSITORY_KEY), Resource.srcStr("default_workspaceDescription")
+ (WorkspacesManager.getCountofWorkspaces() + 1));
workspaceListTree.addWorkspace(workspace.getName(), index);
setCurrent(index, workspace);
isNew = true;
return true;
}
public boolean deleteWorkspace() {
if (!WorkspacesManager.deleteWorkspace(current)) {
return false;
}
if (!workspaceListTree.removeWorkspace(current)) {
return false;
}
// set new current
if (current >= WorkspacesManager.getCountofWorkspaces()) {
current--;
}
if (current >= 0) {
return setCurrent(current);
} else {
return workspaceInformation.clear();
}
}
public boolean saveWorkspace() {
Workspace workspace = workspaceInformation.getCurrentWorkspace();
if (!workspace.isvalid()) {
MessageDialog.showMessageDialog(this, Resource.srcStr("wrong_workspace"));
return false;
}
if (isNew) {
if (!WorkspacesManager.addWorkspace(current, workspace)) {
return false;
}
} else {
if (!WorkspacesManager.updateWorkspace(current, workspace)) {
return false;
}
}
isNew = false;
return workspaceListTree.updateWorkspace(current, workspace.getName());
}
/**
* @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()) {
workspaceInformation.setMoidified(false);
WorkspacesManager.configurationExit();
this.hide();
}
}
/**
* return if go to next workspace
*/
public boolean askforNext() {
if (!workspaceInformation.isMoidified()) {
return true;
}
int option;
if (isNew) {
option = MessageDialog.showConfirmDialog(this, Resource.srcStr("workspace_saveNew"));
} else {
option = MessageDialog.showConfirmDialog(this, Resource.srcStr("workspace_saveModify"));
}
if (option == MessageDialog.YES_OPTION) {
return saveWorkspace();
} else if (option == MessageDialog.NO_OPTION) {
return true;
} else if (option == MessageDialog.CANCEL_OPTION) {
return false;
} else {
return false;
}
}
// clear the workspace information
public void clear() {
workspaceInformation.clear();
}
public void sendNotify(String a_Message) {
if(a_Message.equals(Resource.CHANGE_LOCALE)) {
this.setTitle(Resource.srcStr("WorkspacesConfiguration"));
workspaceInformation.resetLocale();
workspaceListTree.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 WorkspaceListTree extends JTree implements TreeSelectionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private WorkspaceConfiguration workspaceconfiguration;
private DefaultMutableTreeNode m_RootNode;
private DefaultTreeModel m_Model;
private WorkspacePopupMenu workspacepopupmenu;
WorkspaceListTree(WorkspaceConfiguration workspaceconfiguration) {
this.workspaceconfiguration = workspaceconfiguration;
m_RootNode = new DefaultMutableTreeNode("workspace");
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();
workspacepopupmenu = new WorkspacePopupMenu(workspaceconfiguration);
}
public void resetLocale() {
workspacepopupmenu.resetLocale();
}
public boolean updateWorkspace(int current, String name) {
WorkspaceTreeNode updateWorkspace = new WorkspaceTreeNode(name);
m_RootNode.remove(current);
m_RootNode.insert(updateWorkspace, current);
m_Model.reload();
return true;
}
public boolean removeWorkspace(int current) {
try {
m_RootNode.remove(current);
m_Model.reload();
this.repaint();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public void addWorkspace(String name, int index) {
WorkspaceTreeNode newWorkspace = new WorkspaceTreeNode(name);
m_RootNode.insert(newWorkspace, index);
m_Model.reload();
this.repaint();
}
private void buildTree() {
String[] names = WorkspacesManager.getWorkSpacesNames();
WorkspaceTreeNode parentNode = null;
for (int i = 0; i < names.length; i++) {
parentNode = new WorkspaceTreeNode(names[i]);
m_RootNode.add(parentNode);
}
m_Model.setRoot(m_RootNode);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -