Switch between tabs, this swaps out the Document object that's currently being manipulated.
(SketchCode code)
| 1535 | * that's currently being manipulated. |
| 1536 | */ |
| 1537 | public void setCode(SketchCode code) { |
| 1538 | SyntaxDocument document = (SyntaxDocument) code.getDocument(); |
| 1539 | |
| 1540 | if (document == null) { // this document not yet inited |
| 1541 | document = new SyntaxDocument() { |
| 1542 | @Override |
| 1543 | public void beginCompoundEdit() { |
| 1544 | if (compoundEdit == null) |
| 1545 | startCompoundEdit(); |
| 1546 | super.beginCompoundEdit(); |
| 1547 | } |
| 1548 | |
| 1549 | @Override |
| 1550 | public void endCompoundEdit() { |
| 1551 | stopCompoundEdit(); |
| 1552 | super.endCompoundEdit(); |
| 1553 | } |
| 1554 | }; |
| 1555 | code.setDocument(document); |
| 1556 | |
| 1557 | // turn on syntax highlighting |
| 1558 | document.setTokenMarker(mode.getTokenMarker(code)); |
| 1559 | |
| 1560 | // insert the program text into the document object |
| 1561 | try { |
| 1562 | document.insertString(0, code.getProgram(), null); |
| 1563 | } catch (BadLocationException bl) { |
| 1564 | bl.printStackTrace(); |
| 1565 | } |
| 1566 | |
| 1567 | // set up this guy's own undo manager |
| 1568 | // code.undo = new UndoManager(); |
| 1569 | |
| 1570 | document.addDocumentListener(new DocumentListener() { |
| 1571 | |
| 1572 | public void removeUpdate(DocumentEvent e) { |
| 1573 | if (isInserting && isDirectEdit() && !textarea.isOverwriteEnabled()) { |
| 1574 | endTextEditHistory(); |
| 1575 | } |
| 1576 | isInserting = false; |
| 1577 | } |
| 1578 | |
| 1579 | public void insertUpdate(DocumentEvent e) { |
| 1580 | if (!isInserting && !textarea.isOverwriteEnabled() && isDirectEdit()) { |
| 1581 | endTextEditHistory(); |
| 1582 | } |
| 1583 | |
| 1584 | if (!textarea.isOverwriteEnabled()) { |
| 1585 | isInserting = true; |
| 1586 | } |
| 1587 | } |
| 1588 | |
| 1589 | public void changedUpdate(DocumentEvent e) { |
| 1590 | endTextEditHistory(); |
| 1591 | } |
| 1592 | }); |
| 1593 | |
| 1594 | // connect the undo listener to the editor |
no test coverage detected