-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjavaEditor.java
More file actions
355 lines (322 loc) · 12.8 KB
/
javaEditor.java
File metadata and controls
355 lines (322 loc) · 12.8 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;
import java.io.*;
import java.awt.event.*;
import java.beans.PropertyChangeListener;
import javax.swing.plaf.metal.*;
import javax.swing.text.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;
import javax.swing.text.Highlighter.HighlightPainter;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.ToolProvider;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.net.MalformedURLException;
import java.nio.file.Path;
import java.nio.file.Paths;
class editor extends JFrame implements ActionListener
{
JFrame frame;
JTextArea textArea;
JScrollPane scrollPane;
editor()
{
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
JPanel panel3=new JPanel();
JButton findButton=new JButton("Find");
JLabel findLabel=new JLabel("Enter text to Search: ");
JTextField findTextField=new JTextField(20);
JLabel replaceLabel=new JLabel("Enter text to Replace with searched word: ");
JTextField replacTextField=new JTextField(20);
JButton replaceButton=new JButton("Replace");
JLabel compileLabel=new JLabel("Enter Class(which contains main) name ");
JTextField classTextField=new JTextField(20);
JButton compileButton=new JButton("Compile");
panel1.add(findLabel);
panel1.add(findTextField);
panel1.add(findButton);
panel2.add(replaceLabel);
panel2.add(replacTextField);
panel2.add(replaceButton);
panel3.add(compileLabel);
panel3.add(classTextField);
panel3.add(compileButton);
textArea=new JTextArea(35,67);
textArea.getBorder();
JMenuBar menuBar=new JMenuBar();
scrollPane=new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JMenu fileMenu=new JMenu("Files");
JMenuItem File_item_1=new JMenuItem("New");
JMenuItem File_item_2=new JMenuItem("Open");
JMenuItem File_item_3=new JMenuItem("Save");
File_item_1.addActionListener(this);
File_item_2.addActionListener(this);
File_item_3.addActionListener(this);
fileMenu.add(File_item_1);
fileMenu.add(File_item_2);
fileMenu.add(File_item_3);
menuBar.add(fileMenu);
JMenu toolMenu=new JMenu("Tools");
JMenuItem toolMenu_Item_1=new JMenuItem("Cut");
JMenuItem toolMenu_Item_2=new JMenuItem("Copy");
JMenuItem toolMenu_Item_3=new JMenuItem("Paste");
toolMenu_Item_1.addActionListener(this);
toolMenu_Item_2.addActionListener(this);
toolMenu_Item_3.addActionListener(this);
findButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e)
{
textArea.getHighlighter().removeAllHighlights();
//System.out.println(findTextField.getText());
String pat=findTextField.getText().toString();
String txt=textArea.getText().toString();
Highlighter highlighter=textArea.getHighlighter();
HighlightPainter painter=new DefaultHighlighter.DefaultHighlightPainter(Color.RED);
// Rabin-Karp's Algorithm of String Matching.
int d=256;
int M = pat.length();
int N = txt.length();
int i, j;
int q=101;
int p = 0;
int t = 0;
int h = 1;
for (i = 0; i < M-1; i++)
h = (h*d)%q;
for (i = 0; i < M; i++)
{
p = (d*p + pat.charAt(i))%q;
t = (d*t + txt.charAt(i))%q;
}
for (i = 0; i <= N - M; i++)
{
if ( p == t )
{
for (j = 0; j < M; j++)
{
if (txt.charAt(i+j) != pat.charAt(j))
{
break;
}
}
if (j == M)
{
try
{
highlighter.addHighlight(i, i+pat.length(), painter);
}
catch(Exception exception)
{
}
}
}
if ( i < N-M )
{
t = (d*(t - txt.charAt(i)*h) + txt.charAt(i+M))%q;
if (t < 0)
{
t = (t + q);
}
}
}
}
});
replaceButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e)
{
String replaceString=replacTextField.getText().toString();
String onTextArea=textArea.getText().toString();
String newText=onTextArea.replaceAll(findTextField.getText(),replaceString);
textArea.setText(newText);
}
});
// Code Compilation
compileButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
String getClassName=classTextField.getText();
//JOptionPane.showMessageDialog(frame,getClassName);
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
StringWriter writer = new StringWriter();
PrintWriter out = new PrintWriter(writer);
String[] line=textArea.getText().split("\\n");
// for(int l=0;l<line.length;l++)
// {
// line[l]=line[l].replaceAll("\"", "\\\\\"");
// System.out.println(line[l]);
// }
for(int g=0;g<line.length;g++)
{
out.println(line[g]);
}
out.close();
JavaFileObject file = new JavaSourceFromString(getClassName, writer.toString());
Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(file);
CompilationTask task = compiler.getTask(null, null, diagnostics, null, null, compilationUnits);
boolean success = task.call();
String check="";
for (Diagnostic diagnostic : diagnostics.getDiagnostics())
{
System.out.println(diagnostic.getCode());
check=check+diagnostic.getCode()+"\n";
System.out.println(diagnostic.getKind());
check=check + diagnostic.getKind()+"\n";
// System.out.println(diagnostic.getPosition());
// check=check+diagnostic.getPosition()+"\n";
// System.out.println(diagnostic.getStartPosition());
// check=check+diagnostic.getStartPosition()+"\n";
// System.out.println(diagnostic.getEndPosition());
// check=check+diagnostic.getEndPosition()+"\n";
System.out.println(diagnostic.getSource());
check=check+diagnostic.getSource()+"\n";
System.out.println(diagnostic.getMessage(null));
check=check+diagnostic.getMessage(null)+"\n";
}
System.out.println("Success: " + success);
if (success)
{
try
{
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { new File("").toURI().toURL() });
getContentPane().setBackground(Color.BLACK);
JOptionPane.showMessageDialog(frame,"Compiled Succesfully");
}
catch (MalformedURLException es)
{
System.err.println("Url not found" + es);
}
}
else
{
getContentPane().setBackground(Color.LIGHT_GRAY);
JOptionPane.showMessageDialog(frame,check);
}
}
});
toolMenu.add(toolMenu_Item_1);
toolMenu.add(toolMenu_Item_2);
toolMenu.add(toolMenu_Item_3);
menuBar.add(toolMenu);
add(panel1);
add(panel2);
add(panel3);
setJMenuBar(menuBar);
setTitle("Text Editor for Java");
setLayout(new FlowLayout());
add(scrollPane,BorderLayout.EAST);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setBackground(Color.BLACK);
setSize(900,800);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String string=e.getActionCommand();
if( string.equals("Cut"))
{
textArea.cut();
}
else if(string.equals("Copy"))
{
textArea.copy();
}
else if(string.equals("Paste"))
{
textArea.paste();
}
else if(string.equals("New"))
{
textArea.setText("");
}
else if(string.equals("Open"))
{
JFileChooser fileChooser=new JFileChooser("f:");
int r=fileChooser.showOpenDialog(null);
if(r==JFileChooser.APPROVE_OPTION)
{
File file=new File(fileChooser.getSelectedFile().getAbsolutePath());
try
{
String s1="",sl="";
FileReader fileReader=new FileReader(file);
BufferedReader br=new BufferedReader(fileReader);
sl=br.readLine();
while( (s1=br.readLine()) !=null)
{
sl=sl + "\n" + s1;
}
textArea.setText(sl);
} catch (Exception evt)
{
JOptionPane.showMessageDialog(frame,evt.getMessage());
}
}
else
{
JOptionPane.showMessageDialog(frame, "Operation cancelled !");
}
}
else if(string.equals("Save"))
{
JFileChooser fileChooser=new JFileChooser("f:");
int r=fileChooser.showSaveDialog(null);
if(r== JFileChooser.APPROVE_OPTION)
{
File file=new File(fileChooser.getSelectedFile().getAbsolutePath());
try
{
FileWriter wr=new FileWriter(file,false);
BufferedWriter w=new BufferedWriter(wr);
w.write(textArea.getText());
w.flush();
w.close();
} catch (Exception evt)
{
JOptionPane.showMessageDialog(frame, evt.getMessage());
}
}
else
{
JOptionPane.showMessageDialog(frame, "Operation cancelled !");
}
}
}
}
public class javaEditor
{
public static void main(String[] args)
{
editor e=new editor();
}
}
class JavaSourceFromString extends SimpleJavaFileObject {
final String code;
JavaSourceFromString(String name, String code) {
super(URI.create("string:///" + name.replace('.','/') + Kind.SOURCE.extension),Kind.SOURCE);
this.code = code;
}
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return code;
}
}