|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| MovingDocumentRegion.java | 12.5% | 20% | 40% | 22.1% |
|
||||||||||||||
| 1 | /*BEGIN_COPYRIGHT_BLOCK | |
| 2 | * | |
| 3 | * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu) | |
| 4 | * All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * * Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * * Redistributions in binary form must reproduce the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer in the | |
| 12 | * documentation and/or other materials provided with the distribution. | |
| 13 | * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the | |
| 14 | * names of its contributors may be used to endorse or promote products | |
| 15 | * derived from this software without specific prior written permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
| 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| 25 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| 26 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| 27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 28 | * | |
| 29 | * This software is Open Source Initiative approved Open Source Software. | |
| 30 | * Open Source Initative Approved is a trademark of the Open Source Initiative. | |
| 31 | * | |
| 32 | * This file is part of DrJava. Download the current version of this project | |
| 33 | * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/ | |
| 34 | * | |
| 35 | * END_COPYRIGHT_BLOCK*/ | |
| 36 | ||
| 37 | package edu.rice.cs.drjava.model; | |
| 38 | ||
| 39 | import javax.swing.text.Position; | |
| 40 | import javax.swing.text.BadLocationException; | |
| 41 | ||
| 42 | import edu.rice.cs.util.StringOps; | |
| 43 | import edu.rice.cs.util.UnexpectedException; | |
| 44 | import edu.rice.cs.plt.lambda.Thunk; | |
| 45 | ||
| 46 | /** Class for a document region that moves with changes in the document; it also includes a lazy tool-tip and line | |
| 47 | * boundaries. TODO: convert _startPos and _endPos to _cachedStart and _cachedEnd which are updated by updateLines | |
| 48 | * @version $Id: MovingDocumentRegion.java 5175 2010-01-20 08:46:32Z mgricken $Regiong | |
| 49 | */ | |
| 50 | public class MovingDocumentRegion extends DocumentRegion { | |
| 51 | ||
| 52 | // /** Offset of end of last line of this region as recorded in last call on updateLines (or <init>). */ | |
| 53 | // protected volatile int _cachedLineEnd; | |
| 54 | protected final Position _startPos; | |
| 55 | protected final Position _endPos; | |
| 56 | protected volatile Position _lineStartPos; | |
| 57 | protected volatile Position _lineEndPos; | |
| 58 | ||
| 59 | /** Suspension that generates the JTree label excerpt for this region. */ | |
| 60 | protected final Thunk<String> _stringSuspension; | |
| 61 | ||
| 62 | /** Update _lineStartPos and _lineEndPos after line has been edited. */ | |
| 63 | 0 | public void update() { |
| 64 | 0 | try { // _doc is inherited from DocumentRegion |
| 65 | 0 | _lineStartPos = _doc.createPosition(_doc._getLineStartPos(getStartOffset())); |
| 66 | 0 | _lineEndPos = _doc.createPosition(_doc._getLineEndPos(getEndOffset())); |
| 67 | } | |
| 68 | 0 | catch (BadLocationException ble) { throw new UnexpectedException(ble); } // should never happen |
| 69 | } | |
| 70 | ||
| 71 | /** Create a new moving document region. */ | |
| 72 | 20 | public MovingDocumentRegion(final OpenDefinitionsDocument doc, int start, int end, int lineStart, int lineEnd) { |
| 73 | ||
| 74 | 20 | super(doc, start, end); |
| 75 | ||
| 76 | 20 | assert doc != null; |
| 77 | ||
| 78 | 20 | try { |
| 79 | 20 | _startPos = doc.createPosition(start); |
| 80 | 20 | _endPos = doc.createPosition(end); |
| 81 | 20 | _lineStartPos = doc.createPosition(lineStart); |
| 82 | 20 | _lineEndPos = doc.createPosition(lineEnd); |
| 83 | } | |
| 84 | 0 | catch (BadLocationException ble) { throw new UnexpectedException(ble); } // should never happen |
| 85 | ||
| 86 | 20 | _stringSuspension = new Thunk<String>() { |
| 87 | 0 | public String value() { |
| 88 | 0 | try { |
| 89 | 0 | update(); |
| 90 | 0 | int endSel = getEndOffset(); |
| 91 | 0 | int startSel = getStartOffset(); |
| 92 | 0 | int selLength = endSel - startSel; |
| 93 | ||
| 94 | 0 | int excerptEnd = getLineEndOffset(); |
| 95 | 0 | int excerptStart = getLineStartOffset(); |
| 96 | ||
| 97 | // the offsets within the excerpted string of the selection (figuratively in "Red") | |
| 98 | 0 | int startRed = startSel - excerptStart; |
| 99 | 0 | int endRed = endSel - excerptStart; |
| 100 | ||
| 101 | 0 | int excerptLength = Math.min(120, excerptEnd - excerptStart); |
| 102 | 0 | String text = doc.getText(excerptStart, excerptLength); |
| 103 | ||
| 104 | // Construct the matching string and compressed selection prefix and suffix strings within text | |
| 105 | 0 | String prefix, match, suffix; |
| 106 | 0 | if (excerptLength < startRed) { // selection not included in excerpt |
| 107 | 0 | prefix = StringOps.compress(text.substring(0, excerptLength)); |
| 108 | 0 | match = " ..."; |
| 109 | 0 | suffix = ""; |
| 110 | } | |
| 111 | else { | |
| 112 | 0 | prefix = StringOps.compress(text.substring(0, startRed)); |
| 113 | 0 | if (excerptLength < startRed + selLength) { // selection extends beyond excerpt |
| 114 | 0 | match = text.substring(startRed) + " ..."; |
| 115 | 0 | suffix = ""; |
| 116 | } | |
| 117 | else { | |
| 118 | 0 | match = text.substring(startRed, endRed); |
| 119 | 0 | suffix = StringOps.compress(text.substring(endRed, excerptLength)); |
| 120 | } | |
| 121 | } | |
| 122 | ||
| 123 | // COMMENT: We need a global invariant concerning non-displayable characters. | |
| 124 | ||
| 125 | // create the excerpt string | |
| 126 | 0 | StringBuilder sb = new StringBuilder(edu.rice.cs.plt.text.TextUtil.htmlEscape(prefix)); |
| 127 | 0 | sb.append("<font color=#ff0000>"); |
| 128 | // sb.append(LEFT); | |
| 129 | 0 | sb.append(edu.rice.cs.plt.text.TextUtil.htmlEscape(match)); |
| 130 | 0 | sb.append("</font>"); |
| 131 | // sb.append(RIGHT); | |
| 132 | 0 | sb.append(edu.rice.cs.plt.text.TextUtil.htmlEscape(suffix)); |
| 133 | // sb.append("</html>"); | |
| 134 | // sb.append(StringOps.getBlankString(120 - sLength)); // move getBank to StringOps | |
| 135 | 0 | return sb.toString(); |
| 136 | } | |
| 137 | 0 | catch(BadLocationException e) { return ""; /* Ignore the exception. */ } |
| 138 | } | |
| 139 | }; | |
| 140 | } | |
| 141 | ||
| 142 | /** @return the document, or null if it hasn't been established yet */ | |
| 143 | 62 | public OpenDefinitionsDocument getDocument() { return _doc; } |
| 144 | ||
| 145 | /** @return region start. */ | |
| 146 | 29 | public int getStartOffset() { return _startPos.getOffset(); } |
| 147 | ||
| 148 | /** @return region end. */ | |
| 149 | 76 | public int getEndOffset() { return _endPos.getOffset(); } |
| 150 | ||
| 151 | /** @return line start preeding region. */ | |
| 152 | 0 | public int getLineStartOffset() { return _lineStartPos.getOffset(); } |
| 153 | ||
| 154 | /** @return line end following region. */ | |
| 155 | 0 | public int getLineEndOffset() { return _lineEndPos.getOffset(); } |
| 156 | ||
| 157 | /** @return the string it was assigned */ | |
| 158 | 0 | public String getString() { return _stringSuspension.value(); } |
| 159 | ||
| 160 | /** @return true if objects a and b are equal; null values are handled correctly. */ | |
| 161 | 0 | public static boolean equals(Object a, Object b) { |
| 162 | 0 | if (a == null) return (b == null); |
| 163 | 0 | return a.equals(b); |
| 164 | } | |
| 165 | } |
|
||||||||||