Skip to content

Commit b7e7dc2

Browse files
author
mgricken
committed
Fixed some problems in find/replace and vector keystroke config.
git-svn-id: file:///tmp/test-svn/trunk@4885 fe72c1cf-3628-48e9-8b72-1c46755d3cff
1 parent 202fe65 commit b7e7dc2

File tree

8 files changed

+94
-56
lines changed

8 files changed

+94
-56
lines changed

drjava/src/edu/rice/cs/drjava/model/FindReplaceMachine.java

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ public class FindReplaceMachine {
5959
private OpenDefinitionsDocument _firstDoc; // First document where searching started (when searching all documents)
6060
// private Position _current; // Position of the cursor in _doc when machine is stopped
6161
private int _current; // Position of the cursor in _doc when machine is stopped
62-
private int _selectionStart; //start position of the highlighted selection
63-
private int _selectionEnd; //end position of the highlighted selection
62+
private MovingDocumentRegion _selectionRegion; // selected text region
6463
// private Position _start; // Position in _doc from which searching started or will start.
6564
private String _findWord; // Word to find. */
6665
private String _replaceWord; // Word to replace _findword.
@@ -248,13 +247,18 @@ public boolean replaceCurrent() {
248247
catch (BadLocationException e) { throw new UnexpectedException(e); }
249248
}
250249

250+
/** Set the selected text region.
251+
* @param s selected region
252+
*/
253+
public void setSelection(MovingDocumentRegion s) {
254+
_selectionRegion = s;
255+
}
256+
251257
/** Replaces all occurrences of the find word with the replace word in the current document of in all documents
252258
* depending the value of the machine register _searchAllDocuments.
253259
* @return the number of replacements
254260
*/
255-
public int replaceAll(int selectionStart, int selectionEnd) {
256-
_selectionStart = selectionStart;
257-
_selectionEnd = selectionEnd;
261+
public int replaceAll() {
258262
return replaceAll(_searchAllDocuments, _searchSelectedText);
259263
}
260264

@@ -302,22 +306,23 @@ private int _replaceAllInCurrentDoc(boolean searchSelectedText) {
302306
assert EventQueue.isDispatchThread();
303307

304308
if(!searchSelectedText) {
305-
_selectionStart = 0;
306-
_selectionEnd = _doc.getLength();
309+
_selectionRegion = new MovingDocumentRegion(_doc, 0, _doc.getLength(),
310+
_doc._getLineStartPos(0),
311+
_doc._getLineEndPos(_doc.getLength()));
307312
}
308-
if (_isForward) setPosition(Math.min(_selectionStart, _selectionEnd));
309-
else setPosition(Math.max(_selectionStart,_selectionEnd));
313+
if (_isForward) setPosition(_selectionRegion.getStartOffset());
314+
else setPosition(_selectionRegion.getEndOffset());
310315

311316
int count = 0;
312317
FindResult fr = findNext(false); // find next match in current doc
313-
// Utilities.show(fr + " returned by call on findNext()");
318+
// Utilities.show(fr + " returned by call on findNext()");
314319

315-
while (!fr.getWrapped() && fr.getFoundOffset()<=_selectionEnd) {
320+
while (!fr.getWrapped() && fr.getFoundOffset()<=_selectionRegion.getEndOffset()) {
316321
replaceCurrent();
317322
count++;
318-
// Utilities.show("Found " + count + " occurrences. Calling findNext() inside loop");
323+
// Utilities.show("Found " + count + " occurrences. Calling findNext() inside loop");
319324
fr = findNext(false); // find next match in current doc
320-
// Utilities.show("Call on findNext() returned " + fr.toString() + "in doc '" + _doc.getText() + "'");
325+
// Utilities.show("Call on findNext() returned " + fr.toString() + "in doc '" + _doc.getText().substring(0,fr.getFoundOffset())+"[|]"+_doc.getText().substring(fr.getFoundOffset()) + "'");
321326
}
322327
return count;
323328
}
@@ -327,9 +332,8 @@ private int _replaceAllInCurrentDoc(boolean searchSelectedText) {
327332
* @param findAction action to perform on the occurrences; input is the FindResult, output is ignored
328333
* @return the number of processed occurrences
329334
*/
330-
public int processAll(Runnable1<FindResult> findAction, int selectionStart, int selectionEnd) {
331-
_selectionStart = selectionStart;
332-
_selectionEnd = selectionEnd;
335+
public int processAll(Runnable1<FindResult> findAction, MovingDocumentRegion region) {
336+
_selectionRegion = region;
333337
return processAll(findAction, _searchAllDocuments, _searchSelectedText);
334338
}
335339

@@ -377,18 +381,18 @@ else if(searchSelectedText) {
377381
* @return the number of replacements
378382
*/
379383
private int _processAllInCurrentDoc(Runnable1<FindResult> findAction, boolean searchSelectedText) {
380-
381384
if(!searchSelectedText) {
382-
_selectionStart = 0;
383-
_selectionEnd = _doc.getLength();
385+
_selectionRegion = new MovingDocumentRegion(_doc, 0, _doc.getLength(),
386+
_doc._getLineStartPos(0),
387+
_doc._getLineEndPos(_doc.getLength()));
384388
}
385-
if (_isForward) setPosition(Math.min(_selectionStart, _selectionEnd));
386-
else setPosition(Math.max(_selectionStart,_selectionEnd));
389+
if (_isForward) setPosition(_selectionRegion.getStartOffset());
390+
else setPosition(_selectionRegion.getEndOffset());
387391

388392
int count = 0;
389393
FindResult fr = findNext(false); // find next match in current doc
390394

391-
while (! fr.getWrapped() && fr.getFoundOffset()<=_selectionEnd) {
395+
while (! fr.getWrapped() && fr.getFoundOffset()<=_selectionRegion.getEndOffset()) {
392396
findAction.run(fr);
393397
count++;
394398
fr = findNext(false); // find next match in current doc
@@ -523,7 +527,6 @@ private FindResult _findNextInDocSegment(final OpenDefinitionsDocument doc, fina
523527
final boolean wrapped, final boolean allWrapped) {
524528
// Utilities.show("called _findNextInDocSegment(" + doc.getText() + ",\n" + start + ", " + len + ", " + wrapped + " ...)");
525529
boolean inTestCase = (doc.getFileName().endsWith("Test.java"));
526-
System.out.println("_findNextInDocSegment, inTestCase="+inTestCase+", _ignoreTestCases="+_ignoreTestCases);
527530

528531
if (!_ignoreTestCases || ! inTestCase) {
529532
final int docLen = doc.getLength();; // The length of the segment to be searched
@@ -694,4 +697,4 @@ private boolean _shouldIgnore(int foundOffset, OpenDefinitionsDocument odd) {
694697
return (_matchWholeWord && ! wholeWordFoundAtCurrent(odd, foundOffset)) ||
695698
(_ignoreCommentsAndStrings && odd.isShadowed(foundOffset));
696699
}
697-
}
700+
}

drjava/src/edu/rice/cs/drjava/ui/FindReplacePanel.java

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,11 @@ public void run() {
124124
};
125125

126126
/** The action performed when searching forwards */
127-
private Action _findNextAction = new AbstractAction("Find Next") {
127+
Action _findNextAction = new AbstractAction("Find Next") {
128128
public void actionPerformed(ActionEvent e) { findNext(); }
129129
};
130130

131-
private Action _findPreviousAction = new AbstractAction("Find Previous") {
131+
Action _findPreviousAction = new AbstractAction("Find Previous") {
132132
public void actionPerformed(ActionEvent e) { findPrevious(); }
133133
};
134134

@@ -140,15 +140,15 @@ public void run() {
140140
public void actionPerformed(ActionEvent e) { _doFind(); }
141141
};
142142

143-
private Action _replaceAction = new AbstractAction("Replace") {
143+
Action _replaceAction = new AbstractAction("Replace") {
144144
public void actionPerformed(ActionEvent e) { _replace(); }
145145
};
146146

147-
private Action _replaceFindNextAction = new AbstractAction("Replace/Find Next") {
147+
Action _replaceFindNextAction = new AbstractAction("Replace/Find Next") {
148148
public void actionPerformed(ActionEvent e) { _replaceFindNext(); }
149149
};
150150

151-
private Action _replaceFindPreviousAction = new AbstractAction("Replace/Find Previous") {
151+
Action _replaceFindPreviousAction = new AbstractAction("Replace/Find Previous") {
152152
public void actionPerformed(ActionEvent e) { _replaceFindPrevious(); };
153153
};
154154

@@ -350,18 +350,18 @@ public void itemStateChanged(ItemEvent e) {
350350
if(isSelected) {
351351
_ignoreTestCases.setSelected(false);
352352
_searchAllDocuments.setSelected(false);
353-
_findNextButton.setEnabled(false);
354-
_findPreviousButton.setEnabled(false);
355-
_replaceFindNextButton.setEnabled(false);
356-
_replaceButton.setEnabled(false);
357-
_replaceFindPreviousButton.setEnabled(false);
353+
_findNextAction.setEnabled(false);
354+
_findPreviousAction.setEnabled(false);
355+
_replaceFindNextAction.setEnabled(false);
356+
_replaceAction.setEnabled(false);
357+
_replaceFindPreviousAction.setEnabled(false);
358358
}
359359
else {
360-
_findNextButton.setEnabled(true);
361-
_findPreviousButton.setEnabled(true);
362-
_replaceFindNextButton.setEnabled(true);
363-
_replaceButton.setEnabled(true);
364-
_replaceFindPreviousButton.setEnabled(true);
360+
_findNextAction.setEnabled(true);
361+
_findPreviousAction.setEnabled(true);
362+
_replaceFindNextAction.setEnabled(true);
363+
_replaceAction.setEnabled(true);
364+
_replaceFindPreviousAction.setEnabled(true);
365365
}
366366
DrJava.getConfig().setSetting(OptionConstants.FIND_ONLY_SELECTION, isSelected);
367367
_findField.requestFocusInWindow();
@@ -580,7 +580,7 @@ private void _findAll() {
580580
/** Performs "find all" with the specified options. */
581581
public void findAll(String searchStr, final boolean searchAll, final boolean searchSelectedText, final boolean matchCase,
582582
final boolean wholeWord, final boolean noComments, final boolean noTestCases,
583-
final OpenDefinitionsDocument startDoc, final RegionManager<MovingDocumentRegion> rm, MovingDocumentRegion region,
583+
final OpenDefinitionsDocument startDoc, final RegionManager<MovingDocumentRegion> rm, final MovingDocumentRegion region,
584584
final FindResultsPanel panel) {
585585

586586
_machine.setSearchBackwards(false);
@@ -623,7 +623,7 @@ public void findAll(String searchStr, final boolean searchAll, final boolean sea
623623
/* Accumulate all occurrences of searchStr in results. */
624624
final int count = _machine.processAll(new Runnable1<FindResult>() {
625625
public void run(FindResult fr) { results.add(fr); }
626-
}, region.getStartOffset(), region.getEndOffset());
626+
}, region);
627627

628628
_machine.setDocument(oldDoc);
629629
_machine.setFirstDoc(oldFirstDoc);
@@ -654,14 +654,24 @@ public void findAll(String searchStr, final boolean searchAll, final boolean sea
654654

655655
// EventQueue.invokeLater(new Runnable() {
656656
// public void run() {
657-
if (count > 0) _frame.showFindResultsPanel(panel);
658-
else {
659-
Toolkit.getDefaultToolkit().beep();
660-
panel.freeResources();
661-
}
662-
_frame.setStatusMessage("Found " + count + " occurrence" + ((count == 1) ? "" : "s") + ".");
657+
if (count > 0) _frame.showFindResultsPanel(panel);
658+
else {
659+
Toolkit.getDefaultToolkit().beep();
660+
panel.freeResources();
661+
}
662+
_frame.setStatusMessage("Found " + count + " occurrence" + ((count == 1) ? "" : "s") + ".");
663663
// }
664664
// });
665+
666+
if (searchSelectedText) {
667+
EventQueue.invokeLater(new Runnable() { public void run() {
668+
if (_defPane!=null) {
669+
_defPane.requestFocusInWindow();
670+
_defPane.setSelectionStart(region.getStartOffset());
671+
_defPane.setSelectionEnd(region.getEndOffset());
672+
}
673+
} });
674+
}
665675
}
666676
finally {
667677
_frame.hourglassOff();
@@ -678,8 +688,15 @@ private void _replaceAll() {
678688
_machine.setFindWord(_findField.getText());
679689
_machine.setReplaceWord(_replaceField.getText());
680690
_machine.setSearchBackwards(false);
691+
OpenDefinitionsDocument startDoc = _defPane.getOpenDefDocument();
692+
MovingDocumentRegion region = new MovingDocumentRegion(startDoc,
693+
_defPane.getSelectionStart(),
694+
_defPane.getSelectionEnd(),
695+
startDoc._getLineStartPos(_defPane.getSelectionStart()),
696+
startDoc._getLineEndPos(_defPane.getSelectionEnd()));
697+
_machine.setSelection(region);
681698
_frame.clearStatusMessage();
682-
int count = _machine.replaceAll(_defPane.getSelectionStart(), _defPane.getSelectionEnd());
699+
int count = _machine.replaceAll();
683700
Toolkit.getDefaultToolkit().beep();
684701
_frame.setStatusMessage("Replaced " + count + " occurrence" + ((count == 1) ? "" : "s") + ".");
685702
_replaceAction.setEnabled(false);

drjava/src/edu/rice/cs/drjava/ui/MainFrame.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2985,6 +2985,11 @@ public MainFrame() {
29852985
_showDebugger = _model.getDebugger().isAvailable();
29862986
_findReplace = new FindReplacePanel(this, _model);
29872987

2988+
// add listeners to activate/deactivate the find/replace actions in MainFrame together with
2989+
// those in the Find/Replace panel
2990+
Utilities.enableDisableWith(_findReplace._findNextAction, _findNextAction);
2991+
Utilities.enableDisableWith(_findReplace._findPreviousAction, _findPrevAction);
2992+
29882993
if (_showDebugger) {
29892994
_debugPanel = new DebugPanel(this);
29902995
_breakpointsPanel = new BreakpointsPanel(this, _model.getBreakpointManager());

drjava/src/edu/rice/cs/drjava/ui/config/ConfigFrame.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ private void _setupKeyBindingsPanel(ConfigPanel panel) {
755755
Action a = ksd.getAction();
756756
String desc = (String) a.getValue(Action.SHORT_DESCRIPTION);
757757
if (desc == null || desc.equals("")) desc = ksd.getName();
758-
vksoc = new VectorKeyStrokeOptionComponent((VectorOption<KeyStroke>)ksd.getOption(), ksd.getName(), this, desc);
758+
vksoc = new VectorKeyStrokeOptionComponent(ksd.getOption(), ksd.getName(), this, desc);
759759
if (vksoc != null) _comps.add(vksoc);
760760
}
761761
}

drjava/src/edu/rice/cs/drjava/ui/config/VectorKeyStrokeOptionComponent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public int compareTo(VectorKeyStrokeOptionComponent o) {
110110
return this.getLabelText().compareTo(o.getLabelText());
111111
}
112112

113-
Vector<KeyStroke> getKeyStrokes() { return DrJava.getConfig().getSetting(_option); }
113+
Vector<KeyStroke> getKeyStrokes() { return new Vector<KeyStroke>(_data); }
114114

115115
/** Adds buttons to _buttonPanel */
116116
protected void _addButtons() {

drjava/src/edu/rice/cs/drjava/ui/config/VectorOptionComponent.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ protected void _addValue(T value) {
210210
_tableModel.fireTableRowsInserted(_data.size()-1, _data.size()-1);
211211
_table.getSelectionModel().setSelectionInterval(_data.size()-1,_data.size()-1);
212212
notifyChangeListeners();
213-
notifyChangeListeners();
214213
}
215214

216215
protected void _removeIndex(int i) {

drjava/src/edu/rice/cs/util/swing/Utilities.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
import java.awt.event.*;
4242
import javax.swing.*;
4343
import java.awt.datatransfer.*;
44+
import java.beans.PropertyChangeListener;
45+
import java.beans.PropertyChangeEvent;
4446

4547
import edu.rice.cs.util.UnexpectedException;
4648
import edu.rice.cs.util.StringOps;
@@ -247,4 +249,19 @@ public static void setPopupLoc(Window popup, Component owner) {
247249
popup.setSize(frameRect.getSize());
248250
popup.setLocation(frameRect.getLocation());
249251
}
252+
253+
/** Enables/disables the second action whenever the first action is enabled/disabled.
254+
* @param observable the action that is observed (leads)
255+
* @param observer the action that follows
256+
* @return the PropertyChangeListener used to do the observation */
257+
public static PropertyChangeListener enableDisableWith(Action observable, final Action observer) {
258+
PropertyChangeListener pcl = new PropertyChangeListener() {
259+
@SuppressWarnings("unchecked")
260+
public void propertyChange(PropertyChangeEvent e) {
261+
if (e.getPropertyName().equals("enabled")) { observer.setEnabled((Boolean)e.getNewValue()); }
262+
}
263+
};
264+
observable.addPropertyChangeListener(pcl);
265+
return pcl;
266+
}
250267
}
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
#DrJava configuration file
22
#Fri Jun 13 22:30:19 CDT 2008
3-
key.delete.next = shift DELETE
4-
key.delete.previous = shift BACK_SPACE
5-
recent.files = [/private/tmp/DrJava-test39852.java]
3+
key.delete.next = [shift DELETE]
4+
key.delete.previous = [shift BACK_SPACE]
65
window.x = 240
76
window.y = 50
8-
last.dir = /private/tmp/DrJava-test39882.java
9-
last.interactions.dir = /Users/cork
107
#compile.before.junit = true

0 commit comments

Comments
 (0)