Example usage for javax.swing.undo CompoundEdit end

List of usage examples for javax.swing.undo CompoundEdit end

Introduction

In this page you can find the example usage for javax.swing.undo CompoundEdit end.

Prototype

public void end() 

Source Link

Document

Sets inProgress to false.

Usage

From source file:org.fife.ui.rsyntaxtextarea.EOLPreservingRSyntaxDocument.java

private InsertStringResult insertStringImpl(int offset, String str, AttributeSet a)
        throws BadLocationException {
    if (StringUtils.isEmpty(str)) {
        return new InsertStringResult(str, null);
    }/*from  w  w  w  . j a v  a 2s . co  m*/

    PeekReader reader = null;
    CompoundEdit edit = new CompoundEdit();

    try {
        reader = new PeekReader(new StringReader(str));
        StringBuilder builder = new StringBuilder();
        TreeMap<Integer, char[]> tempMap = new TreeMap<Integer, char[]>();
        char[] buff = new char[1024];
        int nch;
        int cOffset = offset;
        boolean wasCR = false;

        while ((nch = reader.read(buff, 0, buff.length)) != -1) {
            for (int i = 0; i < nch; i++) {
                char c = buff[i];

                if (c == '\r') {
                    boolean updated = false;
                    if (i == nch - 1 && !reader.peek() && Arrays.equals(eolMap.get(offset), LF)) {
                        edit.addEdit(new ChangeEOLEdit(offset, CRLF));
                        eolMap.put(offset, CRLF);
                        updated = true;
                    }

                    if (!updated && (wasCR || i == nch - 1 && !reader.peek())) {
                        // Insert CR
                        tempMap.put(cOffset++, CR);
                        builder.append(LF);
                    }

                    wasCR = true;
                } else if (c == '\n') {
                    boolean updated = false;
                    if (cOffset == offset) {
                        if (Arrays.equals(eolMap.get(offset - 1), CR)) {
                            edit.addEdit(new ChangeEOLEdit(offset - 1, CRLF));
                            eolMap.put(offset - 1, CRLF);
                            updated = true;
                        }
                    }

                    if (!updated) {
                        if (wasCR) {
                            // Insert CRLF
                            tempMap.put(cOffset++, CRLF);
                            builder.append(LF);
                        } else {
                            // Insert LF
                            tempMap.put(cOffset++, LF);
                            builder.append(LF);
                        }
                    }

                    wasCR = false;
                } else if (replaceControlCharacters && c != '\t' && (c < ' ' || c == 0x7F)) {
                    if (wasCR) {
                        // Insert previous CR
                        tempMap.put(cOffset++, CR);
                        builder.append(LF);
                    }

                    // Insert control character
                    cOffset++;
                    builder.append((char) (c == 0x7F ? '\u2421' : '\u2400' + c));
                    wasCR = false;
                } else {
                    if (wasCR) {
                        // Insert previous CR
                        tempMap.put(cOffset++, CR);
                        builder.append(LF);
                    }

                    // Insert regular character
                    cOffset++;
                    builder.append(c);
                    wasCR = false;
                }
            }
        }

        str = builder.toString();

        Integer key = eolMap.isEmpty() ? null : eolMap.lastKey();
        while (key != null && key >= offset) {
            edit.addEdit(new ChangeEOLEdit(key, null));
            char[] eol = eolMap.remove(key);

            int newKey = key + str.length();
            edit.addEdit(new ChangeEOLEdit(newKey, eol));
            eolMap.put(newKey, eol);

            key = eolMap.lowerKey(key);
        }

        for (Entry<Integer, char[]> entry : tempMap.entrySet()) {
            edit.addEdit(new ChangeEOLEdit(entry.getKey(), entry.getValue()));
        }
        eolMap.putAll(tempMap);
    } catch (IOException e) {
        // Only using a StringReader, so should not happen
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
            }
        }
    }

    edit.end();
    return new InsertStringResult(str, edit);
}

From source file:org.fife.ui.rsyntaxtextarea.EOLPreservingRSyntaxDocument.java

private RemoveResult removeImpl(int offs, int len) throws BadLocationException {
    CompoundEdit edit = new CompoundEdit();

    // Combine edge CRs and LFs if necessary
    if (Arrays.equals(eolMap.get(offs - 1), CR) && Arrays.equals(eolMap.get(offs + len), LF)) {
        edit.addEdit(new ChangeEOLEdit(offs - 1, CRLF));
        eolMap.put(offs - 1, CRLF);//from w ww  . j av  a2s  .  co m
        len++;
    }

    // Move EOLs past the edge of the removal boundary into their new positions
    for (Integer offset : eolMap.keySet().toArray(new Integer[eolMap.size()])) {
        if (offset >= offs) {
            edit.addEdit(new ChangeEOLEdit(offset, null));
            char[] eol = eolMap.remove(offset);

            if (offset >= offs + len) {
                edit.addEdit(new ChangeEOLEdit(offset - len, eol));
                eolMap.put(offset - len, eol);
            }
        }
    }

    edit.end();
    return new RemoveResult(len, edit);
}

From source file:org.fife.ui.rsyntaxtextarea.EOLPreservingRSyntaxDocument.java

@Override
public void replace(int offset, int length, String text, AttributeSet a) throws BadLocationException {
    lastEdit = null;//from w  w  w. j  av a  2  s  .c o  m

    if (length == 0 && StringUtils.isEmpty(text)) {
        return;
    }

    RemoveResult removeResult = removeImpl(offset, length);
    InsertStringResult insertResult = insertStringImpl(offset, text, a);

    if (removeResult.edit != null) {
        if (insertResult.edit != null) {
            CompoundEdit edit = new CompoundEdit();
            edit.addEdit(removeResult.edit);
            edit.addEdit(insertResult.edit);
            edit.end();
            lastEdit = edit;
        } else {
            lastEdit = removeResult.edit;
        }
    } else if (insertResult.edit != null) {
        lastEdit = insertResult.edit;
    }

    writeLock();
    try {
        super.remove(offset, removeResult.len);
        super.insertString(offset, insertResult.str, a);
    } finally {
        writeUnlock();
    }
}

From source file:org.fife.ui.rsyntaxtextarea.EOLPreservingRSyntaxDocument.java

@Override
protected void fireUndoableEditUpdate(UndoableEditEvent evt) {
    if (lastEdit != null) {
        CompoundEdit edit = new CompoundEdit();
        edit.addEdit(evt.getEdit());// w w  w .  j  a v a2 s .c o m
        edit.addEdit(lastEdit);
        edit.end();
        super.fireUndoableEditUpdate(new UndoableEditEvent(evt.getSource(), edit));
        lastEdit = null;
    } else {
        super.fireUndoableEditUpdate(evt);
    }
}

From source file:org.jcurl.demo.tactics.JCurlShotPlanner.java

@SuppressWarnings("unchecked")
private static CompoundEdit reset(final ComputedTrajectorySet cts, final BroomPromptModel broom,
        final boolean outPosition) {
    final RockSet<Pos> ipos = cts.getInitialPos();
    final RockSet<Vel> ivel = cts.getInitialVel();
    // store the initial state:
    final PosMemento[] pm = new PosMemento[RockSet.ROCKS_PER_SET];
    for (int i16 = RockSet.ROCKS_PER_SET - 1; i16 >= 0; i16--)
        pm[i16] = new PosMemento(ipos, i16, ipos.getRock(i16).p());
    final IndexMemento bi = new IndexMemento(broom, broom.getIdx16());
    final HandleMemento bh = new HandleMemento(broom, broom.getOutTurn());
    final XYMemento bxy = new XYMemento(broom, broom.getBroom());
    final SplitMemento bs = new SplitMemento(broom, broom.getSplitTimeMillis().getValue());
    final boolean preS = cts.getSuspended();
    cts.setSuspended(true);//from  w  ww .  j  av  a  2  s.com
    try {
        // reset:
        RockSet.allZero(ivel);
        broom.setIdx16(-1);
        if (outPosition)
            RockSetUtils.allOut(ipos);
        else
            RockSetUtils.allHome(ipos);
        broom.setIdx16(1);
        broom.setBroom(new Point2D.Double(0, 0));
        broom.getSplitTimeMillis().setValue(3300);
    } finally {
        cts.setSuspended(preS);
    }
    // create a compound edit
    final CompoundEdit ce = new CompoundEdit();
    ce.addEdit(new UndoableMemento(new SuspendMemento(cts, preS), new SuspendMemento(cts, true)));
    for (int i16 = RockSet.ROCKS_PER_SET - 1; i16 >= 0; i16--)
        ce.addEdit(new UndoableMemento(pm[i16], new PosMemento(ipos, i16, ipos.getRock(i16).p())));
    ce.addEdit(new UndoableMemento(bi, new IndexMemento(broom, broom.getIdx16())));
    ce.addEdit(new UndoableMemento(bh, new HandleMemento(broom, broom.getOutTurn())));
    ce.addEdit(new UndoableMemento(bxy, new XYMemento(broom, broom.getBroom())));
    ce.addEdit(new UndoableMemento(bs, new SplitMemento(broom, broom.getSplitTimeMillis().getValue())));
    ce.addEdit(new UndoableMemento(new SuspendMemento(cts, true), new SuspendMemento(cts, preS)));
    ce.end();
    return ce;
}

From source file:uk.ac.ebi.mnb.dialog.tools.DownloadStructuresDialog.java

@Override
public void process(final SpinningDialWaitIndicator wait) {

    List<Identifier> problemIdentifiers = new ArrayList<Identifier>();

    ServiceManager services = DefaultServiceManager.getInstance();

    int i = 0;/*w ww. j a v a  2 s.  c  o m*/
    int n = getSelection().get(Metabolite.class).size();

    CompoundEdit edit = new CompoundEdit();

    // could re-arrange data to make this easier
    for (Metabolite m : getSelection().get(Metabolite.class)) {

        ANNOTATION: for (CrossReference reference : m.getAnnotationsExtending(CrossReference.class)) {
            for (Identifier identifier : resourceSelection.getElements()) {

                if (identifier.getClass().isAssignableFrom(reference.getIdentifier().getClass())) {

                    // get the appropiate service for the given ientifier
                    StructureService service = services.getService(identifier, StructureService.class);
                    if (canUse(service) && isChemicalService(service)) {

                        IAtomContainer structure = service.getStructure(reference.getIdentifier());

                        // don't add empty structures
                        if (!structure.isEmpty()) {

                            Annotation annotation = new AtomContainerAnnotation(structure);
                            edit.addEdit(new AddAnnotationEdit(m, annotation));
                            m.addAnnotation(annotation);

                            // only get first
                            if (!fetchAll.isSelected())
                                break ANNOTATION;

                        } else {
                            // log which couldn't be found
                            problemIdentifiers.add(reference.getIdentifier());
                        }

                    }

                }

            }

        }

        final float perc = (float) ++i / n;
        // update the text on the wait indicator
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                wait.setText(String.format("Retrieving... %.1f%%", perc * 100));
            }
        });

    }

    addEdit(edit);
    edit.end();

    if (!problemIdentifiers.isEmpty()) {
        addMessage(new WarningMessage("The following identifiers had empty or missing structures: "
                + StringUtils.join(problemIdentifiers, ", ")));
    }

}