-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEditorGUI.java
More file actions
389 lines (333 loc) · 14.5 KB
/
EditorGUI.java
File metadata and controls
389 lines (333 loc) · 14.5 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import static java.awt.datatransfer.DataFlavor.stringFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.io.*;
import javax.swing.JOptionPane;
import javax.swing.text.DefaultHighlighter;
public class EditorGUI extends javax.swing.JFrame {
String filename="Untitled";
Clipboard clipboard= getToolkit().getSystemClipboard();
public EditorGUI() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem3 = new javax.swing.JMenuItem();
jScrollPane1 = new javax.swing.JScrollPane();
textArea = new javax.swing.JTextArea();
jMenuBar1 = new javax.swing.JMenuBar();
File = new javax.swing.JMenu();
New = new javax.swing.JMenuItem();
Open = new javax.swing.JMenuItem();
Save = new javax.swing.JMenuItem();
SaveAs = new javax.swing.JMenuItem();
PageSetup = new javax.swing.JMenuItem();
Print = new javax.swing.JMenuItem();
Exit = new javax.swing.JMenuItem();
Edit = new javax.swing.JMenu();
Cut = new javax.swing.JMenuItem();
Copy = new javax.swing.JMenuItem();
Paste = new javax.swing.JMenuItem();
Find = new javax.swing.JMenuItem();
Format = new javax.swing.JMenu();
WordWrap = new javax.swing.JMenuItem();
Font = new javax.swing.JMenuItem();
View = new javax.swing.JMenu();
StatusBar = new javax.swing.JMenuItem();
Help = new javax.swing.JMenu();
About = new javax.swing.JMenuItem();
jMenuItem1.setText("jMenuItem1");
jMenuItem3.setText("jMenuItem3");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
textArea.setColumns(20);
textArea.setRows(5);
jScrollPane1.setViewportView(textArea);
File.setText("File");
New.setText("New");
New.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
NewActionPerformed(evt);
}
});
File.add(New);
Open.setText("Open");
Open.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
OpenActionPerformed(evt);
}
});
File.add(Open);
Save.setText("Save");
Save.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SaveActionPerformed(evt);
}
});
File.add(Save);
SaveAs.setText("Save As...");
SaveAs.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SaveAsActionPerformed(evt);
}
});
File.add(SaveAs);
PageSetup.setText("Page Setup...");
File.add(PageSetup);
Print.setText("Print...");
File.add(Print);
Exit.setText("Exit");
Exit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ExitActionPerformed(evt);
}
});
File.add(Exit);
jMenuBar1.add(File);
Edit.setText("Edit");
Cut.setText("Cut");
Cut.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CutActionPerformed(evt);
}
});
Edit.add(Cut);
Copy.setText("Copy");
Copy.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CopyActionPerformed(evt);
}
});
Edit.add(Copy);
Paste.setText("Paste");
Paste.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
PasteActionPerformed(evt);
}
});
Edit.add(Paste);
Find.setText("Find..");
Find.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
FindActionPerformed(evt);
}
});
Edit.add(Find);
jMenuBar1.add(Edit);
Format.setText("Format");
WordWrap.setText("Word Wrap");
Format.add(WordWrap);
Font.setText("Font...");
Format.add(Font);
jMenuBar1.add(Format);
View.setText("View");
StatusBar.setText("Status Bar");
View.add(StatusBar);
jMenuBar1.add(View);
Help.setText("Help");
About.setText("About Notepad");
Help.add(About);
jMenuBar1.add(Help);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 700, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 679, Short.MAX_VALUE)
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void PasteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PasteActionPerformed
// TODO add your handling code here:
try {
Transferable paste=clipboard.getContents(EditorGUI.this);
String sel=(String)paste.getTransferData(DataFlavor.stringFlavor);
textArea.replaceRange(sel, textArea.getSelectionStart(), textArea.getSelectionEnd());
} catch (Exception e) {
}System.out.println("Didn't Work");
}//GEN-LAST:event_PasteActionPerformed
private void NewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NewActionPerformed
// TODO add your handling code here:
String data=textArea.getText();//read contents of text area into 'data'
String newline = System.getProperty("line.separator");
boolean hasNewline = data.contains(newline);
if ((data.trim().length() > 0) && (!hasNewline)){
int dialogButton = JOptionPane.YES_NO_OPTION;
int dialogResult = JOptionPane.showConfirmDialog (null, "Would You Like to Save your Previous Note First?","Warning",dialogButton);
if(dialogResult == JOptionPane.YES_OPTION){ //The ISSUE is here
SaveActionPerformed(evt);
}
textArea.setText("");
setTitle("untitled");
}
}//GEN-LAST:event_NewActionPerformed
private void OpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OpenActionPerformed
// TODO add your handling code here:
String data=textArea.getText();//read contents of text area into 'data'
String newline = System.getProperty("line.separator");
boolean hasNewline = data.contains(newline);
if ((data.trim().length() > 0) && (!hasNewline)){
int dialogButton = JOptionPane.YES_NO_OPTION;
int dialogResult = JOptionPane.showConfirmDialog (null, "Would You Like to Save your Previous Note First?","Warning",dialogButton);
if(dialogResult == JOptionPane.YES_OPTION){ //The ISSUE is here
SaveActionPerformed(evt);
}
}
FileDialog fileDialog=new FileDialog(EditorGUI.this,"open File",FileDialog.LOAD);
fileDialog.setVisible(true);
if(fileDialog.getFile()!=null){
filename = fileDialog.getDirectory()+fileDialog.getFile();
setTitle(filename);
}
try{
BufferedReader reader=new BufferedReader(new FileReader(filename));
StringBuilder sb=new StringBuilder();
String line=null;
while((line = reader.readLine())!=null)
{
sb.append(line+"\n");
textArea.setText(sb.toString());
}
reader.close();
}
catch(Exception e)
{
System.out.println("File not Found");
}
}//GEN-LAST:event_OpenActionPerformed
private void SaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveActionPerformed
// TODO add your handling code here:
FileDialog fileDialog=new FileDialog(EditorGUI.this,"Save File",FileDialog.SAVE);
fileDialog.setVisible(true);
if(fileDialog.getFile() !=null){
filename=fileDialog.getDirectory()+fileDialog.getFile();
setTitle(filename);
}
try {
FileWriter fileWriter=new FileWriter(filename);
fileWriter.write(textArea.getText());
setTitle(filename);
fileWriter.close();
} catch (IOException e) {
System.out.println("File not Found");
}
{
}
}//GEN-LAST:event_SaveActionPerformed
private void ExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ExitActionPerformed
// TODO add your handling code here:
System.exit(0);
}//GEN-LAST:event_ExitActionPerformed
private void CutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CutActionPerformed
// TODO add your handling code here:
String cut=textArea.getSelectedText();
StringSelection cutSelection=new StringSelection(cut);
clipboard.setContents(cutSelection,cutSelection);
textArea.replaceRange("",textArea.getSelectionStart(),textArea.getSelectionEnd());
}//GEN-LAST:event_CutActionPerformed
private void CopyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CopyActionPerformed
// TODO add your handling code here:
String copy=textArea.getSelectedText();
StringSelection copysel=new StringSelection(copy);
clipboard.setContents(copysel, copysel);
}//GEN-LAST:event_CopyActionPerformed
private void FindActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FindActionPerformed
// TODO add your handling code here:
class myHighlight extends DefaultHighlighter.DefaultHighlightPainter{
public myHighlight(Color color) {
super(color);
}
}
DefaultHighlighter.HighlightPainter highlighter=new myHighlight(Color.yellow);
/*public void searchText(JTextComponent textComp,String textString)
{
try
}*/
}//GEN-LAST:event_FindActionPerformed
private void SaveAsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveAsActionPerformed
// TODO add your handling code here:name
if(filename!="untitled")
{
try {
FileWriter fileWriter=new FileWriter(filename);
fileWriter.write(textArea.getText());
setTitle(filename);
fileWriter.close();
} catch (IOException e) {
System.out.println("File not Found");
}
}
else
{
SaveActionPerformed(evt);
}
}//GEN-LAST:event_SaveAsActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(EditorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(EditorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(EditorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(EditorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new EditorGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JMenuItem About;
private javax.swing.JMenuItem Copy;
private javax.swing.JMenuItem Cut;
private javax.swing.JMenu Edit;
private javax.swing.JMenuItem Exit;
private javax.swing.JMenu File;
private javax.swing.JMenuItem Find;
private javax.swing.JMenuItem Font;
private javax.swing.JMenu Format;
private javax.swing.JMenu Help;
private javax.swing.JMenuItem New;
private javax.swing.JMenuItem Open;
private javax.swing.JMenuItem PageSetup;
private javax.swing.JMenuItem Paste;
private javax.swing.JMenuItem Print;
private javax.swing.JMenuItem Save;
private javax.swing.JMenuItem SaveAs;
private javax.swing.JMenuItem StatusBar;
private javax.swing.JMenu View;
private javax.swing.JMenuItem WordWrap;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea textArea;
// End of variables declaration//GEN-END:variables
}