private String getTextFor(OpenDefinitionsDocument doc) { DefinitionsPane pane = _frame.getDefPaneGivenODD(doc); String endl = "\n"; // was StringOps.EOL; but Swing uses '\n' for newLine int loc = pane.getCaretPosition(); int start = loc; int end = loc; String text; text = doc.getText(); /* get the starting point of 2 lines up... */ for (int i = 0; i < 4; i++) { if (start > 0) start = text.lastIndexOf(endl, start - endl.length()); } if (start == -1) start = 0; // skip the end line, if we're at one // if (doc.getLength() >= endl.length() && text.substring(start, start+endl.length()) == // endl) // start += endl.length(); if (doc.getLength() >= endl.length() && text.substring(start, start + endl.length()).equals(endl)) start += endl.length(); /* get the ending point 2 lines down */ int index; for (int i = 0; i < 4; i++) { if (end < doc.getLength()) { index = text.indexOf(endl, end + endl.length()); if (index != -1) end = index; } } if (end < start) end = start; text = text.substring(start, end); return text; }
/** * Tests that the current location of a document is equal to the caret Position after switching to * another document and back. */ public void testDocLocationAfterSwitch() throws BadLocationException { final DefinitionsPane pane = _frame.getCurrentDefPane(); final OpenDefinitionsDocument doc = pane.getOpenDefDocument(); setDocText(doc.getDocument(), "abcd"); Utilities.invokeAndWait( new Runnable() { public void run() { doc.setCurrentLocation(3); pane.setCaretPosition(3); // The caret is not affected by setCurrentLocation } }); assertEquals("Location of old doc before switch", 3, doc.getCurrentLocation()); assertEquals("Location of cursor in old document", 3, pane.getCaretPosition()); // Create a new file SingleDisplayModel model = _frame.getModel(); final OpenDefinitionsDocument oldDoc = doc; final DefinitionsPane oldPane = pane; final OpenDefinitionsDocument newDoc = model.newFile(); // Current pane should be new doc, pos 0 DefinitionsPane curPane; OpenDefinitionsDocument curDoc; curPane = _frame.getCurrentDefPane(); curDoc = curPane.getOpenDefDocument(); // .getDocument(); assertEquals("New curr DefPane's document", newDoc, curDoc); assertEquals("Location in new document", 0, newDoc.getCurrentLocation()); // Switch back to old document model.setActiveNextDocument(); Utilities.clearEventQueue(); assertEquals("Next active doc", oldDoc, model.getActiveDocument()); // Current pane should be old doc, pos 3 curPane = _frame.getCurrentDefPane(); curDoc = curPane.getOpenDefDocument(); // .getDocument(); assertEquals("Next active pane", oldPane, curPane); assertEquals("Current document is old document", oldDoc, curDoc); assertEquals("Location of caret in old document", 3, curPane.getCaretPosition()); _log.log("testDocLocationAfterSwitch completed"); }