Source Code Cross Referenced for ActionSet.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » awt » text » 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 » org package » org.apache.harmony.awt.text 
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:         * @author Alexander T. Simbirtsev, Evgeniya G. Maenkova
019:         * @version $Revision$
020:         */package org.apache.harmony.awt.text;
021:
022:        import java.awt.Component;
023:        import java.awt.ComponentOrientation;
024:        import java.awt.Point;
025:        import java.awt.Rectangle;
026:        import java.awt.Shape;
027:        import java.awt.Toolkit;
028:        import java.util.HashMap;
029:        import javax.swing.SwingConstants;
030:        import javax.swing.text.AbstractDocument;
031:        import javax.swing.text.BadLocationException;
032:        import javax.swing.text.Document;
033:        import javax.swing.text.Element;
034:        import javax.swing.text.Position;
035:
036:        //TODO: complete bidirectional text support (in almost all actions)
037:        //TODO: lock/unlock document document (in almost all actions)
038:        /**
039:         * Implements a set of text actions that should be used as a base for
040:         * text component implementations.
041:         */
042:        public final class ActionSet implements  ActionNames {
043:
044:            private static class InsertBreakAction extends AWTTextAction {
045:                public InsertBreakAction() {
046:                    isEditAction = true;
047:                }
048:
049:                @Override
050:                public void internalPerformAction(final TextKit tk) {
051:                    tk.replaceSelectedText("\n"); //$NON-NLS-1$
052:                }
053:            }
054:
055:            private static class InsertTabAction extends AWTTextAction {
056:                public InsertTabAction() {
057:                    isEditAction = true;
058:                }
059:
060:                @Override
061:                public void internalPerformAction(final TextKit tk) {
062:                    tk.replaceSelectedText("\t"); //$NON-NLS-1$
063:                }
064:            }
065:
066:            private static class CutAction extends AWTTextAction {
067:                public CutAction() {
068:                    isEditAction = true;
069:                }
070:
071:                @Override
072:                public void internalPerformAction(final TextKit tk) {
073:                    TextUtils.cut(tk);
074:                }
075:            }
076:
077:            private static class CopyAction extends AWTTextAction {
078:                @Override
079:                public void internalPerformAction(final TextKit tk) {
080:                    TextUtils.copy(tk);
081:                }
082:            }
083:
084:            private static class PasteAction extends AWTTextAction {
085:                public PasteAction() {
086:                    isEditAction = true;
087:                }
088:
089:                @Override
090:                public void internalPerformAction(final TextKit tk) {
091:                    TextUtils.paste(tk);
092:                }
093:            }
094:
095:            private static class BeepAction extends AWTTextAction {
096:                @Override
097:                public void internalPerformAction(final TextKit tk) {
098:                    Toolkit.getDefaultToolkit().beep();
099:                }
100:            }
101:
102:            private static class SelectAllAction extends AWTTextAction {
103:                @Override
104:                public void internalPerformAction(final TextKit tk) {
105:                    TextCaret caret = tk.getCaret();
106:                    int length = tk.getDocument().getLength();
107:                    caret.setDot(0, Position.Bias.Backward);
108:                    caret.moveDot(length, Position.Bias.Backward);
109:                }
110:            }
111:
112:            private static class DumpModelAction extends AWTTextAction {
113:                @Override
114:                public void internalPerformAction(final TextKit tk) {
115:                    Document doc = tk.getDocument();
116:                    if (doc instanceof  AbstractDocument) {
117:                        ((AbstractDocument) doc).dump(System.err);
118:                    }
119:                }
120:            }
121:
122:            private static class DeletePrevCharAction extends AWTTextAction {
123:                public DeletePrevCharAction() {
124:                    isEditAction = true;
125:                }
126:
127:                @Override
128:                public void internalPerformAction(final TextKit tk) {
129:                    TextCaret caret = tk.getCaret();
130:                    int curPos = caret.getDot();
131:                    Document doc = tk.getDocument();
132:                    try {
133:                        String selText = tk.getSelectedText();
134:                        if (selText == null || selText.length() == 0) {
135:                            if (curPos > 0) {
136:                                doc.remove(curPos - 1, 1);
137:                            }
138:                        } else {
139:                            int selStart = tk.getSelectionStart();
140:                            int selEnd = tk.getSelectionEnd();
141:                            doc.remove(selStart, selEnd - selStart);
142:                        }
143:                    } catch (final BadLocationException e) {
144:                        // this should not happen ever if doc.remove works fine
145:                        e.printStackTrace();
146:                    }
147:                }
148:            }
149:
150:            private static class PreviousWordAction extends AWTTextAction {
151:                private boolean isMovingCaret;
152:
153:                public PreviousWordAction(final String name) {
154:                    this .isMovingCaret = selectionPreviousWordAction
155:                            .equals(name);
156:                }
157:
158:                @Override
159:                public void internalPerformAction(final TextKit tk) {
160:                    try {
161:                        int oldPos = tk.getCaret().getDot();
162:                        Document doc = tk.getDocument();
163:                        int length = doc.getLength();
164:                        int newPos = (0 == length) ? 0 : TextUtils
165:                                .getPreviousWord(doc,
166:                                        (length != oldPos) ? oldPos
167:                                                : oldPos - 1);
168:                        TextUtils
169:                                .changeCaretPosition(tk, newPos, isMovingCaret);
170:                        TextUtils.setCurrentPositionAsMagic(tk);
171:                    } catch (final BadLocationException e) {
172:                        e.printStackTrace();
173:                    }
174:                }
175:            }
176:
177:            private static class NextVisualPositionAction extends AWTTextAction {
178:                private int direction = 0;
179:
180:                private boolean isMovingCaret;
181:
182:                private static final Position.Bias[] auxBiasArray = new Position.Bias[1];
183:
184:                public NextVisualPositionAction(final String name) {
185:                    if (backwardAction.equals(name)) {
186:                        isMovingCaret = false;
187:                        direction = SwingConstants.WEST;
188:                    } else if (forwardAction.equals(name)) {
189:                        direction = SwingConstants.EAST;
190:                        isMovingCaret = false;
191:                    } else if (upAction.equals(name)) {
192:                        direction = SwingConstants.NORTH;
193:                        isMovingCaret = false;
194:                    } else if (downAction.equals(name)) {
195:                        direction = SwingConstants.SOUTH;
196:                        isMovingCaret = false;
197:                    } else if (selectionForwardAction.equals(name)) {
198:                        direction = SwingConstants.EAST;
199:                        isMovingCaret = true;
200:                    } else if (selectionBackwardAction.equals(name)) {
201:                        direction = SwingConstants.WEST;
202:                        isMovingCaret = true;
203:                    } else if (selectionUpAction.equals(name)) {
204:                        direction = SwingConstants.NORTH;
205:                        isMovingCaret = true;
206:                    } else if (selectionDownAction.equals(name)) {
207:                        direction = SwingConstants.SOUTH;
208:                        isMovingCaret = true;
209:                    }
210:                }
211:
212:                @Override
213:                public void internalPerformAction(final TextKit tk) {
214:                    TextCaret caret = tk.getCaret();
215:                    int oldPos = caret.getDot();
216:                    try {
217:                        Shape shape = tk.getVisibleRect();
218:                        int newPos = tk.getRootView()
219:                                .getNextVisualPositionFrom(oldPos,
220:                                        caret.getDotBias(), shape, direction,
221:                                        auxBiasArray);
222:
223:                        if (newPos >= 0) {
224:                            Point pt = caret.getMagicCaretPosition();
225:                            TextUtils.changeCaretPosition(tk, newPos,
226:                                    isMovingCaret, auxBiasArray[0]);
227:                            caret.setMagicCaretPosition(oldPos, direction, pt);
228:                        }
229:                    } catch (final BadLocationException e) {
230:                        e.printStackTrace();
231:                    }
232:                }
233:            }
234:
235:            private static class DeleteNextCharAction extends AWTTextAction {
236:                public DeleteNextCharAction() {
237:                    isEditAction = true;
238:                }
239:
240:                @Override
241:                public void internalPerformAction(final TextKit tk) {
242:                    TextCaret caret = tk.getCaret();
243:                    int curPos = caret.getDot();
244:                    Document doc = tk.getDocument();
245:                    try {
246:                        String selText = tk.getSelectedText();
247:                        if (selText == null || selText.length() == 0) {
248:                            if (curPos < doc.getLength()) {
249:                                doc.remove(curPos, 1);
250:                            }
251:                        } else {
252:                            int selStart = tk.getSelectionStart();
253:                            int selEnd = tk.getSelectionEnd();
254:                            doc.remove(selStart, selEnd - selStart);
255:                        }
256:                    } catch (final BadLocationException e) {
257:                        // this should not happen ever if doc.remove works fine
258:                        e.printStackTrace();
259:                    }
260:                }
261:            }
262:
263:            private static class EndAction extends AWTTextAction {
264:                private final boolean isMovingCaret;
265:
266:                public EndAction(final String name) {
267:                    isMovingCaret = selectionEndAction.equals(name);
268:                }
269:
270:                @Override
271:                public void internalPerformAction(final TextKit tk) {
272:                    int length = tk.getDocument().getLength();
273:                    TextUtils.changeCaretPosition(tk, length, isMovingCaret);
274:                    TextUtils.setCurrentPositionAsMagic(tk);
275:
276:                }
277:            }
278:
279:            private static class EndLineAction extends AWTTextAction {
280:                private final boolean isMovingCaret;
281:
282:                public EndLineAction(final String name) {
283:                    isMovingCaret = selectionEndLineAction.equals(name);
284:                }
285:
286:                @Override
287:                public void internalPerformAction(final TextKit tk) {
288:                    try {
289:                        TextCaret caret = tk.getCaret();
290:                        int oldPos = caret.getDot();
291:                        int newPos = TextUtils.getRowEnd(tk, oldPos);
292:                        TextUtils
293:                                .changeCaretPosition(tk, newPos, isMovingCaret);
294:                        TextUtils.setCurrentPositionAsMagic(tk);
295:                    } catch (final BadLocationException e) {
296:                        e.printStackTrace();
297:                    }
298:                }
299:            }
300:
301:            private static class EndParagraphAction extends AWTTextAction {
302:                private final boolean isMovingCaret;
303:
304:                public EndParagraphAction(final String name) {
305:                    isMovingCaret = selectionEndParagraphAction.equals(name);
306:                }
307:
308:                @Override
309:                public void internalPerformAction(final TextKit tk) {
310:                    Document doc = tk.getDocument();
311:                    int curPos = tk.getCaret().getDot();
312:                    Element elem = getParagraphElement(doc, curPos);
313:                    int endPos = elem.getEndOffset();
314:                    if (endPos <= doc.getLength()) {
315:                        TextUtils
316:                                .changeCaretPosition(tk, endPos, isMovingCaret);
317:                        TextUtils.setCurrentPositionAsMagic(tk);
318:                    }
319:                }
320:            }
321:
322:            private static class BeginAction extends AWTTextAction {
323:                private final boolean isMovingCaret;
324:
325:                public BeginAction(final String name) {
326:                    isMovingCaret = (selectionBeginAction.equals(name));
327:                }
328:
329:                @Override
330:                public void internalPerformAction(final TextKit tk) {
331:                    TextUtils.changeCaretPosition(tk, 0, isMovingCaret);
332:                    TextUtils.setCurrentPositionAsMagic(tk);
333:
334:                }
335:            }
336:
337:            private static class BeginLineAction extends AWTTextAction {
338:                private final boolean isMovingCaret;
339:
340:                public BeginLineAction(final String name) {
341:                    isMovingCaret = selectionBeginLineAction.equals(name);
342:                }
343:
344:                @Override
345:                public void internalPerformAction(final TextKit tk) {
346:                    try {
347:                        TextCaret caret = tk.getCaret();
348:                        int oldPos = caret.getDot();
349:                        int newPos = TextUtils.getRowStart(tk, oldPos);
350:                        TextUtils
351:                                .changeCaretPosition(tk, newPos, isMovingCaret);
352:                        TextUtils.setCurrentPositionAsMagic(tk);
353:                    } catch (final BadLocationException e) {
354:                        e.printStackTrace();
355:                    }
356:                }
357:            }
358:
359:            private static class BeginParagraphAction extends AWTTextAction {
360:                private final boolean isMovingCaret;
361:
362:                public BeginParagraphAction(final String name) {
363:                    isMovingCaret = selectionBeginParagraphAction.equals(name);
364:                }
365:
366:                @Override
367:                public void internalPerformAction(final TextKit tk) {
368:                    Document doc = tk.getDocument();
369:                    int curPos = tk.getCaret().getDot();
370:                    Element elem = getParagraphElement(doc, curPos);
371:                    int begPos = elem.getStartOffset();
372:                    TextUtils.changeCaretPosition(tk, begPos, isMovingCaret);
373:                    TextUtils.setCurrentPositionAsMagic(tk);
374:                }
375:            }
376:
377:            private static class BeginWordAction extends AWTTextAction {
378:                private final boolean isMovingCaret;
379:
380:                public BeginWordAction(final String name) {
381:                    isMovingCaret = selectionBeginWordAction.equals(name);
382:                }
383:
384:                @Override
385:                public void internalPerformAction(final TextKit tk) {
386:                    try {
387:                        int oldPos = tk.getCaret().getDot();
388:                        int newPos = TextUtils.getWordStart(tk, oldPos);
389:                        TextUtils
390:                                .changeCaretPosition(tk, newPos, isMovingCaret);
391:                    } catch (final BadLocationException e) {
392:                        e.printStackTrace();
393:                    }
394:                }
395:            }
396:
397:            private static class EndWordAction extends AWTTextAction {
398:                private final boolean isMovingCaret;
399:
400:                public EndWordAction(final String name) {
401:                    isMovingCaret = selectionEndWordAction.equals(name);
402:                }
403:
404:                @Override
405:                public void internalPerformAction(final TextKit tk) {
406:                    try {
407:                        int oldPos = tk.getCaret().getDot();
408:                        int length = tk.getDocument().getLength();
409:
410:                        if (length != 0 && oldPos != length) {
411:                            int newPos = TextUtils.getWordEnd(tk, oldPos);
412:                            TextUtils.changeCaretPosition(tk, newPos,
413:                                    isMovingCaret);
414:                        }
415:                    } catch (final BadLocationException e) {
416:                        e.printStackTrace();
417:                    }
418:                }
419:            }
420:
421:            private static class NextWordAction extends AWTTextAction {
422:                private final boolean isMovingCaret;
423:
424:                public NextWordAction(final String name) {
425:                    isMovingCaret = selectionNextWordAction.equals(name);
426:                }
427:
428:                @Override
429:                public void internalPerformAction(final TextKit tk) {
430:                    try {
431:                        int oldPos = tk.getCaret().getDot();
432:                        int length = tk.getDocument().getLength();
433:
434:                        if (length != 0 && oldPos != length) {
435:                            int newPos = TextUtils.getNextWord(
436:                                    tk.getDocument(), oldPos);
437:                            if (newPos == 0 && oldPos != 0) {
438:                                newPos = TextUtils.getWordEnd(tk, oldPos);
439:                            }
440:                            TextUtils.changeCaretPosition(tk, newPos,
441:                                    isMovingCaret);
442:                            TextUtils.setCurrentPositionAsMagic(tk);
443:                        }
444:                    } catch (final BadLocationException e) {
445:                        e.printStackTrace();
446:                    }
447:                }
448:            }
449:
450:            private static class VerticalPageAction extends AWTTextAction {
451:                private int direction = 0;
452:                private boolean isMovingCaret;
453:
454:                public VerticalPageAction(final String name) {
455:                    if (pageUpAction.equals(name)) {
456:                        direction = SwingConstants.NORTH;
457:                        isMovingCaret = false;
458:                    } else if (pageDownAction.equals(name)) {
459:                        direction = SwingConstants.SOUTH;
460:                        isMovingCaret = false;
461:                    } else if (selectionPageUpAction.equals(name)) {
462:                        direction = SwingConstants.NORTH;
463:                        isMovingCaret = true;
464:                    } else if (selectionPageDownAction.equals(name)) {
465:                        direction = SwingConstants.SOUTH;
466:                        isMovingCaret = true;
467:                    }
468:                }
469:
470:                private boolean isUnableToMoveDown(final TextKit tk,
471:                        final int pos) {
472:                    return ((pos == tk.getDocument().getLength() && direction == SwingConstants.SOUTH));
473:                }
474:
475:                private boolean isUnableToMoveUp(final TextKit tk, final int pos)
476:                        throws BadLocationException {
477:
478:                    return (TextUtils.getRowStart(tk, pos) == 0 && direction == SwingConstants.NORTH);
479:                }
480:
481:                @Override
482:                public void internalPerformAction(final TextKit tk) {
483:                    try {
484:                        TextCaret caret = tk.getCaret();
485:                        int oldPos = caret.getDot();
486:                        if (!isUnableToMoveDown(tk, oldPos)
487:                                && !isUnableToMoveUp(tk, oldPos)) {
488:                            Point magicPt = caret.getMagicCaretPosition();
489:
490:                            caret.setMagicCaretPosition(oldPos, direction,
491:                                    magicPt);
492:                            magicPt = caret.getMagicCaretPosition();
493:
494:                            int height = tk.getVisibleRect().height;
495:
496:                            Point oldPoint = tk.modelToView(oldPos).getBounds()
497:                                    .getLocation();
498:                            Point newPoint = new Point(magicPt.x, oldPoint.y);
499:                            newPoint.y += (direction == SwingConstants.NORTH) ? -height
500:                                    : height;
501:                            if (newPoint.y < 0) {
502:                                newPoint.y = 0;
503:                            }
504:                            int newPos = tk.viewToModel(newPoint,
505:                                    new Position.Bias[1]);
506:                            Rectangle r = tk.getVisibleRect();
507:                            r.y += (direction == SwingConstants.NORTH) ? -height
508:                                    : height;
509:
510:                            Rectangle wholeRect = tk.getComponent().getBounds();
511:                            if (r.y < 0) {
512:                                r.y = 0;
513:                            } else if (r.y + r.height > wholeRect.height) {
514:                                r.y -= r.y + r.height - wholeRect.height;
515:                            }
516:                            tk.scrollRectToVisible(r);
517:                            TextUtils.changeCaretPosition(tk, newPos,
518:                                    isMovingCaret);
519:                        }
520:                    } catch (final BadLocationException e) {
521:                        e.printStackTrace();
522:                    }
523:                }
524:            }
525:
526:            private static class SelectLineAction extends AWTTextAction {
527:                @Override
528:                public void internalPerformAction(final TextKit tk) {
529:                    TextCaret caret = tk.getCaret();
530:                    int curPos = caret.getDot();
531:                    try {
532:                        int start = TextUtils.getRowStart(tk, curPos);
533:                        int end = TextUtils.getRowEnd(tk, curPos);
534:                        caret.setDot(start, Position.Bias.Forward);
535:                        caret.moveDot(end, Position.Bias.Backward);
536:                    } catch (final BadLocationException e) {
537:                        e.printStackTrace();
538:                    }
539:                }
540:            }
541:
542:            private static class SelectParagraphAction extends AWTTextAction {
543:                @Override
544:                public void internalPerformAction(final TextKit tk) {
545:                    Document doc = tk.getDocument();
546:                    TextCaret caret = tk.getCaret();
547:                    int curPos = caret.getDot();
548:                    Element elem = getParagraphElement(doc, curPos);
549:                    int start = elem.getStartOffset();
550:                    int end = elem.getEndOffset();
551:                    final int length = doc.getLength();
552:                    if (end >= length) {
553:                        end = length;
554:                    }
555:                    caret.setDot(start, Position.Bias.Forward);
556:                    caret.moveDot(end, Position.Bias.Backward);
557:                }
558:            }
559:
560:            private static class SelectWordAction extends AWTTextAction {
561:                @Override
562:                public void internalPerformAction(final TextKit tk) {
563:                    TextCaret caret = tk.getCaret();
564:                    int curPos = caret.getDot();
565:                    try {
566:                        int length = tk.getDocument().getLength();
567:                        if (length != 0) {
568:                            int start = TextUtils.getWordStart(tk, curPos);
569:                            int end = TextUtils.getWordEnd(tk, curPos);
570:                            caret.setDot(start, Position.Bias.Forward);
571:                            caret.moveDot(end, Position.Bias.Backward);
572:                        }
573:                    } catch (final BadLocationException e) {
574:                        e.printStackTrace();
575:                    }
576:                }
577:            }
578:
579:            private static class UnselectAction extends AWTTextAction {
580:                @Override
581:                public void internalPerformAction(final TextKit tk) {
582:                    TextCaret caret = tk.getCaret();
583:                    caret.setDot(caret.getDot(), caret.getDotBias());
584:                }
585:            }
586:
587:            private static class ToggleComponentOrientationAction extends
588:                    AWTTextAction {
589:                @Override
590:                public void internalPerformAction(final TextKit tk) {
591:                    Component component = tk.getComponent();
592:                    if (component.getComponentOrientation().isLeftToRight()) {
593:                        component
594:                                .setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
595:                    } else {
596:                        component
597:                                .setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
598:                    }
599:                }
600:            }
601:
602:            private static class PageAction extends AWTTextAction {
603:                protected int direction = 0;
604:
605:                private static final Position.Bias[] biasRet = new Position.Bias[1];
606:
607:                public PageAction(final String name) {
608:                    if (selectionPageLeftAction.equals(name)) {
609:                        direction = SwingConstants.NORTH;
610:                    } else if (selectionPageRightAction.equals(name)) {
611:                        direction = SwingConstants.SOUTH;
612:                    }
613:                }
614:
615:                @Override
616:                public void internalPerformAction(final TextKit tk) {
617:                    Rectangle rect = tk.getVisibleRect();
618:                    Point pt;
619:                    int newPos;
620:                    try {
621:                        if (direction == SwingConstants.NORTH) {
622:                            pt = new Point(rect.x + 1, rect.y + 1);
623:                            newPos = tk.viewToModel(pt, biasRet);
624:                            newPos = TextUtils.getRowStart(tk, newPos);
625:                        } else {
626:                            pt = new Point(rect.x + 1, rect.y + rect.height - 1);
627:                            newPos = tk.viewToModel(pt, biasRet);
628:                            newPos = TextUtils.getRowEnd(tk, newPos);
629:                        }
630:                        TextUtils.changeCaretPosition(tk, newPos, true);
631:                    } catch (BadLocationException e) {
632:                        e.printStackTrace();
633:                    }
634:                }
635:            }
636:
637:            private static Element getParagraphElement(final Document doc,
638:                    final int offset) {
639:                Element root = doc.getDefaultRootElement();
640:                return root.getElement(root.getElementIndex(offset));
641:            }
642:
643:            public static HashMap<String, AWTTextAction> actionMap;
644:            static {
645:                actionMap = new HashMap<String, AWTTextAction>();
646:                actionMap.put(deletePrevCharAction, new DeletePrevCharAction());
647:                actionMap.put(insertBreakAction, new InsertBreakAction());
648:                actionMap.put(previousWordAction, new PreviousWordAction(
649:                        previousWordAction));
650:                actionMap.put(selectionPreviousWordAction,
651:                        new PreviousWordAction(selectionPreviousWordAction));
652:                actionMap.put(nextWordAction,
653:                        new NextWordAction(nextWordAction));
654:                actionMap.put(selectionNextWordAction, new NextWordAction(
655:                        selectionNextWordAction));
656:                actionMap.put(insertTabAction, new InsertTabAction());
657:                actionMap.put(dumpModelAction, new DumpModelAction());
658:                actionMap.put(beepAction, new BeepAction());
659:                actionMap.put(copyAction, new CopyAction());
660:                actionMap.put(cutAction, new CutAction());
661:                actionMap.put(pasteAction, new PasteAction());
662:                actionMap.put(selectAllAction, new SelectAllAction());
663:                actionMap.put(backwardAction, new NextVisualPositionAction(
664:                        backwardAction));
665:                actionMap.put(forwardAction, new NextVisualPositionAction(
666:                        forwardAction));
667:                actionMap.put(upAction, new NextVisualPositionAction(upAction));
668:                actionMap.put(downAction, new NextVisualPositionAction(
669:                        downAction));
670:                actionMap.put(selectionBackwardAction,
671:                        new NextVisualPositionAction(selectionBackwardAction));
672:                actionMap.put(selectionForwardAction,
673:                        new NextVisualPositionAction(selectionForwardAction));
674:                actionMap.put(selectionUpAction, new NextVisualPositionAction(
675:                        selectionUpAction));
676:                actionMap.put(selectionDownAction,
677:                        new NextVisualPositionAction(selectionDownAction));
678:                actionMap.put(deleteNextCharAction, new DeleteNextCharAction());
679:                actionMap.put(endAction, new EndAction(endAction));
680:                actionMap.put(selectionEndAction, new EndAction(
681:                        selectionEndAction));
682:                actionMap.put(endLineAction, new EndLineAction(endLineAction));
683:                actionMap.put(selectionEndLineAction, new EndLineAction(
684:                        selectionEndLineAction));
685:                actionMap.put(endParagraphAction, new EndParagraphAction(
686:                        endParagraphAction));
687:                actionMap.put(selectionEndParagraphAction,
688:                        new EndParagraphAction(selectionEndParagraphAction));
689:                actionMap.put(endWordAction, new EndWordAction(endWordAction));
690:                actionMap.put(selectionEndWordAction, new EndWordAction(
691:                        selectionEndWordAction));
692:                actionMap.put(beginAction, new BeginAction(beginAction));
693:                actionMap.put(selectionBeginAction, new BeginAction(
694:                        selectionBeginAction));
695:                actionMap.put(beginLineAction, new BeginLineAction(
696:                        beginLineAction));
697:                actionMap.put(selectionBeginLineAction, new BeginLineAction(
698:                        selectionBeginLineAction));
699:                actionMap.put(beginParagraphAction, new BeginParagraphAction(
700:                        beginParagraphAction));
701:                actionMap
702:                        .put(selectionBeginParagraphAction,
703:                                new BeginParagraphAction(
704:                                        selectionBeginParagraphAction));
705:                actionMap.put(beginWordAction, new BeginWordAction(
706:                        beginWordAction));
707:                actionMap.put(selectionBeginWordAction, new BeginWordAction(
708:                        selectionBeginWordAction));
709:                actionMap.put(selectWordAction, new SelectWordAction());
710:                actionMap.put(selectLineAction, new SelectLineAction());
711:                actionMap.put(selectParagraphAction,
712:                        new SelectParagraphAction());
713:                actionMap.put(unselectAction, new UnselectAction());
714:                actionMap.put(pageUpAction,
715:                        new VerticalPageAction(pageUpAction));
716:                actionMap.put(pageDownAction, new VerticalPageAction(
717:                        pageDownAction));
718:                actionMap.put(selectionPageUpAction, new VerticalPageAction(
719:                        selectionPageUpAction));
720:                actionMap.put(selectionPageDownAction, new VerticalPageAction(
721:                        selectionPageDownAction));
722:                actionMap.put(toggleComponentOrientationAction,
723:                        new ToggleComponentOrientationAction());
724:                actionMap.put(selectionPageLeftAction, new PageAction(
725:                        selectionPageLeftAction));
726:                actionMap.put(selectionPageRightAction, new PageAction(
727:                        selectionPageRightAction));
728:                actionMap.put(beepAction, new BeepAction());
729:            }
730:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.