001: /*
002: * SQLeonardo :: java database frontend
003: * Copyright (C) 2004 nickyb@users.sourceforge.net
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: *
019: */
020:
021: package nickyb.sqleonardo.environment.ctrl.content;
022:
023: import java.awt.GridBagConstraints;
024: import java.awt.GridBagLayout;
025: import java.awt.GridLayout;
026: import java.awt.Insets;
027: import java.awt.event.ActionEvent;
028: import java.awt.event.ActionListener;
029: import java.util.Vector;
030:
031: import javax.swing.JCheckBox;
032: import javax.swing.JComboBox;
033: import javax.swing.JDialog;
034: import javax.swing.JLabel;
035: import javax.swing.JPanel;
036: import javax.swing.JTextField;
037:
038: import nickyb.sqleonardo.common.gui.CommandButton;
039: import nickyb.sqleonardo.environment.Application;
040: import nickyb.sqleonardo.environment.ctrl.ContentPane;
041:
042: public class DialogFindReplace extends JDialog implements
043: ActionListener {
044: private CommandButton btnFind;
045: private CommandButton btnReplace;
046: private CommandButton btnReplaceAll;
047:
048: private JCheckBox chxAll;
049: private JCheckBox chxNullFind;
050: private JCheckBox chxNullReplace;
051: // private JCheckBox chxBlock;
052: // private JCheckBox chxChanges;
053: private JCheckBox chxCase;
054:
055: private JComboBox cbxColumns;
056: private JComboBox cbxOperator;
057:
058: private JTextField txtFind;
059: private JTextField txtReplace;
060:
061: private ContentFlag flag;
062: private ContentView view;
063:
064: public DialogFindReplace(ContentPane content) {
065: super (Application.window, "find/replace");
066: this .flag = new ContentFlag();
067: this .view = content.getView();
068:
069: init();
070: pack();
071:
072: setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
073: setLocationRelativeTo(Application.window);
074: setResizable(false);
075: }
076:
077: public void dispose() {
078: view.mark(null);
079: super .dispose();
080: }
081:
082: private void init() {
083: Vector vCols = new Vector();
084: for (int i = 0; i < view.getColumnCount(); i++)
085: vCols.addElement(view.getColumnName(i));
086:
087: JPanel pnlT = new JPanel(new GridLayout(3, 3, 2, 0));
088: pnlT.add(new JLabel("find into:"));
089: pnlT.add(cbxColumns = new JComboBox(vCols));
090: pnlT.add(chxAll = new JCheckBox("all columns"));
091:
092: pnlT.add(cbxOperator = new JComboBox(new String[] { "equals",
093: "contains", "starts with", "ends with" }));
094: pnlT.add(txtFind = new JTextField());
095: pnlT.add(chxNullFind = new JCheckBox("null"));
096:
097: pnlT.add(new JLabel("replace with:"));
098: pnlT.add(txtReplace = new JTextField());
099: pnlT.add(chxNullReplace = new JCheckBox("null"));
100:
101: JPanel pnlC = new JPanel(new GridLayout(1, 3, 2, 0));
102: // pnlC.add(chxBlock = new JCheckBox("only current block"));
103: // pnlC.add(chxChanges = new JCheckBox("only changed cells"));
104: pnlC.add(chxCase = new JCheckBox("case sensitive"));
105:
106: JPanel pnlB = new JPanel();
107: pnlB.add(btnFind = new CommandButton("find", this ));
108: pnlB.add(btnReplace = new CommandButton("replace", this ));
109: pnlB
110: .add(btnReplaceAll = new CommandButton("replace all",
111: this ));
112:
113: btnReplace.setEnabled(false);
114:
115: GridBagConstraints gbc = new GridBagConstraints();
116: gbc.insets = new Insets(8, 10, 0, 10);
117: gbc.fill = GridBagConstraints.HORIZONTAL;
118: gbc.gridwidth = GridBagConstraints.REMAINDER;
119:
120: getContentPane().setLayout(new GridBagLayout());
121: getContentPane().add(pnlT, gbc);
122: getContentPane().add(pnlC, gbc);
123: getContentPane().add(pnlB, gbc);
124:
125: chxAll.addActionListener(this );
126: chxNullFind.addActionListener(this );
127: chxNullReplace.addActionListener(this );
128: }
129:
130: private void mark(int row, int col) {
131: flag.block = (row / ContentModel.MAX_BLOCK_RECORDS) + 1;
132: flag.row = row
133: - (ContentModel.MAX_BLOCK_RECORDS * (flag.block - 1));
134: flag.col = col;
135:
136: view.getControl().getSlider().setValue(flag.block - 1);
137: view.mark(flag);
138: }
139:
140: private boolean find() {
141: int cStart = view.getColumn();
142: if (cStart == -1)
143: cStart = 0;
144:
145: int rStart = view.getFlatRow();
146: if (rStart == -1)
147: rStart = 0;
148:
149: if (!chxAll.isSelected()) {
150: cStart = view.getColumnIndex(cbxColumns.getSelectedItem()
151: .toString());
152: }
153:
154: int col = cStart;
155: int row = rStart;
156:
157: view.mark(null);
158:
159: do {
160: if (chxAll.isSelected()) {
161: if (++col == view.getColumnCount()) {
162: col = 0;
163: if (++row == view.getFlatRowCount())
164: row = 0;
165: }
166: } else {
167: if (++row == view.getFlatRowCount())
168: row = 0;
169: }
170:
171: Object value = view.getFlatValueAt(row, col);
172:
173: boolean bmark = false;
174: if (value != null) {
175: String f = chxCase.isSelected() ? txtFind.getText()
176: : txtFind.getText().toUpperCase();
177: String v = chxCase.isSelected() ? value.toString()
178: : value.toString().toUpperCase();
179:
180: if (cbxOperator.getSelectedItem().toString().equals(
181: "equals")) {
182: bmark = v.equals(f);
183: } else if (cbxOperator.getSelectedItem().toString()
184: .equals("contains")) {
185: bmark = v.indexOf(f) != -1;
186: } else if (cbxOperator.getSelectedItem().toString()
187: .equals("starts with")) {
188: bmark = v.startsWith(f);
189: } else if (cbxOperator.getSelectedItem().toString()
190: .equals("ends with")) {
191: bmark = v.endsWith(f);
192: }
193: } else {
194: bmark = chxNullFind.isSelected();
195: }
196:
197: if (bmark) {
198: mark(row, col);
199: return true;
200: }
201: } while (!(col == cStart && row == rStart));
202:
203: return false;
204: }
205:
206: private boolean replace() {
207: Object value = txtReplace.getText();
208: if (chxNullReplace.isSelected())
209: value = null;
210:
211: view.getControl().getSlider().setValue(flag.block - 1);
212: view.setValueAt(value, flag.row, flag.col);
213: view.mark(null);
214:
215: return find();
216: }
217:
218: public void actionPerformed(ActionEvent ae) {
219: cbxColumns.setEnabled(!chxAll.isSelected());
220: cbxOperator.setEnabled(!chxNullFind.isSelected());
221: txtFind.setEnabled(!chxNullFind.isSelected());
222: txtReplace.setEnabled(!chxNullReplace.isSelected());
223:
224: if (ae.getSource() == btnFind) {
225: btnReplace.setEnabled(find());
226: } else if (ae.getSource() == btnReplace) {
227: btnReplace.setEnabled(replace());
228: } else if (ae.getSource() == btnReplaceAll) {
229: btnReplace.setEnabled(false);
230: while (replace())
231: ;
232: }
233: }
234: }
|