Java Code Examples for javax.swing.undo.UndoManager

The following examples show how to use javax.swing.undo.UndoManager. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: netbeans   Source File: OverrideEditorActions.java    License: Apache License 2.0 6 votes vote down vote up
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();
        }
    }});
}
 
Example 2
Source Project: netbeans   Source File: AbstractModelTest.java    License: Apache License 2.0 6 votes vote down vote up
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());
}
 
Example 3
Source Project: netbeans   Source File: KerberosProfile.java    License: Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 4
Source Project: netbeans   Source File: STSIssuedEndorsingProfile.java    License: Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 5
Source Project: netbeans   Source File: STSIssuedSupportingTokenProfile.java    License: Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 6
Source Project: netbeans   Source File: EndorsingCertificateProfile.java    License: Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 7
Source Project: netbeans   Source File: SAMLHolderOfKeyProfile.java    License: Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 8
Source Project: netbeans   Source File: STSIssuedCertProfile.java    License: Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 9
Source Project: netbeans   Source File: AbstractModelTest.java    License: Apache License 2.0 6 votes vote down vote up
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));
}
 
Example 10
Source Project: netbeans   Source File: ViewHierarchyTest.java    License: Apache License 2.0 6 votes vote down vote up
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();
}
 
Example 11
Source Project: netbeans   Source File: PlainDocumentTest.java    License: Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 12
Source Project: netbeans   Source File: SyncTest.java    License: Apache License 2.0 6 votes vote down vote up
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());
}
 
Example 13
Source Project: netbeans   Source File: SyncTest.java    License: Apache License 2.0 6 votes vote down vote up
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());
}
 
Example 14
Source Project: netbeans   Source File: AbstractModelTest.java    License: Apache License 2.0 6 votes vote down vote up
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));
}
 
Example 15
Source Project: netbeans   Source File: AbstractModelTest.java    License: Apache License 2.0 6 votes vote down vote up
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());
}
 
Example 16
Source Project: ganttproject   Source File: UndoManagerImpl.java    License: GNU General Public License v3.0 5 votes vote down vote up
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"));
    }
  });
}
 
Example 17
Source Project: SikuliX1   Source File: EditorPaneUndoRedo.java    License: MIT License 5 votes vote down vote up
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);
  }
 
Example 18
public CommandUndo(UndoManager undo) {
    super("Undo");  
	this.undo=undo;
    setEnabled(false);
    
    putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_Z, ActionEvent.CTRL_MASK));
}
 
Example 19
Source Project: netbeans   Source File: AbstractModelTest.java    License: Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 20
Source Project: netbeans   Source File: CustomizationWSEditor.java    License: Apache License 2.0 5 votes vote down vote up
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);
        }
    }
}
 
Example 21
Source Project: jadx   Source File: TextStandardActions.java    License: Apache License 2.0 5 votes vote down vote up
public TextStandardActions(JTextComponent textComponent) {
	this.textComponent = textComponent;
	this.undoManager = new UndoManager();

	initActions();
	addPopupItems();
	addKeyActions();

	registerListeners();
}
 
Example 22
Source Project: netbeans   Source File: NbEditorKit.java    License: Apache License 2.0 5 votes vote down vote up
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);
        }
    }
}
 
Example 23
Source Project: netbeans   Source File: ServiceTopComponent.java    License: Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 24
Source Project: groovy   Source File: TextUndoManager.java    License: Apache License 2.0 5 votes vote down vote up
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());
}
 
Example 25
Source Project: gcs   Source File: RedoCommand.java    License: Mozilla Public License 2.0 5 votes vote down vote up
@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"));
    }
}
 
Example 26
Source Project: gcs   Source File: UndoCommand.java    License: Mozilla Public License 2.0 5 votes vote down vote up
@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"));
    }
}
 
Example 27
Source Project: netbeans   Source File: AbstractModelTest.java    License: Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 28
Source Project: netbeans   Source File: AbstractModel.java    License: Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 29
Source Project: netbeans   Source File: DocumentContentTesting.java    License: Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 30
Source Project: netbeans   Source File: AbstractModelTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testUndoWithFaultInEventFiring() throws Exception {
    setupForUndoRedoFault(new FaultInjector() {
        @Override
        public void injectFaultAndCheck(Object actor) throws Exception { 
            mModel.injectFaultInEventFiring();
            ((UndoManager)actor).undo();
        }
    }, false);
}