001: /*
002: * ConfirmReplacementDialog.java
003: *
004: * Copyright (C) 1998-2003 Peter Graves
005: * $Id: ConfirmReplacementDialog.java,v 1.4 2003/10/13 23:53:47 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program 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
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.awt.Dimension;
025: import java.awt.Point;
026: import java.awt.Rectangle;
027: import java.awt.event.ActionEvent;
028: import java.awt.event.KeyEvent;
029: import javax.swing.JPanel;
030:
031: public final class ConfirmReplacementDialog extends AbstractDialog {
032: private static Rectangle rect;
033:
034: private Editor editor;
035: private Replacement replacement;
036: private boolean inFiles;
037:
038: public ConfirmReplacementDialog(Replacement replacement,
039: boolean inFiles) {
040: super (Editor.currentEditor(), "Confirm Replacement", true);
041: this .replacement = replacement;
042: editor = replacement.getEditor();
043: this .inFiles = inFiles;
044: JPanel textPanel = new JPanel();
045: textPanel.setAlignmentX(LEFT_ALIGNMENT);
046: textPanel.add(new Label("Replace this occurrence?"));
047: mainPanel.add(textPanel);
048: JPanel buttonBox = new JPanel();
049: buttonBox.setAlignmentX(LEFT_ALIGNMENT);
050: buttonBox.add(createButton("Yes", 'Y'));
051: buttonBox.add(createButton("No", 'N'));
052: buttonBox.add(createButton("Cancel", 'C'));
053: if (inFiles)
054: buttonBox.add(createButton("Skip File", 'S'));
055: buttonBox.add(createButton("Replace All", 'R'));
056: mainPanel.add(buttonBox);
057: getContentPane().add(mainPanel);
058: pack();
059: if (rect != null)
060: setBounds(rect);
061: else {
062: // Center dialog at top of editor window.
063: Dimension dimParent = editor.getFrame().getSize();
064: Dimension dimDialog = getSize();
065: Point p = editor.getFrame().getLocation();
066: p.translate((dimParent.width - dimDialog.width) / 2, 0);
067: setLocation(p);
068: }
069: }
070:
071: private StandardButton createButton(String text, char mnemonic) {
072: StandardButton button = new StandardButton(text);
073: button.setMnemonic(mnemonic);
074: button.addActionListener(this );
075: button.addKeyListener(this );
076: return button;
077: }
078:
079: protected void enter() {
080: yes();
081: }
082:
083: private void yes() {
084: replacement.replaceOccurrence();
085: replacement.getEditor().updateDisplay();
086: Position pos = replacement.find(editor.getBuffer(), editor
087: .getDot());
088: if (pos == null) {
089: dispose();
090: return;
091: }
092: if (replacement.restrictToSelection()) {
093: final Region region = replacement.getRegion();
094: if (region != null) {
095: if (pos.lineNumber() > region.getEndLineNumber()
096: || (pos.getLine() == region.getEndLine() && pos
097: .getOffset()
098: + replacement.getPatternLength() > region
099: .getEndOffset())) {
100: dispose();
101: return;
102: }
103: }
104: }
105: editor.moveDotTo(pos);
106: editor.markFoundPattern(replacement);
107: editor.updateDisplay();
108: }
109:
110: private void no() {
111: Position start = new Position(editor.getDot());
112: if (!start.next()) {
113: dispose();
114: return;
115: }
116: Position pos = replacement.find(editor.getBuffer(), start);
117: if (pos == null) {
118: dispose();
119: return;
120: }
121: if (replacement.restrictToSelection()) {
122: final Region region = replacement.getRegion();
123: if (region != null) {
124: if (pos.lineNumber() > region.getEndLineNumber()
125: || (pos.getLine() == region.getEndLine() && pos
126: .getOffset()
127: + replacement.getPatternLength() > region
128: .getEndOffset())) {
129: dispose();
130: return;
131: }
132: }
133: }
134: editor.moveDotTo(pos);
135: editor.markFoundPattern(replacement);
136: editor.updateDisplay();
137: }
138:
139: private void skipFile() {
140: dispose();
141: }
142:
143: private void replaceAll() {
144: Position saved = new Position(editor.getDot());
145: // Replace current occurrence.
146: replacement.replaceOccurrence();
147: // Replace all the rest.
148: Position pos;
149: while ((pos = replacement.find(editor.getBuffer(), editor
150: .getDot())) != null) {
151: editor.addUndo(SimpleEdit.MOVE);
152: editor.getDot().moveTo(pos);
153: if (replacement.restrictToSelection()) {
154: final Region region = replacement.getRegion();
155: if (region != null) {
156: if (editor.getDotLineNumber() > region
157: .getEndLineNumber()
158: || (editor.getDotLine() == region
159: .getEndLine() && editor
160: .getDotOffset()
161: + replacement.getPatternLength() > region
162: .getEndOffset())) {
163: break;
164: }
165: }
166: }
167: if (replacement.isMultilinePattern())
168: editor.markFoundPattern(replacement);
169: replacement.replaceOccurrence();
170: }
171: editor.addUndo(SimpleEdit.MOVE);
172: editor.getDot().moveTo(saved);
173: editor.moveCaretToDotCol();
174: editor.updateDisplay();
175: dispose();
176: if (inFiles)
177: replacement.setConfirmChanges(false);
178: }
179:
180: public void actionPerformed(ActionEvent e) {
181: if (e.getActionCommand().equals("Yes")) {
182: yes();
183: return;
184: }
185: if (e.getActionCommand().equals("No")) {
186: no();
187: return;
188: }
189: if (e.getActionCommand().equals("Cancel")) {
190: cancel();
191: return;
192: }
193: if (e.getActionCommand().equals("Skip File")) {
194: skipFile();
195: return;
196: }
197: if (e.getActionCommand().equals("Replace All")) {
198: replaceAll();
199: return;
200: }
201: }
202:
203: public void keyPressed(KeyEvent e) {
204: if (e.isConsumed())
205: return;
206: // Mask off the bits we don't care about (Java 1.4).
207: final int modifiers = e.getModifiers() & 0x0f;
208: if (modifiers == 0) {
209: int keyCode = e.getKeyCode();
210: switch (keyCode) {
211: case KeyEvent.VK_Y:
212: e.consume();
213: yes();
214: return;
215: case KeyEvent.VK_N:
216: e.consume();
217: no();
218: return;
219: case KeyEvent.VK_C:
220: e.consume();
221: cancel();
222: return;
223: case KeyEvent.VK_S:
224: e.consume();
225: if (inFiles) {
226: skipFile();
227: return;
228: }
229: break;
230: case KeyEvent.VK_R:
231: e.consume();
232: replaceAll();
233: return;
234: }
235: }
236: super .keyPressed(e);
237: }
238:
239: public void dispose() {
240: rect = getBounds();
241: super.dispose();
242: }
243: }
|