forked from pH-7/Simple-Java-Text-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFind.java
More file actions
187 lines (162 loc) · 5.16 KB
/
Find.java
File metadata and controls
187 lines (162 loc) · 5.16 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* @name Simple Java NotePad
* @package ph.notepad
* @file Find.java
* @author SORIA Pierre-Henry
* @email pierrehs@hotmail.com
* @link http://github.com/pH-7
* @copyright Copyright Pierre-Henry SORIA, All Rights Reserved.
* @license Apache (http://www.apache.org/licenses/LICENSE-2.0)
* @create 2012-05-05
* @update 2012-05-06
*/
package ph.notepad;
import javax.swing.*;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Find extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
int startIndex=0;
JLabel lab1, lab2;
JTextField textF, textR;
JButton findBtn, findNext, replace, replaceAll, cancel;
private JTextArea txt;
public Find(JTextArea text) {
this.txt = text;
lab1 = new JLabel("Find:");
lab2 = new JLabel("Replace:");
textF = new JTextField(30);
textR = new JTextField(30);
findBtn = new JButton("Find");
findNext = new JButton("Find Next");
replace = new JButton("Replace");
replaceAll = new JButton("Replace All");
cancel = new JButton("Cancel");
// Set Layout NULL
setLayout(null);
// Set the width and height of the label
int labWidth = 80;
int labHeight = 20;
// Adding labels
lab1.setBounds(10,10, labWidth, labHeight);
add(lab1);
textF.setBounds(10+labWidth, 10, 120, 20);
add(textF);
lab2.setBounds(10, 10+labHeight+10, labWidth, labHeight);
add(lab2);
textR.setBounds(10+labWidth, 10+labHeight+10, 120, 20);
add(textR);
// Adding buttons
findBtn.setBounds(225, 6, 115, 20);
add(findBtn);
findBtn.addActionListener(this);
findNext.setBounds(225, 28, 115, 20);
add(findNext);
findNext.addActionListener(this);
replace.setBounds(225, 50, 115, 20);
add(replace);
replace.addActionListener(this);
replaceAll.setBounds(225, 72, 115, 20);
add(replaceAll);
replaceAll.addActionListener(this);
cancel.setBounds(225, 94, 115, 20);
add(cancel);
cancel.addActionListener(this);
// Set the width and height of the window
int width = 360;
int height = 160;
// Set size window
setSize(width,height);
// Set window position
Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
setLocation(center.x-width/2, center.y-height/2);
setVisible(true);
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
}
public void find() {
int select_start = txt.getText().indexOf(textF.getText());
if(select_start == -1)
{
startIndex = 0;
JOptionPane.showMessageDialog(null, "Could not find \"" + textF.getText() + "\"!");
return;
}
if(select_start == txt.getText().lastIndexOf(textF.getText()))
{
startIndex = 0;
}
int select_end = select_start + textF.getText().length();
txt.select(select_start, select_end);
}
public void findNext() {
String selection = txt.getSelectedText();
try
{
selection.equals("");
}
catch(NullPointerException e)
{
selection = textF.getText();
try
{
selection.equals("");
}
catch(NullPointerException e2)
{
selection = JOptionPane.showInputDialog("Find:");
textF.setText(selection);
}
}
try
{
int select_start = txt.getText().indexOf(selection, startIndex);
int select_end = select_start+selection.length();
txt.select(select_start, select_end);
startIndex = select_end+1;
if(select_start == txt.getText().lastIndexOf(selection))
{
startIndex = 0;
}
}
catch(NullPointerException e)
{}
}
public void replace() {
try
{
find();
txt.replaceSelection(textR.getText());
}
catch(NullPointerException e)
{
System.out.print("Null Pointer Exception: "+e);
}
}
public void replaceAll() {
txt.setText(txt.getText().replaceAll(textF.getText(), textR.getText()));
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == findBtn)
{
find();
}
else if(e.getSource() == findNext)
{
findNext();
}
else if(e.getSource() == replace)
{
replace();
}
else if(e.getSource() == replaceAll)
{
replaceAll();
}
else if(e.getSource() == cancel)
{
this.setVisible(false);
}
}
}