Source Code Cross Referenced for FindDialogSupport.java in  » IDE-Netbeans » editor » org » netbeans » editor » ext » 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 » IDE Netbeans » editor » org.netbeans.editor.ext 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003:         *
004:         * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005:         *
006:         * The contents of this file are subject to the terms of either the GNU
007:         * General Public License Version 2 only ("GPL") or the Common
008:         * Development and Distribution License("CDDL") (collectively, the
009:         * "License"). You may not use this file except in compliance with the
010:         * License. You can obtain a copy of the License at
011:         * http://www.netbeans.org/cddl-gplv2.html
012:         * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013:         * specific language governing permissions and limitations under the
014:         * License.  When distributing the software, include this License Header
015:         * Notice in each file and include the License file at
016:         * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
017:         * particular file as subject to the "Classpath" exception as provided
018:         * by Sun in the GPL Version 2 section of the License file that
019:         * accompanied this code. If applicable, add the following below the
020:         * License Header, with the fields enclosed by brackets [] replaced by
021:         * your own identifying information:
022:         * "Portions Copyrighted [year] [name of copyright owner]"
023:         *
024:         * Contributor(s):
025:         *
026:         * The Original Software is NetBeans. The Initial Developer of the Original
027:         * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028:         * Microsystems, Inc. All Rights Reserved.
029:         *
030:         * If you wish your version of this file to be governed by only the CDDL
031:         * or only the GPL Version 2, indicate your decision by adding
032:         * "[Contributor] elects to include this software in this distribution
033:         * under the [CDDL or GPL Version 2] license." If you do not indicate a
034:         * single choice of license, a recipient has the option to distribute
035:         * your version of this file under either the CDDL, the GPL Version 2 or
036:         * to extend the choice of license to its licensees as provided above.
037:         * However, if you add GPL Version 2 code and therefore, elected the GPL
038:         * Version 2 license, then the option applies only if the new code is
039:         * made subject to such option by the copyright holder.
040:         */
041:
042:        package org.netbeans.editor.ext;
043:
044:        import java.awt.Component;
045:        import java.awt.Dialog;
046:        import java.awt.event.*;
047:        import java.beans.PropertyChangeEvent;
048:        import java.beans.PropertyChangeListener;
049:        import java.util.ArrayList;
050:        import java.util.List;
051:        import java.util.Map;
052:        import java.util.HashMap;
053:        import java.util.Collections;
054:        import java.util.ResourceBundle;
055:        import java.util.Vector;
056:        import javax.swing.*;
057:        import javax.swing.JComponent;
058:        import javax.swing.text.JTextComponent;
059:        import javax.swing.text.BadLocationException;
060:        import javax.swing.text.Position;
061:        import org.netbeans.editor.BaseKit;
062:        import org.netbeans.editor.FindSupport.SearchPatternWrapper;
063:        import org.netbeans.editor.SettingsNames;
064:        import org.netbeans.editor.FindSupport;
065:        import org.netbeans.editor.DialogSupport;
066:        import org.netbeans.editor.GuardedException;
067:        import org.netbeans.editor.Utilities;
068:        import java.util.Iterator;
069:        import javax.swing.text.DefaultEditorKit;
070:        import javax.swing.text.Document;
071:        import org.netbeans.editor.BaseDocument;
072:        import org.openide.util.NbBundle;
073:
074:        /**
075:         * Support for displaying find and replace dialogs
076:         *
077:         * @author Miloslav Metelka
078:         * @version 1.00
079:         * @deprecated Without any replacement.
080:         */
081:
082:        public class FindDialogSupport extends WindowAdapter implements 
083:                ActionListener {
084:
085:            /** This lock is used to create a barrier between showing/hiding/changing
086:             *  the dialog and testing if the dialog is already shown.
087:             *  it is used to make test-and-change / test-and-display actions atomic.
088:             *  It covers the following four fields: findDialog, isReplaceDialog,
089:             *  findPanel, findButtons
090:             */
091:            private static Object dialogLock = new Object();
092:
093:            /** Whether the currently visible dialog is for replace */
094:            private static boolean isReplaceDialog = false;
095:
096:            /** The buttons used in the visible dialog */
097:            private static JButton findButtons[];
098:
099:            private static JButton findDialogButtons[];
100:            private static JButton replaceDialogButtons[];
101:
102:            /** The FindPanel used inside the visible dialog */
103:            private static FindPanel findPanel;
104:
105:            /** Currently visible dialog */
106:            private static Dialog findDialog = null;
107:
108:            private int caretPosition;
109:
110:            private static FindDialogSupport singleton = null;
111:            private static PropertyChangeListener historyChangeListener;
112:
113:            private boolean findPerformed = false;
114:
115:            private static int xPos = Integer.MIN_VALUE;
116:            private static int yPos = Integer.MIN_VALUE;
117:
118:            /** Flag for determining a dialog invocation. It the dialog
119:             *  is invoked by keystroke or by the menu the value is true.
120:             *  If the dialog was already shown and the focus was bring to it only,
121:             *  value is false - needed for fixing the issue #68021
122:             */
123:            private static boolean dialogInvokedViaKeystroke;
124:
125:            public static FindDialogSupport getFindDialogSupport() {
126:                if (singleton == null) {
127:                    singleton = new FindDialogSupport();
128:                }
129:                return singleton;
130:            }
131:
132:            private FindDialogSupport() {
133:            }
134:
135:            private void createFindButtons() {
136:                if (findButtons == null) {
137:                    ResourceBundle bundle = NbBundle.getBundle(BaseKit.class);
138:                    findButtons = new JButton[] {
139:                            new JButton(bundle.getString("find-button-find")), // NOI18N
140:                            new JButton(bundle.getString("find-button-replace")), // NOI18N
141:                            new JButton(bundle
142:                                    .getString("find-button-replace-all")), // NOI18N
143:                            new JButton(bundle.getString("find-button-cancel")) // NOI18N
144:                    };
145:
146:                    findButtons[0].setMnemonic(bundle.getString(
147:                            "find-button-find-mnemonic").charAt(0)); // NOI18N
148:                    findButtons[1].setMnemonic(bundle.getString(
149:                            "find-button-replace-mnemonic").charAt(0)); // NOI18N
150:                    findButtons[2].setMnemonic(bundle.getString(
151:                            "find-button-replace-all-mnemonic").charAt(0)); // NOI18N
152:
153:                    findButtons[0].getAccessibleContext()
154:                            .setAccessibleDescription(
155:                                    bundle.getString("ACSD_find-button-find")); // NOI18N
156:                    findButtons[1]
157:                            .getAccessibleContext()
158:                            .setAccessibleDescription(
159:                                    bundle
160:                                            .getString("ACSD_find-button-replace")); // NOI18N
161:                    findButtons[2]
162:                            .getAccessibleContext()
163:                            .setAccessibleDescription(
164:                                    bundle
165:                                            .getString("ACSD_find-button-replace-all")); // NOI18N
166:                    findButtons[3]
167:                            .getAccessibleContext()
168:                            .setAccessibleDescription(
169:                                    bundle.getString("ACSD_find-button-cancel")); // NOI18N
170:
171:                    findDialogButtons = new JButton[2];
172:                    findDialogButtons[0] = findButtons[0];
173:                    findDialogButtons[1] = findButtons[3];
174:
175:                    replaceDialogButtons = new JButton[4];
176:                    replaceDialogButtons[0] = findButtons[0];
177:                    replaceDialogButtons[1] = findButtons[1];
178:                    replaceDialogButtons[2] = findButtons[2];
179:                    replaceDialogButtons[3] = findButtons[3];
180:                }
181:            }
182:
183:            private void createFindPanel() {
184:                if (findPanel == null) {
185:                    findPanel = new FindPanel();
186:                }
187:            }
188:
189:            private Dialog createFindDialog(JPanel findPanel,
190:                    final JButton[] buttons, final ActionListener l) {
191:                Dialog d = DialogSupport.createDialog(
192:                        isReplaceDialog ? NbBundle.getBundle(BaseKit.class)
193:                                .getString("replace-title") : NbBundle
194:                                .getBundle(BaseKit.class).getString(
195:                                        "find-title"), // NOI18N
196:                        findPanel, false, // non-modal
197:                        buttons, true, // sidebuttons,
198:                        0, // defaultIndex = 0 => findButton
199:                        isReplaceDialog ? 3 : 1, // cancelIndex = 3 => cancelButton
200:                        l //listener
201:                        );
202:
203:                return d;
204:            }
205:
206:            private void showFindDialogImpl(boolean isReplace,
207:                    KeyEventBlocker blocker) {
208:                dialogInvokedViaKeystroke = true;
209:                synchronized (dialogLock) {
210:                    if (findDialog != null && isReplaceDialog != isReplace) {
211:                        xPos = findDialog.getLocation().x;
212:                        yPos = findDialog.getLocation().y;
213:                        findDialog.dispose();
214:                        findDialog = null;
215:                    }
216:                    if (findDialog == null) { // create and show new dialog of required type
217:                        isReplaceDialog = isReplace;
218:                        createFindButtons();
219:                        createFindPanel();
220:                        findPanel.changeVisibility(isReplace);
221:
222:                        findDialog = createFindDialog(findPanel,
223:                                isReplace ? replaceDialogButtons
224:                                        : findDialogButtons, this );
225:                        findDialog.addWindowListener(this );
226:                        ((JDialog) findDialog).getRootPane()
227:                                .setFocusable(false);
228:                        if (xPos > Integer.MIN_VALUE) {
229:                            findDialog.setLocation(xPos, yPos);
230:                        }
231:                    }
232:                } // end of synchronized section
233:
234:                findDialog.pack();
235:                findPanel.init(isReplace, blocker);
236:                findDialog.setVisible(true);
237:                findPanel.showNotify();
238:                updateCaretPosition();
239:            }
240:
241:            private void updateCaretPosition() {
242:                JTextComponent c = Utilities.getLastActiveComponent();
243:                if (c != null) {
244:                    caretPosition = c.getCaret().getDot();
245:                }
246:            }
247:
248:            public void windowActivated(WindowEvent evt) {
249:                findPerformed = false;
250:                createFindPanel();
251:                findPanel.initBlockSearch();
252:                updateCaretPosition();
253:            }
254:
255:            public void windowDeactivated(WindowEvent evt) {
256:                Map findProps = findPanel.getFindProps();
257:                JTextComponent c = Utilities.getLastActiveComponent();
258:                if (c != null) {
259:                    boolean blockSearch = getBooleanProp(
260:                            SettingsNames.FIND_BLOCK_SEARCH, findProps);
261:                    if (blockSearch && !findPerformed) {
262:                        Integer bsStartInt = (Integer) findProps
263:                                .get(SettingsNames.FIND_BLOCK_SEARCH_START);
264:                        int bsStart = (bsStartInt == null) ? -1 : bsStartInt
265:                                .intValue();
266:                        Position pos = (Position) findProps
267:                                .get(SettingsNames.FIND_BLOCK_SEARCH_END);
268:                        int bsEnd = (pos != null) ? pos.getOffset() : -1;
269:                        if (bsStart >= 0 && bsEnd > 0) {
270:                            c.select(bsStart, bsEnd);
271:                        }
272:                    } else {
273:                        //                EditorUI editorUI = ((BaseTextUI)c.getUI()).getEditorUI();
274:                        //                DrawLayerFactory.IncSearchLayer incLayer
275:                        //                = (DrawLayerFactory.IncSearchLayer)editorUI.findLayer(
276:                        //                      DrawLayerFactory.INC_SEARCH_LAYER_NAME);
277:                        //                if (incLayer != null) {
278:                        //                    if (incLayer.isEnabled()) {
279:                        //                        int offs = incLayer.getOffset();
280:                        //                        int len = incLayer.getLength();
281:                        //                        if (len > 0){
282:                        //                            c.select(offs, offs + len);
283:                        //                        }
284:                        //                    }
285:                        //                }
286:                    }
287:                }
288:                FindSupport.getFindSupport().incSearchReset();
289:                findPanel.resetBlockSearch();
290:                KeyEventBlocker blocker = findPanel.getBlocker();
291:                if (blocker != null) {
292:                    blocker.stopBlocking(false);
293:                }
294:            }
295:
296:            public void windowClosing(WindowEvent e) {
297:                hideDialog();
298:            }
299:
300:            public void windowClosed(WindowEvent e) {
301:                synchronized (dialogLock) {
302:                    if (findDialog != null) {
303:                        xPos = findDialog.getLocation().x;
304:                        yPos = findDialog.getLocation().y;
305:                    }
306:                }
307:                Map findProps = findPanel.getFindProps();
308:                FindSupport.getFindSupport().incSearchReset();
309:                findPanel.resetBlockSearch();
310:                FindSupport.getFindSupport().setBlockSearchHighlight(0, 0);
311:                findProps.put(SettingsNames.FIND_BLOCK_SEARCH, Boolean.FALSE);
312:                findProps.put(SettingsNames.FIND_BLOCK_SEARCH_START,
313:                        new Integer(0));
314:                findProps.put(SettingsNames.FIND_BLOCK_SEARCH_END, null);
315:                FindSupport.getFindSupport().putFindProperties(findProps);
316:                KeyEventBlocker blocker = findPanel.getBlocker();
317:                if (blocker != null) {
318:                    blocker.stopBlocking(false);
319:                }
320:                findPanel.reset();
321:
322:                Utilities.returnFocus();
323:            }
324:
325:            public void showFindDialog(KeyEventBlocker blocker) {
326:                showFindDialogImpl(false, blocker);
327:            }
328:
329:            public void showReplaceDialog(KeyEventBlocker blocker) {
330:                showFindDialogImpl(true, blocker);
331:            }
332:
333:            public void hideDialog() {
334:                synchronized (dialogLock) {
335:                    if (findDialog != null) {
336:                        xPos = findDialog.getLocation().x;
337:                        yPos = findDialog.getLocation().y;
338:                        findDialog.dispose();
339:                    }
340:                    findDialog = null;
341:                }
342:            }
343:
344:            private Vector getHistoryVector() {
345:                List histList = (List) FindSupport.getFindSupport()
346:                        .getHistory();
347:                if (histList == null)
348:                    histList = new ArrayList();
349:                boolean isRegExpChecked = ((Boolean) findPanel.getFindProps()
350:                        .get(SettingsNames.FIND_REG_EXP)).booleanValue();
351:                Vector vec = new Vector();
352:                for (int i = 0; i < histList.size(); i++) {
353:                    SearchPatternWrapper spw = (SearchPatternWrapper) histList
354:                            .get(i);
355:                    String searchExpression = spw.getSearchExpression();
356:                    if (isRegExpChecked == spw.isRegExp()
357:                            && !vec.contains(searchExpression)) {
358:                        vec.add(searchExpression);
359:                    }
360:                }
361:                return vec;
362:            }
363:
364:            private boolean getBooleanProp(String propName, Map map) {
365:                Boolean b = (Boolean) map.get(propName);
366:                return (b != null) ? b.booleanValue() : false;
367:            }
368:
369:            public void actionPerformed(ActionEvent evt) {
370:                if (findButtons == null)
371:                    return;
372:
373:                Object src = evt.getSource();
374:                FindSupport fSup = FindSupport.getFindSupport();
375:                Map findPanelMap = findPanel.getFindProps();
376:
377:                SearchPatternWrapper spw = new SearchPatternWrapper(
378:                        (String) findPanelMap.get(SettingsNames.FIND_WHAT),
379:                        getBooleanProp(SettingsNames.FIND_WHOLE_WORDS,
380:                                findPanelMap),
381:                        getBooleanProp(SettingsNames.FIND_MATCH_CASE,
382:                                findPanelMap),
383:                        getBooleanProp(SettingsNames.FIND_REG_EXP, findPanelMap));
384:
385:                if (src == findButtons[0]) { // Find button
386:                    fSup.addToHistory(spw);
387:                    fSup.putFindProperties(findPanelMap);
388:                    fSup.find(null, false);
389:                    updateCaretPosition();
390:                    findPerformed = true;
391:                } else if (src == findButtons[1]) { // Replace button
392:                    fSup.addToHistory(spw);
393:                    findPanel.updateReplaceHistory();
394:                    fSup.putFindProperties(findPanelMap);
395:                    try {
396:                        if (fSup.replace(null, false)) { // replaced
397:                            fSup.find(null, false);
398:                        }
399:                    } catch (GuardedException e) {
400:                        // replace in guarded block
401:                    } catch (BadLocationException e) {
402:                        e.printStackTrace();
403:                    }
404:                    updateCaretPosition();
405:                    findPerformed = true;
406:                } else if (src == findButtons[2]) { // Replace All button
407:                    fSup.addToHistory(spw);
408:                    findPanel.updateReplaceHistory();
409:                    fSup.putFindProperties(findPanelMap);
410:                    fSup.replaceAll(null);
411:                    findPerformed = true;
412:                } else if (src == findButtons[3]) { // Cancel button
413:                    hideDialog();
414:                }
415:            }
416:
417:            private int getBlockEndOffset() {
418:                Position pos = (Position) FindSupport.getFindSupport()
419:                        .getFindProperties().get(
420:                                SettingsNames.FIND_BLOCK_SEARCH_END);
421:                return (pos != null) ? pos.getOffset() : -1;
422:            }
423:
424:            /** Panel that holds the find logic */
425:            private class FindPanel extends FindDialogPanel implements 
426:                    ItemListener, KeyListener, ActionListener, FocusListener {
427:
428:                private Map findProps = Collections
429:                        .synchronizedMap(new HashMap(20));
430:                private Map objToProps = Collections
431:                        .synchronizedMap(new HashMap(20));
432:
433:                private javax.swing.DefaultComboBoxModel findHistory = new javax.swing.DefaultComboBoxModel();
434:                private javax.swing.DefaultComboBoxModel replaceHistory = new javax.swing.DefaultComboBoxModel();
435:
436:                private KeyEventBlocker blocker;
437:
438:                private int blockSearchStartPos = 0;
439:                private int blockSearchEndPos = 0;
440:
441:                FindPanel() {
442:                    objToProps.put(findWhat, SettingsNames.FIND_WHAT);
443:                    objToProps
444:                            .put(replaceWith, SettingsNames.FIND_REPLACE_WITH);
445:                    objToProps.put(highlightSearch,
446:                            SettingsNames.FIND_HIGHLIGHT_SEARCH);
447:                    objToProps.put(incSearch, SettingsNames.FIND_INC_SEARCH);
448:                    objToProps.put(matchCase, SettingsNames.FIND_MATCH_CASE);
449:                    //objToProps.put(smartCase, SettingsNames.FIND_SMART_CASE);
450:                    objToProps.put(wholeWords, SettingsNames.FIND_WHOLE_WORDS);
451:                    objToProps.put(regExp, SettingsNames.FIND_REG_EXP);
452:                    objToProps.put(bwdSearch,
453:                            SettingsNames.FIND_BACKWARD_SEARCH);
454:                    objToProps.put(wrapSearch, SettingsNames.FIND_WRAP_SEARCH);
455:                    objToProps
456:                            .put(blockSearch, SettingsNames.FIND_BLOCK_SEARCH);
457:
458:                    findProps.putAll(FindSupport.getFindSupport()
459:                            .getFindProperties());
460:                    revertMap();
461:
462:                    findWhat.setModel(findHistory);
463:                    findWhat.getEditor().setItem(getProperty(findWhat));
464:                    Component editorC = findWhat.getEditor()
465:                            .getEditorComponent();
466:                    if (editorC instanceof  JComponent) {
467:                        InputMap inputMap = ((JComponent) editorC)
468:                                .getInputMap();
469:                        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT,
470:                                InputEvent.SHIFT_MASK),
471:                                DefaultEditorKit.pasteAction);
472:                        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT,
473:                                InputEvent.CTRL_MASK),
474:                                DefaultEditorKit.copyAction);
475:                    }
476:                    replaceWith.setModel(replaceHistory);
477:                    replaceWith.getEditor().setItem(getProperty(replaceWith));
478:                    highlightSearch
479:                            .setSelected(getBooleanProperty(highlightSearch));
480:                    incSearch.setSelected(getBooleanProperty(incSearch));
481:                    matchCase.setSelected(getBooleanProperty(matchCase));
482:                    //smartCase.setSelected(getBooleanProperty(smartCase));
483:                    wholeWords.setSelected(getBooleanProperty(wholeWords));
484:                    regExp.setSelected(getBooleanProperty(regExp));
485:                    bwdSearch.setSelected(getBooleanProperty(bwdSearch));
486:                    wrapSearch.setSelected(getBooleanProperty(wrapSearch));
487:
488:                    findWhat.getEditor().getEditorComponent().addKeyListener(
489:                            this );
490:                    findWhat.addActionListener(this );
491:                    replaceWith.getEditor().getEditorComponent()
492:                            .addKeyListener(this );
493:                    replaceWith.addActionListener(this );
494:                    highlightSearch.addItemListener(this );
495:                    incSearch.addItemListener(this );
496:                    matchCase.addItemListener(this );
497:                    //smartCase.addItemListener(this);
498:                    wholeWords.addItemListener(this );
499:                    regExp.addItemListener(this );
500:                    bwdSearch.addItemListener(this );
501:                    wrapSearch.addItemListener(this );
502:                    blockSearch.addItemListener(this );
503:                    historyChangeListener = new PropertyChangeListener() {
504:                        public void propertyChange(PropertyChangeEvent evt) {
505:                            if (evt == null
506:                                    || !FindSupport.FIND_HISTORY_CHANGED_PROP
507:                                            .equals(evt.getPropertyName())) {
508:                                return;
509:                            }
510:                            updateFindHistory();
511:                        }
512:                    };
513:                    FindSupport.getFindSupport().addPropertyChangeListener(
514:                            historyChangeListener);
515:                }
516:
517:                protected Map getFindProps() {
518:                    return findProps;
519:                }
520:
521:                private KeyEventBlocker getBlocker() {
522:                    return blocker;
523:                }
524:
525:                private void putProperty(Object component, Object value) {
526:                    String prop = (String) objToProps.get(component);
527:                    if (prop != null) {
528:                        findProps.put(prop, value);
529:                    }
530:                }
531:
532:                private Object getProperty(Object component) {
533:                    String prop = (String) objToProps.get(component);
534:                    return (prop != null) ? findProps.get(prop) : null;
535:                }
536:
537:                private boolean getBooleanProperty(Object component) {
538:                    Object prop = getProperty(component);
539:                    return (prop != null) ? ((Boolean) prop).booleanValue()
540:                            : false;
541:                }
542:
543:                protected void changeVisibility(boolean v) {
544:                    replaceWith.setVisible(v);
545:                    replaceWithLabel.setVisible(v);
546:                }
547:
548:                public void resetBlockSearch() {
549:                    blockSearch.setSelected(false);
550:                    blockSearch.setEnabled(false);
551:                    findProps.put(SettingsNames.FIND_BLOCK_SEARCH,
552:                            Boolean.FALSE);
553:                    findProps.put(SettingsNames.FIND_BLOCK_SEARCH_START,
554:                            new Integer(0));
555:                    findProps.put(SettingsNames.FIND_BLOCK_SEARCH_END, null);
556:                    blockSearchStartPos = 0;
557:                    blockSearchEndPos = 0;
558:                    FindSupport.getFindSupport().setBlockSearchHighlight(0, 0);
559:                    FindSupport.getFindSupport().putFindProperties(findProps);
560:                }
561:
562:                private void initBlockSearch() {
563:                    JTextComponent c = Utilities.getLastActiveComponent();
564:                    String selText = null;
565:                    int startSelection = 0;
566:                    int endSelection = 0;
567:                    boolean blockSearchVisible = false;
568:
569:                    if (c != null) {
570:                        startSelection = c.getSelectionStart();
571:                        endSelection = c.getSelectionEnd();
572:
573:                        Document doc = c.getDocument();
574:                        if (doc instanceof  BaseDocument) {
575:                            BaseDocument bdoc = (BaseDocument) doc;
576:                            try {
577:                                int startLine = Utilities.getLineOffset(bdoc,
578:                                        startSelection);
579:                                int endLine = Utilities.getLineOffset(bdoc,
580:                                        endSelection);
581:                                if (endLine > startLine) {
582:                                    blockSearchVisible = true;
583:                                }
584:                            } catch (BadLocationException ble) {
585:                            }
586:                        }
587:
588:                        caretPosition = bwdSearch.isSelected() ? c
589:                                .getSelectionEnd() : c.getSelectionStart();
590:
591:                        if (blockSearchVisible == false
592:                                && dialogInvokedViaKeystroke) {
593:                            dialogInvokedViaKeystroke = false;
594:                            selText = c.getSelectedText();
595:                            if (selText != null) {
596:                                int n = selText.indexOf('\n');
597:                                if (n >= 0)
598:                                    selText = selText.substring(0, n);
599:                                findWhat.getEditor().setItem(selText);
600:                                changeFindWhat(true);
601:                            }
602:                        }
603:
604:                        blockSearchStartPos = blockSearchVisible ? startSelection
605:                                : 0;
606:                        blockSearchEndPos = blockSearchVisible ? endSelection
607:                                : 0;
608:
609:                        try {
610:                            blockSearch.setEnabled(blockSearchVisible);
611:                            blockSearch.setSelected(blockSearchVisible);
612:                            findProps.put(SettingsNames.FIND_BLOCK_SEARCH,
613:                                    Boolean.valueOf(blockSearchVisible));
614:                            findProps.put(
615:                                    SettingsNames.FIND_BLOCK_SEARCH_START,
616:                                    new Integer(blockSearchStartPos));
617:                            int be = getBlockEndOffset();
618:                            if (be < 0) {
619:                                findProps.put(
620:                                        SettingsNames.FIND_BLOCK_SEARCH_END,
621:                                        doc.createPosition(blockSearchEndPos));
622:                            } else {
623:                                blockSearchEndPos = be;
624:                            }
625:                            FindSupport.getFindSupport()
626:                                    .setBlockSearchHighlight(
627:                                            blockSearchStartPos,
628:                                            blockSearchEndPos);
629:                        } catch (BadLocationException ble) {
630:                            blockSearch.setSelected(false);
631:                            findProps.put(SettingsNames.FIND_BLOCK_SEARCH,
632:                                    Boolean.FALSE);
633:                            findProps
634:                                    .put(SettingsNames.FIND_BLOCK_SEARCH_START,
635:                                            null);
636:                        }
637:                    }
638:
639:                }
640:
641:                protected void init(boolean isReplace, KeyEventBlocker blocker) {
642:                    this .blocker = blocker;
643:                    findHistory.setSelectedItem(null);
644:                    replaceHistory.setSelectedItem(null);
645:                    findWhat.getEditor().getEditorComponent().addFocusListener(
646:                            this );
647:                    if (isReplace) {
648:                        replaceWith.getEditor().getEditorComponent()
649:                                .addFocusListener(this );
650:                    }
651:
652:                    findProps.putAll(FindSupport.getFindSupport()
653:                            .getFindProperties());
654:                    revertMap();
655:
656:                    highlightSearch
657:                            .setSelected(getBooleanProperty(highlightSearch));
658:                    incSearch.setSelected(getBooleanProperty(incSearch));
659:                    matchCase.setSelected(getBooleanProperty(matchCase));
660:                    //smartCase.setSelected(getBooleanProperty(smartCase));
661:                    wholeWords.setSelected(getBooleanProperty(wholeWords));
662:                    boolean regExpValue = getBooleanProperty(regExp);
663:                    regExp.setSelected(regExpValue);
664:                    wholeWords.setEnabled(!regExpValue);
665:                    incSearch.setEnabled(!regExpValue);
666:                    bwdSearch.setSelected(getBooleanProperty(bwdSearch));
667:                    wrapSearch.setSelected(getBooleanProperty(wrapSearch));
668:                    findHistory = new DefaultComboBoxModel(getHistoryVector());
669:                    findWhat.setModel(findHistory);
670:                }
671:
672:                protected void reset() {
673:                    this .blocker = null;
674:                }
675:
676:                protected void showNotify() {
677:                    // fix of issue #66217 
678:                    boolean focused = findWhat.getEditor().getEditorComponent()
679:                            .requestFocusInWindow();
680:                    if (focused == false) {
681:                        SwingUtilities.invokeLater(new Runnable() {
682:                            public void run() {
683:                                findWhat.getEditor().getEditorComponent()
684:                                        .requestFocusInWindow();
685:                            }
686:                        });
687:                    }
688:                }
689:
690:                private void updateHistory(JComboBox c,
691:                        javax.swing.DefaultComboBoxModel history) {
692:                    Object item = c.getEditor().getItem();
693:                    if (item != null && !item.equals("")) { //NOI18N
694:                        history.removeElement(item);
695:                        history.insertElementAt(item, 0);
696:                        history.setSelectedItem(null);
697:                    }
698:                    c.getEditor().setItem(item);
699:                }
700:
701:                protected void updateFindHistory() {
702:                    //updateHistory(findWhat, findHistory);
703:                    /*
704:                    List list = new ArrayList();
705:                    for (int i = 0; i<findHistory.getSize(); i++){
706:                        list.add(findHistory.getElementAt(i));
707:                    }
708:                    FindSupport.getFindSupport().putFindProperty(SettingsNames.FIND_HISTORY, list);
709:                    findProps.put(SettingsNames.FIND_HISTORY, list);
710:                     */
711:                    Object obj = findWhat.getEditor().getItem();
712:                    findHistory = new DefaultComboBoxModel(getHistoryVector());
713:                    findWhat.setModel(findHistory);
714:                    if (obj != null) {
715:                        findWhat.getEditor().setItem(obj);
716:                    }
717:                }
718:
719:                protected void updateReplaceHistory() {
720:                    updateHistory(replaceWith, replaceHistory);
721:                }
722:
723:                private void revertMap() {
724:                    Object prop = findProps.get(FindSupport.REVERT_MAP);
725:                    if (!(prop instanceof  Map))
726:                        return;
727:                    Map revertMap = (Map) prop;
728:
729:                    for (Iterator i = revertMap.keySet().iterator(); i
730:                            .hasNext();) {
731:                        String key = (String) i.next();
732:
733:                        Object obj = findProps.get(key);
734:                        boolean value = (obj != null) ? ((Boolean) obj)
735:                                .booleanValue() : false;
736:                        if (value != ((Boolean) revertMap.get(key))
737:                                .booleanValue())
738:                            ;
739:                        findProps
740:                                .put(key, value ? Boolean.FALSE : Boolean.TRUE);
741:                    }
742:
743:                    findProps.put(FindSupport.REVERT_MAP, null);
744:                }
745:
746:                private void changeFindWhat(boolean performIncSearch) {
747:                    Object old = getProperty(findWhat);
748:                    Object cur = findWhat.getEditor().getItem();
749:                    if ((old == null && cur != null && !cur.equals(""))
750:                            || (old != null && !old.equals(cur))) { // NOI18N
751:                        putProperty(findWhat, cur);
752:                        if (performIncSearch) {
753:                            findPerformed = FindSupport.getFindSupport()
754:                                    .incSearch(getFindProps(), caretPosition);
755:                        }
756:                    }
757:                }
758:
759:                private void changeReplaceWith() {
760:                    Object old = getProperty(replaceWith);
761:                    Object cur = replaceWith.getEditor().getItem();
762:                    if ((old == null && cur != null && !cur.equals(""))
763:                            || (old != null && !old.equals(cur))) { // NOI18N
764:                        putProperty(replaceWith, cur);
765:                    }
766:                }
767:
768:                private void postChangeCombos(final boolean performIncSearch) {
769:                    SwingUtilities.invokeLater(new Runnable() {
770:                        public void run() {
771:                            changeFindWhat(performIncSearch);
772:                            changeReplaceWith();
773:                        }
774:                    });
775:                }
776:
777:                public void keyPressed(KeyEvent evt) {
778:                    if (evt.getKeyChar() == '\n') {
779:                        evt.consume();
780:                    }
781:                }
782:
783:                public void keyReleased(KeyEvent evt) {
784:                    if (evt.getKeyChar() == '\n') {
785:                        evt.consume();
786:                    } else if (evt.getKeyCode() == KeyEvent.VK_INSERT) {
787:                        postChangeCombos(true);
788:                    }
789:                }
790:
791:                public void keyTyped(KeyEvent evt) {
792:                    if (evt.getKeyChar() == '\n') {
793:                        findButtons[0].doClick(20);
794:                        evt.consume();
795:                        ((JComboBox) ((JTextField) evt.getSource()).getParent())
796:                                .hidePopup();
797:                    } else {
798:                        postChangeCombos(true);
799:                    }
800:                }
801:
802:                public void itemStateChanged(ItemEvent evt) {
803:                    Boolean val = (evt.getStateChange() == ItemEvent.SELECTED) ? Boolean.TRUE
804:                            : Boolean.FALSE;
805:                    if (evt.getItem() == bwdSearch) {
806:                        if (blockSearch.isEnabled() && blockSearch.isSelected()) {
807:                            boolean value = val.booleanValue();
808:                            JTextComponent c = Utilities
809:                                    .getLastActiveComponent();
810:                            if (c != null) {
811:                                c.getCaret().setDot(
812:                                        value ? blockSearchEndPos
813:                                                : blockSearchStartPos);
814:                                updateCaretPosition();
815:                            }
816:
817:                        }
818:                    }
819:                    if (evt.getItem() == regExp) {
820:                        boolean value = !val.booleanValue();
821:                        incSearch.setEnabled(value);
822:                        wholeWords.setEnabled(value);
823:                    }
824:                    if (evt.getItem() == blockSearch) {
825:                        boolean value = val.booleanValue();
826:                        if (value) {
827:                            if (blockSearchStartPos <= 0
828:                                    && blockSearchEndPos <= 0) {
829:                                initBlockSearch();
830:                            } else {
831:                                JTextComponent c = Utilities
832:                                        .getLastActiveComponent();
833:                                if (c != null) {
834:                                    c
835:                                            .getCaret()
836:                                            .setDot(
837:                                                    bwdSearch.isSelected() ? blockSearchEndPos
838:                                                            : blockSearchStartPos);
839:                                    updateCaretPosition();
840:                                }
841:                            }
842:                            FindSupport.getFindSupport()
843:                                    .setBlockSearchHighlight(
844:                                            blockSearchStartPos,
845:                                            blockSearchEndPos);
846:                        } else {
847:                            FindSupport.getFindSupport().putFindProperty(
848:                                    SettingsNames.FIND_BLOCK_SEARCH,
849:                                    Boolean.FALSE);
850:                            FindSupport.getFindSupport()
851:                                    .setBlockSearchHighlight(0, 0);
852:                        }
853:                    }
854:
855:                    putProperty(evt.getSource(), val);
856:
857:                    if (evt.getItem() == regExp) {
858:                        updateFindHistory();
859:                    }
860:                }
861:
862:                public void actionPerformed(ActionEvent evt) {
863:                    postChangeCombos(false);
864:                }
865:
866:                public void focusGained(FocusEvent e) {
867:                    if (e.getSource() instanceof  JTextField) {
868:                        ((JTextField) e.getSource()).selectAll();
869:                        if (blocker != null) {
870:                            blocker.stopBlocking();
871:                        }
872:                    }
873:                    ((JComponent) e.getSource()).removeFocusListener(this );
874:                }
875:
876:                public void focusLost(FocusEvent e) {
877:
878:                }
879:
880:            }
881:
882:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.