38 |
package javax.swing; |
package javax.swing; |
39 |
|
|
40 |
import java.awt.Dimension; |
import java.awt.Dimension; |
41 |
|
import java.awt.FontMetrics; |
42 |
import javax.swing.text.BadLocationException; |
import javax.swing.text.BadLocationException; |
43 |
import javax.swing.text.Document; |
import javax.swing.text.Document; |
44 |
|
import javax.swing.text.Element; |
45 |
import javax.swing.text.JTextComponent; |
import javax.swing.text.JTextComponent; |
46 |
import javax.swing.text.PlainDocument; |
import javax.swing.text.PlainDocument; |
47 |
|
|
371 |
firePropertyChange("tabSize", oldValue, tabSize); |
firePropertyChange("tabSize", oldValue, tabSize); |
372 |
} |
} |
373 |
|
|
374 |
|
protected int getColumnWidth() |
375 |
|
{ |
376 |
|
FontMetrics metrics = getToolkit().getFontMetrics(getFont()); |
377 |
|
return metrics.charWidth('m'); |
378 |
|
} |
379 |
|
|
380 |
|
public int getLineCount() |
381 |
|
{ |
382 |
|
return getDocument().getDefaultRootElement().getElementCount(); |
383 |
|
} |
384 |
|
|
385 |
|
public int getLineStartOffset(int line) |
386 |
|
throws BadLocationException |
387 |
|
{ |
388 |
|
int lineCount = getLineCount(); |
389 |
|
|
390 |
|
if (line < 0 || line > lineCount) |
391 |
|
throw new BadLocationException("Non-existing line number", line); |
392 |
|
|
393 |
|
Element lineElem = getDocument().getDefaultRootElement().getElement(line); |
394 |
|
return lineElem.getStartOffset(); |
395 |
|
} |
396 |
|
|
397 |
|
public int getLineEndOffset(int line) |
398 |
|
throws BadLocationException |
399 |
|
{ |
400 |
|
int lineCount = getLineCount(); |
401 |
|
|
402 |
|
if (line < 0 || line > lineCount) |
403 |
|
throw new BadLocationException("Non-existing line number", line); |
404 |
|
|
405 |
|
Element lineElem = getDocument().getDefaultRootElement().getElement(line); |
406 |
|
return lineElem.getEndOffset(); |
407 |
|
} |
408 |
|
|
409 |
|
public int getLineOfOffset(int offset) |
410 |
|
throws BadLocationException |
411 |
|
{ |
412 |
|
Document doc = getDocument(); |
413 |
|
|
414 |
|
if (offset < doc.getStartPosition().getOffset() |
415 |
|
|| offset >= doc.getEndPosition().getOffset()) |
416 |
|
throw new BadLocationException("offset outside of document", offset); |
417 |
|
|
418 |
|
return doc.getDefaultRootElement().getElementIndex(offset); |
419 |
|
} |
420 |
|
|
421 |
|
protected int getRowHeight() |
422 |
|
{ |
423 |
|
FontMetrics metrics = getToolkit().getFontMetrics(getFont()); |
424 |
|
return metrics.getHeight(); |
425 |
|
} |
426 |
|
|
427 |
/** |
/** |
428 |
* Inserts the supplied text at the specified position. Nothing |
* Inserts the supplied text at the specified position. Nothing |
429 |
* happens in the case that the model or the supplied string is null |
* happens in the case that the model or the supplied string is null |
431 |
* |
* |
432 |
* @param string The string of text to insert. |
* @param string The string of text to insert. |
433 |
* @param position The position at which to insert the supplied text. |
* @param position The position at which to insert the supplied text. |
434 |
* @throws IllegalArgumentException if the position is < 0 or greater |
* @throws IllegalArgumentException if the position is < 0 or greater |
435 |
* than the length of the current text. |
* than the length of the current text. |
436 |
*/ |
*/ |
437 |
public void insert(String string, int position) |
public void insert(String string, int position) |
438 |
{ |
{ |
439 |
Document document; |
// Retrieve the document model. |
440 |
|
Document document = getDocument(); |
441 |
|
|
442 |
/* Retrieve the document model */ |
// Check the model and string for validity. |
443 |
document = getDocument(); |
if (document == null |
444 |
/* Check the model and string for validity */ |
|| string == null |
445 |
if (document == null || string == null || string.length() == 0) |
|| string.length() == 0) |
446 |
{ |
return; |
447 |
return; /* Do nothing */ |
|
448 |
} |
// Insert the text into the model. |
|
/* Insert the text into the model */ |
|
449 |
try |
try |
450 |
{ |
{ |
451 |
document.insertString(position, string, null); |
document.insertString(position, string, null); |
452 |
} |
} |
453 |
catch (BadLocationException exception) |
catch (BadLocationException exception) |
454 |
{ |
{ |
455 |
throw new IllegalArgumentException("The supplied position, " + |
throw new IllegalArgumentException("The supplied position, " |
456 |
position + ", was invalid."); |
+ position + ", was invalid."); |
457 |
} |
} |
458 |
} |
} |
459 |
|
|
460 |
|
public void replaceRange(String text, int start, int end) |
461 |
|
{ |
462 |
|
Document doc = getDocument(); |
463 |
|
|
464 |
|
if (start > end |
465 |
|
|| start < doc.getStartPosition().getOffset() |
466 |
|
|| end >= doc.getEndPosition().getOffset()) |
467 |
|
throw new IllegalArgumentException(); |
468 |
|
|
469 |
|
try |
470 |
|
{ |
471 |
|
doc.remove(start, end); |
472 |
|
doc.insertString(start, text, null); |
473 |
|
} |
474 |
|
catch (BadLocationException e) |
475 |
|
{ |
476 |
|
// This cannot happen as we check offset above. |
477 |
|
} |
478 |
|
} |
479 |
} |
} |