📄 filefilteroptionpanel.java
字号:
/*
* 03/01/2004
*
* FileFilterOptionPanel.java - Option panel letting the user configure
* what extensions get opened with what syntax highlighting
* scheme.
* 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.rtext;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;
import javax.swing.AbstractCellEditor;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.plaf.ButtonUI;
import javax.swing.plaf.basic.BasicButtonUI;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;
import org.fife.RListSelectionModel;
import org.fife.ui.OptionsDialogPanel;
import org.fife.ui.RButton;
import org.fife.ui.RScrollPane;
import org.fife.ui.UIUtilities;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
/**
* Option panel letting the user configure what files get opened with what
* syntax highlighting scheme.
*
* @author Robert Futrell
* @version 0.2
*/
class FileFilterOptionPanel extends OptionsDialogPanel
implements ActionListener {
private JTable filterTable;
private FilterTableModel filterTableModel;
private RText rtext;
/*****************************************************************************/
/**
* Constructor.
*
* @param rtext The owner of the options dialog in which this panel
* appears.
* @param msg The resource bundle to use.
*/
public FileFilterOptionPanel(final RText rtext, final ResourceBundle msg) {
super(msg.getString("OptFFName"));
this.rtext = rtext;
Border empty5Border = UIUtilities.getEmpty5Border();
setBorder(empty5Border);
setLayout(new BorderLayout());
JPanel filterPanel = new JPanel();
filterPanel.setBorder(BorderFactory.createCompoundBorder(
new OptionPanelBorder(msg.getString("OptFFLabel")),
empty5Border));
filterPanel.setLayout(new BoxLayout(filterPanel, BoxLayout.Y_AXIS));
JLabel desc = new JLabel(msg.getString("OptFFDesc"));
desc.setAlignmentX(Component.LEFT_ALIGNMENT);
filterPanel.add(desc);
filterPanel.add(Box.createVerticalStrut(5));
filterTableModel = new FilterTableModel(msg.getString("OptFFCol1"), msg.getString("OptFFCol2"));
filterTable = new JTable(filterTableModel);
filterTable.getColumn(msg.getString("OptFFCol2")).setCellEditor(new FileFilterCellEditor());
filterTable.setSelectionModel(new RListSelectionModel());
filterTable.setRowSelectionAllowed(true);
filterTable.getTableHeader().setReorderingAllowed(false);
filterTable.setPreferredScrollableViewportSize(new Dimension(300,150));
RScrollPane filterScrollPane = new RScrollPane(300,150, filterTable);
filterScrollPane.setAlignmentX(Component.LEFT_ALIGNMENT);
filterPanel.add(filterScrollPane);
add(filterPanel);
RButton defaultsButton = new RButton(msg.getString("RestoreDefaults"));
defaultsButton.setActionCommand("RestoreDefaults");
defaultsButton.addActionListener(this);
JPanel temp = new JPanel(new BorderLayout());
temp.add(defaultsButton, BorderLayout.WEST);
add(temp, BorderLayout.SOUTH);
}
/*****************************************************************************/
/**
* Listens for actions in this panel.
*/
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
if (actionCommand.equals("RestoreDefaults")) {
// Empty constructor returns defaults.
SyntaxFilters defaultFilters = new SyntaxFilters();
setSyntaxFilters(defaultFilters);
}
}
/*****************************************************************************/
/**
* 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() {
// Make sure each filter value in the table is valid.
int rowCount = filterTable.getRowCount();
for (int i=0; i<rowCount; i++) {
String filters = (String)filterTable.getValueAt(i, 1);
if (SyntaxFilters.isValidFileFilterString(filters)==false) {
String temp = rtext.getResourceBundle().
getString("InvalidFFString").
replaceFirst("%filterString%", filters);
OptionsPanelCheckResult result =
new OptionsPanelCheckResult(filterTable, temp);
return result;
}
}
// Otherwise, the input was okay.
return null;
}
/*****************************************************************************/
/**
* Returns the syntax file filters accepted by the user.
*
* @return The syntax file filters.
*/
public SyntaxFilters getSyntaxFilters() {
SyntaxFilters filters = new SyntaxFilters();
for (int i=0; i<SyntaxConstants.MAX_SYNTAX_STYLE_NUMBER; i++)
// "i+1" below to skip 0==NO_SYNTAX_STYLE.
filters.setFiltersForSyntaxStyle(i+1, (String)filterTableModel.
getValueAt(i,1));
return filters;
}
/*****************************************************************************/
/**
* 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.
*/
public JComponent getTopJComponent() {
return filterTable;
}
/*****************************************************************************/
/**
* Sets the syntax filters displayed for each file type.
*
* @param syntaxFilters the filters for each type.
* @see SyntaxFilters
*/
public void setSyntaxFilters(SyntaxFilters syntaxFilters) {
String filterString;
/* FIXME: We skip i==0 here because assembler SH isn't done. */
for (int i=0; i<SyntaxConstants.MAX_SYNTAX_STYLE_NUMBER; i++) {
filterString = syntaxFilters.getFilterString(i+1);
filterTableModel.setValueAt(filterString, i,1);
}
}
/*****************************************************************************/
/********************* PRIVATE INNER CLASSES *********************************/
/*****************************************************************************/
/**
* Table data for the "File filter" table.
*/
class FilterTableModel extends AbstractTableModel {
public String[] columnNames;
public Object[][] data = {
{ "Assembler (x86)", null },
{ "C", null },
{ "C++", null },
{ "C#", null },
{ "CSS", null },
{ "Fortran", null },
{ "HTML", null },
{ "Java", null },
{ "JavaScript", null },
{ "Perl", null },
{ "Properties files", null },
{ "Python", null },
{ "SAS", null },
{ "SQL", null },
{ "UNIX shell scripts", null },
{ "Windows Batch", null },
{ "XML", null },
};
public FilterTableModel(String fileTypeHeader, String filterHeader) {
super();
columnNames = new String[2];
columnNames[0] = fileTypeHeader;
columnNames[1] = filterHeader;
}
public Class getColumnClass(int columnIndex) {
return data[0][columnIndex].getClass();
}
public int getColumnCount() {
return columnNames.length;
}
public String getColumnName(int column) {
return columnNames[column];
}
public int getRowCount() {
return data.length;
}
public Object getValueAt(int row, int column) {
return data[row][column];
}
public boolean isCellEditable(int row, int column) {
return (column==1); // Only the filters are editable.
}
public void setValueAt(Object value, int row, int column) {
String oldValue = (String)data[row][column]; // Might be null.
String newValue = ((String)value).trim() + " "; // Just to make it neater; trailing space is needed.
if ( !newValue.equals(oldValue) ) {
data[row][column] = newValue;
fireTableCellUpdated(row, column);
hasUnsavedChanges = true;
firePropertyChange("FileFilterOptionPanel.fileFilterString",
oldValue, value);
}
}
}
/*****************************************************************************/
/**
* Cell editor for file filters. We do it this way because, with a
* default (text field) editor, the user can enter a bad value, but
* before pressing Enter, press OK, and our Options dialog disappears,
* appearing to not validate the bad value. But in actuality,
* that bad value was never set, but the text field still contains
* that value.
*/
class FileFilterCellEditor extends AbstractCellEditor
implements TableCellEditor, ActionListener {
private JButton button; // The table's "editor."
FileFilterCellEditor() {
button = new JButton() {
public void setUI(ButtonUI ui) {
super.setUI(new BasicButtonUI());
}
};
button.setHorizontalAlignment(SwingConstants.LEFT);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String oldValue = (String)filterTableModel.getValueAt(filterTable.getSelectedRow(), 1);
button.setText(oldValue);
String filterType = (String)filterTableModel.getValueAt(filterTable.getSelectedRow(), 0);
String input;
while (true) {
input = JOptionPane.showInputDialog(null,
"Enter the new value for the '" + filterType + "' file filter:");
if (input!=null) {
if (!SyntaxFilters.isValidFileFilterString(input)) {
// We build it this way because String.replaceFirst() can throw regex expressions
// depending on the replacement string, even though the javadoc doesn't imply
// this...
ResourceBundle msg = rtext.getResourceBundle();
String temp = msg.getString("InvalidFFString");
int i = temp.indexOf("%filterString%");
temp = temp.substring(0,i) + input + temp.substring(i+14);
JOptionPane.showMessageDialog(null,
temp,
msg.getString("ErrorDialogTitle"),
JOptionPane.ERROR_MESSAGE);
continue;
}
button.setText(input);
}
break;
} // End of while (true).
fireEditingStopped();
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
button.setBorder(UIManager.
getBorder("Table.focusCellHighlightBorder"));
button.setForeground(table.getSelectionForeground());
button.setBackground(table.getSelectionBackground());
button.setText((String)value);
return button;
}
public Object getCellEditorValue() {
return button.getText();
}
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -