package org.alvin.chapter2;
import java.awt.EventQueue;
public class UndoFrame extends JFrame {
private JPanel contentPane;
private JTree tree;
private KeyStroke restroe_key = KeyStroke.getKeyStroke("control Y");
private KeyStroke back_key = KeyStroke.getKeyStroke("control Z");
private UndoManager undo = new UndoManager();
private RedoEditAction redoAction = new RedoEditAction("恢复");
private UndoAction undoAction = new UndoAction("撤销");
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UndoFrame frame = new UndoFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public UndoFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 632, 456);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
gbl_contentPane.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 39,
0, 0 };
gbl_contentPane.columnWeights = new double[] { 1.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
gbl_contentPane.rowWeights = new double[] { 1.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
contentPane.setLayout(gbl_contentPane);
JScrollPane scrollPane = new JScrollPane();
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
gbc_scrollPane.gridheight = 10;
gbc_scrollPane.gridwidth = 9;
gbc_scrollPane.insets = new Insets(0, 0, 5, 0);
gbc_scrollPane.fill = GridBagConstraints.BOTH;
gbc_scrollPane.gridx = 0;
gbc_scrollPane.gridy = 0;
contentPane.add(scrollPane, gbc_scrollPane);
MyTextArea textArea = new MyTextArea();
scrollPane.setViewportView(textArea);
JButton button_1 = new JButton("撤销");
GridBagConstraints gbc_button_1 = new GridBagConstraints();
gbc_button_1.anchor = GridBagConstraints.NORTH;
gbc_button_1.insets = new Insets(0, 0, 0, 5);
gbc_button_1.gridx = 5;
gbc_button_1.gridy = 10;
contentPane.add(button_1, gbc_button_1);
JButton button = new JButton("恢复");
GridBagConstraints gbc_button = new GridBagConstraints();
gbc_button.anchor = GridBagConstraints.NORTH;
gbc_button.insets = new Insets(0, 0, 0, 5);
gbc_button.gridx = 7;
gbc_button.gridy = 10;
contentPane.add(button, gbc_button);
injectAction(textArea);
textArea.getDocument().addUndoableEditListener(undo);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (undo.canRedo()) {
undo.redo();
} else {
System.out.println("没有恢复的了");
}
}
});
button_1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (undo.canUndo()) {
undo.undo();
} else {
System.out.println("没有撤销的了");
}
}
});
}
abstract class KeyAction extends AbstractAction {
@Override
public abstract boolean isEnabled();
@Override
protected void firePropertyChange(String propertyName, Object oldValue,
Object newValue) {
boolean value = isEnabled();
super.firePropertyChange("enabled", Boolean.valueOf(!value),
Boolean.valueOf(value));
}
}
public void injectAction(JComponent component) {
InputMap input = component.getInputMap();
ActionMap action = component.getActionMap();
action.put("redo", redoAction);
input.put(restroe_key, "redo");
action.put("undo", undoAction);
input.put(back_key, "undo");
}
class RedoEditAction extends AbstractAction {
private static final long serialVersionUID = 1L;
RedoEditAction(String text) {
super(text);
}
RedoEditAction(String text, Icon icon) {
super(text, icon);
}
@Override
public boolean isEnabled() {
return undo.canRedo();
}
@Override
public void actionPerformed(ActionEvent e) {
if (undo.canRedo()) {
undo.redo();
}
}
}
class UndoAction extends AbstractAction {
public UndoAction(String text) {
super(text);
}
public UndoAction(String text, Icon icon) {
super(text, icon);
}
@Override
public void actionPerformed(ActionEvent e) {
if (undo.canUndo()) {
undo.undo();
}
}
}
class MyTextArea extends JTextArea implements DropTargetListener {
public MyTextArea() {
new DropTarget(this, DnDConstants.ACTION_COPY_OR_MOVE, this);
}
public void dragEnter(DropTargetDragEvent dtde) {
}
public void dragOver(DropTargetDragEvent dtde) {
}
public void dropActionChanged(DropTargetDragEvent dtde) {
}
public void dragExit(DropTargetEvent dte) {
}
public void drop(DropTargetDropEvent dtde) {
try {
Transferable tr = dtde.getTransferable();
if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
System.out.println("file cp");
List list = (List) (dtde.getTransferable()
.getTransferData(DataFlavor.javaFileListFlavor));
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
File f = (File) iterator.next();
this.setText(f.getAbsolutePath());
}
dtde.dropComplete(true);
this.updateUI();
} else {
dtde.rejectDrop();
}
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (UnsupportedFlavorException ufe) {
ufe.printStackTrace();
}
}
}
}