Source Code Cross Referenced for FindSupport.java in  » Swing-Library » abeille-forms-designer » org » netbeans » editor » 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 » Swing Library » abeille forms designer » org.netbeans.editor 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *                 Sun Public License Notice
003:         * 
004:         * The contents of this file are subject to the Sun Public License
005:         * Version 1.0 (the "License"). You may not use this file except in
006:         * compliance with the License. A copy of the License is available at
007:         * http://www.sun.com/
008:         * 
009:         * The Original Code is NetBeans. The Initial Developer of the Original
010:         * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
011:         * Microsystems, Inc. All Rights Reserved.
012:         */
013:
014:        package org.netbeans.editor;
015:
016:        import java.awt.Rectangle;
017:        import java.beans.PropertyChangeListener;
018:        import java.text.MessageFormat;
019:        import java.util.HashMap;
020:        import java.util.Map;
021:
022:        import javax.swing.text.BadLocationException;
023:        import javax.swing.text.Caret;
024:        import javax.swing.text.JTextComponent;
025:
026:        /**
027:         * Find management
028:         * 
029:         * @author Miloslav Metelka
030:         * @version 1.00
031:         */
032:
033:        public class FindSupport {
034:
035:            private static final String FOUND_LOCALE = "find-found"; // NOI18N
036:            private static final String NOT_FOUND_LOCALE = "find-not-found"; // NOI18N
037:            private static final String WRAP_START_LOCALE = "find-wrap-start"; // NOI18N
038:            private static final String WRAP_END_LOCALE = "find-wrap-end"; // NOI18N
039:            private static final String ITEMS_REPLACED_LOCALE = "find-items-replaced"; // NOI18N
040:
041:            /** Shared instance of FindSupport class */
042:            static FindSupport findSupport;
043:
044:            /** Find properties */
045:            private Map findProps;
046:
047:            /** Support for firing change events */
048:            WeakPropertyChangeSupport changeSupport = new WeakPropertyChangeSupport();
049:
050:            /**
051:             * Current finder creator. It can be changed by setFinderCreator.
052:             */
053:            FinderCreator finderCreator;
054:
055:            /**
056:             * Were the find properties already initialized from settings?
057:             */
058:            private boolean findPropsInited;
059:
060:            private FindSupport() {
061:                // prevent instance creation
062:            }
063:
064:            /** Get shared instance of find support */
065:            public static FindSupport getFindSupport() {
066:                if (findSupport == null) {
067:                    findSupport = new FindSupport();
068:                }
069:                return findSupport;
070:            }
071:
072:            /** Get current finder creator */
073:            public FinderCreator getFinderCreator() {
074:                if (finderCreator == null) {
075:                    finderCreator = new DefaultFinderCreator();
076:                }
077:                return finderCreator;
078:            }
079:
080:            /** Set customized finder creator */
081:            public void setFinderCreator(FinderCreator finderCreator) {
082:                this .finderCreator = finderCreator;
083:                firePropertyChange(null, null, null);
084:            }
085:
086:            public Map getDefaultFindProperties() {
087:                HashMap props = new HashMap();
088:                Class kitClass = BaseKit.class;
089:                props.put(SettingsNames.FIND_WHAT, Settings.getValue(kitClass,
090:                        SettingsNames.FIND_WHAT));
091:                props.put(SettingsNames.FIND_REPLACE_WITH, Settings.getValue(
092:                        kitClass, SettingsNames.FIND_REPLACE_WITH));
093:                props.put(SettingsNames.FIND_HIGHLIGHT_SEARCH,
094:                        Settings.getValue(kitClass,
095:                                SettingsNames.FIND_HIGHLIGHT_SEARCH));
096:                props.put(SettingsNames.FIND_INC_SEARCH, Settings.getValue(
097:                        kitClass, SettingsNames.FIND_INC_SEARCH));
098:                props
099:                        .put(SettingsNames.FIND_BACKWARD_SEARCH, Settings
100:                                .getValue(kitClass,
101:                                        SettingsNames.FIND_BACKWARD_SEARCH));
102:                props.put(SettingsNames.FIND_WRAP_SEARCH, Settings.getValue(
103:                        kitClass, SettingsNames.FIND_WRAP_SEARCH));
104:                props.put(SettingsNames.FIND_MATCH_CASE, Settings.getValue(
105:                        kitClass, SettingsNames.FIND_MATCH_CASE));
106:                props.put(SettingsNames.FIND_WHOLE_WORDS, Settings.getValue(
107:                        kitClass, SettingsNames.FIND_WHOLE_WORDS));
108:                props.put(SettingsNames.FIND_REG_EXP, Settings.getValue(
109:                        kitClass, SettingsNames.FIND_REG_EXP));
110:                props.put(SettingsNames.FIND_HISTORY, Settings.getValue(
111:                        kitClass, SettingsNames.FIND_HISTORY));
112:
113:                return props;
114:            }
115:
116:            public Map getFindProperties() {
117:                if (findProps == null) {
118:                    findProps = getDefaultFindProperties();
119:                }
120:                return findProps;
121:            }
122:
123:            /** Get find property with specified name */
124:            public Object getFindProperty(String name) {
125:                return getFindProperties().get(name);
126:            }
127:
128:            private Map getValidFindProperties(Map props) {
129:                return (props != null) ? props : getFindProperties();
130:            }
131:
132:            /** Get finder depending on find properties */
133:            public FinderFactory.StringFinder getStringFinder(BaseDocument doc,
134:                    Map props) {
135:                FinderFactory.StringFinder stringFinder = (FinderFactory.StringFinder) doc
136:                        .getProperty(BaseDocument.STRING_FINDER_PROP);
137:
138:                if (stringFinder == null) {
139:                    stringFinder = getFinderCreator().createFinder(doc,
140:                            getValidFindProperties(props));
141:                    doc.putProperty(BaseDocument.STRING_FINDER_PROP,
142:                            stringFinder);
143:                }
144:
145:                return stringFinder;
146:            }
147:
148:            /** Get opposite direction finder depending on find properties */
149:            public FinderFactory.StringFinder getStringBwdFinder(
150:                    BaseDocument doc, Map props) {
151:                FinderFactory.StringFinder stringBwdFinder = (FinderFactory.StringFinder) doc
152:                        .getProperty(BaseDocument.STRING_BWD_FINDER_PROP);
153:
154:                if (stringBwdFinder == null) {
155:                    stringBwdFinder = getFinderCreator().createBwdFinder(doc,
156:                            getValidFindProperties(props));
157:                    doc.putProperty(BaseDocument.STRING_BWD_FINDER_PROP,
158:                            stringBwdFinder);
159:                }
160:
161:                return stringBwdFinder;
162:            }
163:
164:            /** Get position pairs finder depending on find properties */
165:            public FinderFactory.BlocksFinder getBlocksFinder(BaseDocument doc,
166:                    Map props) {
167:                FinderFactory.BlocksFinder blocksFinder = (FinderFactory.BlocksFinder) doc
168:                        .getProperty(BaseDocument.BLOCKS_FINDER_PROP);
169:
170:                if (blocksFinder == null) {
171:                    blocksFinder = getFinderCreator().createBlocksFinder(doc,
172:                            getValidFindProperties(props));
173:                }
174:                return blocksFinder;
175:            }
176:
177:            int[] getBlocks(int[] blocks, BaseDocument doc, int startPos,
178:                    int endPos) throws BadLocationException {
179:                FinderFactory.BlocksFinder ppf = getBlocksFinder(doc, null);
180:                if (ppf == null) { // not yet assigned
181:                    return blocks;
182:                }
183:                synchronized (ppf) {
184:                    ppf.setBlocks(blocks);
185:                    doc.find(ppf, startPos, endPos);
186:                    return ppf.getBlocks();
187:                }
188:            }
189:
190:            /**
191:             * Get find property without performing initialization of find properties.
192:             * This is useful for example for base document when it wants to query
193:             * whether it should do highlight search.
194:             */
195:            Object getPropertyNoInit(String name) {
196:                if (findProps == null) {
197:                    return null;
198:                } else {
199:                    return getFindProperty(name);
200:                }
201:            }
202:
203:            /**
204:             * Set find property with specified name and fire change.
205:             */
206:            public void putFindProperty(String name, Object newValue) {
207:                Object oldValue = getFindProperty(name);
208:                if ((oldValue == null && newValue == null)
209:                        || (oldValue != null && oldValue.equals(newValue))) {
210:                    return;
211:                }
212:                if (newValue != null) {
213:                    getFindProperties().put(name, newValue);
214:                } else {
215:                    getFindProperties().remove(name);
216:                }
217:                firePropertyChange(name, oldValue, newValue);
218:            }
219:
220:            /**
221:             * Add/replace properties from some other map to current find properties. If
222:             * the added properties are different than the original ones, the property
223:             * change is fired.
224:             */
225:            public void putFindProperties(Map propsToAdd) {
226:                if (!getFindProperties().equals(propsToAdd)) {
227:                    getFindProperties().putAll(propsToAdd);
228:                    firePropertyChange(null, null, null);
229:                }
230:            }
231:
232:            public boolean incSearch(Map props) {
233:                props = getValidFindProperties(props);
234:                Boolean b = (Boolean) props.get(SettingsNames.FIND_INC_SEARCH);
235:                if (b != null && b.booleanValue()) { // inc search enabled
236:                    JTextComponent c = Utilities.getLastActiveComponent();
237:                    if (c != null) {
238:                        Caret caret = c.getCaret();
239:                        BaseDocument doc = (BaseDocument) c.getDocument();
240:                        int dot = caret.getDot();
241:                        Finder finder = getFinderCreator().createFinder(doc,
242:                                props);
243:                        int pos;
244:                        try {
245:                            b = (Boolean) props
246:                                    .get(SettingsNames.FIND_BACKWARD_SEARCH);
247:                            boolean back = (b != null && b.booleanValue());
248:                            if (back) {
249:                                pos = doc.find(finder, dot, 0); // !!! handle wrap
250:                                // search
251:                            } else {
252:                                pos = doc.find(finder, dot, -1); // !!! handle wrap
253:                                // search
254:                            }
255:                        } catch (BadLocationException e) {
256:                            if (Boolean.getBoolean("netbeans.debug.exceptions")) { // NOI18N
257:                                e.printStackTrace();
258:                            }
259:                            return false;
260:                        }
261:
262:                        // possibly create incSearch layer
263:                        BaseTextUI ui = (BaseTextUI) c.getUI();
264:                        EditorUI editorUI = ui.getEditorUI();
265:                        DrawLayerFactory.IncSearchLayer incLayer = (DrawLayerFactory.IncSearchLayer) editorUI
266:                                .findLayer(DrawLayerFactory.INC_SEARCH_LAYER_NAME);
267:                        if (incLayer == null) {
268:                            incLayer = new DrawLayerFactory.IncSearchLayer();
269:                            if (!editorUI
270:                                    .addLayer(
271:                                            incLayer,
272:                                            DrawLayerFactory.INC_SEARCH_LAYER_VISIBILITY)) {
273:                                return false; // couldn't add layer
274:                            }
275:                        } else {
276:                            if (incLayer.isEnabled()) {
277:                                incLayer.setEnabled(false);
278:                                try {
279:                                    editorUI
280:                                            .repaintOffset(incLayer.getOffset());
281:                                } catch (BadLocationException e) {
282:                                    if (Boolean
283:                                            .getBoolean("netbeans.debug.exceptions")) { // NOI18N
284:                                        e.printStackTrace();
285:                                    }
286:                                }
287:                            }
288:                        }
289:
290:                        if (pos >= 0) {
291:                            String s = (String) props
292:                                    .get(SettingsNames.FIND_WHAT);
293:                            int len = (s != null) ? s.length() : 0;
294:                            if (len > 0) {
295:                                incLayer.setEnabled(true);
296:                                incLayer.setArea(pos, len);
297:                                try {
298:                                    Rectangle r0 = (Rectangle) ui.modelToView(
299:                                            c, pos, null);
300:                                    Rectangle r = (Rectangle) ui.modelToView(c,
301:                                            pos + len, null);
302:                                    r.add(r0);
303:                                    editorUI.repaintOffset(pos);
304:                                    editorUI.scrollRectToVisible(r,
305:                                            EditorUI.SCROLL_DEFAULT);
306:                                } catch (BadLocationException e) {
307:                                    if (Boolean
308:                                            .getBoolean("netbeans.debug.exceptions")) { // NOI18N
309:                                        e.printStackTrace();
310:                                        return false;
311:                                    }
312:                                }
313:                                return true;
314:                            }
315:                        } else { // string not found
316:                            // !!! ((BaseCaret)c.getCaret()).dispatchUpdate();
317:                        }
318:                    }
319:                } else { // inc search not enabled
320:                    incSearchReset();
321:                }
322:                return false;
323:            }
324:
325:            public void incSearchReset() {
326:                JTextComponent c = Utilities.getLastActiveComponent();
327:                EditorUI editorUI = ((BaseTextUI) c.getUI()).getEditorUI();
328:                DrawLayerFactory.IncSearchLayer incLayer = (DrawLayerFactory.IncSearchLayer) editorUI
329:                        .findLayer(DrawLayerFactory.INC_SEARCH_LAYER_NAME);
330:                if (incLayer != null) {
331:                    if (incLayer.isEnabled()) {
332:                        incLayer.setEnabled(false);
333:                        try {
334:                            editorUI.repaintOffset(incLayer.getOffset());
335:                        } catch (BadLocationException e) {
336:                            if (Boolean.getBoolean("netbeans.debug.exceptions")) { // NOI18N
337:                                e.printStackTrace();
338:                            }
339:                        }
340:                    }
341:                }
342:            }
343:
344:            private boolean isBackSearch(Map props, boolean oppositeDir) {
345:                Boolean b = (Boolean) props
346:                        .get(SettingsNames.FIND_BACKWARD_SEARCH);
347:                boolean back = (b != null && b.booleanValue());
348:                if (oppositeDir) {
349:                    back = !back;
350:                }
351:                return back;
352:            }
353:
354:            /**
355:             * Find the text from the caret position.
356:             * 
357:             * @param props
358:             *            search properties
359:             * @param oppositeDir
360:             *            whether search in opposite direction
361:             */
362:            public boolean find(Map props, boolean oppositeDir) {
363:                incSearchReset();
364:                props = getValidFindProperties(props);
365:                boolean back = isBackSearch(props, oppositeDir);
366:                JTextComponent c = Utilities.getLastActiveComponent();
367:                Object findWhat = props.get(SettingsNames.FIND_WHAT);
368:                if (findWhat == null) { // nothing to search for
369:                    return true;
370:                }
371:
372:                String exp = "'" + findWhat + "' "; // NOI18N
373:                if (c != null) {
374:                    Utilities.clearStatusText(c);
375:                    Caret caret = c.getCaret();
376:                    int dotPos = caret.getDot();
377:                    try {
378:                        int[] blk = findInBlock(c, dotPos, 0, -1, props,
379:                                oppositeDir);
380:                        if (blk != null) {
381:                            if (caret instanceof  BaseCaret) { // support extended
382:                                // scroll mode
383:                                BaseCaret bCaret = (BaseCaret) caret;
384:                                if (back) { // back direction
385:                                    bCaret.setDot(blk[1], bCaret,
386:                                            EditorUI.SCROLL_FIND);
387:                                    bCaret.moveDot(blk[0], bCaret,
388:                                            EditorUI.SCROLL_FIND);
389:
390:                                } else { // forward direction
391:                                    bCaret.setDot(blk[0], bCaret,
392:                                            EditorUI.SCROLL_FIND);
393:                                    bCaret.moveDot(blk[1], bCaret,
394:                                            EditorUI.SCROLL_FIND);
395:                                }
396:
397:                            } else { // regular caret
398:                                if (back) {
399:                                    caret.setDot(blk[1]);
400:                                    caret.moveDot(blk[0]);
401:                                } else { // forward direction
402:                                    caret.setDot(blk[0]);
403:                                    caret.moveDot(blk[1]);
404:                                }
405:                            }
406:
407:                            JumpList.checkAddEntry();
408:                            String msg = exp
409:                                    + LocaleSupport.getString(FOUND_LOCALE)
410:                                    + ' '
411:                                    + Utilities.debugPosition((BaseDocument) c
412:                                            .getDocument(), blk[0]);
413:                            if (blk[2] == 1) { // wrap was done
414:                                msg += "; "; // NOI18N
415:                                msg += back ? LocaleSupport
416:                                        .getString(WRAP_END_LOCALE)
417:                                        : LocaleSupport
418:                                                .getString(WRAP_START_LOCALE);
419:
420:                                Utilities.setStatusBoldText(c, msg);
421:                            } else {
422:                                Utilities.setStatusText(c, msg);
423:                            }
424:                            return true;
425:                        } else { // not found
426:                            Utilities
427:                                    .setStatusBoldText(
428:                                            c,
429:                                            exp
430:                                                    + LocaleSupport
431:                                                            .getString(NOT_FOUND_LOCALE));
432:                            // issue 14189 - selection was not removed
433:                            c.getCaret().setDot(c.getCaret().getDot());
434:                        }
435:                    } catch (BadLocationException e) {
436:                        if (Boolean.getBoolean("netbeans.debug.exceptions")) { // NOI18N
437:                            e.printStackTrace();
438:                        }
439:                    }
440:                }
441:                return false;
442:            }
443:
444:            /**
445:             * Find the searched expression
446:             * 
447:             * @param startPos
448:             *            position from which to search. It must be inside the block.
449:             * @param blockStartPos
450:             *            starting position of the block. It must be valid position
451:             *            greater or equal than zero. It must be lower than or equal to
452:             *            blockEndPos (except blockEndPos=-1).
453:             * @param blockEndPos
454:             *            ending position of the block. It can be -1 for the end of
455:             *            document. It must be greater or equal than blockStartPos
456:             *            (except blockEndPos=-1).
457:             * @param props
458:             *            search properties
459:             * @param oppositeDir
460:             *            whether search in opposite direction
461:             * @param displayWrap
462:             *            whether display messages about the wrapping
463:             * @return either null when nothing was found or integer array with three
464:             *         members ret[0] - starting position of the found string ret[1] -
465:             *         ending position of the found string ret[2] - 1 or 0 when wrap was
466:             *         or wasn't performed in order to find the string
467:             */
468:            public int[] findInBlock(JTextComponent c, int startPos,
469:                    int blockStartPos, int blockEndPos, Map props,
470:                    boolean oppositeDir) throws BadLocationException {
471:                if (c != null) {
472:                    props = getValidFindProperties(props);
473:                    BaseDocument doc = (BaseDocument) c.getDocument();
474:                    FinderFactory.StringFinder sf = oppositeDir ? getStringBwdFinder(
475:                            doc, props)
476:                            : getStringFinder(doc, props);
477:                    int pos = -1;
478:                    boolean wrapDone = false;
479:
480:                    boolean back = isBackSearch(props, oppositeDir);
481:                    Boolean b = (Boolean) props
482:                            .get(SettingsNames.FIND_WRAP_SEARCH);
483:                    boolean wrap = (b != null && b.booleanValue());
484:                    int docLen = doc.getLength();
485:                    if (blockEndPos == -1) {
486:                        blockEndPos = docLen;
487:                    }
488:
489:                    while (true) {
490:                        pos = doc.find(sf, startPos, back ? blockStartPos
491:                                : blockEndPos);
492:                        if (pos != -1) {
493:                            break;
494:                        }
495:
496:                        if (wrap) {
497:                            if (back) {
498:                                blockStartPos = startPos;
499:                                startPos = blockEndPos;
500:                            } else {
501:                                blockEndPos = startPos;
502:                                startPos = blockStartPos;
503:                            }
504:                            wrapDone = true;
505:                            wrap = false; // only one loop
506:                        } else { // no wrap set
507:                            break;
508:                        }
509:
510:                    }
511:
512:                    if (pos != -1) {
513:                        int[] ret = new int[3];
514:                        ret[0] = pos;
515:                        ret[1] = pos + sf.getFoundLength();
516:                        ret[2] = wrapDone ? 1 : 0;
517:                        return ret;
518:                    }
519:                }
520:                return null;
521:            }
522:
523:            public boolean replace(Map props, boolean oppositeDir)
524:                    throws BadLocationException {
525:                incSearchReset();
526:                props = getValidFindProperties(props);
527:                Boolean b = (Boolean) props
528:                        .get(SettingsNames.FIND_BACKWARD_SEARCH);
529:                boolean back = (b != null && b.booleanValue());
530:                if (oppositeDir) {
531:                    back = !back;
532:                }
533:
534:                JTextComponent c = Utilities.getLastActiveComponent();
535:                if (c != null) {
536:                    Caret caret = c.getCaret();
537:                    if (!caret.isSelectionVisible()) {
538:                        if (!find(props, oppositeDir)) { // nothing found
539:                            return false;
540:                        }
541:                    }
542:                    // now there's selected text to be replaced
543:                    BaseDocument doc = (BaseDocument) c.getDocument();
544:                    int startPos = c.getSelectionStart();
545:                    int len = c.getSelectionEnd() - startPos;
546:                    doc.atomicLock();
547:                    try {
548:                        if (len > 0) {
549:                            doc.remove(startPos, len);
550:                        }
551:                        String s = (String) props
552:                                .get(SettingsNames.FIND_REPLACE_WITH);
553:                        if (s != null && s.length() > 0) {
554:                            doc.insertString(startPos, s, null);
555:                        }
556:                    } finally {
557:                        doc.atomicUnlock();
558:                    }
559:                    if (back) {
560:                        // Handle repetitive search ('a' replaced by 'aa') by moving the
561:                        // caret
562:                        if (caret instanceof  BaseCaret) {
563:                            ((BaseCaret) caret).setDot(startPos,
564:                                    (BaseCaret) caret, EditorUI.SCROLL_FIND);
565:
566:                        } else { // regular caret
567:                            caret.setDot(startPos);
568:                        }
569:                    }
570:                }
571:                return true;
572:            }
573:
574:            public void replaceAll(Map props) {
575:                incSearchReset();
576:                JTextComponent c = Utilities.getLastActiveComponent();
577:                BaseDocument doc = (BaseDocument) c.getDocument();
578:                int maxCnt = doc.getLength();
579:                int replacedCnt = 0;
580:                int totalCnt = 0;
581:
582:                props = getValidFindProperties(props);
583:                props = new HashMap(props);
584:                props.put(SettingsNames.FIND_WRAP_SEARCH, Boolean.FALSE);
585:
586:                String replaceWith = (String) props
587:                        .get(SettingsNames.FIND_REPLACE_WITH);
588:
589:                if (c != null) {
590:                    doc.atomicLock();
591:                    try {
592:                        int pos = 0; // actual position
593:                        while (true) {
594:                            int[] blk = findInBlock(c, pos, 0, -1, props, false);
595:                            if (blk == null) {
596:                                break;
597:                            }
598:                            totalCnt++;
599:                            int len = blk[1] - blk[0];
600:                            boolean skip = false; // cannot remove (because of guarded
601:                            // block)?
602:                            try {
603:                                doc.remove(blk[0], len);
604:                            } catch (GuardedException e) {
605:                                // replace in guarded block
606:                                skip = true;
607:                            }
608:                            if (skip) {
609:                                pos = blk[0] + len;
610:
611:                            } else { // can and will insert the new string
612:                                if (replaceWith != null
613:                                        && replaceWith.length() > 0) {
614:                                    doc.insertString(blk[0], replaceWith, null);
615:                                }
616:                                pos = blk[0]
617:                                        + ((replaceWith != null) ? replaceWith
618:                                                .length() : 0);
619:                                replacedCnt++;
620:                            }
621:                        }
622:                    } catch (BadLocationException e) {
623:                        e.printStackTrace();
624:                    } finally {
625:                        doc.atomicUnlock();
626:                    }
627:
628:                    MessageFormat fmt = new MessageFormat(LocaleSupport
629:                            .getString(ITEMS_REPLACED_LOCALE));
630:                    String msg = fmt.format(new Object[] {
631:                            new Integer(replacedCnt), new Integer(totalCnt) });
632:                    Utilities.setStatusText(c, msg);
633:                }
634:            }
635:
636:            /** Get position of wrap mark for some document */
637:            public int getWrapSearchMarkPos(BaseDocument doc) {
638:                Mark mark = (Mark) doc
639:                        .getProperty(BaseDocument.WRAP_SEARCH_MARK_PROP);
640:                try {
641:                    return (mark != null) ? mark.getOffset() : doc.getLength();
642:                } catch (InvalidMarkException e) {
643:                    throw new RuntimeException(); // shouldn't happen
644:                }
645:            }
646:
647:            /** Set new position of wrap mark for some document */
648:            public void setWrapSearchMarkPos(BaseDocument doc, int pos) {
649:                // !!!
650:            }
651:
652:            /**
653:             * Add weak listener to listen to change of any property. The caller must
654:             * hold the listener object in some instance variable to prevent it from
655:             * being garbage collected.
656:             */
657:            public void addPropertyChangeListener(PropertyChangeListener l) {
658:                changeSupport.addPropertyChangeListener(l);
659:            }
660:
661:            public synchronized void addPropertyChangeListener(
662:                    String findPropertyName, PropertyChangeListener l) {
663:                changeSupport.addPropertyChangeListener(findPropertyName, l);
664:            }
665:
666:            /** Remove listener for changes in properties */
667:            public void removePropertyChangeListener(PropertyChangeListener l) {
668:                changeSupport.removePropertyChangeListener(l);
669:            }
670:
671:            void firePropertyChange(String settingName, Object oldValue,
672:                    Object newValue) {
673:                changeSupport.firePropertyChange(this , settingName, oldValue,
674:                        newValue);
675:            }
676:
677:            public interface FinderCreator {
678:
679:                /** Create finder for regular finding */
680:                public FinderFactory.StringFinder createFinder(
681:                        BaseDocument doc, Map searchProps);
682:
683:                /** Create finder for finding in opposite direction than regular finder */
684:                public FinderFactory.StringFinder createBwdFinder(
685:                        BaseDocument doc, Map searchProps);
686:
687:                /** Create finder that returns position pairs useful for highlight search */
688:                public FinderFactory.BlocksFinder createBlocksFinder(
689:                        BaseDocument doc, Map searchProps);
690:
691:            }
692:
693:            public static class DefaultFinderCreator implements  FinderCreator {
694:
695:                protected Finder createFinder(BaseDocument doc,
696:                        Map searchProps, boolean oppositeDir,
697:                        boolean blocksFinder) {
698:
699:                    String text = (String) searchProps
700:                            .get(SettingsNames.FIND_WHAT);
701:                    if (text == null || text.length() == 0) {
702:                        if (blocksFinder) {
703:                            return new FinderFactory.FalseBlocksFinder();
704:                        } else {
705:                            return new FinderFactory.FalseFinder();
706:                        }
707:                    }
708:
709:                    Boolean b = (Boolean) searchProps
710:                            .get(SettingsNames.FIND_BACKWARD_SEARCH);
711:                    boolean bwdSearch = (b != null && b.booleanValue());
712:                    if (oppositeDir) { // negate for opposite direction search
713:                        bwdSearch = !bwdSearch;
714:                    }
715:
716:                    b = (Boolean) searchProps
717:                            .get(SettingsNames.FIND_MATCH_CASE);
718:                    boolean matchCase = (b != null && b.booleanValue());
719:                    b = (Boolean) searchProps
720:                            .get(SettingsNames.FIND_SMART_CASE);
721:                    boolean smartCase = (b != null && b.booleanValue());
722:                    b = (Boolean) searchProps
723:                            .get(SettingsNames.FIND_WHOLE_WORDS);
724:                    boolean wholeWords = (b != null && b.booleanValue());
725:
726:                    if (smartCase && !matchCase) {
727:                        int cnt = text.length();
728:                        for (int i = 0; i < cnt; i++) {
729:                            if (Character.isUpperCase(text.charAt(i))) {
730:                                matchCase = true;
731:                            }
732:                        }
733:                    }
734:
735:                    // searchProps.get(SettingsNames.FIND_REG_EXP);
736:                    if (blocksFinder) {
737:                        if (wholeWords) {
738:                            return new FinderFactory.WholeWordsBlocksFinder(
739:                                    doc, text, matchCase);
740:                        } else {
741:                            return new FinderFactory.StringBlocksFinder(text,
742:                                    matchCase);
743:                        }
744:                    } else {
745:                        if (wholeWords) {
746:                            if (bwdSearch) {
747:                                return new FinderFactory.WholeWordsBwdFinder(
748:                                        doc, text, matchCase);
749:                            } else {
750:                                return new FinderFactory.WholeWordsFwdFinder(
751:                                        doc, text, matchCase);
752:                            }
753:                        } else {
754:                            if (bwdSearch) {
755:                                return new FinderFactory.StringBwdFinder(text,
756:                                        matchCase);
757:                            } else {
758:                                return new FinderFactory.StringFwdFinder(text,
759:                                        matchCase);
760:                            }
761:                        }
762:                    }
763:                }
764:
765:                public FinderFactory.StringFinder createFinder(
766:                        BaseDocument doc, Map searchProps) {
767:                    return (FinderFactory.StringFinder) createFinder(doc,
768:                            searchProps, false, false);
769:                }
770:
771:                public FinderFactory.StringFinder createBwdFinder(
772:                        BaseDocument doc, Map searchProps) {
773:                    return (FinderFactory.StringFinder) createFinder(doc,
774:                            searchProps, true, false);
775:                }
776:
777:                /**
778:                 * Creates finder that makes position pairs of begining and end of the
779:                 * matched word.
780:                 */
781:                public FinderFactory.BlocksFinder createBlocksFinder(
782:                        BaseDocument doc, Map searchProps) {
783:                    return (FinderFactory.BlocksFinder) createFinder(doc,
784:                            searchProps, false, true);
785:                }
786:
787:            }
788:
789:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.