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

📄 projecteditor.java

📁 用Swing实现的CHM制作工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package g2w.app.gchm.gui;

import g2w.app.gchm.GChm;
import g2w.app.gchm.lib.GConsole;
import g2w.app.gchm.lib.GLightBox;
import g2w.app.gchm.util.ProjectUtility;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URISyntaxException;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.tree.TreePath;

import org.jdom.JDOMException;

/**
 * ProjectEditor is the main editor of GCHMCreator.
 * 
 * @author GreatGhoul
 * @version 020 2009-3-22 8:51:46
 */
public class ProjectEditor extends JPanel implements TreeSelectionListener {
	private Project project;
	
	/** A light box to show the project config. */ 
	private GLightBox projectConfigView;
	
	/** A horizontal separator to separate editors. */ 
	private JSplitPane hSeparator;
	
	/** A vertical separator to separate editors and the console. */
	private JSplitPane vSeparator;
	
	/** A light box to show the content editor. */
	private GLightBox contentEditorView;
	
	/** A light box to show the topic editor. */
	private GLightBox topicEditorView;
	
	/** A file chooser to choose files */
	private JFileChooser chooser;
	
	/** Af light box to show the console. */
	private GLightBox consoleView;
	
	/** The console */
	private GConsole console;
	
	/** The welcome pane. */
	private JPanel welcomeView;
	
	/**
	 * Construct a project editor with the default setup.
	 */
	public ProjectEditor() {
		super();
		initCompoments();
		updateActions();
	}

	/**
	 * Construct a project teditor with a given project.
	 * 
	 * @param project The given project.
	 */
	public ProjectEditor(Project project) {
		super();
		initCompoments();
		setProject(project);
		updateActions();
	}

	/**
	 * Initialize all compoments.
	 */
	private void initCompoments() {
		// Configure the compoment.
		setLayout(new BorderLayout());
		
		// Initialize the file chooser.
		chooser = new JFileChooser(GChm.getProperty("Workspace"));
		
		// Initialize the toolbar.
		add(ToolBarProvider.getProjectEditorToolBar(), BorderLayout.NORTH);
		
		// Initialize the vertical separator.
		vSeparator = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		// Add the welcome view.
		vSeparator.setTopComponent(createWelcomeView());
		// Add the console view.
		vSeparator.setBottomComponent(createConsoleView());
		// Add the vertical separator.
		vSeparator.setDividerLocation(400);
		add(vSeparator, BorderLayout.CENTER);
		
		// Initialize the horizontal separator.
		hSeparator = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
		hSeparator.setDividerLocation(200);
		// Add the content editor view.
		hSeparator.setLeftComponent(createContentEditorView());
		// Add the project config view.
		hSeparator.setRightComponent(createProjectConfigView());
		
		// Initialize the topic editor view.
		topicEditorView = new GLightBox("编辑标题", false);
		topicEditorView.setMinimumSize(new Dimension(400, 300));
		topicEditorView.setPreferredSize(new Dimension(400, 400));
	}

	/**
	 * Create the project config view.
	 * 
	 * @return The project config view.
	 */
	private GLightBox createProjectConfigView() {
		// Initialize the project config view.
		projectConfigView = new GLightBox("工程属性", false);
		projectConfigView.setMinimumSize(new Dimension(400, 300));
		projectConfigView.setPreferredSize(new Dimension(400, 400));
		
		return projectConfigView;
	}

	/**
	 * Create the content editor view.
	 * 
	 * @return The content editor view.
	 */
	private GLightBox createContentEditorView() {
		// Initialize the content editor view.
		contentEditorView = new GLightBox("文档目录", false);
		contentEditorView.setMinimumSize(new Dimension(200, 300));
		contentEditorView.setPreferredSize(new Dimension(200, 400));
		
		return contentEditorView;
	}

	/**
	 * Create the console view.
	 * 
	 * @return The console view.
	 */
	private GLightBox createConsoleView() {
		// Initialize the console.
		console = new GConsole();
		
		// Configure the console view.
		consoleView = new GLightBox("控制台");
		consoleView.setPreferredSize(new Dimension(700, 150));
		consoleView.setView(console);
		consoleView.setVisible(false);
		
		return consoleView;
	}

	/**
	 * Create the welcome view.
	 * 
	 * @return The welcome view.
	 */
	private JPanel createWelcomeView() {
		welcomeView = new JPanel();
		welcomeView.setPreferredSize(new Dimension(700, 400));
		welcomeView.setMinimumSize(new Dimension(700, 300));
		welcomeView.setLayout(new BoxLayout(welcomeView, BoxLayout.Y_AXIS));
		
		welcomeView.add(Box.createVerticalStrut(50));
		welcomeView.add(Box.createVerticalGlue());
		
		JPanel center = new JPanel(); 
		Dimension hugeDimension = new Dimension(150, 150);
		
		JButton openProject = new JButton(GChm.getAction("OpenProject"));
		openProject.setIcon(GChm.getImage("/resources/project_open_big.png"));
		openProject.setPreferredSize(hugeDimension);
		openProject.setMaximumSize(hugeDimension);
		openProject.setHorizontalTextPosition(SwingConstants.CENTER);  
		openProject.setVerticalTextPosition(SwingConstants.BOTTOM);   
		center.add(openProject);
		
		JButton createProject = new JButton(GChm.getAction("NewProject"));
		createProject.setIcon(GChm.getImage("/resources/project_new_big.png"));
		createProject.setPreferredSize(hugeDimension);
		createProject.setMaximumSize(hugeDimension);
		createProject.setHorizontalTextPosition(SwingConstants.CENTER);  
		createProject.setVerticalTextPosition(SwingConstants.BOTTOM);  
		center.add(createProject);
		
		welcomeView.add(center);
		
		welcomeView.add(Box.createVerticalGlue());
		welcomeView.add(Box.createVerticalStrut(50));
		
		return welcomeView;
	}

	/**
	 * Unload the current project.
	 */
	public void closeProject() {
		if (!confirmOnClose()) return;
		
		vSeparator.setTopComponent(welcomeView);
		
		// Dispose the content editor.
		ContentEditor contentEditor = (ContentEditor) contentEditorView.getView();
		// Get the topic editor.
		TopicEditor topicEditor = (TopicEditor) topicEditorView.getView();
		// Get the project config.
		
		// Remove listeners.
		contentEditor.removeContentTreeSelectionListener(this);
		topicEditor.removeSubmitListener(contentEditor);
		contentEditor.removeAll();
		topicEditor.removeAll();
		
		// Reset the views.
		contentEditorView.setView(null);
		topicEditorView.setView(null);
		projectConfigView.setView(null);
		
		// Display the welcome pane.
		vSeparator.setTopComponent(welcomeView);
		
		project = null;
		
		updateActions();
		revalidate();
		repaint();
	}
	
	/**
	 * Load a new project.
	 * 
	 * @param project The given project.
	 */
	public void setProject(Project project) {
		if (!confirmOnClose()) return;
		
		this.project = project;

		// Initialize the project config.
		ProjectConfig projectConfig = new ProjectConfig(this);
		projectConfigView.setView(projectConfig);
		hSeparator.setRightComponent(projectConfigView);
		
		// Initialize the content editor.
		ContentEditor contentEditor = new ContentEditor(this);
		// Add a selection listener to switch the node editor in need.
		// In addition, the listener will update related actions in need.
		contentEditor.addContentTreeSelectionListener(this);
		// Select the root node of the content tree.
		contentEditorView.setView(contentEditor);
		
		TopicEditor topicEditor = new TopicEditor(this);
		topicEditor.addSubmitListener(contentEditor);
		topicEditorView.setView(topicEditor);
		
		contentEditor.selectRoot();
		vSeparator.setTopComponent(hSeparator);
		SwingUtilities.updateComponentTreeUI(this);
		revalidate();

⌨️ 快捷键说明

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