001: /*
002: * SearchCriteriaPanel.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.components;
013:
014: import java.awt.BorderLayout;
015: import java.awt.Component;
016: import java.awt.GridLayout;
017: import java.awt.Window;
018: import javax.swing.JCheckBox;
019: import javax.swing.JLabel;
020: import javax.swing.JPanel;
021: import javax.swing.JTextField;
022: import workbench.gui.WbSwingUtilities;
023: import workbench.interfaces.ValidatingComponent;
024: import workbench.resource.ResourceMgr;
025: import workbench.resource.Settings;
026:
027: /**
028: * A search dialog panel.
029: *
030: * @see workbench.gui.components.TableReplacer
031: * @see workbench.gui.editor.SearchAndReplace
032: *
033: * @author support@sql-workbench.net
034: */
035: public class SearchCriteriaPanel extends JPanel implements
036: ValidatingComponent {
037: private String caseProperty;
038: private String wordProperty;
039: private String regexProperty;
040: private String criteriaProperty;
041: private String highlightProperty;
042:
043: private JCheckBox ignoreCase;
044: private JCheckBox wholeWord;
045: private JCheckBox useRegEx;
046: private JCheckBox highlightAll;
047:
048: protected JTextField criteria;
049: protected JLabel label;
050:
051: public SearchCriteriaPanel() {
052: this (null);
053: }
054:
055: public SearchCriteriaPanel(String initialValue) {
056: this (initialValue, "workbench.sql.search", false);
057: }
058:
059: public SearchCriteriaPanel(String initialValue, String settingsKey,
060: boolean showHighlight) {
061: caseProperty = settingsKey + ".ignoreCase";
062: wordProperty = settingsKey + ".wholeWord";
063: regexProperty = settingsKey + ".useRegEx";
064: criteriaProperty = settingsKey + ".lastValue";
065: if (showHighlight) {
066: highlightProperty = settingsKey + ".highlight";
067: }
068:
069: this .ignoreCase = new JCheckBox(ResourceMgr
070: .getString("LblSearchIgnoreCase"));
071: this .ignoreCase.setName("ignorecase");
072: this .ignoreCase.setToolTipText(ResourceMgr
073: .getDescription("LblSearchIgnoreCase"));
074: this .ignoreCase.setSelected(Settings.getInstance()
075: .getBoolProperty(caseProperty, true));
076:
077: this .wholeWord = new JCheckBox(ResourceMgr
078: .getString("LblSearchWordsOnly"));
079: this .wholeWord.setToolTipText(ResourceMgr
080: .getDescription("LblSearchWordsOnly"));
081: this .wholeWord.setSelected(Settings.getInstance()
082: .getBoolProperty(wordProperty, false));
083: this .wholeWord.setName("wholeword");
084:
085: this .useRegEx = new JCheckBox(ResourceMgr
086: .getString("LblSearchRegEx"));
087: this .useRegEx.setToolTipText(ResourceMgr
088: .getDescription("LblSearchRegEx"));
089: this .useRegEx.setSelected(Settings.getInstance()
090: .getBoolProperty(regexProperty, false));
091: this .useRegEx.setName("regex");
092:
093: if (showHighlight) {
094: this .highlightAll = new JCheckBox(ResourceMgr
095: .getString("LblHighlightAll"));
096: this .highlightAll.setToolTipText(ResourceMgr
097: .getDescription("LblHighlightAll"));
098: this .highlightAll.setSelected(Settings.getInstance()
099: .getBoolProperty(highlightProperty, false));
100: }
101:
102: this .label = new JLabel(ResourceMgr
103: .getString("LblSearchCriteria"));
104: this .criteria = new JTextField();
105: this .criteria.setName("searchtext");
106:
107: this .criteria.setColumns(40);
108: if (initialValue != null) {
109: this .criteria.setText(initialValue);
110: this .criteria.selectAll();
111: } else {
112: this .criteria.setText(Settings.getInstance().getProperty(
113: criteriaProperty, null));
114: this .criteria.selectAll();
115: }
116:
117: this .criteria
118: .addMouseListener(new TextComponentMouseListener());
119:
120: this .setLayout(new BorderLayout());
121: JPanel p = new JPanel();
122: p.setLayout(new BorderLayout(5, 5));
123: p.add(this .label, BorderLayout.WEST);
124: p.add(this .criteria, BorderLayout.CENTER);
125: this .add(p, BorderLayout.CENTER);
126: p = new JPanel();
127: if (showHighlight) {
128: p.setLayout(new GridLayout(3, 2));
129: p.add(this .ignoreCase);
130: p.add(this .highlightAll);
131: p.add(this .wholeWord);
132: p.add(new JPanel());
133: p.add(this .useRegEx);
134: } else {
135: p.setLayout(new GridLayout(3, 1));
136: p.add(this .ignoreCase);
137: p.add(this .wholeWord);
138: p.add(this .useRegEx);
139: }
140:
141: this .add(p, BorderLayout.SOUTH);
142: }
143:
144: public String getCriteria() {
145: return this .criteria.getText();
146: }
147:
148: public boolean getWholeWordOnly() {
149: return this .wholeWord.isSelected();
150: }
151:
152: public boolean getIgnoreCase() {
153: return this .ignoreCase.isSelected();
154: }
155:
156: public boolean getUseRegex() {
157: return this .useRegEx.isSelected();
158: }
159:
160: public boolean getHighlightAll() {
161: if (this .highlightAll == null)
162: return false;
163: return this .highlightAll.isSelected();
164: }
165:
166: public void setSearchCriteria(String aValue) {
167: this .criteria.setText(aValue);
168: if (aValue != null) {
169: this .criteria.selectAll();
170: }
171: }
172:
173: public boolean showFindDialog(Component caller) {
174: return showFindDialog(caller, ResourceMgr
175: .getString("TxtWindowTitleSearchText"));
176: }
177:
178: public boolean showFindDialog(Component caller, String title) {
179: Window w = WbSwingUtilities.getWindowAncestor(caller);
180: boolean result = ValidatingDialog.showConfirmDialog(w, this ,
181: title, caller, 0, false);
182:
183: Settings.getInstance().setProperty(caseProperty,
184: this .getIgnoreCase());
185: Settings.getInstance().setProperty(criteriaProperty,
186: this .getCriteria());
187: Settings.getInstance().setProperty(wordProperty,
188: this .getWholeWordOnly());
189: Settings.getInstance().setProperty(regexProperty,
190: this .getUseRegex());
191: if (this .highlightProperty != null) {
192: Settings.getInstance().setProperty(highlightProperty,
193: getHighlightAll());
194: }
195: return result;
196: }
197:
198: public boolean validateInput() {
199: return true;
200: }
201:
202: public void componentDisplayed() {
203: criteria.requestFocusInWindow();
204: }
205:
206: }
|