-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindAndReplace.java
More file actions
151 lines (132 loc) · 3.23 KB
/
Copy pathFindAndReplace.java
File metadata and controls
151 lines (132 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class FindAndReplace extends JDialog implements ActionListener{
boolean foundOne,isReplace;
JTextField searchText,replaceText;
JCheckBox cbCase,cbWhole;
JRadioButton up,down;
JLabel statusInfo;
JFrame owner;
JPanel north,center,south;
public FindAndReplace(JFrame owner,boolean isReplace){
super(owner,true); // modality is set to true
this.isReplace = isReplace;
north = new JPanel();
center = new JPanel();
south = new JPanel();
if(isReplace){
setTitle("Find And Replace");
}
else{
setTitle("Find");
setFindPanel(north);
}
}
private void setFindPanel(JPanel panel){
final JButton NEXT = new JButton("Find Next");
NEXT.setEnabled(false);
NEXT.addActionListener(this);
searchText = new JTextField(20);
searchText.addActionListener(this);
searchText.addKeyListener(new KeyAdapter(){
@Override
public void keyReleased(KeyEvent e){
boolean state = (searchText.getDocument().getLength()>0);
NEXT.setEnabled(true);
foundOne = false;
}
});
if(searchText.getText().length()>0){
NEXT.setEnabled(true);
}
panel.add(new JLabel("Find Word: "));
panel.add(searchText);
panel.add(NEXT);
}
private void setFindAndReplacePanel(JPanel panel){
}
private int search(String text,String word,int caret){
boolean found = false;
int all = text.length();
int check = word.length();
if(isSearchDown()){
int add = 0;
for(int i=caret+1;i<(all-check);i++){
String temp = text.substring(i,i+check);
if(temp.equals(word)){
if(wholeWordIsSelected()){
if(checkForWholeWord(check,text,add,caret)){
caret = i;
found = true;
break;
}
}
else{ // not whole word found
caret = i;
found = true;
break;
}
}
}
}
else{
int add = caret;
for(int i=caret-1;i>=check;i--){
add--;
String temp = text.substring(i-check,i);
if(temp.equals(word)){
if(wholeWordIsSelected()){
if(checkForWholeWord(check,text,add,caret)){
caret = i;
found = true;
break;
}
}
else{ // not whole word found
caret = i;
found = true;
break;
}
}
}
}
Main.getArea().setCaretPosition(0);
if(found){
Main.getArea().requestFocus();
if(isSearchDown()){
Main.getArea().select(caret, caret+check);
}
else{
Main.getArea().select(caret-check, caret);
}
foundOne = true;
}
else{
}
return -1;
}
private boolean isSearchDown(){
return down.isSelected();
}
private boolean isSearchUp(){
return up.isSelected();
}
private boolean wholeWordIsSelected(){
return cbWhole.isSelected();
}
private boolean checkForWholeWord(int check,String text,int add,int caret){
int offsetLeft = (caret+add)-1;
int offsetRight = (caret+add)+ check;
if(offsetLeft<0 || offsetRight>text.length()){
return true;
}
return ((!Character.isLetterOrDigit(text.charAt(offsetLeft))) && (!(Character.isLetterOrDigit(text.charAt(offsetRight)))));
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}