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

📄 projecteditor.java

📁 用Swing实现的CHM制作工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		repaint();
		
		updateActions();
		
	}
	

	/**
	 * Add a topic.
	 */
	public void addTopic() {
		// Get the content editor.
		ContentEditor contentEditor = getContentEditor();
		ContentTreeNode node = ContentTreeNode.createNewTopicNode();
		contentEditor.appendAfterSelection(node);
		contentEditor.setSelection(node);
	}

	/**
	 * Remove the selected topic.
	 */
	public void removeTopic() {
		// Get the content editor.
		ContentEditor contentEditor = getContentEditor();
		ContentTreeNode neighbor = contentEditor.removeSelection();
		contentEditor.setSelection(neighbor);
	}

	/**
	 * Create a new project.
	 */
	public void CreateProject() {
		ProjectCreatorDialog dialog = new ProjectCreatorDialog(GChm.instance);
		Project project = dialog.showDialog();
		if (project == null) {
			return;
		} else {
			setProject(project);
		}
	}

	/**
	 * Open a project.
	 */
	public void openProject() {
		if (!confirmOnClose()) return;
		
		chooser.setMultiSelectionEnabled(false);
		chooser.setAcceptAllFileFilterUsed(false);
		chooser.setFileFilter(new FileNameExtensionFilter("GCHMCreator工程文件(*.gchm)", "gchm"));
	    if(chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
	    	Project project;
			try {
				project = new Project(ProjectUtility.getProjectFile(chooser.getSelectedFile()));
				if (project != null)
					setProject(project);
				return;
			} catch (FileNotFoundException e) {
				console.alert(e.getMessage());
			} catch (JDOMException e) {
				console.alert(e.getMessage());
			} catch (IOException e) {
				console.alert(e.getMessage());
			}
			JOptionPane.showMessageDialog(GChm.instance, "打开工程失败");
			showConsole();
	    }
	}

	/**
	 * Save the current project.
	 */
	public void saveProject() {
		try {
			project.save();
			console.hey("工程保存成功!");
			updateActions();
			return;
		} catch (FileNotFoundException e) {
			console.alert(e.getMessage());
		} catch (URISyntaxException e) {
			console.alert(e.getMessage());
		} catch (IOException e) {
			console.alert(e.getMessage());
		}
		JOptionPane.showMessageDialog(GChm.instance, "工程保存失败");
	}
	
	/**
	 * Get the current project.
	 * @return
	 */
	public Project getProject() {
		return this.project;
	}

	/**
	 * Get the topic editor.
	 * 
	 * @return The topic editor.
	 */
	public TopicEditor getTopicEditor() {
		return (TopicEditor) topicEditorView.getView();
	}
	
	/**
	 * Get the content editor.
	 * 
	 * @return The content editor.
	 */
	public ContentEditor getContentEditor() {
		return (ContentEditor) contentEditorView.getView();
	}
	
	/**
	 * If the selection of the content tree changed
	 * this method will be invoked to edit the selection.
	 */
	@Override
	public void valueChanged(TreeSelectionEvent evt) {
		try {
			// Get the selection path.
			TreePath treePath = evt.getPath();
			// Get the selection node.
			ContentTreeNode treeNode = 
				(ContentTreeNode) treePath.getLastPathComponent();
			if (treeNode.isRoot()) {
				// If the selection node is the root of the tree,
				// display the project's configuration editor.
				hSeparator.setRightComponent(projectConfigView);
				
				// Update related actions.
				GChm.getAction("AddTopic").setEnabled(true);
				GChm.getAction("RemoveTopic").setEnabled(false);
			} else {
				// If the selection node is a topic node,
				// display the topic editor to edit the selection topic.
				getTopicEditor().setTopic(treeNode);
				hSeparator.setRightComponent(topicEditorView);
				
				// Update related actions.
				GChm.getAction("AddTopic").setEnabled(true);
				GChm.getAction("RemoveTopic").setEnabled(true);
			}
		} catch (NullPointerException e) {
			// If no selection found, display the project's configureation editor.
			hSeparator.setRightComponent(projectConfigView);
			
			// Update related actions.
			GChm.getAction("AddTopic").setEnabled(false);
			GChm.getAction("RemoveTopic").setEnabled(false);
		}
	}

	/**
	 * Get the console.
	 * 
	 * @return The console.
	 */
	public GConsole getConsole() {
		return console;
	}
	
	public void showConsole() {
		consoleView.setVisible(true);
		vSeparator.setBottomComponent(consoleView);
	}
	
	public void updateActions() {
		GChm.getAction("SaveProject").setEnabled(project != null && project.isChanged());
		GChm.getAction("CompileProject").setEnabled(project != null);
		GChm.getAction("OpenProjectFolder").setEnabled(project != null);
	}
	
	/**
	 * Confirm on closing current project.
	 * 
	 * @return true if closing is confirmed. 
	 */
	public boolean confirmOnClose() {
		if (project != null && project.isChanged()) {
			int state = JOptionPane.showConfirmDialog(GChm.instance, 
					"是否放弃对工程\"" + project.getTitle() + "\"已经的更改?",
					"确认放关闭工程",
					JOptionPane.YES_NO_OPTION); 
			if (state == JOptionPane.OK_OPTION) {
				return true;
			} else {
				return false;
			}
		} else {
			return true;
		}

	}

	/**
	 * Compile the project.
	 */
	public void compileProject() {
		saveProject();
		showConsole();
		console.hey("正在 生成索引文件..");
		try {
			ProjectUtility.createHHKFile(project.getDocument());
			console.hey("生成索引文件成功!");
		} catch (IOException e) {
			console.alert("生成索引文件失败: " + e.getMessage());
			return;
		} catch (JDOMException e) {
			console.alert("生成索引文件失败: " + e.getMessage());
			return;
		}
		
		console.say("");
		console.hey("正在生成目录文件..");
		try {
			ProjectUtility.createHHCFile(project.getDocument());
			console.hey("生成目录文件成功!");
		} catch (IOException e) {
			console.alert("生成目录文件失败: " + e.getMessage());
		}
		
		
		console.say("");
		console.hey("正在生成工程文件..");
		File hhpFile = null;
		try {
			hhpFile = ProjectUtility.createHHPFile(project.getDocument());
			console.hey("生成工程文件成功!");
		} catch (JDOMException e) {
			console.alert("生成工程文件失败: " + e.getMessage());
		} catch (IOException e) {
			console.alert("生成工程文件失败: " + e.getMessage());
		}
		
		console.say("");
		console.hey("正在编译,请耐心等待");
		try {
			console.exec("bin\\hhc.exe \"" + hhpFile.getAbsolutePath() + "\"", true);
		} catch (IOException e) {
			console.alert("编译失败: " + e.getMessage());
		} catch (NullPointerException e) {
			console.alert("编译失败: " + e.getMessage());
		}
	}
	
	/**
	 * Return if the given file is contained in current project.
	 * 
	 * @param file The given file.
	 * @return If contains.
	 */
	public boolean isFileInProject(File file) {
		File projectFile = new File(project.getProjectPath());
		if (projectFile == null) return false; 
		boolean contains = false;
		while ((file = file.getParentFile()) != null) {
			if (file.equals(projectFile)) {
				contains = true;
				break;
			}
		}
		return contains;
	}

	/**
	 * Open the project folder.
	 */
	public void openProjectFolder() {
		String cmd = "explorer.exe \"" + project.getProjectPath() + "\"";
		try {
			Runtime.getRuntime().exec(cmd);
		} catch (IOException e) {}
	}
}

⌨️ 快捷键说明

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