📄 macrooptionpanel.java
字号:
/*
* 10/17/2004
*
* MacroOptionPanel.java - Option panel letting the user manage their recorded
* macros.
* 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.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ResourceBundle;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.fife.RListSelectionModel;
import org.fife.ui.OptionsDialogPanel;
import org.fife.ui.RButton;
import org.fife.ui.RScrollPane;
import org.fife.ui.UIUtilities;
/**
* Option panel letting the user manage their recorded macros.
*
* @author Robert Futrell
* @version 0.1
*/
class MacroOptionPanel extends OptionsDialogPanel
implements ActionListener, ListSelectionListener {
private JList macroList;
private DefaultListModel listModel;
private RButton removeButton;
/*****************************************************************************/
/**
* Constructor.
*
* @param rtext The owner of the options dialog in which this panel
* appears.
* @param msg The resource bundle to use.
*/
public MacroOptionPanel(final RText rtext, final ResourceBundle msg) {
super(msg.getString("OptMaName"));
Border empty5Border = UIUtilities.getEmpty5Border();
setBorder(BorderFactory.createCompoundBorder(
empty5Border,
BorderFactory.createCompoundBorder(
new OptionPanelBorder(msg.getString("OptMaTit")),
empty5Border)));
setLayout(new BorderLayout());
JPanel macroListPanel = new JPanel();
macroListPanel.setLayout(new BoxLayout(macroListPanel,
BoxLayout.Y_AXIS));
listModel = new DefaultListModel();
macroList = new JList(listModel);
macroList.setSelectionModel(new RListSelectionModel());
macroList.addListSelectionListener(this);
RScrollPane scrollPane = new RScrollPane(macroList);
macroListPanel.add(scrollPane);
add(macroListPanel);
JPanel buttonPanel = new JPanel(new BorderLayout());
buttonPanel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
removeButton = new RButton(msg.getString("RemoveButtonLabel"));
removeButton.setActionCommand("Remove");
removeButton.addActionListener(this);
buttonPanel.add(removeButton, BorderLayout.WEST);
add(buttonPanel, BorderLayout.SOUTH);
}
/*****************************************************************************/
/**
* Listens for actions on this panel.
*
* @param e The action that occured.
*/
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
if (actionCommand.equals("Remove")) {
ListData selection = (ListData)macroList.getSelectedValue();
if (selection==null)
throw new InternalError("Macro list had no selection!");
File macroFile = selection.getFile();
boolean success = macroFile.delete();
if (success==false) {
// FIXME: Add dialog box message or somethin'.
}
int index = macroList.getSelectedIndex();
listModel.remove(index);
macroList.setSelectedIndex(
Math.min(index, listModel.getSize()-1));
removeButton.setEnabled(macroList.getSelectedIndex()>-1);
}
}
/*****************************************************************************/
/**
* 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 macroList;
}
/*****************************************************************************/
/**
* Initializes the macro list.
*/
public void initialize() {
// Get the (guaranteed non-null) macro files array.
File[] macroFiles = RTextUtilities.getSavedMacroFiles();
int count = macroFiles.length;
// Initialize the macro list.
listModel.removeAllElements();
if (count>0) {
for (int i=0; i<count; i++)
listModel.addElement(new ListData(macroFiles[i]));
macroList.setSelectedIndex(0);
}
// Enable/disable the "Remove" button appropriately.
removeButton.setEnabled(count>0);
}
/*****************************************************************************/
/**
* Called when the user changes the language in the language list.
* Do not override.
*/
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting()==false) {
int firstIndex = e.getFirstIndex();
int lastIndex = e.getLastIndex();
if (firstIndex==lastIndex && lastIndex==-1)
macroList.setSelectedIndex(-1);
}
}
/*****************************************************************************/
/********************** PRIVATE INNER CLASSES ********************************/
/*****************************************************************************/
/**
* Wrapper class for the data in the macro list.
*/
private class ListData {
private File file;
public ListData(File file) {
this.file = file;
}
public File getFile() {
return file;
}
public String toString() {
return RTextUtilities.getMacroName(file);
}
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -