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

📄 projectconfig.java

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

import g2w.app.gchm.GChm;
import g2w.app.gchm.util.FileUtility;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 * ProjectConfigure is a dialog to edit the compiling detail.
 * 
 * @author GreatGhoul
 * @version 014 2009-3-22 9:04:54
 */
public class ProjectConfig extends JPanel implements DocumentListener {
	
	/** The filename of the chm file */
	private JTextField chmFileName;
	
	/** The default page's path. */
	private JTextField defaultPagePath;
	
	/** The about page's path. */
	private JTextField aboutPagePath;
	
	/** The home page's path. */
	private JTextField homePagePath;
	
	/** The title of the chm file. */
	private JTextField title;
	
	/** The ownner editor. */
	private ProjectEditor owner;
	
	/** A file chooser. */
	private JFileChooser chooser;
	
	/** A flag to indicate if the content has changed. */
	private boolean configChanged;
	
	/** A button to submit the changes. */
	private JButton submit;
	
	/**
	 * Construct a project config with the given owner.
	 * 
	 * @param project The project to config.
	 */
	public ProjectConfig(ProjectEditor owner) {
		super();
		this.owner = owner;
		initComponents();
		configChanged = false;
		updateSubmitStatus();
	}

	/**
	 * Initialize all components.
	 */
	private void initComponents() {
		// Initialize the file chooser.
		chooser = new JFileChooser(owner.getProject().getProjectPath());
		chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		chooser.setFileFilter(new FileNameExtensionFilter("HTML文档(*.htm, *.html)", "htm", "html"));
		chooser.setAcceptAllFileFilterUsed(false);
		chooser.setMultiSelectionEnabled(false);
		
		// Configure the component.
		setLayout(new BorderLayout());
		setBorder(BorderFactory.createEmptyBorder(8, 10, 8, 10));
		// Add the content wrap.
		add(createContentWrap(), BorderLayout.NORTH);
		
		// Add the control wrap.
		add(createControlWrap(), BorderLayout.SOUTH);
	}

	/**
	 * Initialize the control wrap.
	 * 
	 * @return The control wrap.
	 */
	private Component createControlWrap() {
		JPanel wrap = new JPanel();
		wrap.setLayout(new FlowLayout(FlowLayout.RIGHT));
		submit = new JButton(new AbstractAction("确定") {
			@Override
			public void actionPerformed(ActionEvent evt) {
				submit();
			}
		});
		submit.setIcon(GChm.getImage("/resources/submit.png"));
		submit.setToolTipText("提交对工程属性的更改");
		wrap.add(submit);
		return wrap;
	}

	/**
	 * Submit changes.
	 */
	protected void submit() {
		String title = this.title.getText().trim();
		String defaultPagePath = this.defaultPagePath.getText().trim();
		String aboutPagePath = this.aboutPagePath.getText().trim();
		String homePagePath = this.homePagePath.getText().trim();
		String chmFileName = this.chmFileName.getText().trim();
		
		if (!chmFileName.isEmpty() && !chmFileName.endsWith(".chm")) {
			chmFileName += ".chm";
			this.chmFileName.setText(chmFileName);
		}
		
		String errMsg = "";
		if (title.isEmpty())
			errMsg += "没有设置CHM文档窗口标题.\n";
		if (!FileUtility.isValidFileName(chmFileName))
			errMsg += "\"" + chmFileName + "\"不是一个有效的文件名.\n";
		if (!errMsg.isEmpty()) {
			JOptionPane.showMessageDialog(GChm.instance, errMsg);
			return;
		}
		
		Project project = owner.getProject();
		project.setTitle(title);
		project.setHomePage(homePagePath);
		project.setDefaultPage(defaultPagePath);
		project.setAboutPage(aboutPagePath);
		project.setChmFileName(chmFileName);
		configChanged = false;
		updateSubmitStatus();
		owner.updateActions();
	}

	/**
	 * Initialize the content wrap.
	 * 
	 * @return The content wrap.
	 */
	private Component createContentWrap() {
		JPanel wrap = new JPanel();
		wrap.setLayout(new BoxLayout(wrap, BoxLayout.Y_AXIS));
		
		// Get the project.
		Project project = owner.getProject();
		
		Box row1 = Box.createHorizontalBox();
		row1.add(new JLabel("标 题"));
		row1.add(Box.createHorizontalStrut(3));
		title = new JTextField();
		title.setText(project.getTitle());
		title.getDocument().addDocumentListener(this);
		row1.add(title);
		wrap.add(row1);
		wrap.add(Box.createVerticalStrut(5));
		
		Box row2 = Box.createHorizontalBox();
		row2.add(new JLabel("首 页"));
		row2.add(Box.createHorizontalStrut(5));
		homePagePath = new JTextField();
		homePagePath.setEditable(false);
		homePagePath.setText(project.getHomePage());
		homePagePath.getDocument().addDocumentListener(this);
		row2.add(homePagePath);
		row2.add(Box.createHorizontalStrut(3));
		row2.add(Box.createHorizontalGlue());
		row2.add(new JButton(new AbstractAction("浏览..") {
			@Override
			public void actionPerformed(ActionEvent evt) {
				chooser.setDialogTitle("选择要作为首页的文件");
				File file = pickFile();
				if (file != null) {
					homePagePath.setText(file.getAbsolutePath());
				}
			}
		}));
		wrap.add(row2);
		wrap.add(Box.createVerticalStrut(5));
		
		Box row3 = Box.createHorizontalBox();
		row3.add(new JLabel("默认页"));
		row3.add(Box.createHorizontalStrut(5));
		defaultPagePath = new JTextField();
		defaultPagePath.setEditable(false);
		defaultPagePath.setText(project.getDefaultPage());
		defaultPagePath.getDocument().addDocumentListener(this);
		row3.add(defaultPagePath);
		row3.add(Box.createHorizontalStrut(3));
		row3.add(Box.createHorizontalGlue());
		row3.add(new JButton(new AbstractAction("浏览..") {
			@Override
			public void actionPerformed(ActionEvent evt) {
				chooser.setDialogTitle("选择要作为默认页的文件");
				File file = pickFile();
				if (file != null)
					defaultPagePath.setText(file.getAbsolutePath());
			}
		}));
		wrap.add(row3);
		wrap.add(Box.createVerticalStrut(5));
		
		Box row4 = Box.createHorizontalBox();
		row4.add(new JLabel("版权页"));
		row4.add(Box.createHorizontalStrut(5));
		aboutPagePath = new JTextField();
		aboutPagePath.setEditable(false);
		aboutPagePath.setText(project.getAboutPage());
		aboutPagePath.getDocument().addDocumentListener(this);
		row4.add(aboutPagePath);
		row4.add(Box.createHorizontalStrut(3));
		row4.add(Box.createHorizontalGlue());
		row4.add(new JButton(new AbstractAction("浏览..") {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				chooser.setDialogTitle("选择要作为默认页的文件");
				File file = pickFile();
				if (file != null) 
					aboutPagePath.setText(file.getAbsolutePath());
			}
		}));
		wrap.add(row4);
		wrap.add(Box.createVerticalStrut(5));
		
		Box row5 = Box.createHorizontalBox();
		row5.add(new JLabel("文件名"));
		row5.add(Box.createHorizontalStrut(2));
		chmFileName = new JTextField();
		chmFileName.setText(project.getChmFileName());
		chmFileName.getDocument().addDocumentListener(this);
		row5.add(chmFileName);
		wrap.add(row5);
		wrap.add(Box.createVerticalStrut(5));
		
		return wrap;
	}
	
	/**
	 * Pack a html file.
	 * 
	 * @return The picked file. null if empty.
	 */
	public File pickFile() {
		int state = chooser.showOpenDialog(this);
		File file = chooser.getSelectedFile();
		if (file != null && state == JFileChooser.APPROVE_OPTION) {
			if (!owner.isFileInProject(file)) {
				JOptionPane.showMessageDialog(this, "请先将文件添加到工程目录下!");
				return null;
			}
			return file;
		} else {
			return null;
		}
	}

	/**
	 * Update the submit button's status.
	 */
	public void updateSubmitStatus() {
		submit.setEnabled(configChanged);
	}
	
	@Override
	public void changedUpdate(DocumentEvent evt) {
		configChanged = true;
		updateSubmitStatus();
	}

	@Override
	public void insertUpdate(DocumentEvent arg0) {
		configChanged = true;
		updateSubmitStatus();
	}

	@Override
	public void removeUpdate(DocumentEvent arg0) {
		configChanged = true;
		updateSubmitStatus();
	}
}

⌨️ 快捷键说明

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