下面列出了怎么用javax.swing.undo.UndoManager的API类实例代码及写法,或者点击链接到github查看源代码。
static void flushUndoQueue(Document d) {
SwingUtilities.invokeLater(() -> {
if (d == null) {
return;
}
for (TopComponent tc : TopComponent.getRegistry().getOpened()) {
if (!(tc instanceof ConsoleEditor)) {
continue;
}
ConsoleEditor cake = (ConsoleEditor)tc;
if (cake.getEditorPane() == null) {
continue;
}
Document check = cake.getEditorPane().getDocument();
if (check != d) {
continue;
}
UndoRedo ur = tc.getUndoRedo();
if (ur instanceof UndoManager) {
((UndoManager)ur).discardAllEdits();
}
}});
}
public void testUndoRedoWithoutIdentity() throws Exception {
mModel = Util.loadModel("resources/test1_noname.xml");
UndoManager ur = new UndoManager();
mModel.addUndoableEditListener(ur);
E e1 = mModel.getRootComponent().getChild(E.class);
assertNull(e1.getValue());
mModel.startTransaction();
String v = "new test value";
e1.setValue(v);
mModel.endTransaction();
assertEquals(v, e1.getValue());
ur.undo();
assertNull("expect null, get "+e1.getValue(), e1.getValue());
ur.redo();
assertEquals(v, e1.getValue());
}
@Override()
public void displayConfig(WSDLComponent component, UndoManager undoManager) {
UndoCounter undoCounter = new UndoCounter();
WSDLModel model = component.getModel();
model.addUndoableEditListener(undoCounter);
JPanel profConfigPanel = new Kerberos(component, this);
DialogDescriptor dlgDesc = new DialogDescriptor(profConfigPanel, getDisplayName());
Dialog dlg = DialogDisplayer.getDefault().createDialog(dlgDesc);
dlg.setVisible(true);
if (dlgDesc.getValue() == DialogDescriptor.CANCEL_OPTION) {
for (int i=0; i<undoCounter.getCounter();i++) {
if (undoManager.canUndo()) {
undoManager.undo();
}
}
}
model.removeUndoableEditListener(undoCounter);
}
@Override()
public void displayConfig(WSDLComponent component, UndoManager undoManager) {
UndoCounter undoCounter = new UndoCounter();
WSDLModel model = component.getModel();
model.addUndoableEditListener(undoCounter);
JPanel profConfigPanel = new STSIssuedEndorsing(component, this);
DialogDescriptor dlgDesc = new DialogDescriptor(profConfigPanel, getDisplayName());
Dialog dlg = DialogDisplayer.getDefault().createDialog(dlgDesc);
dlg.setVisible(true);
if (dlgDesc.getValue() == DialogDescriptor.CANCEL_OPTION) {
for (int i=0; i<undoCounter.getCounter();i++) {
if (undoManager.canUndo()) {
undoManager.undo();
}
}
}
model.removeUndoableEditListener(undoCounter);
}
@Override()
public void displayConfig(WSDLComponent component, UndoManager undoManager) {
UndoCounter undoCounter = new UndoCounter();
WSDLModel model = component.getModel();
model.addUndoableEditListener(undoCounter);
JPanel profConfigPanel = new STSIssuedSupportingToken(component, this);
DialogDescriptor dlgDesc = new DialogDescriptor(profConfigPanel, getDisplayName());
Dialog dlg = DialogDisplayer.getDefault().createDialog(dlgDesc);
dlg.setVisible(true);
if (dlgDesc.getValue() == DialogDescriptor.CANCEL_OPTION) {
for (int i=0; i<undoCounter.getCounter();i++) {
if (undoManager.canUndo()) {
undoManager.undo();
}
}
}
model.removeUndoableEditListener(undoCounter);
}
@Override()
public void displayConfig(WSDLComponent component, UndoManager undoManager) {
UndoCounter undoCounter = new UndoCounter();
WSDLModel model = component.getModel();
model.addUndoableEditListener(undoCounter);
JPanel profConfigPanel = new EndorsingCertificate(component, this);
DialogDescriptor dlgDesc = new DialogDescriptor(profConfigPanel, getDisplayName());
Dialog dlg = DialogDisplayer.getDefault().createDialog(dlgDesc);
dlg.setVisible(true);
if (dlgDesc.getValue() == DialogDescriptor.CANCEL_OPTION) {
for (int i=0; i<undoCounter.getCounter();i++) {
if (undoManager.canUndo()) {
undoManager.undo();
}
}
}
model.removeUndoableEditListener(undoCounter);
}
@Override()
public void displayConfig(WSDLComponent component, UndoManager undoManager) {
UndoCounter undoCounter = new UndoCounter();
WSDLModel model = component.getModel();
model.addUndoableEditListener(undoCounter);
JPanel profConfigPanel = new SAMLHolderOfKey(component, this);
DialogDescriptor dlgDesc = new DialogDescriptor(profConfigPanel, getDisplayName());
Dialog dlg = DialogDisplayer.getDefault().createDialog(dlgDesc);
dlg.setVisible(true);
if (dlgDesc.getValue() == DialogDescriptor.CANCEL_OPTION) {
for (int i=0; i<undoCounter.getCounter();i++) {
if (undoManager.canUndo()) {
undoManager.undo();
}
}
}
model.removeUndoableEditListener(undoCounter);
}
@Override()
public void displayConfig(WSDLComponent component, UndoManager undoManager) {
UndoCounter undoCounter = new UndoCounter();
WSDLModel model = component.getModel();
model.addUndoableEditListener(undoCounter);
JPanel profConfigPanel = new STSIssuedCert(component, this);
DialogDescriptor dlgDesc = new DialogDescriptor(profConfigPanel, getDisplayName());
Dialog dlg = DialogDisplayer.getDefault().createDialog(dlgDesc);
dlg.setVisible(true);
if (dlgDesc.getValue() == DialogDescriptor.CANCEL_OPTION) {
for (int i=0; i<undoCounter.getCounter();i++) {
if (undoManager.canUndo()) {
undoManager.undo();
}
}
}
model.removeUndoableEditListener(undoCounter);
}
public void testUndoOnMutationFromSyncEvent() throws Exception {
defaultSetup();
mModel.addComponentListener(new Handler());
UndoManager um = new UndoManager();
mModel.addUndoableEditListener(um);
Util.setDocumentContentTo(mDoc, "resources/test1_2.xml");
mModel.sync();
D d = mModel.getRootComponent().getChild(D.class);
assertNotNull(d.getChild(B.class));
um.undo();
mModel.getAccess().flush(); // after fix for 83963 need manual flush after undo/redo
assertNull(mModel.getRootComponent().getChild(D.class));
mModel = Util.dumpAndReloadModel(mModel);
assertNull(mModel.getRootComponent().getChild(D.class));
}
public void testLongLineUndo() throws Exception {
loggingOn();
JEditorPane pane = ViewUpdatesTesting.createPane();
Document doc = pane.getDocument();
UndoManager undoManager = ViewUpdatesTesting.getUndoManager(doc);
int lineLen = 4000;
StringBuilder sb = new StringBuilder(lineLen + 10);
for (int i = 0; i < lineLen; i++) {
sb.append('a');
}
sb.append('\n');
doc.insertString(0, sb.toString(), null);
pane.modelToView(0);
doc.remove(0, lineLen);
pane.modelToView(0);
undoManager.undo();
undoManager.redo();
}
public void testBehaviour() throws Exception {
Document doc = new PlainDocument();
doc.insertString(0, "test hello world", null);
UndoManager undo = new UndoManager();
doc.addUndoableEditListener(undo);
Position pos = doc.createPosition(2);
doc.remove(0, 3);
assert (pos.getOffset() == 0);
undo.undo();
assert (pos.getOffset() == 2);
Position pos2 = doc.createPosition(5);
doc.remove(4, 2);
Position pos3 = doc.createPosition(4);
assertSame(pos2, pos3);
undo.undo();
assert (pos3.getOffset() == 5);
}
public void testCreateGlobalElementUndoRedo() throws Exception {
SchemaModel model = Util.loadSchemaModel("resources/Empty.xsd");
UndoManager ur = new UndoManager();
model.addUndoableEditListener(ur);
SchemaComponentFactory fact = model.getFactory();
GlobalElement ge = fact.createGlobalElement();
model.startTransaction();
model.getSchema().addElement(ge);
ge.setName("Foo"); // edit #1
LocalComplexType lct = fact.createLocalComplexType();
Sequence seq = fact.createSequence();
lct.setDefinition(seq);
ge.setInlineType(lct);
model.endTransaction();
assertEquals(1, model.getSchema().getElements().size());
ur.undo();
assertEquals(0, model.getSchema().getElements().size());
ur.redo();
ge = model.getSchema().getElements().iterator().next();
assertEquals("Foo", ge.getName());
assertNotNull(ge.getInlineType());
assertNotNull(((LocalComplexType)ge.getInlineType()).getDefinition());
}
public void testSyncUndoRename() throws Exception {
SchemaModel model = Util.loadSchemaModel(TEST_XSD);
UndoManager um = new UndoManager();
model.addUndoableEditListener(um);
assertEquals(2, model.getSchema().getElements().size());
Util.setDocumentContentTo(model, "resources/PurchaseOrder_SyncUndoRename.xsd");
model.sync();
assertEquals(2, model.getSchema().getElements().size());
assertEquals("purchaseOrder2", model.getSchema().getElements().iterator().next().getName());
um.undo();
assertEquals(2, model.getSchema().getElements().size());
assertEquals("purchaseOrder", model.getSchema().getElements().iterator().next().getName());
um.redo();
assertEquals(2, model.getSchema().getElements().size());
assertEquals("purchaseOrder2", model.getSchema().getElements().iterator().next().getName());
}
public void testMultipleMutationUndoRedo() throws Exception {
mModel = Util.loadModel("resources/Empty.xml");
UndoManager urListener = new UndoManager();
mModel.addUndoableEditListener(urListener);
//setup
mModel.startTransaction();
B b2 = new B(mModel, 2);
mModel.getRootComponent().addAfter(b2.getName(), b2, TestComponent3._A);
String v = "testComponentListener.b2";
b2.setValue(v);
mModel.endTransaction();
b2 = mModel.getRootComponent().getChild(B.class);
assertEquals(v, b2.getAttribute(TestAttribute3.VALUE));
urListener.undo();
b2 = mModel.getRootComponent().getChild(B.class);
assertNull(b2);
urListener.redo();
b2 = mModel.getRootComponent().getChild(B.class);
assertEquals(v, b2.getAttribute(TestAttribute3.VALUE));
}
public void testSyncUndoRedo() throws Exception {
defaultSetup();
UndoManager urListener = new UndoManager();
mModel.addUndoableEditListener(urListener);
assertEquals("setup: initial", 1, mModel.getRootComponent().getChildren(C.class).size());
Util.setDocumentContentTo(mDoc, "resources/test2.xml");
mModel.sync();
assertEquals("setup: sync", 0, mModel.getRootComponent().getChildren(C.class).size());
urListener.undo();
assertEquals("undo sync", 1, mModel.getRootComponent().getChildren(C.class).size());
urListener.redo();
assertEquals("undo sync", 0, mModel.getRootComponent().getChildren(C.class).size());
}
public UndoManagerImpl(IGanttProject project, ParserFactory parserFactory, DocumentManager documentManager) {
myProject = project;
myParserFactory = parserFactory;
myDocumentManager = documentManager;
mySwingUndoManager = new UndoManager();
myUndoEventDispatcher = new UndoableEditSupport();
GanttLanguage.getInstance().addListener(new GanttLanguage.Listener() {
public void languageChanged(Event event) {
UIManager.getDefaults().put("AbstractUndoableEdit.undoText", GanttLanguage.getInstance().getText("undo"));
UIManager.getDefaults().put("AbstractUndoableEdit.redoText", GanttLanguage.getInstance().getText("redo"));
}
});
}
public EditorPaneUndoRedo(EditorPane pane) {
undoManager = new UndoManager();
undoAction = new UndoAction();
redoAction = new RedoAction();
editorPane = pane;
// editorPane.getInputMap().put(undoKeystroke, "undoKeystroke");
// editorPane.getActionMap().put("undoKeystroke", undoAction);
//
// editorPane.getInputMap().put(redoKeystroke, "redoKeystroke");
// editorPane.getActionMap().put("redoKeystroke", redoAction);
}
public CommandUndo(UndoManager undo) {
super("Undo");
this.undo=undo;
setEnabled(false);
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_Z, ActionEvent.CTRL_MASK));
}
public void testRedoWithFaultInFindComponent() throws Exception {
setupForUndoRedoFault(new FaultInjector() {
@Override
public void injectFaultAndCheck(Object actor) throws Exception {
((UndoManager)actor).undo();
mModel.injectFaultInFindComponent();
((UndoManager)actor).redo();
}
}, true);
}
private void initializeModels(Node node) throws Exception {
if (wsdlModels.isEmpty()) {
undoManager = new UndoManager();
WSDLModel primaryModel = getPrimaryModel(node);
populateAllModels(primaryModel);
Set<WSDLModel> modelSet = wsdlModels.keySet();
for (WSDLModel wsdlModel : modelSet) {
wsdlModel.addUndoableEditListener(undoManager);
}
}
}
public TextStandardActions(JTextComponent textComponent) {
this.textComponent = textComponent;
this.undoManager = new UndoManager();
initActions();
addPopupItems();
addKeyActions();
registerListeners();
}
public @Override void actionPerformed(ActionEvent evt, JTextComponent target) {
Document doc = target.getDocument();
if (doc.getProperty(BaseDocument.UNDO_MANAGER_PROP) != null ||
doc.getProperty(UndoManager.class) != null )
{
super.actionPerformed(evt, target);
} else { // Deleagte to system undo action
// Delegate to system redo action
RedoAction ra = (RedoAction)SystemAction.get(RedoAction.class);
if (ra != null && ra.isEnabled()) {
ra.actionPerformed(evt);
}
}
}
public ServiceTopComponent(Service service,
JaxWsModel jaxWsModel, WSDLModel wsdlModel, Node node, UndoManager undoManager) {
setLayout(new BorderLayout());
this.wsdlModel = wsdlModel;
this.undoManager = undoManager;
this.node = node;
this.service = service;
this.jaxWsModel = jaxWsModel;
}
protected void trimEdits(int from, int to) {
boolean undoable = canUndo();
boolean redoable = canRedo();
boolean changed = hasChanged();
super.trimEdits(from, to);
firePropertyChangeEvent(UndoManager.UndoName, undoable, canUndo());
firePropertyChangeEvent(UndoManager.RedoName, redoable, canRedo());
}
@Override
public void adjust() {
Undoable undoable = getTarget(Undoable.class);
if (undoable != null) {
UndoManager mgr = undoable.getUndoManager();
setEnabled(mgr.canRedo());
setTitle(mgr.getRedoPresentationName());
} else {
setEnabled(false);
setTitle(I18n.Text("Can't Redo"));
}
}
@Override
public void adjust() {
Undoable undoable = getTarget(Undoable.class);
if (undoable != null) {
UndoManager mgr = undoable.getUndoManager();
setEnabled(mgr.canUndo());
setTitle(mgr.getUndoPresentationName());
} else {
setEnabled(false);
setTitle(I18n.Text("Can't Undo"));
}
}
public void testRedoWithFaultInSyncUpdater() throws Exception {
setupForUndoRedoFault(new FaultInjector() {
@Override
public void injectFaultAndCheck(Object actor) throws Exception {
((UndoManager)actor).undo();
mModel.injectFaultInSyncUpdater();
((UndoManager)actor).redo();
}
}, true);
}
@Override
public synchronized void addUndoableRefactorListener(UndoableEditListener uel) {
//
savedUndoableEditListeners = ues.getUndoableEditListeners();
if (savedUndoableEditListeners != null) {
for (UndoableEditListener saved : savedUndoableEditListeners) {
if (saved instanceof UndoManager) {
((UndoManager)saved).discardAllEdits();
}
}
}
ues = new ModelUndoableEditSupport();
ues.addUndoableEditListener(uel);
}
public static UndoManager getValidUndoManager(PropertyProvider provider) {
UndoManager undoManager = getUndoManager(provider);
if (undoManager == null) {
throw new IllegalStateException("Null UndoManager for property provider " + provider); // NOI18N
}
return undoManager;
}
public void testUndoWithFaultInEventFiring() throws Exception {
setupForUndoRedoFault(new FaultInjector() {
@Override
public void injectFaultAndCheck(Object actor) throws Exception {
mModel.injectFaultInEventFiring();
((UndoManager)actor).undo();
}
}, false);
}