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

📄 rtextfilechooseroptionpanel.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * 10/21/2004
 *
 * RTextFileChooserOptionPanel.java - Option panel for configuring an
 *                                    RTextFileChooser.
 * Copyright (C) 2004 Robert Futrell
 * email@address.com
 * www.website.com
 *
 * 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 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.fife.ui.rtextfilechooser;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Iterator;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;

import org.fife.RUtilities;
import org.fife.TextColorCellRenderer;
import org.fife.ui.OptionsDialogPanel;
import org.fife.ui.RColorSwatchesButton;
import org.fife.ui.UIUtilities;
import org.fife.ui.modifiabletable.*;


/**
 * Option panel for configuring an <code>RTextFileChooser</code>.  Your
 * subclass of {@link org.fife.ui.OptionsDialog} should call this class's
 * {@link #initialize} method in the dialog's <code>initialize</code> method,
 * and this class's {@link #configureFileChooser} method in the dialog's
 * <code>doApplyImpl</code> method.
 *
 * @author Robert Futrell
 * @version 0.3
 */
public class RTextFileChooserOptionPanel extends OptionsDialogPanel
					implements ActionListener, ModifiableTableListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -355970427641354167L;

	public static final String AUTO_COMPLETE_PROPERTY	= "AutoComplete";
	public static final String COLOR_CHANGED_PROPERTY	= "ColorChanged";
	public static final String HIDDEN_FILES_PROPERTY	= "HiddenFiles";

	private JCheckBox hiddenFilesCheckBox;
	private JCheckBox autoCompleteCheckBox;
	private RColorSwatchesButton hiddenColorButton;
	private ColorTableModel colorTableModel;
	private static String defaultColorString;

	private static final String MAPPING_DIALOG_BUNDLE	=
				"org.fife.ui.rtextfilechooser.ExtensionColorMappingDialog";


/*****************************************************************************/


	/**
	 * Constructor.  All strings in the file chooser are initialized via the
	 * current locale.
	 */
	public RTextFileChooserOptionPanel() {

		ResourceBundle msg = ResourceBundle.getBundle(
							"org.fife.ui.rtextfilechooser.FileChooser");

		defaultColorString = msg.getString("DefaultColorLabel");

		setName(msg.getString("FileChooser"));
		setBorder(UIUtilities.getEmpty5Border());
		setLayout(new BorderLayout());

		JPanel temp = new JPanel();
		temp.setLayout(new BoxLayout(temp, BoxLayout.Y_AXIS));
		temp.setBorder(new OptionPanelBorder(msg.getString("General")));
		hiddenFilesCheckBox = RUtilities.createCheckBox(msg,
							"HiddenFilesLabel", "HiddenFilesMnemonic");
		hiddenFilesCheckBox.setActionCommand("HiddenFiles");
		hiddenFilesCheckBox.addActionListener(this);
		JPanel temp2 = new JPanel();
		temp2.setLayout(new BoxLayout(temp2, BoxLayout.X_AXIS));
		temp2.add(hiddenFilesCheckBox);
		hiddenColorButton = new RColorSwatchesButton();
		temp2.add(hiddenColorButton);
		temp2.add(Box.createHorizontalGlue());
		temp.add(temp2);
		autoCompleteCheckBox = RUtilities.createCheckBox(msg,
										"AutoCompleteLabel",
										"AutoCompleteMnemonic");
		autoCompleteCheckBox.setActionCommand("AutoComplete");
		autoCompleteCheckBox.addActionListener(this);
		addLeftAlignedComponent(autoCompleteCheckBox, temp);
		add(temp, BorderLayout.NORTH);

		JPanel customColorsPanel = new JPanel(new BorderLayout());
		customColorsPanel.setBorder(new OptionPanelBorder(
									msg.getString("Colors")));
		colorTableModel = new ColorTableModel(msg.getString("Extension"),
										msg.getString("Color"));
		ModifiableTable modifiableTable = new ModifiableTable(colorTableModel);
		modifiableTable.setRowHandler(new FileChooserRowHandler());
		modifiableTable.addModifiableTableListener(this);
		JTable colorTable = modifiableTable.getTable();
colorTable.setPreferredScrollableViewportSize(new java.awt.Dimension(300,200));
		colorTable.getColumnModel().getColumn(1).setCellRenderer(
							new TextColorCellRenderer("filename.ext"));
		customColorsPanel.add(modifiableTable);
		add(customColorsPanel);

	}


/*****************************************************************************/


	/**
	 * Listens for actions on this panel.
	 *
	 * @param e The action that occured.
	 */
	public void actionPerformed(ActionEvent e) {

		String actionCommand = e.getActionCommand();

		// Let people know if the "View Hidden Files" checkbox is changed.
		if (actionCommand.equals("HiddenFiles")) {
			boolean checked = hiddenFilesCheckBox.isSelected();
			hiddenColorButton.setEnabled(checked);
			firePropertyChange(HIDDEN_FILES_PROPERTY, !checked, checked);
		}

		// If they check the "Auto-Complete" checkbox...
		else if (actionCommand.equals("AutoComplete")) {
			boolean checked = autoCompleteCheckBox.isSelected();
			firePropertyChange(AUTO_COMPLETE_PROPERTY, !checked, checked);
		}

	}


/*****************************************************************************/


	/**
	 * Adds the specified component to a specified panel left-aligned.
	 *
	 * @param c The component to add.
	 * @param panel The panel to which to add the component.
	 */
	private static final void addLeftAlignedComponent(Component c,
										JPanel panel) {
		JPanel temp = new JPanel();
		temp.setLayout(new BoxLayout(temp, BoxLayout.X_AXIS));
		temp.add(c);
		temp.add(Box.createHorizontalGlue());
		panel.add(temp);
	}


/*****************************************************************************/


	/**
	 * Configures the passed-in file chooser to have its parameters set like
	 * those specified in this option panel.
	 *
	 * @param chooser The file chooser to configure.
	 * @see #initialize
	 */
	public void configureFileChooser(RTextFileChooser chooser) {

		chooser.setShowHiddenFiles(hiddenFilesCheckBox.isSelected());
		chooser.setHiddenFileColor(hiddenColorButton.getColor());
		chooser.setFileSystemAware(autoCompleteCheckBox.isSelected());
		chooser.clearExtensionColorMap(); // In case they removed any.

		// Do row 0... guaranteed to be "default" color.
		chooser.setDefaultFileColor((Color)colorTableModel.getValueAt(0,1));

		// Do all of the other colors.
		int rowCount = colorTableModel.getRowCount();
		for (int i=1; i<rowCount; i++) {
			String ext = (String)colorTableModel.getValueAt(i,0);
			Color color = (Color)colorTableModel.getValueAt(i,1);
			chooser.setColorForExtension(ext, color);
		}

	}


/*****************************************************************************/


	/**
	 * Checks whether or not all input the user specified on this panel is
	 * valid.  This should be overridden to check, for example, whether
	 * text fields have valid values, etc.  This method will be called
	 * whenever the user clicks "OK" or "Apply" on the options dialog to
	 * ensure all input is valid.  If it isn't, the component with invalid
	 * data will be given focus and the user will be prompted to fix it.<br>
	 * 
	 *
	 * @return <code>null</code> if the panel has all valid inputs, or an
	 *         <code>OptionsPanelCheckResult</code> if an input was invalid.
	 *         This component is the one that had the error and will be
	 *         given focus, and the string is an error message that will be
	 *         displayed.
	 */
	public OptionsPanelCheckResult ensureValidInputs() {
		// They can't input invalid stuff on this options panel.
		return null;
	}


/*****************************************************************************/


	/**
	 * Returns the <code>JComponent</code> at the "top" of this Options
	 * panel.  This is the component that will receive focus if the user
	 * switches to this Options panel in the Options dialog.  As an added
	 * bonus, if this component is a <code>JTextComponent</code>, its
	 * text is selected for easy changing.
	 *
	 * @return The top <code>JComponent</code>.
	 */
	public JComponent getTopJComponent() {
		return hiddenFilesCheckBox;
	}

⌨️ 快捷键说明

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