1 21 22 package org.armedbear.j; 23 24 import java.io.UnsupportedEncodingException ; 25 26 public class TextLine extends AbstractLine implements Line 27 { 28 private static final int SAVED = 0x0001; 29 private static final int NEW = 0x0002; 30 31 private int flags; 32 private String text; 33 private String originalText; 34 private int bits; 35 36 protected TextLine() 37 { 38 } 39 40 public TextLine(String s) 41 { 42 text = s; 43 } 44 45 protected final void init(String s) 46 { 47 text = s; 48 } 49 50 public final synchronized int flags() 51 { 52 return flags; 53 } 54 55 public final synchronized void setFlags(int flags) 56 { 57 this.flags = flags; 58 } 59 60 public final synchronized String getText() 61 { 62 return text != null ? text : ""; 63 } 64 65 public final synchronized void setText(String s) 66 { 67 if (originalText == null) 68 originalText = text; 69 text = s; 70 if (text != null && text.equals(originalText)) 71 originalText = null; 72 bits &= ~SAVED; 73 } 74 75 public final String getOriginalText() 76 { 77 return originalText; 78 } 79 80 public final void setOriginalText(String s) 81 { 82 originalText = s; 83 } 84 85 public final boolean isModified() 86 { 87 return originalText != null || isNew(); 88 } 89 90 public final boolean isNew() 91 { 92 return (bits & NEW) == NEW; 93 } 94 95 public final void setNew(boolean b) 96 { 97 if (b) 98 bits |= NEW; 99 else 100 bits &= ~NEW; 101 } 102 103 public final boolean isSaved() 104 { 105 return (bits & SAVED) == SAVED; 106 } 107 108 public final void setSaved(boolean b) 109 { 110 if (b) 111 bits |= SAVED; 112 else 113 bits &= ~SAVED; 114 } 115 116 public final void unmodified() 117 { 118 originalText = null; 119 bits &= (~SAVED & ~NEW); 120 } 121 122 public final char charAt(int i) 123 { 124 return getText().charAt(i); 125 } 126 127 public final String substring(int beginIndex) 128 { 129 return getText().substring(beginIndex); 130 } 131 132 public final String substring(int beginIndex, int endIndex) 133 { 134 return getText().substring(beginIndex, endIndex); 135 } 136 137 public final String trim() 138 { 139 return getText().trim(); 140 } 141 142 public final int length() 143 { 144 return getText().length(); 145 } 146 147 public final int getWidth() 148 { 149 return length() * Display.getCharWidth(); 150 } 151 152 public final byte[] getBytes(String encoding) throws UnsupportedEncodingException 153 { 154 byte[] bytes = getText().getBytes(encoding); 155 if (bytes.length >= 2) { 156 if ((bytes[0] == (byte) 0xfe && bytes[1] == (byte) 0xff) || 157 (bytes[0] == (byte) 0xff && bytes[1] == (byte) 0xfe)) { 158 byte[] newBytes = new byte[bytes.length-2]; 160 for (int i = 0; i < newBytes.length; i++) 161 newBytes[i] = bytes[i+2]; 162 return newBytes; 163 } 164 } 165 return bytes; 166 } 167 168 public final boolean isBlank() 169 { 170 String s = getText(); 171 172 for (int i = s.length(); i-- > 0;) 173 if (!Character.isWhitespace(s.charAt(i))) 174 return false; 175 176 return true; 177 } 178 179 public Line copy() 181 { 182 TextLine line = new TextLine(text); 183 line.originalText = originalText; 184 line.bits = bits; 185 return line; 186 } 187 188 public void copy(Line line) 190 { 191 if (line instanceof TextLine) { 192 TextLine textLine = (TextLine) line; 193 text = textLine.text; 194 originalText = textLine.originalText; 195 bits = textLine.bits; 196 } else 197 Debug.bug(); 198 } 199 } 200 | Popular Tags |