38 |
package javax.swing.text; |
package javax.swing.text; |
39 |
|
|
40 |
import java.awt.AWTEvent; |
import java.awt.AWTEvent; |
41 |
|
import java.awt.Color; |
42 |
import java.awt.Component; |
import java.awt.Component; |
43 |
import java.awt.Dimension; |
import java.awt.Dimension; |
44 |
import java.awt.Image; |
import java.awt.Image; |
68 |
public abstract class JTextComponent extends JComponent |
public abstract class JTextComponent extends JComponent |
69 |
implements Scrollable, Accessible |
implements Scrollable, Accessible |
70 |
{ |
{ |
|
// public class AccessibleJTextComponent extends AccessibleJComponent |
|
|
// implements AccessibleText, CaretListener, DocumentListener, |
|
|
// AccessibleAction, AccessibleEditableText |
|
|
// { |
|
|
// } |
|
|
|
|
71 |
/** |
/** |
72 |
* AccessibleJTextComponent |
* AccessibleJTextComponent |
73 |
*/ |
*/ |
77 |
private static final long serialVersionUID = 7664188944091413696L; |
private static final long serialVersionUID = 7664188944091413696L; |
78 |
|
|
79 |
/** |
/** |
|
* caretPos |
|
|
*/ |
|
|
int caretPos; |
|
|
|
|
|
/** |
|
80 |
* Constructor AccessibleJTextComponent |
* Constructor AccessibleJTextComponent |
81 |
* @param component TODO |
* @param component TODO |
82 |
*/ |
*/ |
270 |
public KeyStroke key; |
public KeyStroke key; |
271 |
public String actionName; |
public String actionName; |
272 |
|
|
273 |
|
/** |
274 |
|
* Creates a new <code>KeyBinding</code> instance. |
275 |
|
* |
276 |
|
* @param key a <code>KeyStroke</code> value |
277 |
|
* @param actionName a <code>String</code> value |
278 |
|
*/ |
279 |
public KeyBinding(KeyStroke key, String actionName) |
public KeyBinding(KeyStroke key, String actionName) |
280 |
{ |
{ |
281 |
this.key = key; |
this.key = key; |
290 |
|
|
291 |
private Document doc; |
private Document doc; |
292 |
private Caret caret; |
private Caret caret; |
293 |
|
private Highlighter highlighter; |
294 |
|
private Color caretColor; |
295 |
|
private Color disabledTextColor; |
296 |
|
private Color selectedTextColor; |
297 |
|
private Color selectionColor; |
298 |
private boolean editable; |
private boolean editable; |
299 |
|
|
300 |
|
/** |
301 |
|
* Creates a new <code>JTextComponent</code> instance. |
302 |
|
*/ |
303 |
public JTextComponent() |
public JTextComponent() |
304 |
{ |
{ |
305 |
enableEvents(AWTEvent.KEY_EVENT_MASK); |
enableEvents(AWTEvent.KEY_EVENT_MASK); |
315 |
|
|
316 |
public Document getDocument() |
public Document getDocument() |
317 |
{ |
{ |
|
if (doc == null) |
|
|
System.out.println("doc == null !!!"); |
|
318 |
return doc; |
return doc; |
319 |
} |
} |
320 |
|
|
321 |
/** |
/** |
322 |
* Get the AccessibleContext of this object |
* Get the <code>AccessibleContext<code> of this object. |
323 |
|
* |
324 |
|
* @return an <code>AccessibleContext</code> object |
325 |
*/ |
*/ |
326 |
public AccessibleContext getAccessibleContext() |
public AccessibleContext getAccessibleContext() |
327 |
{ |
{ |
355 |
*/ |
*/ |
356 |
public String getText() |
public String getText() |
357 |
{ |
{ |
358 |
return getDocument().getText(0, getDocument().getLength()); |
if (doc == null) |
359 |
|
return null; |
360 |
|
|
361 |
|
try |
362 |
|
{ |
363 |
|
return doc.getText(0, doc.getLength()); |
364 |
|
} |
365 |
|
catch (BadLocationException e) |
366 |
|
{ |
367 |
|
// This should never happen. |
368 |
|
return ""; |
369 |
|
} |
370 |
} |
} |
371 |
|
|
372 |
/** |
/** |
404 |
return "JTextComponent"; |
return "JTextComponent"; |
405 |
} |
} |
406 |
|
|
407 |
|
/** |
408 |
|
* This method returns the label's UI delegate. |
409 |
|
* |
410 |
|
* @return The label's UI delegate. |
411 |
|
*/ |
412 |
public TextUI getUI() |
public TextUI getUI() |
413 |
{ |
{ |
414 |
return (TextUI) UIManager.getUI(this); |
return (TextUI) ui; |
415 |
|
} |
416 |
|
|
417 |
|
/** |
418 |
|
* This method sets the label's UI delegate. |
419 |
|
* |
420 |
|
* @param ui The label's UI delegate. |
421 |
|
*/ |
422 |
|
public void setUI(TextUI newUI) |
423 |
|
{ |
424 |
|
super.setUI(newUI); |
425 |
} |
} |
426 |
|
|
427 |
|
/** |
428 |
|
* This method resets the label's UI delegate to the default UI for the |
429 |
|
* current look and feel. |
430 |
|
*/ |
431 |
public void updateUI() |
public void updateUI() |
432 |
{ |
{ |
433 |
setUI(getUI()); |
setUI((TextUI) UIManager.getUI(this)); |
434 |
} |
} |
435 |
|
|
436 |
public Dimension getPreferredScrollableViewportSize() |
public Dimension getPreferredScrollableViewportSize() |
482 |
} |
} |
483 |
|
|
484 |
/** |
/** |
485 |
|
* Sets a new <code>Caret</code> for this text component. |
486 |
|
* |
487 |
|
* @param newCaret the new <code>Caret</code> to set |
488 |
|
*/ |
489 |
|
public void setCaret(Caret newCaret) |
490 |
|
{ |
491 |
|
firePropertyChange("caret", caret, newCaret); |
492 |
|
caret = newCaret; |
493 |
|
} |
494 |
|
|
495 |
|
public Color getCaretColor() |
496 |
|
{ |
497 |
|
return caretColor; |
498 |
|
} |
499 |
|
|
500 |
|
public void setCaretColor(Color newColor) |
501 |
|
{ |
502 |
|
firePropertyChange("caretColor", caretColor, newColor); |
503 |
|
caretColor = newColor; |
504 |
|
} |
505 |
|
|
506 |
|
public Color getDisabledTextColor() |
507 |
|
{ |
508 |
|
return disabledTextColor; |
509 |
|
} |
510 |
|
|
511 |
|
public void setDisabledTextColor(Color newColor) |
512 |
|
{ |
513 |
|
firePropertyChange("disabledTextColor", caretColor, newColor); |
514 |
|
disabledTextColor = newColor; |
515 |
|
} |
516 |
|
|
517 |
|
public Color getSelectedTextColor() |
518 |
|
{ |
519 |
|
return selectedTextColor; |
520 |
|
} |
521 |
|
|
522 |
|
public void setSelectedTextColor(Color newColor) |
523 |
|
{ |
524 |
|
firePropertyChange("selectedTextColor", caretColor, newColor); |
525 |
|
selectedTextColor = newColor; |
526 |
|
} |
527 |
|
|
528 |
|
public Color getSelectionColor() |
529 |
|
{ |
530 |
|
return selectionColor; |
531 |
|
} |
532 |
|
|
533 |
|
public void setSelectionColor(Color newColor) |
534 |
|
{ |
535 |
|
firePropertyChange("selectionColor", caretColor, newColor); |
536 |
|
selectionColor = newColor; |
537 |
|
} |
538 |
|
|
539 |
|
/** |
540 |
* Retrisves the current caret position. |
* Retrisves the current caret position. |
541 |
* |
* |
542 |
* @return the current position |
* @return the current position |
577 |
caret.moveDot(position); |
caret.moveDot(position); |
578 |
} |
} |
579 |
|
|
580 |
|
public Highlighter getHighlighter() |
581 |
|
{ |
582 |
|
return highlighter; |
583 |
|
} |
584 |
|
|
585 |
|
public void setHighlighter(Highlighter newHighlighter) |
586 |
|
{ |
587 |
|
firePropertyChange("highlighter", highlighter, newHighlighter); |
588 |
|
highlighter = newHighlighter; |
589 |
|
} |
590 |
|
|
591 |
/** |
/** |
592 |
* Returns the start postion of the currently selected text. |
* Returns the start postion of the currently selected text. |
593 |
* |
* |
656 |
select(0, doc.getLength()); |
select(0, doc.getLength()); |
657 |
} |
} |
658 |
|
|
659 |
|
public synchronized void replaceSelection(String content) |
660 |
|
{ |
661 |
|
int dot = caret.getDot(); |
662 |
|
int mark = caret.getMark(); |
663 |
|
|
664 |
|
// If content is empty delete selection. |
665 |
|
if (content == null) |
666 |
|
{ |
667 |
|
caret.setDot(dot); |
668 |
|
return; |
669 |
|
} |
670 |
|
|
671 |
|
try |
672 |
|
{ |
673 |
|
// Remove selected text. |
674 |
|
if (dot != mark) |
675 |
|
doc.remove(Math.min(dot, mark), Math.max(dot, mark)); |
676 |
|
|
677 |
|
// Insert new text. |
678 |
|
doc.insertString(Math.min(dot, mark), content, null); |
679 |
|
} |
680 |
|
catch (BadLocationException e) |
681 |
|
{ |
682 |
|
// This should never happen. |
683 |
|
System.out.println("Michael: JTextComponent.replaceSelection: Error"); |
684 |
|
} |
685 |
|
} |
686 |
|
|
687 |
public boolean getScrollableTracksViewportHeight() |
public boolean getScrollableTracksViewportHeight() |
688 |
{ |
{ |
689 |
if (getParent() instanceof JViewport) |
if (getParent() instanceof JViewport) |