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