📄 templateoptionpanel.java
字号:
/*
* 01/12/2005
*
* TemplateOptionPanel.java - An option panel to manage RSyntaxTextArea's
* templates.
* Copyright (C) 2005 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.rsyntaxtextarea;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.DocumentFilter;
import org.fife.RUtilities;
import org.fife.ui.OptionsDialogPanel;
import org.fife.ui.RButton;
import org.fife.ui.RScrollPane;
import org.fife.ui.modifiabletable.*;
/**
* An options panel that can be added to an
* <code>org.fife.ui.OptionsDialog</code> to manage the templates
* available to all instances of <code>RSyntaxTextArea</code>.
*
* @author Robert Futrell
* @version 0.5
*/
public class TemplateOptionPanel extends OptionsDialogPanel {
/**
*
*/
private static final long serialVersionUID = 2364665660401431073L;
private ModifiableTable templateTable;
private TemplateTableModel tableModel;
private ResourceBundle msg;
private static final String TEMPLATE_PROPERTY = "template";
/*****************************************************************************/
/**
* Constructor.
*/
public TemplateOptionPanel() {
super();
msg = ResourceBundle.getBundle(
"org.fife.ui.rsyntaxtextarea.TemplateOptionPanel");
setName(msg.getString("Title"));
Listener listener = new Listener();
tableModel = new TemplateTableModel(msg.getString("Template"),
msg.getString("Expansion"));
templateTable = new ModifiableTable(tableModel);
templateTable.getTable().getColumn(msg.getString("Expansion")).
setCellRenderer(new ExpansionCellRenderer());
templateTable.addModifiableTableListener(listener);
// Put it all together.
setLayout(new BorderLayout());
Border empty5Border = BorderFactory.createEmptyBorder(5,5,5,5);
setBorder(BorderFactory.createCompoundBorder(
empty5Border,
BorderFactory.createCompoundBorder(
new OptionPanelBorder(msg.getString("Templates")),
empty5Border)));
add(templateTable); // Add to center so table stretches properly.
// Must create and set row handler later, after this option panel has
// been added to the parent Options dialog.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
RowHandler rowHandler = new TemplateDialog();
templateTable.setRowHandler(rowHandler);
}
});
}
/*****************************************************************************/
/**
* Checks whether or not all input the user specified on this panel is
* valid.
*
* @return This method always returns <code>null</code> as the user cannot
* mess up input in this panel.
*/
public OptionsPanelCheckResult ensureValidInputs() {
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.
*/
public JComponent getTopJComponent() {
return templateTable.getTable();
}
/*****************************************************************************/
/**
* Initializes this component's widgets to reflect the state of templates
* for <code>RSyntaxTextArea</code>s.
*/
public void initialize() {
CodeTemplateManager manager = RSyntaxTextArea.
getCodeTemplateManager();
// Remove all old templates and load the new ones.
tableModel.setTemplates(manager);
}
/*****************************************************************************/
/**
* Updates <code>RSyntaxTextArea</code>'s code template manager
* with the information entered here by the user.
*/
public void updateCodeTemplateManager() {
CodeTemplateManager manager = RSyntaxTextArea.
getCodeTemplateManager();
if (manager!=null) {
int count = tableModel.getRowCount();
CodeTemplate[] templates = new CodeTemplate[count];
for (int i=0; i<count; i++) {
// Make deep copies in case they're clicking "Apply."
templates[i] = new CodeTemplate(
(CodeTemplate)tableModel.getValueAt(i, 1));
}
manager.replaceTemplates(templates);
templates = null;
}
}
/*****************************************************************************/
/********************* PRIVATE INNER CLASSES *********************************/
/*****************************************************************************/
/**
* Renders an "expansion" cell for the table. This simply paints newlines
* as "\n" and tabs as "\t".
*/
class ExpansionCellRenderer extends DefaultTableCellRenderer {
/**
*
*/
private static final long serialVersionUID = 8804207480291519150L;
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected,
boolean hasFocus, int row, int column)
{
super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
CodeTemplate template = (CodeTemplate)value;
String foo = template.getBeforeCaretText() +
template.getAfterCaretText();
this.setText(foo.replaceAll("\\n", "\\\\n").
replaceAll("\\t", "\\\\t"));
return this;
}
}
/*****************************************************************************/
/**
* Listens for events in this option panel.
*/
class Listener implements ModifiableTableListener {
// A row was added, removed or modified in the template table.
public void modifiableTableChanged(ModifiableTableChangeEvent e) {
hasUnsavedChanges = true;
TemplateOptionPanel.this.firePropertyChange(TEMPLATE_PROPERTY,
null, templateTable);
}
}
/*****************************************************************************/
/**
* Dialog for modifying a template.
*/
class TemplateDialog extends JDialog implements ActionListener,
DocumentListener, RowHandler {
/**
*
*/
private static final long serialVersionUID = -5439289587879677620L;
private JTextField idField;
private JTextArea bcTextArea;
private JTextArea acTextArea;
private RButton okButton;
private RButton cancelButton;
private char[] id;
private String beforeCaret;
private String afterCaret;
public TemplateDialog() {
super(getOptionsDialog());
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -