⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gchm.java

📁 用Swing实现的CHM制作工具
💻 JAVA
字号:
package g2w.app.gchm;

import java.awt.event.ActionEvent;
import java.io.File;
import java.net.URL;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import g2w.app.gchm.gui.MainView;
import g2w.app.gchm.lib.GSplashWindow;
import g2w.app.gchm.util.ActionManager;
import g2w.app.gchm.util.PropertiesManager;

import com.pagosoft.plaf.PlafOptions;

/**
 * The main class of GCHMCreator.
 * 
 * @author GreatGhoul
 * @version 020 2009-3-21 23:13:55
 */
public class GChm {
	/** Properties Manager */
	static PropertiesManager propertiesManager = null;
	
	/** Action Manager */
	static ActionManager actionManager = null;
	
	/** The only instance. */
	public static MainView instance;
	
	public static String getVersion() {
		return "1.1";
	}
	
	public static String getName() {
		return "GCHMCreator";
	}
	
	public static String getFullName() {
		return getName() + " " + getVersion(); 
	}

	public static String getProperty(String name) {
		String value = null;
		if (propertiesManager != null) {
			value = propertiesManager.getProperty(name);
		}
		
		return value;
	}
	
	public static void setProperty(String name, String value) {
		if (propertiesManager != null) {
			propertiesManager.setProperty(name, value);
		}
	}
	
	/**
	 * The main method of GCHMCreator.
	 * 
	 * @param args Command line arguments.
	 */
	public static void main(String[] args) {
		// Checks java version 1.6+
		String javaVersion = System.getProperty("java.version");
		if(javaVersion.compareTo("1.6") < 0) {
			System.err.println("你安装的Java版本: "
				+ javaVersion + '.');
			System.err.println("GCHMCreator需要1.6或者更高版本.");
			System.exit(1);
		}
		
		// Initializes the splash window.
		GSplashWindow splash = 
			new GSplashWindow(getImage("/resources/splash.png").getImage());
		// Starts the splash window.
		splash.start();

		// Initializes the properties manager.
		splash.setStatus("读取配置文件..");
		initPropertiesManager();
		
		// Initializes the action manager.
		splash.setStatus("读取动作列表..");
		initActionManager();
		
		try {
			// Sets the Plaf-look and feel.
			splash.setStatus("加载主题文件..");
			PlafOptions.setAsLookAndFeel();
			
			checkWorkspace();
			
			// Shows the main view.
			splash.setStatus("正在 打开主窗口..");
			prepareActions();
			instance = new MainView();
			instance.setVisible(true);
			
			// Disposes the splash window.
			splash.stop();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e);
			e.printStackTrace();
		}
 	}

	/**
	 * Initializes the action manager.
	 */
	private static void initActionManager() {
		actionManager = new ActionManager();
	}
	
	/**
	 * Puts action into action manager using the key <i>key</key>
	 * 
	 * @param key The key of the action to put into.
	 * @param action The refered action.
	 */
	public static Action putAction(Object key, Action action) {
		if (actionManager != null)
			actionManager.put(key, action);
		return action;
	}
	
	/**
	 * 返回键key关联的的动作
	 *
	 * @param key 动作的键
	 * @return 关联的动作
	 */
	public static Action getAction(Object key) {
		if (actionManager == null)
			return null;
		else
			return actionManager.get(key);
	}
	
	/**
	 * Initializes the properties manager.
	 */
	private static void initPropertiesManager() {
		propertiesManager = new PropertiesManager();
	}
	
	/**
	 * Returns the standard URL of the path <i>path</i>.
	 * 
	 * @param path The specified path.
	 * @return The URL.
	 */
	public static URL getResource(String path) {
		return GChm.class.getResource(path);
	}
	
	/**
	 * Returns an image icon from the specified path. 
	 * @param path The specified path.
	 * @return The image icon.
	 */
	public static ImageIcon getImage(String path) {
		return new ImageIcon(getResource(path));
	}
	
	public static void checkWorkspace() {
		String workspacePath = getProperty("Workspace");
		if (workspacePath == null || !new File(workspacePath).exists()) {
			JFileChooser chooser = new JFileChooser();
			chooser.setDialogTitle("设定工作空间");
			chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			int state = chooser.showOpenDialog(null);
			File workspace = chooser.getSelectedFile();
			if(workspace != null && state == JFileChooser.APPROVE_OPTION) {
				if (!workspace.exists())
					workspace.mkdirs();
				setProperty("Workspace", workspace.getAbsolutePath() + "\\");
			} else {
				System.exit(0);
			}
		}
	}
	
	/**
	 * Get the action manager.
	 * 
	 * @return The action manager.
	 */
	public static ActionManager getActionManager() {
		return actionManager;
	}
	
	/**
	 * Prepare actions that will be used as buttons,menuitems etc..
	 */
	private static void prepareActions() {
		putAction("NewProject", new AbstractAction("新建工程", 
				getImage("/resources/project_new.png")) {
			@Override
			public void actionPerformed(ActionEvent evt) {
				instance.getProjectEditor().CreateProject();
			}
		});
		
		putAction("OpenProject", new AbstractAction("打开工程", 
				getImage("/resources/project_open.png")) {
			@Override
			public void actionPerformed(ActionEvent evt) {
				instance.getProjectEditor().openProject();
			}
		});
		
		putAction("SaveProject", new AbstractAction("保存工程", 
				getImage("/resources/project_save.png")) {
			@Override
			public void actionPerformed(ActionEvent evt) {
				instance.getProjectEditor().saveProject();
			}
		});
		
		putAction("CloseProject", new AbstractAction("关闭工程") {
			@Override
			public void actionPerformed(ActionEvent evt) {
				instance.getProjectEditor().closeProject();
			}
		});
		
		putAction("AddTopic", new AbstractAction("添加标题", 
				getImage("/resources/topic_new.png")) {
			@Override
			public void actionPerformed(ActionEvent evt) {
				instance.getProjectEditor().addTopic();
			}
		});
		
		putAction("Exit", new AbstractAction("退出程序") {
			@Override
			public void actionPerformed(ActionEvent evt) {
				instance.exit();
			}
		});
		
		putAction("RemoveTopic", new AbstractAction("删除标题", 
				getImage("/resources/topic_remove.png")) {
			@Override
			public void actionPerformed(ActionEvent evt) {
				instance.getProjectEditor().removeTopic();
			}
		});
		
		putAction("ShowConsole", new AbstractAction("显示控制台",
				getImage("/resources/show_console.png")) {
			@Override
			public void actionPerformed(ActionEvent evt) {
				instance.getProjectEditor().showConsole();
			}
		});
		
		putAction("CompileProject", new AbstractAction("编译工程",
				getImage("/resources/project_compile.png")) {
			@Override
			public void actionPerformed(ActionEvent evt) {
				instance.getProjectEditor().compileProject();
			}
		});
		
		putAction("ShowAbout", new AbstractAction("关于本软件..") {
			@Override
			public void actionPerformed(ActionEvent evt) {
				instance.showAboutDialog();
			}
		});
		
		putAction("VisitWebsite", new AbstractAction("访问作者网站") {
			@Override
			public void actionPerformed(ActionEvent evt) {
				instance.visitWebsite();
			}
		});
		
		putAction("OpenProjectFolder", new AbstractAction("打开工程文件夹") {
			@Override
			public void actionPerformed(ActionEvent evt) {
				instance.getProjectEditor().openProjectFolder();
			}
		});
		
		putAction("SetWorkspace", new AbstractAction("设定工作空间") {
			@Override
			public void actionPerformed(ActionEvent evt) {
				instance.setWorkspace();
			}
		});
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -