Source Code Cross Referenced for TextArea.java in  » Apache-Harmony-Java-SE » java-package » java » awt » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » java package » java.awt 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:
018:        package java.awt;
019:
020:        import java.awt.event.InputEvent;
021:        import java.awt.event.KeyEvent;
022:        import java.awt.event.MouseEvent;
023:        import java.awt.event.MouseListener;
024:        import java.awt.event.MouseMotionListener;
025:        import java.util.HashSet;
026:        import java.util.Set;
027:        import javax.accessibility.AccessibleContext;
028:        import javax.accessibility.AccessibleState;
029:        import javax.accessibility.AccessibleStateSet;
030:        import javax.swing.text.BadLocationException;
031:        import javax.swing.text.View;
032:        import org.apache.harmony.awt.ScrollStateController;
033:        import org.apache.harmony.awt.Scrollable;
034:        import org.apache.harmony.awt.internal.nls.Messages;
035:        import org.apache.harmony.awt.text.TextFactory;
036:        import org.apache.harmony.awt.wtk.NativeWindow;
037:
038:        public class TextArea extends TextComponent {
039:            protected class AccessibleAWTTextArea extends
040:                    AccessibleAWTTextComponent {
041:                private static final long serialVersionUID = 3472827823632144419L;
042:
043:                @Override
044:                public AccessibleStateSet getAccessibleStateSet() {
045:                    AccessibleStateSet set = super .getAccessibleStateSet();
046:                    set.add(AccessibleState.MULTI_LINE);
047:                    return set;
048:                }
049:            }
050:
051:            /**
052:             * Scrolling behavior implementation
053:             */
054:            class TextScrollable implements  Scrollable {
055:                public Adjustable getVAdjustable() {
056:                    return vAdjustable;
057:                }
058:
059:                public Adjustable getHAdjustable() {
060:                    return hAdjustable;
061:                }
062:
063:                public Insets getInsets() {
064:                    return getNativeInsets();
065:                }
066:
067:                public Point getLocation() {
068:                    Point p = getViewPosition();
069:                    p.setLocation(-p.x, -p.y);
070:                    return p;
071:                }
072:
073:                public void setLocation(Point p) {
074:                    Point pos = new Point(-p.x, -p.y);
075:                    setViewPosition(pos);
076:                }
077:
078:                public Component getComponent() {
079:                    return TextArea.this ;
080:                }
081:
082:                public Dimension getSize() {
083:                    return getModelRect().getSize();
084:                }
085:
086:                public void doRepaint() {
087:                    TextArea.this .doRepaint();
088:                }
089:
090:                public int getAdjustableWidth() {
091:                    return (vAdjustable != null ? vAdjustable.getBounds().width
092:                            : 0);
093:                }
094:
095:                public int getAdjustableHeight() {
096:                    return (hAdjustable != null ? hAdjustable.getBounds().height
097:                            : 0);
098:                }
099:
100:                public void setAdjustableSizes(Adjustable adj, int vis,
101:                        int min, int max) {
102:                    ((ScrollPaneAdjustable) adj).setSizes(vis, min, max);
103:                }
104:
105:                public int getAdjustableMode(Adjustable adj) {
106:                    switch (getScrollbarVisibility()) {
107:                    case SCROLLBARS_BOTH:
108:                        return Scrollable.ALWAYS;
109:                    case SCROLLBARS_HORIZONTAL_ONLY:
110:                        return Scrollable.HORIZONTAL_ONLY;
111:                    case SCROLLBARS_NONE:
112:                        return Scrollable.NEVER;
113:                    case SCROLLBARS_VERTICAL_ONLY:
114:                        return Scrollable.VERTICAL_ONLY;
115:                    default:
116:                        return Scrollable.NEVER;
117:                    }
118:                }
119:
120:                public void setAdjustableBounds(Adjustable adj, Rectangle r) {
121:                    ((ScrollPaneAdjustable) adj).setBounds(r);
122:                }
123:
124:                public int getWidth() {
125:                    return TextArea.this .getWidth();
126:                }
127:
128:                public int getHeight() {
129:                    return TextArea.this .getHeight();
130:                }
131:
132:                public void doRepaint(Rectangle r) {
133:                    TextArea.this .doRepaint(r);
134:                }
135:            }
136:
137:            /**
138:             * Helper class which filters out all mouse
139:             * events on non-client area and switches cursors
140:             * (default cursor is always shown above non-client area)
141:             */
142:            class MouseEventFilter implements  MouseListener,
143:                    MouseMotionListener {
144:                private final MouseListener mListener;
145:
146:                private final MouseMotionListener mmListener;
147:
148:                private boolean inside = true;
149:
150:                boolean clientDrag;
151:
152:                boolean scrollDrag;
153:
154:                public MouseEventFilter(MouseListener ml,
155:                        MouseMotionListener mml) {
156:                    mListener = ml;
157:                    mmListener = mml;
158:                }
159:
160:                private boolean accept(MouseEvent e) {
161:                    return getClient().contains(e.getPoint());
162:                }
163:
164:                public void mouseClicked(MouseEvent e) {
165:                    if (inside = accept(e)) {
166:                        mListener.mouseClicked(e);
167:                    } else {
168:                        setDefaultCursor();
169:                    }
170:                }
171:
172:                public void mouseEntered(MouseEvent e) {
173:                    inside = accept(e);
174:                    if (!inside) {
175:                        setDefaultCursor();
176:                    }
177:                    mListener.mouseEntered(e);
178:                }
179:
180:                public void mouseExited(MouseEvent e) {
181:                    mListener.mouseExited(e);
182:                }
183:
184:                public void mousePressed(MouseEvent e) {
185:                    if (inside = accept(e)) {
186:                        clientDrag = true;
187:                        mListener.mousePressed(e);
188:                    } else {
189:                        scrollDrag = true;
190:                        setDefaultCursor();
191:                    }
192:                }
193:
194:                public void mouseReleased(MouseEvent e) {
195:                    if (inside = accept(e) || clientDrag) {
196:                        mListener.mouseReleased(e);
197:                    } else {
198:                        setDefaultCursor();
199:                    }
200:                    clientDrag = false;
201:                    scrollDrag = false;
202:                }
203:
204:                public void mouseDragged(MouseEvent e) {
205:                    if (!scrollDrag && (accept(e) || clientDrag)) {
206:                        mmListener.mouseDragged(e);
207:                    }
208:                }
209:
210:                public void mouseMoved(MouseEvent e) {
211:                    if (accept(e)) {
212:                        if (!inside) {
213:                            setCursor();
214:                            inside = true;
215:                        }
216:                        mmListener.mouseMoved(e);
217:                    } else if (inside) {
218:                        setDefaultCursor();
219:                        inside = false;
220:                    }
221:                }
222:
223:                private void setDefaultCursor() {
224:                    Window topLevel = getWindowAncestor();
225:                    if (topLevel == null) {
226:                        return;
227:                    }
228:                    NativeWindow wnd = topLevel.getNativeWindow();
229:                    if (wnd == null) {
230:                        return;
231:                    }
232:                    Cursor.getDefaultCursor().getNativeCursor().setCursor(
233:                            wnd.getId());
234:                }
235:            }
236:
237:            private static final long serialVersionUID = 3692302836626095722L;
238:
239:            public static final int SCROLLBARS_BOTH = 0;
240:
241:            public static final int SCROLLBARS_VERTICAL_ONLY = 1;
242:
243:            public static final int SCROLLBARS_HORIZONTAL_ONLY = 2;
244:
245:            public static final int SCROLLBARS_NONE = 3;
246:
247:            private int rows;
248:
249:            private int columns;
250:
251:            private int scrollbarVisibility = SCROLLBARS_BOTH;
252:
253:            private ScrollPaneAdjustable hAdjustable;
254:
255:            private ScrollPaneAdjustable vAdjustable;
256:
257:            private final ScrollStateController stateController;
258:
259:            private final Scrollable scrollable;
260:
261:            private MouseEventFilter filter;
262:
263:            public TextArea() throws HeadlessException {
264:                this (new String(), 0, 0, SCROLLBARS_BOTH);
265:                toolkit.lockAWT();
266:                try {
267:                } finally {
268:                    toolkit.unlockAWT();
269:                }
270:            }
271:
272:            public TextArea(String text, int rows, int columns, int scrollbars)
273:                    throws HeadlessException {
274:                super ();
275:                toolkit.lockAWT();
276:                try {
277:                    Toolkit.checkHeadless();
278:                    setFont(new Font("Dialog", Font.PLAIN, 12)); // QUICK FIX //$NON-NLS-1$
279:                    setText(text);
280:                    this .rows = Math.max(0, rows);
281:                    this .columns = Math.max(0, columns);
282:                    if ((scrollbars < SCROLLBARS_BOTH)
283:                            || (scrollbars > SCROLLBARS_NONE)) {
284:                        scrollbars = SCROLLBARS_BOTH;
285:                    }
286:                    scrollbarVisibility = scrollbars;
287:                    if (noHorizontalScroll()) {
288:                        replaceView();
289:                    }
290:                    setFocusTraversalKeys();
291:                    // init scrolling
292:                    hAdjustable = new ScrollPaneAdjustable(this ,
293:                            Adjustable.HORIZONTAL);
294:                    vAdjustable = new ScrollPaneAdjustable(this ,
295:                            Adjustable.VERTICAL);
296:                    scrollable = new TextScrollable();
297:                    stateController = new ScrollStateController(scrollable);
298:                    addScrolling();
299:                } finally {
300:                    toolkit.unlockAWT();
301:                }
302:            }
303:
304:            /**
305:             * Excludes &lt;Tab>, &lt;Shift-Tab> from default focus traversal keys sets
306:             */
307:            private void setFocusTraversalKeys() {
308:                int id = KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS;
309:                Set<AWTKeyStroke> set = new HashSet<AWTKeyStroke>(
310:                        getFocusTraversalKeys(id));
311:                AWTKeyStroke tab = AWTKeyStroke.getAWTKeyStroke(
312:                        KeyEvent.VK_TAB, 0);
313:                AWTKeyStroke shiftTab = AWTKeyStroke.getAWTKeyStroke(
314:                        KeyEvent.VK_TAB, InputEvent.SHIFT_DOWN_MASK);
315:                set.remove(tab);
316:                setFocusTraversalKeys(id, set);
317:                id = KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS;
318:                set = new HashSet<AWTKeyStroke>(getFocusTraversalKeys(id));
319:                set.remove(shiftTab);
320:                setFocusTraversalKeys(id, set);
321:            }
322:
323:            /**
324:             * Replaces default plain view with wrapped plain view
325:             * to get word-wrapping behavior of TextArea without
326:             * horizontal scrollbar
327:             */
328:            private void replaceView() {
329:                TextFactory factory = TextFactory.getTextFactory();
330:                View view = factory.createWrappedPlainView(document
331:                        .getDefaultRootElement());
332:                rootViewContext.getView().replace(0, 1, new View[] { view });
333:            }
334:
335:            private boolean noHorizontalScroll() {
336:                return ((scrollbarVisibility == SCROLLBARS_NONE) || (scrollbarVisibility == SCROLLBARS_VERTICAL_ONLY));
337:            }
338:
339:            public TextArea(String text, int rows, int columns)
340:                    throws HeadlessException {
341:                this (text, rows, columns, SCROLLBARS_BOTH);
342:                toolkit.lockAWT();
343:                try {
344:                } finally {
345:                    toolkit.unlockAWT();
346:                }
347:            }
348:
349:            public TextArea(String text) throws HeadlessException {
350:                this (text, 0, 0, SCROLLBARS_BOTH);
351:                toolkit.lockAWT();
352:                try {
353:                } finally {
354:                    toolkit.unlockAWT();
355:                }
356:            }
357:
358:            public TextArea(int rows, int columns) throws HeadlessException {
359:                this (new String(), rows, columns, SCROLLBARS_BOTH);
360:                toolkit.lockAWT();
361:                try {
362:                } finally {
363:                    toolkit.unlockAWT();
364:                }
365:            }
366:
367:            public void append(String str) {
368:                try {
369:                    document.insertString(document.getLength(), str, null);
370:                } catch (BadLocationException e) {
371:                    throw new RuntimeException(e);
372:                }
373:                //        toolkit.lockAWT();
374:                //        try {
375:                if (isDisplayable()) {
376:                    // update caret position on text append
377:                    int newCaretPos = document.getLength();
378:                    if (caret.getDot() != newCaretPos) {
379:                        caret.setDot(newCaretPos, caret.getDotBias());
380:                    }
381:                }
382:                //        } finally {
383:                //            toolkit.unlockAWT();
384:                //        }
385:            }
386:
387:            public void insert(String str, int pos) {
388:                int oldPos = caret.getDot();
389:                try {
390:                    document.insertString(pos, str, null);
391:                } catch (BadLocationException e) {
392:                    throw new IndexOutOfBoundsException();
393:                }
394:                //        toolkit.lockAWT();
395:                //        try {
396:                if (isDisplayable()) {
397:                    // update caret position on text insertion
398:                    int newCaretPos = pos + str.length();
399:                    if (caret.getDot() != newCaretPos) {
400:                        caret.setDot(newCaretPos, caret.getDotBias());
401:                    }
402:                } else if (caret.getDot() != oldPos) {
403:                    // move caret back:
404:                    caret.setDot(oldPos, caret.getDotBias());
405:                }
406:                //        } finally {
407:                //            toolkit.unlockAWT();
408:                //        }
409:            }
410:
411:            @Override
412:            public void addNotify() {
413:                toolkit.lockAWT();
414:                try {
415:                    super .addNotify();
416:                } finally {
417:                    toolkit.unlockAWT();
418:                }
419:            }
420:
421:            @Deprecated
422:            public void appendText(String str) {
423:                append(str);
424:            }
425:
426:            @Override
427:            public AccessibleContext getAccessibleContext() {
428:                toolkit.lockAWT();
429:                try {
430:                    return super .getAccessibleContext();
431:                } finally {
432:                    toolkit.unlockAWT();
433:                }
434:            }
435:
436:            public int getColumns() {
437:                toolkit.lockAWT();
438:                try {
439:                    return columns;
440:                } finally {
441:                    toolkit.unlockAWT();
442:                }
443:            }
444:
445:            @Override
446:            public Dimension getMinimumSize() {
447:                toolkit.lockAWT();
448:                try {
449:                    return minimumSize();
450:                } finally {
451:                    toolkit.unlockAWT();
452:                }
453:            }
454:
455:            public Dimension getMinimumSize(int rows, int columns) {
456:                toolkit.lockAWT();
457:                try {
458:                    return minimumSize(rows, columns);
459:                } finally {
460:                    toolkit.unlockAWT();
461:                }
462:            }
463:
464:            public Dimension getPreferredSize(int rows, int columns) {
465:                toolkit.lockAWT();
466:                try {
467:                    return preferredSize(rows, columns);
468:                } finally {
469:                    toolkit.unlockAWT();
470:                }
471:            }
472:
473:            @Override
474:            public Dimension getPreferredSize() {
475:                toolkit.lockAWT();
476:                try {
477:                    return preferredSize();
478:                } finally {
479:                    toolkit.unlockAWT();
480:                }
481:            }
482:
483:            public int getRows() {
484:                toolkit.lockAWT();
485:                try {
486:                    return rows;
487:                } finally {
488:                    toolkit.unlockAWT();
489:                }
490:            }
491:
492:            public int getScrollbarVisibility() {
493:                toolkit.lockAWT();
494:                try {
495:                    return scrollbarVisibility;
496:                } finally {
497:                    toolkit.unlockAWT();
498:                }
499:            }
500:
501:            /**
502:             * @deprecated
503:             */
504:            @Deprecated
505:            public void insertText(String str, int pos) {
506:                insert(str, pos);
507:            }
508:
509:            /**
510:             * @deprecated
511:             */
512:            @SuppressWarnings("deprecation")
513:            @Deprecated
514:            @Override
515:            public Dimension minimumSize() {
516:                toolkit.lockAWT();
517:                try {
518:                    if ((rows > 0) && (columns > 0)) {
519:                        return minimumSize(rows, columns);
520:                    }
521:                    return super .minimumSize();
522:                } finally {
523:                    toolkit.unlockAWT();
524:                }
525:            }
526:
527:            /**
528:             * @deprecated
529:             */
530:            @SuppressWarnings("deprecation")
531:            @Deprecated
532:            public Dimension minimumSize(int rows, int columns) {
533:                toolkit.lockAWT();
534:                try {
535:                    Dimension minSize = calcSize(rows, columns);
536:                    if (minSize == null) {
537:                        return super .minimumSize();
538:                    }
539:                    return minSize;
540:                } finally {
541:                    toolkit.unlockAWT();
542:                }
543:            }
544:
545:            @Override
546:            protected String paramString() {
547:                /* The format is based on 1.5 release behavior 
548:                 * which can be revealed by the following code:
549:                 * System.out.println(new TextArea());
550:                 */
551:                toolkit.lockAWT();
552:                try {
553:                    String strScrollbarVis = null;
554:                    switch (getScrollbarVisibility()) {
555:                    case SCROLLBARS_BOTH:
556:                        strScrollbarVis = "both"; //$NON-NLS-1$
557:                        break;
558:                    case SCROLLBARS_HORIZONTAL_ONLY:
559:                        strScrollbarVis = "horizontal only"; //$NON-NLS-1$
560:                        break;
561:                    case SCROLLBARS_NONE:
562:                        strScrollbarVis = "none"; //$NON-NLS-1$
563:                        break;
564:                    case SCROLLBARS_VERTICAL_ONLY:
565:                        strScrollbarVis = "vertical only"; //$NON-NLS-1$
566:                        break;
567:                    }
568:                    return (super .paramString()
569:                            + ",rows=" + getRows() + ",columns=" + getColumns() //$NON-NLS-1$ //$NON-NLS-2$
570:                            + ",scrollbarVisibility=" + strScrollbarVis); //$NON-NLS-1$
571:                } finally {
572:                    toolkit.unlockAWT();
573:                }
574:            }
575:
576:            /**
577:             * @deprecated
578:             */
579:            @SuppressWarnings("deprecation")
580:            @Deprecated
581:            @Override
582:            public Dimension preferredSize() {
583:                toolkit.lockAWT();
584:                try {
585:                    if ((rows > 0) && (columns > 0)) {
586:                        return preferredSize(rows, columns);
587:                    }
588:                    return super .preferredSize();
589:                } finally {
590:                    toolkit.unlockAWT();
591:                }
592:            }
593:
594:            /**
595:             * @deprecated
596:             */
597:            @SuppressWarnings("deprecation")
598:            @Deprecated
599:            public Dimension preferredSize(int rows, int columns) {
600:                toolkit.lockAWT();
601:                try {
602:                    Dimension prefSize = calcSize(rows, columns);
603:                    if (prefSize == null) {
604:                        return super .preferredSize();
605:                    }
606:                    return prefSize;
607:                } finally {
608:                    toolkit.unlockAWT();
609:                }
610:            }
611:
612:            public void replaceRange(String str, int start, int end) {
613:                int length = end - start;
614:                int oldPos = caret.getDot();
615:                try {
616:                    document.replace(start, length, str, null);
617:                } catch (BadLocationException e) {
618:                    // ignore exception silently
619:                }
620:                toolkit.lockAWT();
621:                try {
622:                    if (isDisplayable()) {
623:                        // update caret position on text replacement
624:                        int newCaretPos = start + str.length();
625:                        if (caret.getDot() != newCaretPos) {
626:                            caret.setDot(newCaretPos, caret.getDotBias());
627:                        }
628:                    } else if (caret.getDot() != oldPos) {
629:                        // move caret back:
630:                        caret.setDot(oldPos, caret.getDotBias());
631:                    }
632:                } finally {
633:                    toolkit.unlockAWT();
634:                }
635:            }
636:
637:            /**
638:             * @deprecated
639:             */
640:            @Deprecated
641:            public void replaceText(String str, int start, int end) {
642:                replaceRange(str, start, end);
643:            }
644:
645:            public void setColumns(int columns) {
646:                toolkit.lockAWT();
647:                try {
648:                    if (columns < 0) {
649:                        // awt.69=columns less than zero.
650:                        throw new IllegalArgumentException(Messages
651:                                .getString("awt.69")); //$NON-NLS-1$
652:                    }
653:                    this .columns = columns;
654:                } finally {
655:                    toolkit.unlockAWT();
656:                }
657:            }
658:
659:            public void setRows(int rows) {
660:                toolkit.lockAWT();
661:                try {
662:                    if (rows < 0) {
663:                        // awt.6A=rows less than zero.
664:                        throw new IllegalArgumentException(Messages
665:                                .getString("awt.6A")); //$NON-NLS-1$
666:                    }
667:                    this .rows = rows;
668:                } finally {
669:                    toolkit.unlockAWT();
670:                }
671:            }
672:
673:            @Override
674:            boolean isPrepainter() {
675:                return true;
676:            }
677:
678:            @Override
679:            Dimension getDefaultMinimumSize() {
680:                return calcSize(10, 60);
681:            }
682:
683:            @Override
684:            Dimension getDefaultPreferredSize() {
685:                if (getFont() == null) {
686:                    return null;
687:                }
688:                return getDefaultMinimumSize();
689:            }
690:
691:            /**
692:             * Calculates minimum size required for specified
693:             * number of rows and columns
694:             */
695:            private Dimension calcSize(int rows, int columns) {
696:                FontMetrics fm = getFontMetrics(getFont());
697:                if ((fm == null) || !isDisplayable()) {
698:                    return null;
699:                }
700:                int avWidth = fm.charWidth('_'); // take width of an 'average' character
701:                return new Dimension(avWidth * columns + 4,
702:                        (fm.getHeight() + 1) * rows + 4);
703:            }
704:
705:            /**
706:             * Sets scrolling position to point.
707:             * If no horizontal scrolling is available
708:             * x scroll coordinate of point is changed to 0
709:             * before setting the position.
710:             */
711:            @Override
712:            void setViewPosition(Point point) {
713:                if (noHorizontalScroll()) {
714:                    point.x = 0; // don't allow horizontal scrolling
715:                }
716:                super .setViewPosition(point);
717:            }
718:
719:            /**
720:             * Scrolls(updates scroll position) to make rectangle r visible.
721:             * Also updates necessary scrollbar values.
722:             * @see TextComponent.scrollRectToVisible(Rectangle)
723:             */
724:            @Override
725:            void scrollRectToVisible(Rectangle r) {
726:                super .scrollRectToVisible(r);
727:                // update scrollbar positions:
728:                if ((hAdjustable != null) && !hAdjustable.getBounds().isEmpty()) {
729:                    hAdjustable
730:                            .setValue(getViewPosition().x + getInsets().left);
731:                }
732:                if ((vAdjustable != null) && !vAdjustable.getBounds().isEmpty()) {
733:                    vAdjustable.setValue(getViewPosition().y + getInsets().top);
734:                }
735:            }
736:
737:            @Override
738:            AccessibleContext createAccessibleContext() {
739:                return new AccessibleAWTTextArea();
740:            }
741:
742:            private void doRepaint(Rectangle r) {
743:                if (isDisplayable()) {
744:                    invalidate();
745:                    if (isShowing() && (r != null)) {
746:                        repaint(r.x, r.y, r.width, r.height);
747:                    }
748:                }
749:            }
750:
751:            private void doRepaint() {
752:                stateController.layoutScrollbars();
753:                doRepaint(new Rectangle(new Point(), getSize()));
754:            }
755:
756:            /**
757:             * Adds scrolling capabilities to TextArea:
758:             * initializes all required fields and scrollbar listeners
759:             */
760:            private void addScrolling() {
761:                addAWTComponentListener(stateController);
762:                addAWTMouseWheelListener(stateController);
763:                hAdjustable.addAWTAdjustmentListener(stateController);
764:                vAdjustable.addAWTAdjustmentListener(stateController);
765:            }
766:
767:            @Override
768:            void prepaint(Graphics g) {
769:                super .prepaint(g);
770:                Shape oldClip = g.getClip();
771:                if (vAdjustable != null) {
772:                    Rectangle r = vAdjustable.getBounds();
773:                    g.clipRect(r.x, r.y, r.width, r.height);
774:                    vAdjustable.prepaint(g);
775:                }
776:                if (hAdjustable != null) {
777:                    g.setClip(oldClip);
778:                    Rectangle r = hAdjustable.getBounds();
779:                    g.clipRect(r.x, r.y, r.width, r.height);
780:                    hAdjustable.prepaint(g);
781:                }
782:                g.setClip(oldClip);
783:            }
784:
785:            /**
786:             * Adds scrollbar area to default insets
787:             */
788:            @Override
789:            Insets getInsets() {
790:                Insets ins = super .getInsets();
791:                if (scrollable == null) {
792:                    return ins;
793:                }
794:                int sv = getScrollbarVisibility();
795:                if ((sv == SCROLLBARS_BOTH)
796:                        || (sv == SCROLLBARS_HORIZONTAL_ONLY)) {
797:                    ins.bottom += scrollable.getAdjustableHeight();
798:                }
799:                if ((sv == SCROLLBARS_BOTH) || (sv == SCROLLBARS_VERTICAL_ONLY)) {
800:                    ins.right += scrollable.getAdjustableWidth();
801:                }
802:                return ins;
803:            }
804:
805:            /**
806:             * Re-calculates internal layout of TextArea:
807:             * lays out scrollbars and sets their values
808:             */
809:            @Override
810:            void revalidate() {
811:                stateController.layoutScrollbars();
812:                super .revalidate();
813:            }
814:
815:            /**
816:             * Returns the rectangle required to contain all the text, not only
817:             * visible part(which is component's bounds),
818:             * relative to TextArea origin
819:             */
820:            @Override
821:            Rectangle getModelRect() {
822:                Rectangle mRect = super .getModelRect();
823:                if (noHorizontalScroll()) {
824:                    return mRect;
825:                }
826:                int xSpan = (int) rootViewContext.getView().getPreferredSpan(
827:                        View.X_AXIS);
828:
829:                mRect.width = Math.max(xSpan, mRect.width);
830:
831:                return mRect;
832:            }
833:
834:            /**
835:             * Creates mouse event filter
836:             * @return mouse event filter for caret
837:             */
838:            MouseEventFilter createFilter() {
839:                filter = new MouseEventFilter((MouseListener) caret,
840:                        (MouseMotionListener) caret);
841:                return filter;
842:            }
843:
844:            @Override
845:            MouseMotionListener getMotionHandler() {
846:                if (filter == null) {
847:                    filter = createFilter();
848:                }
849:                return filter;
850:            }
851:
852:            @Override
853:            MouseListener getMouseHandler() {
854:                if (filter == null) {
855:                    filter = createFilter();
856:                }
857:                return filter;
858:            }
859:
860:            @Override
861:            String autoName() {
862:                return ("text" + toolkit.autoNumber.nextTextArea++); //$NON-NLS-1$
863:            }
864:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.