📄 workspacesmanager.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.Dimension;
import java.awt.Toolkit;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import eti.bi.alphaminer.ui.ApplicationWindow;
import eti.bi.common.Locale.Resource;
import eti.bi.common.System.SysConfig;
import eti.bi.exception.SysException;
public class WorkspacesManager {
private static ApplicationWindow m_container;
private static boolean hasload = false;
private static Workspaces workspaces;
private static String DEFALUT_WORKSPACE_PATH;
private static WorkspaceConfiguration workspaceConfiguration = null;
public static void setContainer(ApplicationWindow applicationWindow){
if(m_container==null){
m_container = applicationWindow;
}
}
public static void load() throws SysException{
if(hasload&&workspaces!=null){
return;
}
WorkspacesLoader.setDefinitionFile(SysConfig.getProperty("workspacesdefinition"));
File definitionFile = new File(WorkspacesLoader.getDefinitionFile());
if(definitionFile.isFile()){
WorkspacesLoader.loadWorkSpaces();
workspaces = WorkspacesLoader.getWorkSpaces();
}
if(workspaces==null){
workspaces = new Workspaces();
}
//add default
if(workspaces.size()==0){
DEFALUT_WORKSPACE_PATH = SysConfig.getHome()+File.separator+"workspace";
Workspace workspace = new Workspace(Resource.srcStr("default_workspaceName"),DEFALUT_WORKSPACE_PATH,Resource.srcStr("default_workspaceDescription"));
workspaces.AddWorkspaceCurrent(workspace);
}
//set current path
SysConfig.setCurrentWorkSpacePath(CurrentWorkSpacePath());
hasload = true;
}
public static Workspaces getWorkSpaces(){
return workspaces;
}
public static Workspace getWorkspace(int index){
if(workspaces==null){
return null;
}
return workspaces.getWorkspace(index);
}
public static String[] getWorkSpacesNames() {
if(workspaces!=null){
return workspaces.getWorkspacesNames();
}
else{
return new String[0];
}
}
public static int getCountofWorkspaces(){
if(workspaces==null){
return 0;
}
else{
return workspaces.size();
}
}
public static int CurrentWorkSpaceIndex() {
if(workspaces!=null){
return workspaces.CurrentWorkspaceIndex();
}
else{
return -1;
}
}
public static String CurrentWorkSpacePath(){
if(workspaces!=null){
return workspaces.CurrentWorkspace().getPath();
}
else{
return DEFALUT_WORKSPACE_PATH;
}
}
/**
* @author Xiaojun Chen
* @param reload true if the current workspace need reloaded
* */
public static boolean SwitchWorkSpace(int index, boolean reload){
if(index==CurrentWorkSpaceIndex()){
if(!reload){
return true;
}
}
if(workspaces==null){
return false;
}
if(!workspaces.SwitchCurrentWorkspace(index)){
return false;
}
SysConfig.setCurrentWorkSpacePath(CurrentWorkSpacePath());
if(!writeXML()){
return false;
}
return m_container.switchWorkspaces();
}
/**
* @author Xiaojun Chen
* reload the current workspace
* */
public static boolean ReloadWorkSpace() {
return SwitchWorkSpace(CurrentWorkSpaceIndex(),true);
}
@SuppressWarnings("deprecation")
public static void configuration(){
if(workspaceConfiguration==null){
workspaceConfiguration= new WorkspaceConfiguration();
}
int formWidth = workspaceConfiguration.getWidth();
int formHeight = workspaceConfiguration.getHeight();
Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
int formLocationX = (((screenDim.width - formWidth) / 2) < m_container
.getLocation().x) ? m_container.getLocation().x : (screenDim.width - formWidth) / 2;
int formLocationY= ((screenDim.height / 2 - formHeight) < m_container
.getLocation().y) ? m_container.getLocation().y : (screenDim.height / 2 - formHeight);
workspaceConfiguration.setLocation(formLocationX,formLocationY);
workspaceConfiguration.show();
}
/**
* @author Xiaojun Chen
* load by WorkspaceConfiguration.close()
* */
public static void configurationExit(){
m_container.updateWorkspaces();
}
public static boolean deleteWorkspace(int index){
if(workspaces==null){
return false;
}
if(!workspaces.deleteWorkspace(index)){
return false;
}
if(!writeXML()){
return false;
}
return true;
}
public static boolean updateWorkspace(int index, Workspace workspace){
if(workspaces==null){
return false;
}
if(!workspaces.updateWorkspace(index, workspace)){
return false;
}
if(!writeXML()){
return false;
}
return true;
}
public static boolean addWorkspace(int index, Workspace workspace){
if(workspaces==null){
return false;
}
if(!workspaces.AddWorkspace(workspace, index)){
return false;
}
if(!writeXML()){
return false;
}
return true;
}
/**
* return if writing workspaces configuration file succeed
* */
public static boolean writeXML(){
if(workspaces==null){
return false;
}
//backup
BufferedReader definitionFile = null;
StringBuffer sb = new StringBuffer();
FileWriter definition;
try {
definitionFile = new BufferedReader(new FileReader(WorkspacesLoader.getDefinitionFile()));
String aLine = null;
while ((aLine = definitionFile.readLine()) != null)
{
sb.append(aLine+"\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
if(definitionFile!=null){
definitionFile.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
BufferedWriter bfw = null;
try {
definition = new FileWriter(WorkspacesLoader.getDefinitionFile());
bfw = new BufferedWriter(definition);
bfw.write(workspaces.toXML());
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
//recover
e.printStackTrace();
if(sb.length()>0){
try {
definition = new FileWriter(WorkspacesLoader.getDefinitionFile());
bfw = new BufferedWriter(definition);
bfw.write(sb.toString());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
return false;
}
finally{
try {
if(bfw!=null){
bfw.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -