001: package org.columba.core.gui.util;
002:
003: import java.awt.BorderLayout;
004: import java.awt.event.ActionEvent;
005: import java.awt.event.ActionListener;
006:
007: import javax.swing.BorderFactory;
008: import javax.swing.JDialog;
009: import javax.swing.JOptionPane;
010: import javax.swing.JPanel;
011: import javax.swing.JTextField;
012:
013: import org.columba.core.gui.base.ButtonWithMnemonic;
014: import org.columba.core.gui.base.CheckBoxWithMnemonic;
015: import org.columba.core.gui.base.LabelWithMnemonic;
016: import org.columba.core.resourceloader.GlobalResourceLoader;
017: import org.columba.core.resourceloader.IconKeys;
018: import org.columba.core.resourceloader.ImageLoader;
019: import org.columba.core.util.FindReplace;
020: import org.columba.core.util.IEditableText;
021:
022: import com.jgoodies.forms.layout.CellConstraints;
023: import com.jgoodies.forms.layout.FormLayout;
024:
025: @SuppressWarnings("serial")
026: public class FindReplaceDialog extends JDialog implements
027: ActionListener {
028: private static final String RESOURCE_PATH = "org.columba.core.i18n.dialog";
029:
030: private LabelWithMnemonic findLabel;
031: private LabelWithMnemonic replaceLabel;
032:
033: private ButtonWithMnemonic findButton;
034: private ButtonWithMnemonic replaceButton;
035: private ButtonWithMnemonic replaceAllButton;
036: private ButtonWithMnemonic closeButton;
037:
038: private CheckBoxWithMnemonic wholeWordCheckBox;
039: private CheckBoxWithMnemonic caseSensCheckBox;
040: private CheckBoxWithMnemonic findBackCheckBox;
041:
042: private JTextField findTextField;
043: private JTextField replaceWithTextField;
044:
045: private IEditableText text;
046: private FindReplace finder = new FindReplace();
047:
048: public FindReplaceDialog(IEditableText text) {
049: setTitle(GlobalResourceLoader.getString(RESOURCE_PATH,
050: "findreplace", "findreplace.title"));
051: this .text = text;
052: initComponents();
053: setResizable(false);
054: pack();
055: setVisible(true);
056: }
057:
058: private void createComponents() {
059:
060: findLabel = new LabelWithMnemonic(GlobalResourceLoader
061: .getString(RESOURCE_PATH, "findreplace", "findlabel"));
062: replaceLabel = new LabelWithMnemonic(GlobalResourceLoader
063: .getString(RESOURCE_PATH, "findreplace",
064: "replacewithlabel"));
065:
066: findTextField = new JTextField(16);
067: replaceWithTextField = new JTextField(16);
068:
069: wholeWordCheckBox = new CheckBoxWithMnemonic(
070: GlobalResourceLoader.getString(RESOURCE_PATH,
071: "findreplace", "matchword"));
072: caseSensCheckBox = new CheckBoxWithMnemonic(
073: GlobalResourceLoader.getString(RESOURCE_PATH,
074: "findreplace", "matchcase"));
075: findBackCheckBox = new CheckBoxWithMnemonic(
076: GlobalResourceLoader.getString(RESOURCE_PATH,
077: "findreplace", "findbackwards"));
078:
079: findButton = new ButtonWithMnemonic(GlobalResourceLoader
080: .getString(RESOURCE_PATH, "findreplace", "find"));
081: replaceButton = new ButtonWithMnemonic(GlobalResourceLoader
082: .getString(RESOURCE_PATH, "findreplace", "replace"));
083: replaceAllButton = new ButtonWithMnemonic(GlobalResourceLoader
084: .getString(RESOURCE_PATH, "findreplace", "replaceall"));
085: closeButton = new ButtonWithMnemonic(GlobalResourceLoader
086: .getString(RESOURCE_PATH, "findreplace", "close"));
087:
088: }
089:
090: private void initComponents() {
091:
092: FormLayout layout = new FormLayout("pref, 6dlu, pref", // columns
093: "pref, 3dlu, pref"); // rows
094:
095: CellConstraints cc = new CellConstraints();
096:
097: createComponents();
098:
099: JPanel panelMain = new JPanel(layout);
100: panelMain.setBorder(BorderFactory.createEmptyBorder(12, 12, 12,
101: 12));
102:
103: panelMain.add(createInputPanel(), cc.xy(1, 1));
104: panelMain.add(createButtonPanel(), cc.xywh(3, 1, 1, 3));
105: panelMain.add(createOptionPanel(), cc.xy(1, 3));
106:
107: getContentPane().add(panelMain, BorderLayout.SOUTH);
108: getContentPane().add(
109: new DialogHeaderPanel(GlobalResourceLoader.getString(
110: RESOURCE_PATH, "findreplace", "header_title"),
111: GlobalResourceLoader.getString(RESOURCE_PATH,
112: "findreplace", "header_description"),
113: ImageLoader.getIcon(IconKeys.SEARCH)),
114: BorderLayout.NORTH);
115: }
116:
117: private JPanel createInputPanel() {
118: FormLayout layout = new FormLayout(
119: "left:pref, 6dlu, left:pref", "pref, 3dlu, pref");
120: CellConstraints cc = new CellConstraints();
121: JPanel inputPanel = new JPanel(layout);
122:
123: inputPanel.add(findLabel, cc.xy(1, 1));
124: findLabel.setLabelFor(findTextField);
125: inputPanel.add(findTextField, cc.xy(3, 1));
126:
127: inputPanel.add(replaceLabel, cc.xy(1, 3));
128: replaceLabel.setLabelFor(replaceWithTextField);
129: inputPanel.add(replaceWithTextField, cc.xy(3, 3));
130:
131: return inputPanel;
132: }
133:
134: private JPanel createOptionPanel() {
135: // create a panel with a border and 3 options
136: FormLayout layout = new FormLayout(
137: "3dlu, left:pref, 30dlu, right:pref, 3dlu", // columns
138: "pref, 3dlu, pref"); // rows
139:
140: CellConstraints cc = new CellConstraints();
141:
142: JPanel optionPanel = new JPanel(layout);
143: optionPanel.setBorder(BorderFactory
144: .createTitledBorder(GlobalResourceLoader.getString(
145: RESOURCE_PATH, "findreplace", "options")));
146:
147: optionPanel.add(caseSensCheckBox, cc.xy(2, 1));
148: optionPanel.add(wholeWordCheckBox, cc.xywh(2, 3, 3, 1));
149: optionPanel.add(findBackCheckBox, cc.xy(4, 1));
150:
151: return optionPanel;
152: }
153:
154: private JPanel createButtonPanel() {
155: // create a panel with all 4 buttons of the window
156: FormLayout layout = new FormLayout("fill:pref", // columns
157: "pref, 3dlu, pref, 3dlu, pref, 3dlu, pref"); // rows
158: CellConstraints cc = new CellConstraints();
159: JPanel buttonPanel = new JPanel(layout);
160:
161: findButton.addActionListener(this );
162: findButton.setActionCommand("FIND");
163: this .getRootPane().setDefaultButton(findButton);
164:
165: replaceButton.addActionListener(this );
166: replaceButton.setActionCommand("REPLACE");
167:
168: replaceAllButton.addActionListener(this );
169: replaceAllButton.setActionCommand("REPLACEALL");
170:
171: closeButton.addActionListener(this );
172: closeButton.setActionCommand("CLOSE");
173:
174: buttonPanel.add(findButton, cc.xy(1, 1));
175: buttonPanel.add(replaceButton, cc.xy(1, 3));
176: buttonPanel.add(replaceAllButton, cc.xy(1, 5));
177: buttonPanel.add(closeButton, cc.xy(1, 7));
178:
179: return buttonPanel;
180: }
181:
182: public void actionPerformed(ActionEvent e) {
183: // if some action was performed
184: String action = e.getActionCommand();
185:
186: String source = text.getText();
187:
188: // setup the finder
189: finder.setPattern(findTextField.getText());
190: finder.setSource(source);
191: finder.setCaseSensitive(caseSensCheckBox.isSelected());
192: finder.setMatchWholeWord(wholeWordCheckBox.isSelected());
193: finder.setBackwards(findBackCheckBox.isSelected());
194: finder.setReplaceWith(replaceWithTextField.getText());
195:
196: // find action performed
197: if (action.equals("FIND")) {
198: int position = finder.findnext();
199: if (position >= 0) {
200: // mark found pattern
201: mark(position, position
202: + findTextField.getText().length());
203: //text.grabFocus();
204: } else {
205: // delete marks
206: mark(0, 0);
207: JOptionPane.showMessageDialog(this ,
208: GlobalResourceLoader.getString(RESOURCE_PATH,
209: "findreplace", "string_not_found"));
210: }
211: }
212: //replace action performed
213: else if (action.equals("REPLACE")) {
214: int position = finder.replace();
215: if (position >= 0) {
216: //text.grabFocus();
217: text.setText(finder.getSource());
218: mark(position, position
219: + replaceWithTextField.getText().length());
220: } else {
221: mark(0, 0);
222: JOptionPane.showMessageDialog(this ,
223: GlobalResourceLoader.getString(RESOURCE_PATH,
224: "findreplace", "string_not_found"));
225: }
226: }
227: //replaceall action performed
228: else if (action.equals("REPLACEALL")) {
229: finder.replaceAll();
230: if (finder.getSource().equals(text.getText())) {
231: //text.grabFocus();
232: text.setText(finder.getSource());
233: JOptionPane.showMessageDialog(this ,
234: GlobalResourceLoader.getString(RESOURCE_PATH,
235: "findreplace", "string_not_found"));
236: } else
237: text.setText(finder.getSource());
238: } else if (action.equals("CLOSE")) {
239: this .dispose();
240: }
241: }
242:
243: // to mark a text in the JTextPane
244: private void mark(int start, int end) {
245: text.setCaretPosition(start);
246: text.moveCaretPosition(end);
247: }
248: }
|