下面列出了javax.swing.undo.UndoManager#undo ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override()
public void displayConfig(WSDLComponent component, UndoManager undoManager) {
UndoCounter undoCounter = new UndoCounter();
WSDLModel model = component.getModel();
model.addUndoableEditListener(undoCounter);
JPanel profConfigPanel = new MutualCertificates(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 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 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);
}
public void testSetAttributeOnGlobalComplexTypeUndoRedo() throws Exception {
SchemaModel model = Util.loadSchemaModel("resources/PurchaseOrder.xsd");
UndoManager ur = new UndoManager();
model.addUndoableEditListener(ur);
GlobalComplexType potype = model.getSchema().getComplexTypes().iterator().next();
assertEquals("PurchaseOrderType", potype.getName());
model.startTransaction();
potype.setAbstract(Boolean.TRUE);
model.endTransaction();
ur.undo();
assertNull(potype.getAttribute(SchemaAttributes.ABSTRACT));
ur.redo();
assertNotNull(potype.getAttribute(SchemaAttributes.ABSTRACT));
}
@Override()
public void displayConfig(WSDLComponent component, UndoManager undoManager) {
UndoCounter undoCounter = new UndoCounter();
WSDLModel model = component.getModel();
model.addUndoableEditListener(undoCounter);
JPanel profConfigPanel = new UsernameAuthPasswordDerivedKeys(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 STSIssued(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 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());
}
@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 testUndoRedoWithIdentity() throws Exception {
mModel = Util.loadModel("resources/test1_name.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());
}
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 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 void testRollback() throws Exception {
UndoManager um = new UndoManager();
schema.getModel().addUndoableEditListener(um);
GlobalElement stick = schema.getModel().getFactory().createGlobalElement();
stick.setName("stickAfterRollbackElement");
model.startTransaction();
schema.addElement(stick);
model.endTransaction();
GlobalElement ge = schema.getModel().getFactory().createGlobalElement();
ge.setName("newElement");
int initialCount = schema.getElements().size();
model.startTransaction();
schema.addElement(ge);
assertEquals(initialCount+1, schema.getElements().size());
String text = (( AbstractDocumentModel)model).getAccess().getCurrentDocumentText();
assertTrue(text.indexOf("newElement") > 0);
( (AbstractModel)model).rollbackTransaction();
text = (( AbstractDocumentModel)model).getAccess().getCurrentDocumentText();
assertTrue(text.indexOf("newElement") == -1);
assertEquals(initialCount, schema.getElements().size());
assertTrue(text.indexOf("stickAfterRollbackElement") > 0);
um.undo();
text = (( AbstractDocumentModel)model).getAccess().getCurrentDocumentText();
assertTrue(text.indexOf("stickAfterRollbackElement") == -1);
um.redo();
text = (( AbstractDocumentModel)model).getAccess().getCurrentDocumentText();
assertTrue(text.indexOf("stickAfterRollbackElement") > 0);
}
public void testDeleteRollback() throws Exception {
UndoManager um = new UndoManager();
schema.getModel().addUndoableEditListener(um);
GlobalElement stick = schema.getModel().getFactory().createGlobalElement();
stick.setName("stickAfterRollbackElement");
model.startTransaction();
schema.addElement(stick);
model.endTransaction();
model.startTransaction();
ArrayList<GlobalComplexType> types = new ArrayList(schema.getComplexTypes());
ArrayList<GlobalElement> elements = new ArrayList(schema.getElements());
GlobalElement element = elements.get(0);
if(element.getName().equals("purchaseOrder")) {
schema.removeElement(element);
String text = (( AbstractDocumentModel)model).getAccess().getCurrentDocumentText();
assertTrue(text.indexOf("purchaseOrder")== -1);
( (AbstractModel)model).rollbackTransaction();
text = (( AbstractDocumentModel)model).getAccess().getCurrentDocumentText();
assertTrue(text.indexOf("purchaseOrder") > 0);
assertTrue(text.indexOf("stickAfterRollbackElement") > 0);
um.undo();
text = (( AbstractDocumentModel)model).getAccess().getCurrentDocumentText();
assertTrue(text.indexOf("stickAfterRollbackElement") == -1);
um.redo();
text = (( AbstractDocumentModel)model).getAccess().getCurrentDocumentText();
assertTrue(text.indexOf("stickAfterRollbackElement") > 0);
}
}
public void testSyncRemoveAttribute() throws Exception {
defaultSetup();
UndoManager um = new UndoManager();
mModel.addUndoableEditListener(um);
A a1 = mModel.getRootComponent().getChild(TestComponent3.A.class);
assertNull("setup", a1.getValue());
mModel.startTransaction();
String testValue = "edit #1: testRemoveAttribute";
a1.setValue(testValue);
mModel.endTransaction();
assertEquals(testValue, a1.getValue());
um.undo();
assertNull("after undo expect no attribute 'value'", a1.getValue());
um.redo();
assertEquals(testValue, a1.getValue());
Util.setDocumentContentTo(mDoc, "resources/test1.xml");
mModel.sync();
assertNull("sync back to original, expect no attribute 'value'", a1.getValue());
plistener.assertEvent("value", testValue, null);
listener.assertEvent(ComponentEvent.EventType.VALUE_CHANGED, a1);
um.undo();
mModel.getAccess().flush(); // after fix for 83963 need flush after undo/redo
assertEquals(testValue, a1.getValue());
mModel = Util.dumpAndReloadModel(mModel);
a1 = mModel.getRootComponent().getChild(A.class);
assertEquals(testValue, a1.getValue());
}
public void testRemoveNewline() throws Exception {
loggingOn();
JEditorPane pane = ViewUpdatesTesting.createPane();
Document doc = pane.getDocument();
UndoManager undoManager = ViewUpdatesTesting.getUndoManager(doc);
doc.insertString(0, "a\nb", null);
doc.remove(1, 1);
undoManager.undo();
pane.modelToView(0);
}
/**
* Checks the index is created after reaching threshold amount of
* global components and also the index removed if the amound goes down
* below another threshold.
*/
@Test
public void testIndexCreationThreshold() throws Exception {
SchemaModel sm;
//
// sm = Util.loadSchemaModel2("resources/performance2/B.xsd"); // NOI18N
sm = Util.loadSchemaModel2("resources/performance2.zip", "B.xsd"); // NOI18N
//
assertTrue(sm.getState() == State.VALID);
//
assertTrue(sm instanceof SchemaModelImpl);
SchemaModelImpl smImpl = SchemaModelImpl.class.cast(sm);
GlobalComponentsIndexSupport indexSupport = smImpl.getGlobalComponentsIndexSupport();
assertNotNull(indexSupport);
GlobalComponentsIndexSupport.JUnitTestSupport testSupport =
indexSupport.getJUnitTestSupport();
assertNotNull(testSupport);
//
// Initiate index building
GlobalElement found = sm.findByNameAndType("B000", GlobalElement.class);
assertNotNull(found);
Thread.sleep(500); // Wait the index is build
//
assertTrue(testSupport.isSupportIndex());
int indexSise = testSupport.getIndexSize();
assertEquals(indexSise, 90);
//
UndoManager um = new javax.swing.undo.UndoManager();
AbstractDocumentModel.class.cast(sm).addUndoableEditListener(um);
//
sm.startTransaction();
try {
Schema schema = sm.getSchema();
java.util.List<SchemaComponent> gChildren = schema.getChildren();
int counter = 0;
for (SchemaComponent child : gChildren) {
//
assertTrue(child instanceof GlobalElement);
GlobalElement gElem = GlobalElement.class.cast(child);
schema.removeElement(gElem);
//
counter++;
if (counter >= 50) {
break;
}
}
} finally {
sm.endTransaction();
}
//
int childrenCount = sm.getSchema().getChildren().size();
assertEquals(childrenCount, 40);
assertFalse(testSupport.isSupportIndex());
//
//
um.undo();
//
// Initiate index building again
found = sm.findByNameAndType("B000", GlobalElement.class);
assertNotNull(found);
Thread.sleep(500); // Wait the index is build
//
assertTrue(testSupport.isSupportIndex());
indexSise = testSupport.getIndexSize();
assertEquals(indexSise, 90);
//
System.out.println("============================="); // NOI18N
System.out.println(" testIndexCreationThreshold "); // NOI18N
System.out.println("=============LOG============="); // NOI18N
String log = testSupport.printLog();
System.out.print(log);
System.out.println("============================="); // NOI18N
//
}
public void testSourceEditSyncUndo() throws Exception {
defaultSetup();
UndoManager urListener = new UndoManager();
Document doc = mModel.getBaseDocument();
mModel.addUndoableEditListener(urListener);
mModel.startTransaction();
B b2 = new B(mModel, 2);
mModel.getRootComponent().addAfter(b2.getName(), b2, TestComponent3._A);
mModel.endTransaction();
assertEquals("first edit setup", 2, mModel.getRootComponent().getChildren(B.class).size());
// see fix for issue 83963, with this fix we need coordinate edits from
// on XDM model and on document buffer. This reduce XDM undo/redo efficiency,
// but is the best we can have to satisfy fine-grained text edit undo requirements.
mModel.removeUndoableEditListener(urListener);
doc.addUndoableEditListener(urListener);
Util.setDocumentContentTo(doc, "resources/test2.xml");
assertEquals("undo sync", 1, mModel.getRootComponent().getChildren(C.class).size());
mModel.sync();
doc.removeUndoableEditListener(urListener);
assertEquals("sync setup", 1, mModel.getRootComponent().getChildren(B.class).size());
assertEquals("sync setup", 0, mModel.getRootComponent().getChildren(C.class).size());
// setDocumentContentTo did delete all, then insert, hence 2 undo's'
urListener.undo(); urListener.undo();
mModel.sync(); // the above undo's are just on document buffer, needs sync (inefficient).
assertEquals("undo sync", 1, mModel.getRootComponent().getChildren(C.class).size());
assertEquals("undo sync", 2, mModel.getRootComponent().getChildren(B.class).size());
urListener.undo();
assertEquals("undo first edit before sync", 1, mModel.getRootComponent().getChildren(B.class).size());
urListener.redo();
assertEquals("redo first edit", 1, mModel.getRootComponent().getChildren(C.class).size());
assertEquals("redo first edit", 2, mModel.getRootComponent().getChildren(B.class).size());
// needs to back track the undo's, still needs sync'
urListener.redo(); urListener.redo();
mModel.sync();
assertEquals("redo to sync", 1, mModel.getRootComponent().getChildren(B.class).size());
assertEquals("redo to sync", 0, mModel.getRootComponent().getChildren(C.class).size());
}
/**
* Tests Issue #166177
* @throws Exception
*/
public void testUndoNsPrefixCreation() throws Exception {
defaultSetup();
final UndoManager ur = new UndoManager();
model.addUndoableEditListener(ur);
A a = model.getRootComponent().getChild(A.class);
//
String xdmModelTextInitial = Util.getXdmBasedModelText(model);
String xamModelTextInitial = Util.getXamBasedModelText(model);
//
// Add child component to A element
model.startTransaction();
try {
TestComponent3.Aa newChildAa = new TestComponent3.Aa(model, 1);
a.appendChild("setup", newChildAa);
} finally {
model.endTransaction();
}
//
TestComponent3.Aa newAa = a.getChild(TestComponent3.Aa.class);
assertEquals(TestComponent3.NS2_URI, newAa.getNamespaceURI());
//
String xdmModelTextAfterAdd = Util.getXdmBasedModelText(model);
String xamModelTextAfterAdd = Util.getXamBasedModelText(model);
//
ur.undo();
//
// Check the XDM and XAM models' structure return back to initial state after undo.
String xdmModelText = Util.getXdmBasedModelText(model);
assertEquals(xdmModelText, xdmModelTextInitial);
//
String xamModelText = Util.getXamBasedModelText(model);
assertEquals(xamModelText, xamModelTextInitial);
//
ur.redo();
//
xdmModelText = Util.getXdmBasedModelText(model);
assertEquals(xdmModelText, xdmModelTextAfterAdd);
//
xamModelText = Util.getXamBasedModelText(model);
assertEquals(xamModelText, xamModelTextAfterAdd);
//
ur.undo();
//
xdmModelText = Util.getXdmBasedModelText(model);
assertEquals(xdmModelText, xdmModelTextInitial);
//
xamModelText = Util.getXamBasedModelText(model);
assertEquals(xamModelText, xamModelTextInitial);
//
}
public void testIssue83963() throws Exception {
SchemaModel model = Util.loadSchemaModel("resources/undoredo.xsd");
BaseDocument doc = (BaseDocument) model.getModelSource().
getLookup().lookup(BaseDocument.class);
Schema s = model.getSchema();
TestComponentListener listener = new TestComponentListener();
model.addComponentListener(listener);
UndoManager ur = new UndoManager();
model.addUndoableEditListener(ur);
String original = doc.getText(0, doc.getLength());
//System.out.println("doc before add ComplexType"+doc.getText(0, doc.getLength()));
GlobalComplexType gct = model.getFactory().createGlobalComplexType();
model.startTransaction();
s.addComplexType(gct);
model.endTransaction();
model.removeUndoableEditListener(ur);
doc.addUndoableEditListener(ur);
//System.out.println("doc after add ComplexType"+doc.getText(0, doc.getLength()));
String stStr = " <xsd:simpleType name=\"lend\">\n <xsd:list>\n <xsd:simpleType>\n <xsd:restriction base=\"xsd:string\"/>\n </xsd:simpleType>\n </xsd:list>\n </xsd:simpleType>";
String afterInsert = doc.getText(0, doc.getLength());
//System.out.println("doc after insert simpleType"+doc.getText(290, 10));
// position was changing which is weird but doesn't matter for undo-redo testing
int schemaTagPosition = afterInsert.length() - 10;
doc.insertString(schemaTagPosition, "\n", null);
model.sync();
doc.insertString(schemaTagPosition + 1, stStr, null);
model.sync();
//System.out.println("doc after insert simpleType"+doc.getText(0, doc.getLength()));
ur.undo();
//System.out.println("doc after first undo"+doc.getText(0, doc.getLength()));
ur.undo();
assertEquals(afterInsert,doc.getText(0, doc.getLength()));
//System.out.println("doc after second undo"+doc.getText(0, doc.getLength()));
ur.undo();
//System.out.println("doc after third undo"+doc.getText(0, doc.getLength()));
assertEquals(original, doc.getText(0, doc.getLength()));
ur.redo();
assertEquals(afterInsert,doc.getText(0, doc.getLength()));
//System.out.println("doc after first redo"+doc.getText(0, doc.getLength()));
ur.redo();
//System.out.println("doc after second redo"+doc.getText(0, doc.getLength()));
ur.redo();
//System.out.println("doc after third redo"+doc.getText(0, doc.getLength()));
}