001: /*
002: * Copyright (c) 2000, Jacob Smullyan.
003: *
004: * This is part of SkunkDAV, a WebDAV client. See http://skunkdav.sourceforge.net/
005: * for the latest version.
006: *
007: * SkunkDAV is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License as published
009: * by the Free Software Foundation; either version 2, or (at your option)
010: * any later version.
011: *
012: * SkunkDAV is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with SkunkDAV; see the file COPYING. If not, write to the Free
019: * Software Foundation, 59 Temple Place - Suite 330, Boston, MA
020: * 02111-1307, USA.
021: */
022:
023: package org.skunk.dav.client.gui.editor;
024:
025: import java.awt.Font;
026: import java.awt.GridBagConstraints;
027: import java.awt.GridBagLayout;
028: import java.awt.Insets;
029: import java.awt.event.ActionEvent;
030: import java.awt.event.ActionListener;
031: import javax.swing.Box;
032: import javax.swing.JButton;
033: import javax.swing.JLabel;
034: import org.skunk.dav.client.gui.ExplorerApp;
035: import org.skunk.dav.client.gui.ResourceManager;
036: import org.skunk.dav.client.gui.StateMonitor;
037: import org.skunk.dav.client.gui.View;
038: import org.skunk.dav.client.gui.editor.action.SearchAndReplaceAction;
039: import org.skunk.trace.Debug;
040:
041: /**
042: * a panel for performing search and replace operations
043: */
044: public class SearchAndReplacePanel extends AbstractSearchPanel {
045: private JButton findNextButton, replaceAllButton, replaceButton;
046:
047: public SearchAndReplacePanel() {
048: super ();
049: }
050:
051: protected void initComponents() {
052: JLabel findLabel = new JLabel(ResourceManager
053: .getMessage(ResourceManager.REPLACE_PROMPT));
054: JLabel replaceLabel = new JLabel(ResourceManager
055: .getMessage(ResourceManager.REPLACE_WITH_PROMPT));
056: createEntryField();
057: createReverseCheckBox();
058: createSearchModeButtonGroup();
059: createReplaceField();
060: createFindNextButton();
061: createReplaceButton();
062: createReplaceAllButton();
063:
064: this .setLayout(new GridBagLayout());
065: GridBagConstraints gbc = new GridBagConstraints();
066: gbc.gridx = 0;
067: gbc.gridy = 0;
068: gbc.anchor = GridBagConstraints.NORTHWEST;
069: gbc.fill = GridBagConstraints.BOTH;
070: gbc.insets = new Insets(2, 2, 2, 2);
071: gbc.gridwidth = 1;
072: gbc.gridheight = 1;
073:
074: this .add(findLabel, gbc);
075:
076: gbc.gridx++;
077: this .add(entryField, gbc);
078:
079: gbc.gridx++;
080: this .add(findNextButton, gbc);
081:
082: gbc.gridx = 0;
083: gbc.gridy++;
084: this .add(replaceLabel, gbc);
085:
086: gbc.gridx++;
087: this .add(replaceField, gbc);
088:
089: gbc.gridx++;
090: this .add(replaceButton, gbc);
091:
092: gbc.gridy++;
093: this .add(replaceAllButton, gbc);
094:
095: gbc.gridx = 0;
096: gbc.gridy++;
097: Box optionBox = Box.createHorizontalBox();
098: optionBox.add(reverseButton);
099: optionBox.add(literalButton);
100: optionBox.add(caseButton);
101: optionBox.add(regexpButton);
102: gbc.gridwidth = 3;
103: gbc.anchor = GridBagConstraints.CENTER;
104: this .add(optionBox, gbc);
105: }
106:
107: public void addNotify() {
108: super .addNotify();
109: getRootPane().setDefaultButton(findNextButton);
110: }
111:
112: protected void createFindNextButton() {
113: findNextButton = new JButton(ResourceManager
114: .getMessage(ResourceManager.FIND_NEXT_OPTION));
115: findNextButton.addActionListener(new ActionListener() {
116: public void actionPerformed(ActionEvent ae) {
117: fireActionEvent(isReverse() ? SEARCH_FROM_CARET
118: : SEARCH_FROM_SELECTION);
119: }
120: });
121: }
122:
123: private StateMonitor.Mapper mapper;
124: private StateMonitor.DomainMatcher matcher;
125:
126: private StateMonitor.Mapper getButtonValueMapper() {
127: if (mapper == null) {
128: mapper = new StateMonitor.Mapper() {
129: public Object getTargetValue(Object applicationValue) {
130: return new Boolean(applicationValue != null);
131: }
132:
133: public boolean canMapValue(Object applicationValue) {
134: return (applicationValue == null || applicationValue instanceof SimpleTextEditor);
135: }
136: };
137: }
138: return mapper;
139: }
140:
141: private StateMonitor.DomainMatcher getButtonDomainMatcher() {
142: if (matcher == null) {
143: matcher = new StateMonitor.DomainMatcher() {
144: public boolean domainMatches(Object domainKey) {
145: View v = ExplorerApp.getAppContext()
146: .getCurrentView();
147: return (v != null && domainKey != null && domainKey
148: .equals(v.getFocussedBuffer()));
149: }
150: };
151: }
152: return matcher;
153: }
154:
155: protected void createReplaceButton() {
156: replaceButton = new JButton(ResourceManager
157: .getMessage(ResourceManager.REPLACE_OPTION));
158: replaceButton.setEnabled(false);
159: StateMonitor.registerProperty(replaceButton, "enabled",
160: SearchAndReplaceAction.MATCH_PROPERTY,
161: getButtonValueMapper(), getButtonDomainMatcher());
162:
163: replaceButton.addActionListener(new ActionListener() {
164: public void actionPerformed(ActionEvent ae) {
165: SearchAndReplacePanel.this .replaceButton
166: .setEnabled(false);
167: fireActionEvent(REPLACE_NEXT);
168: }
169: });
170: }
171:
172: protected void createReplaceAllButton() {
173: replaceAllButton = new JButton(ResourceManager
174: .getMessage(ResourceManager.REPLACE_ALL_OPTION));
175: replaceAllButton.setEnabled(false);
176: StateMonitor.registerProperty(replaceAllButton, "enabled",
177: SearchAndReplaceAction.MATCH_PROPERTY,
178: getButtonValueMapper(), getButtonDomainMatcher());
179: replaceAllButton.addActionListener(new ActionListener() {
180: public void actionPerformed(ActionEvent ae) {
181: SearchAndReplacePanel.this .replaceAllButton
182: .setEnabled(false);
183: fireActionEvent(REPLACE_ALL);
184: }
185: });
186: }
187: }
|