1 22 23 27 28 package org.netbeans.lib.terminalemulator; 29 30 import java.awt.*; 31 import java.awt.datatransfer.*; 32 33 73 74 class Sel implements ClipboardOwner { 75 private static final int SEL_NONE = 0; 77 private static final int SEL_CHAR = 1; 78 private static final int SEL_WORD = 2; 79 private static final int SEL_LINE = 3; 80 81 public static final int INT_NONE = 0; 83 public static final int INT_ABOVE = 1; 84 public static final int INT_ON = 2; 85 public static final int INT_STRADDLES = 3; 86 public static final int INT_BELOW = 4; 87 88 private int sel_tracking; 89 private int old_sel_tracking; 90 91 private Coord sel_origin; 93 public Coord sel_extent; 94 95 private Extent initial_word; 98 99 private Term term; 100 private State state; 101 102 private Color color = new Color(204, 204, 255); void setColor(Color color) { this.color = color; } 105 Color getColor() { return color; } 106 107 private Color xor_color = Color.white; 108 void setXORColor(Color color) { xor_color = color; } 109 Color getXORColor() { return xor_color; } 110 111 Sel(Term term, State state) { 112 this.term = term; 113 this.state = state; 114 } 115 116 126 void adjust(int afirstline, int amount, int alastline) { 127 128 if (sel_origin == null) 129 return; 130 131 136 137 if (sel_origin.compareTo(sel_extent) >= 0) { 138 sel_extent.row += amount; 140 if (sel_extent.row < afirstline) 141 sel_extent.row = afirstline; 142 143 sel_origin.row += amount; 144 if (sel_origin.row >= alastline) { 145 sel_origin.row = alastline-1; 146 sel_origin.col = term.buf.totalCols(); 147 } 148 if (sel_origin.row < afirstline || sel_extent.row > alastline) { 149 sel_extent = null; 151 sel_origin = null; 152 } 153 154 } else { 155 sel_origin.row += amount; 157 if (sel_origin.row < afirstline) 158 sel_origin.row = afirstline; 159 160 sel_extent.row += amount; 161 if (sel_extent.row >= alastline) { 162 sel_extent.row = alastline-1; 163 sel_extent.col = term.buf.totalCols(); 164 } 165 if (sel_extent.row < afirstline || sel_origin.row > alastline) { 166 sel_origin = null; 168 sel_extent = null; 169 } 170 } 171 172 term.fireSelectionExtentChanged(); 173 } 174 175 void relocate(int from, int to) { 176 if (sel_origin == null) 177 return; 178 int delta = to - from; 179 sel_origin.row += delta; 180 sel_extent.row += delta; 181 } 182 183 Extent getExtent() { 184 if (sel_origin == null) 185 return null; 186 Extent x = new Extent(sel_origin, sel_extent); 187 x.order(); 188 return x; 189 } 190 191 void setExtent(Extent extent) { 192 cancel(false); 193 extent.order(); 194 sel_origin = (Coord) extent.begin.clone(); 195 sel_extent = (Coord) extent.end.clone(); 196 done(); } 198 199 public void select_word(Extent range) { 200 sel_origin = new Coord(range.begin); 201 sel_extent = new Coord(range.end); 202 sel_tracking = Sel.SEL_WORD; 203 old_sel_tracking = Sel.SEL_NONE; 204 initial_word = range; 205 } 206 207 public void select_line(Coord coord) { 208 209 211 sel_origin = Coord.make(coord.row, 0); 212 sel_extent = Coord.make(coord.row, term.buf.totalCols()); 213 sel_tracking = Sel.SEL_LINE; 214 old_sel_tracking = Sel.SEL_NONE; 215 } 216 217 private boolean extend_work(Coord p, int tracking) { 218 221 if (tracking == Sel.SEL_NONE) { 222 return false; 223 224 } else if (tracking == Sel.SEL_CHAR) { 225 sel_extent = p; 226 227 } else if (tracking == Sel.SEL_WORD) { 228 BExtent Bnew_range = term.buf.find_word(term.word_delineator, p.toBCoord(term.firsta)); 229 Extent new_range = Bnew_range.toExtent(term.firsta); 230 if (p.compareTo(initial_word.begin) < 0) { 231 sel_origin = new Coord(new_range.begin); 232 sel_extent = initial_word.end; 233 } else if (p.compareTo(initial_word.end) > 0) { 234 sel_origin = initial_word.begin; 235 sel_extent = new Coord(new_range.end); 236 } else { 237 sel_origin = initial_word.begin; 238 sel_extent = initial_word.end; 239 } 240 241 } else if (tracking == Sel.SEL_LINE) { 242 if (p.compareTo(sel_origin) > 0) { 243 sel_origin = Coord.make(sel_origin.row, 0); 244 sel_extent = Coord.make(p.row, term.buf.totalCols()); 245 } else { 246 sel_origin = Coord.make(sel_origin.row, term.buf.totalCols()); 247 sel_extent = Coord.make(p.row, 0); 248 } 249 } 250 return true; 251 } 252 253 public void track(Coord p) { 254 if (sel_tracking == Sel.SEL_NONE) { 255 sel_origin = p; 257 sel_extent = p; 258 sel_tracking = Sel.SEL_CHAR; 259 old_sel_tracking = Sel.SEL_NONE; 260 } 261 extend_work(p, sel_tracking); 262 } 263 264 public boolean extend(Coord p) { 265 if (sel_origin == null) 267 return false; 268 else 269 return extend_work(p, old_sel_tracking); 270 } 271 272 276 private boolean cancelHelp(boolean and_fire) { 277 if (sel_origin == null) 278 return false; 279 old_sel_tracking = Sel.SEL_NONE; 280 sel_tracking = Sel.SEL_NONE; 281 sel_origin = null; 282 sel_extent = null; 283 initial_word = null; 284 285 if (and_fire) 286 term.fireSelectionExtentChanged(); 287 288 return true; 289 } 290 291 public boolean cancel(boolean and_fire) { 292 if (!cancelHelp(and_fire)) 293 return false; 294 term.copyToSelection(); 295 return true; 296 } 297 298 public void done() { 299 old_sel_tracking = sel_tracking; 302 sel_tracking = Sel.SEL_NONE; 303 304 term.copyToSelection(); 305 306 term.fireSelectionExtentChanged(); 307 } 308 309 public void lostOwnership(Clipboard cb, Transferable c) { 310 315 318 if (cancelHelp(true)) 319 term.repaint(false); 320 } 321 322 public String getSelection() { 323 324 Extent x = getExtent(); 325 if (x == null) 326 return null; 327 328 if (x.begin != null && x.end != null) { 329 final StringBuffer text = new StringBuffer (); 330 term.visitLines(x.begin, x.end, true, new LineVisitor() { 331 public boolean visit(Line l, int row, int bcol, int ecol) { 332 text.append(l.text(bcol, ecol)); 333 return true; 334 } 335 } ); 336 return text.toString(); 337 } 338 339 return ""; } 341 342 346 int intersection(int line) { 347 356 357 Extent x = getExtent(); 358 if (x == null) 359 return INT_NONE; 360 x.order(); 361 362 if (x.end.row < line) 363 return INT_ABOVE; 364 else if (x.end.row == line) 365 return INT_ON; 366 else if (x.begin.row > line) 367 return INT_BELOW; 368 else 369 return INT_STRADDLES; 370 } 371 372 376 private void paint(Graphics g, int row, int bcol, int ecol) { 377 378 380 if (row < state.firstx) 382 return; 383 if (row > state.firstx + state.rows) 384 return; 385 386 BCoord begin = new BCoord(row, bcol); 388 BCoord end = new BCoord(row, ecol); 389 390 begin = term.toViewCoord(begin); 391 end = term.toViewCoord(end); 392 393 if (begin == null || end == null) { 395 return; 396 } 397 398 int lw; Line l = term.buf.lineAt(row); 400 lw = l.width(term.metrics, ecol); 401 402 Point pbegin = term.toPixel(begin); 403 Point pend = term.toPixel(end); 404 pend.y += term.metrics.height; 405 pend.x += term.metrics.width * lw; 407 Dimension dim = new Dimension(pend.x - pbegin.x, 408 pend.y - pbegin.y); 409 Rectangle rect = new Rectangle(pbegin, dim); 410 411 412 if (term.selection_xor) 413 g.setXORMode(xor_color); 414 else 415 g.setColor(color); 416 417 g.fillRect(rect.x, rect.y, rect.width, rect.height); 418 } 419 420 void paint(final Graphics g) { 421 424 425 Extent x = getExtent(); 426 if (x == null) 427 return; 428 x.order(); 429 430 432 term.visitLines(x.begin, x.end, true, new LineVisitor() { 433 public boolean visit(Line l, int row, int bcol, int ecol) { 434 paint(g, row, bcol, ecol); 435 return true; 436 } 437 } ); 438 } 439 } 440 | Popular Tags |