📄 templateoptionpanel.java
字号:
contentPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
JLabel idLabel = RUtilities.createLabel(msg,
"ID", "IDMnemonic");
idField = new JTextField(20);
Document doc = idField.getDocument();
doc.addDocumentListener(this);
if (doc instanceof AbstractDocument) {
((AbstractDocument)doc).setDocumentFilter(
new TemplateNameDocumentFilter());
}
idLabel.setLabelFor(idField);
JPanel temp = new JPanel();
temp.setLayout(new BoxLayout(temp, BoxLayout.X_AXIS));
temp.add(idLabel);
temp.add(Box.createHorizontalStrut(5));
temp.add(idField);
temp.add(Box.createHorizontalGlue());
JPanel temp2 = new JPanel(new BorderLayout());
temp2.add(temp, BorderLayout.WEST);
contentPane.add(temp2, BorderLayout.NORTH);
temp = new JPanel(new GridLayout(2,1, 5,5));
temp.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
JLabel label = RUtilities.createLabel(msg,
"BeforeCaretText", "BeforeCaretMnemonic");
bcTextArea = new JTextArea(4, 30);
label.setLabelFor(bcTextArea);
RScrollPane sp = new RScrollPane(bcTextArea);
temp2 = new JPanel(new BorderLayout());
temp2.add(label, BorderLayout.NORTH);
temp2.add(sp);
temp.add(temp2);
label = RUtilities.createLabel(msg, "AfterCaretText",
"AfterCaretMnemonic");
acTextArea = new JTextArea(4, 30);
label.setLabelFor(acTextArea);
sp = new RScrollPane(acTextArea);
temp2 = new JPanel(new BorderLayout());
temp2.add(label, BorderLayout.NORTH);
temp2.add(sp);
temp.add(temp2);
contentPane.add(temp);
temp = new JPanel();
temp2 = new JPanel(new GridLayout(1,2, 5,5));
okButton = RUtilities.createRButton(msg,
"OK", "OKMnemonic");
okButton.addActionListener(this);
okButton.setEnabled(false);
temp2.add(okButton);
cancelButton = RUtilities.createRButton(msg,
"Cancel", "CancelMnemonic");
cancelButton.addActionListener(this);
temp2.add(cancelButton);
temp.add(temp2);
contentPane.add(temp, BorderLayout.SOUTH);
setContentPane(contentPane);
setLocationRelativeTo(TemplateOptionPanel.this);
setModal(true);
getRootPane().setDefaultButton(okButton);
pack();
}
public void actionPerformed(ActionEvent e) {
this.setVisible(false);
Object source = e.getSource();
if (source==okButton) {
id = idField.getText().toCharArray();
beforeCaret = bcTextArea.getText();
afterCaret = acTextArea.getText();
}
else if (source==cancelButton) {
id = null;
beforeCaret = afterCaret = null;
}
}
public void changedUpdate(DocumentEvent e) {
}
/**
* @return The after-caret text entered, or <code>null</code> if
* the dialog was cancelled.
*/
public String getAfterCaretText() {
return afterCaret;
}
/**
* @return The before-caret text entered, or <code>null</code> if
* the dialog was cancelled.
*/
public String getBeforeCaretText() {
return beforeCaret;
}
/**
* @return The id entered, or <code>null</code> if
* the dialog was cancelled.
*/
public char[] getID() {
return id;
}
public Object[] getNewRowInfo(Object[] oldData) {
if (oldData==null) { // Adding a new row.
setTitle(msg.getString("AddTemplateTitle"));
setID(null);
setBeforeCaretText(null);
setAfterCaretText(null);
}
else { // Modifying a row.
setTitle(msg.getString("ModifyTemplateTitle"));
setID((String)oldData[0]);
CodeTemplate template = (CodeTemplate)oldData[1];
setBeforeCaretText(template.getBeforeCaretText());
setAfterCaretText(template.getAfterCaretText());
}
setVisible(true);
char[] id = getID();
Object[] objs = null;
if (id!=null) {
String idString = new String(id);
objs = new Object[] { idString,
new CodeTemplate(idString,
getBeforeCaretText(),
getAfterCaretText())
};
}
return objs;
}
public void insertUpdate(DocumentEvent e) {
if (!okButton.isEnabled())
okButton.setEnabled(true);
}
public void removeUpdate(DocumentEvent e) {
int length = idField.getDocument().getLength();
if (length==0)
okButton.setEnabled(false);
}
public void setAfterCaretText(String text) {
acTextArea.setText(text);
}
public void setBeforeCaretText(String text) {
bcTextArea.setText(text);
}
public void setID(String id) {
idField.setText(id);
}
public boolean shouldRemoveRow(int row) {
return true;
}
}
/*****************************************************************************/
/**
* A document filter that only allows letters, numbers, and
* '_' to go through.
*/
class TemplateNameDocumentFilter extends DocumentFilter {
private final String cleanse(String text) {
boolean beep = false;
if (text!=null) {
int length = text.length();
for (int i=0; i<length; i++) {
if (!RSyntaxUtilities.
isLetterOrDigit(text.charAt(i)) &&
text.charAt(i)!='_') {
text = text.substring(0,i) + text.substring(i+1);
i--;
length--;
beep = true;
}
}
}
if (beep)
UIManager.getLookAndFeel().provideErrorFeedback(null);
return text;
}
public void insertString(DocumentFilter.FilterBypass fb,
int offset, String text, AttributeSet attr)
throws BadLocationException {
fb.insertString(offset, cleanse(text), attr);
}
public void remove(DocumentFilter.FilterBypass fb,
int offset, int length)
throws BadLocationException {
fb.remove(offset, length);
}
public void replace(DocumentFilter.FilterBypass fb,
int offset, int length, String text, AttributeSet attr)
throws BadLocationException {
fb.replace(offset, length, cleanse(text), attr);
}
}
/*****************************************************************************/
/**
* Table data for the "templates" table.
*/
class TemplateTableModel extends DefaultTableModel {
/**
*
*/
private static final long serialVersionUID = -2857623546361968293L;
public String[] columnNames;
public TemplateTableModel(String templateHeader,
String expansionHeader) {
super();
columnNames = new String[2];
columnNames[0] = templateHeader;
columnNames[1] = expansionHeader;
}
public int getColumnCount() {
return columnNames.length;
}
public String getColumnName(int column) {
return columnNames[column];
}
/**
* Always returns false; modifications are made via the buttons
* on the panel.
*
* @return <code>false</code> always.
*/
public boolean isCellEditable(int row, int column) {
return false;
}
/**
* Sets this table's contents to be the templates known by the
* specified template manager.
*/
public void setTemplates(CodeTemplateManager manager) {
setRowCount(0);
if (manager!=null) {
CodeTemplate[] templates = manager.getTemplates();
int count = templates.length;
for (int i=0; i<count; i++) {
tableModel.addRow(new Object[] {
new String(templates[i].getID()),
// Deep copy.
new CodeTemplate(templates[i])
});
}
}
}
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -