-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimpleTextEditor.java
More file actions
777 lines (735 loc) · 21.3 KB
/
SimpleTextEditor.java
File metadata and controls
777 lines (735 loc) · 21.3 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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
/**
*
* @author Aman
*/
import java.lang.*;
import java.awt.*;
import java.awt.Component;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import javax.swing.undo.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.util.Properties;
import java.lang.System;
import java.io.IOException;
import java.lang.Runtime;
import java.lang.Process;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import javax.swing.border.*;
/** SimpleTextEditor is a very basic text editor application which supports
* File Open, File Save, and the basic text editor functions for cut, copy
* and paste and interfaces with the system clipboard.
*/
// start of class
public class SimpleTextEditor extends Frame implements ActionListener,KeyListener
{
// variables used
JFrame frame;
public JTextPane pane;
TextArea ta = new TextArea(10,60);
int i=0;
TextField t1 = new TextField();
JTextArea editor;
String unif;
int flag = 0;
int flag2;
int j = 0;
String msgkey = "";
String[] iconFiles = {"new.gif","open.gif","save.gif","cut.gif","copy.gif","paste.gif","compile.gif","execute.gif"};
String[] buttonLables = {"New","Open","Save","Cut","Copy","Paste","Compile","Execute"};
ImageIcon[] icons= new ImageIcon[iconFiles.length];
JButton[] buttons = new JButton[buttonLables.length];
String[ ] fileItems = new String[ ] { "New", "Open", "Save","Print","Exit" };
String[ ] editItems = new String[ ] { "Cut", "Copy", "Paste" };
char[ ] fileShortcuts = { 'N','O','S','P','X' };
char[ ] editShortcuts = {'X','C','V' };
ColorDialog cd;
Color currentColor = new Color(255,255,255);
FontDialog fd;
Font currentFont=new Font("Monospaced",Font.PLAIN,12);
//clipboard 8*/
Clipboard clipboard;
// clipboardstring is the Transferrable object used to store
// String text data for clear, cut, copy, and paste operations
StringSelection clipboardstring;
boolean debug = false; //set to true for debugging messages
Font editorfont = new Font("Monospaced",Font.PLAIN,12);
int tabsize = 5;
public void init( )
{
SecurityManager securitymanager = System.getSecurityManager();
if (securitymanager != null)
{
try{
securitymanager.checkSystemClipboardAccess();
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
if (debug) System.out.println("Using system clipboard");
}
catch (SecurityException se)
{
clipboard = new Clipboard("SimpleTextEditor");
System.out.println("SystemClipboardAccess is denied.");
System.out.println("SecurityException: " + se.getMessage());
if (debug) System.out.println("Using application clipboard");
}
}
else{
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
if (debug) System.out.println("Using system clipboard");
}
frame = new JFrame("SimpleTextEditor");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//button controls
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
JMenu formatMenu = new JMenu("Format");
JMenu compileMenu = new JMenu("Compile");
JMenu helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(formatMenu);
menuBar.add(compileMenu);
JMenuItem formatFont=new JMenuItem("Font");
JMenuItem formatColor=new JMenuItem("Color");
//compile menu item
JMenuItem compileCompile= new JMenuItem("Compile");
JMenuItem compileExecute= new JMenuItem("Execute");
formatMenu.add(formatFont);
formatMenu.addSeparator();
formatMenu.add(formatColor);
//add complie menuitem to compile item
compileMenu.add(compileCompile);
compileMenu.addSeparator();
compileMenu.add(compileExecute);
//help action
compileCompile.addActionListener(this);
compileExecute.addActionListener(this);
formatColor.addActionListener(this);
formatFont.addActionListener(this);
for (int i=0; i < fileItems.length; i++) {
JMenuItem item = new JMenuItem(fileItems[i]);
item.setAccelerator(KeyStroke.getKeyStroke(fileShortcuts[i],Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(), false));
item.addActionListener(this);
fileMenu.add(item);
}
// Assemble the File menus with keyboard accelerators.
for (int i=0; i < editItems.length; i++) {
JMenuItem item = new JMenuItem(editItems[i]);
item.setAccelerator(KeyStroke.getKeyStroke(editShortcuts[i],Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(), false));
item.addActionListener(this);
editMenu.add(item);
}
frame.getContentPane().add(menuBar,"North");
for(int i=0;i<buttonLables.length;++i)
{
icons[i] = new ImageIcon(iconFiles[i]);
buttons[i] = new JButton(icons[i]);
buttons[i].setToolTipText(buttonLables[i]);
buttons[i].addActionListener(this);
menuBar.add(buttons[i]);
}
//editor
editor = new JTextArea(new PlainDocument(),"",60,60); //initializes a JTextArea with width 60& height 30
editor.setFont(editorfont);
editor.setTabSize(tabsize);
ta.setEditable(false);
frame.getContentPane().add(ta,"South");
JScrollPane scrollpane = new JScrollPane(editor);
frame.getContentPane().add(scrollpane,"Center");
frame.pack();
frame.show();
addKeyListener(this);
requestFocus();
}
public void keyPressed(KeyEvent ke)
{
int key = ke.getKeyCode();
switch(key)
{
case KeyEvent.VK_ENTER:
msgkey = "Enter";
break;
}
if (msgkey.equals("Enter"))
{
j++;
System.out.println(j);
}
}
public void keyReleased(KeyEvent ke)
{
}
public void keyTyped(KeyEvent ke)
{
msgkey += ke.getKeyChar();
System.out.println(msgkey);
}
public void actionPerformed(ActionEvent actionevent)
{
String actioncommand = actionevent.getActionCommand();
if (actioncommand.equals(fileItems[0]) || actionevent.getSource() == buttons[0])
{
editor.setText("");
ta.setText("");
}
else if(actioncommand.equals(fileItems[1])||actionevent.getSource() == buttons[1])
{
flag2 = 1;
File f = getAFileToOpen();
writeFileToEditor(f);
}
else
if (actioncommand.equals(fileItems[2]) || actionevent.getSource() == buttons[2])
{
flag2 = 0;
String s = editor.getText();
File f = getAFileForSave();
writeStringToFile(f,s);
unif=f.getName();
showMessage("Contents of the Text Editor saved to file " + f.getName());
}
else if (actioncommand.compareTo(fileItems[3]) == 0)
{
print();
}
else if(actioncommand.compareTo(fileItems[4]) == 0)
{
this.setVisible(false);
Main mm=new Main();
mm.show();
//System.exit(0);
}
else if (actioncommand.compareTo("Clear") == 0)
{
clear();
}
else
if (actioncommand.equals(editItems[0]) || actionevent.getSource() == buttons[3])
{
cut();
}
else
if (actioncommand.equals(editItems[1]) || actionevent.getSource() == buttons[4])
{
copy();
}
else
if (actioncommand.equals(editItems[2]) || actionevent.getSource() == buttons[5])
{
paste();
}
else
if (actioncommand.compareTo("Font") == 0)
{
fd=new FontDialog(SimpleTextEditor.this,currentFont,new FontSelectHandler());
fd.show();
}
else
if (actioncommand.compareTo("Color") == 0)
{
cd=new ColorDialog(SimpleTextEditor.this,currentColor,new ColorSelectHandler());
cd.show();
}
else if (actioncommand.compareTo("Compile") == 0 || actionevent.getSource() == buttons[6])
{
if (flag2==1) showMessage("Please Save The File First");
else
{
System.out.println("name : -" + unif);
try
{
Runtime r = Runtime.getRuntime();
Process p = r.exec("javac "+ unif );
ta.setText("");
BufferedReader kbdInput = new BufferedReader(new InputStreamReader(p.getErrorStream()));
int line;
while((line = kbdInput.read())!=-1)
{
StringBuffer sb= new StringBuffer("");
sb.append((char)line);
String ss= sb.toString();
ta.append(ss);
}
System.out.println(ta.getText());
flag = 1;
}
catch(Exception ioe)
{
showMessage("Error in Compiling");
System.out.println("Error");
}
}
}
else
if (actioncommand.compareTo("Execute") == 0 || actionevent.getSource() == buttons[7])
{
if(flag==1)
{
StringBuffer sb = new StringBuffer(unif);
StringBuffer s1= sb.delete(sb.length()-5, sb.length());
System.out.println("name : -" + s1);
try
{
Runtime r = Runtime.getRuntime();
Process p = r.exec("java " + s1);
BufferedReader kbdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while((line = kbdInput.readLine())!=null)
{
StringBuffer sb1= new StringBuffer("");
sb1.append(line);
String ss= sb1.toString();
ta.append(ss+"\n");
}
System.out.println(ta.getText());
System.out.println(line);
}
catch(IOException ioe)
{
showMessage("Error in Execution");
System.out.println("Error");
}
}
else
{
showMessage("Please Compile First");
}
}
}
/** Conveniently displays a message to the user and waits for
* confirmation.
*/
public void showMessage(String s)
{
JOptionPane.showMessageDialog(frame,s);
}
/** Conveniently displays an exception error message to the user
* and waits for confirmation.
*/
public void showExceptionErrorMessage(String s)
{
JOptionPane.showMessageDialog(frame,s,s,JOptionPane.ERROR_MESSAGE);
}
class FontSelectHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
currentFont=fd.getFont();
fd.dispose();
editor.setFont(currentFont);
}
}
class FontDialog extends Dialog
{
String fontName;
int fontStyle;
int fontSize;
String fontNames[];
String styleNames[]={"Plain","Bold","Italic","Bold Italic"};
String sizeNames[]={"10","12","14","18","24","36","72"};
int styles[]={Font.PLAIN,Font.BOLD,Font.ITALIC,Font.BOLD+Font.ITALIC};
int sizes[]={10,12,14,18,24,36,72};
MyList fontList;
MyList styleList=new MyList(5,false,styleNames);
MyList sizeList = new MyList(5,false,sizeNames);
Toolkit toolkit;
Font newFont;
boolean fontChanged;
ActionListener ah;
public FontDialog(Frame parent,Font currentFont,ActionListener ah)
{
super(parent,"Select a font:",true);
toolkit=parent.getToolkit();
newFont=currentFont;
setupFonts();
setupPanels();
setBackground(Color.lightGray);
setForeground(Color.black);
this.ah=ah;
pack();
addWindowListener(new WindowEventHandler());
}
void setupFonts()
{
fontName=newFont.getName();
fontStyle=newFont.getStyle();
fontSize=newFont.getSize();
fontNames=toolkit.getFontList();
fontList=new MyList(5,false,fontNames);
}
void setupPanels()
{
Panel mainPanel=new Panel();
mainPanel.setLayout(new GridLayout(1,3));
Panel fontPanel = new Panel();
fontPanel.setLayout(new BorderLayout());
Label fontLabel=new Label("Font:");
fontPanel.add("North",fontLabel);
fontPanel.add("Center",fontList);
Panel stylePanel=new Panel();
stylePanel=new Panel();
stylePanel.setLayout(new BorderLayout());
Label styleLabel=new Label("Style:");
stylePanel.add("North",styleLabel);
stylePanel.add("Center",styleList);
Panel sizePanel=new Panel();
sizePanel.setLayout(new BorderLayout());
Label sizeLabel=new Label("Size:");
sizePanel.add("North",sizeLabel);
sizePanel.add("Center",sizeList);
mainPanel.add(fontPanel);
mainPanel.add(stylePanel);
mainPanel.add(sizePanel);
Font plainFont=new Font("Helvetica",Font.PLAIN,12);
Font boldFont=new Font("Helvetica",Font.BOLD,12);
mainPanel.setFont(plainFont);
fontLabel.setFont(boldFont);
styleLabel.setFont(boldFont);
sizeLabel.setFont(boldFont);
Panel buttonPanel=new Panel();
buttonPanel.setLayout(new FlowLayout());
Button selectButton=new Button("Select");
Button cancelButton = new Button("Cancel");
ButtonHandler bh=new ButtonHandler();
selectButton.addActionListener(bh);
cancelButton.addActionListener(bh);
buttonPanel.add(selectButton);
buttonPanel.add(cancelButton);
buttonPanel.setFont(boldFont);
add("Center",mainPanel);
add("South",buttonPanel);
}
public boolean isChanged()
{
return fontChanged;
}
public Font getFont()
{
return newFont;
}
void updatenewFont()
{
if(fontList.getSelectedIndex()!=-1)
fontName=fontList.getSelectedItem();
if(styleList.getSelectedIndex()!=-1)
fontStyle=styles[styleList.getSelectedIndex()];
if(sizeList.getSelectedIndex()!=-1)
fontSize=sizes[sizeList.getSelectedIndex()];
newFont=new Font(fontName,fontStyle,fontSize);
fontChanged=true;
}
class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();
if("Select".equals(s))
{
updatenewFont();
ah.actionPerformed(new ActionEvent(FontDialog.this,ActionEvent.ACTION_PERFORMED,"Select"));
FontDialog.this.setVisible(false);
}
else if("Cancel".equals(s))
{
FontDialog.this.dispose();
}
}
}
class WindowEventHandler extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
FontDialog.this.dispose();
}
}
}
class MyList extends List
{
public MyList(int rows,boolean multiple,String labels[])
{
super(rows,multiple);
int length=labels.length;
for(int i=0;i<length;++i)
{
try
{
addItem(labels[i]);
}
catch (NullPointerException ex)
{
addItem("");
}
}
}
}
class ColorSelectHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
currentColor=cd.getColor();
cd.dispose();
editor.setForeground(currentColor);
}
}
class ColorDialog extends Dialog
{
Color colors[] = {Color.black,Color.blue,Color.cyan,Color.darkGray,Color.gray,
Color.green,Color.lightGray,Color.magenta,Color.orange,Color.pink,Color.red,
Color.white,Color.yellow};
String colorNames[] = {"black","blue","cyan","darkGray","gray","green",
"lightGray","magenta","orange","pink","red",
"white","yellow"};
MyList colorList = new MyList(5,false,colorNames);
Color newColor;
boolean colorChanged;
ActionListener ah;
public ColorDialog(Frame parent,Color currentColor,ActionListener ah)
{
super(parent,"Select a color:",true);
setupPanels();
setBackground(Color.lightGray);
setForeground(Color.black);
this.ah=ah;
pack();
addWindowListener(new WindowEventHandler());
}
void setupPanels()
{
Panel colorPanel = new Panel();
colorPanel.setLayout(new BorderLayout());
Label colorLabel = new Label("Color:");
colorPanel.add("North",colorLabel);
colorPanel.add("Center",colorList);
Font plainFont = new Font("Helvetica",Font.PLAIN,12);
Font boldFont = new Font("Helvetica",Font.BOLD,12);
colorLabel.setFont(boldFont);
colorList.setFont(plainFont);
Panel buttonPanel = new Panel();
buttonPanel.setLayout(new FlowLayout());
Button selectButton = new Button("Select");
Button cancelButton = new Button("Cancel");
ButtonHandler bh = new ButtonHandler();
selectButton.addActionListener(bh);
cancelButton.addActionListener(bh);
buttonPanel.add(selectButton);
buttonPanel.add(cancelButton);
buttonPanel.setFont(boldFont);
add("Center",colorPanel);
add("South",buttonPanel);
}
public boolean isChanged()
{
return colorChanged;
}
public Color getColor()
{
return newColor;
}
class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if("Select".equals(s))
{
if(colorList.getSelectedIndex() != -1)
newColor = colors[colorList.getSelectedIndex()];
colorChanged = true;
ah.actionPerformed(new ActionEvent(ColorDialog.this,
ActionEvent.ACTION_PERFORMED,"Select"));
ColorDialog.this.setVisible(false);
}else if("Cancel".equals(s))
{
ColorDialog.this.dispose();
}
}
}
class WindowEventHandler extends WindowAdapter
{
public void windowClosing(WindowEvent e){
ColorDialog.this.dispose();
}
}
}
public File getAFileForSave()
{
File file = null;
File currentdirectory = new File(".");
JFileChooser filechooser = new JFileChooser(currentdirectory);
int replycode = filechooser.showSaveDialog(frame);
if (replycode == JFileChooser.APPROVE_OPTION)
{
file = filechooser.getSelectedFile();
}
return file;
}
/* public File getAFileForSaveas()
{
File file = null;
File currentdirectory = new File(".");
JFileChooser filechooser = new JFileChooser(currentdirectory);
int replycode = filechooser.showSaveDialog(frame);
if (replycode == JFileChooser.APPROVE_OPTION)
{
file = filechooser.getSelectedFile();
}
return file;
}
*/
public void writeStringToFile(File file, String s)
{
try{
FileWriter filewriter = new FileWriter(file);
StringReader stringreader = new StringReader(s);
BufferedReader bufferedreader = new BufferedReader(stringreader);
String lineread = "";
while ((lineread = bufferedreader.readLine()) != null){
filewriter.write(lineread + "\r\n");
}
filewriter.close();
}catch (FileNotFoundException fnfe)
{
System.err.println("FileNotFoundException: " + fnfe.getMessage());
showExceptionErrorMessage("FileNotFoundException: " + fnfe.getMessage());
}catch (IOException ioe)
{
System.err.println("IOException: " + ioe.getMessage());
showExceptionErrorMessage("IOException: " + ioe.getMessage());
}
}
public void writeFileToEditor(File file)
{
try{
FileReader filereader = new FileReader(file);
BufferedReader bufferedreader = new BufferedReader(filereader);
String lineread = "";
while ((lineread = bufferedreader.readLine()) != null)
{
editor.append(lineread + "\n");
}
filereader.close();
}catch (FileNotFoundException fnfe)
{
System.err.println("FileNotFoundException: " + fnfe.getMessage());
showExceptionErrorMessage("FileNotFoundException: " + fnfe.getMessage());
}catch (IOException ioe)
{
System.err.println("IOException: " + ioe.getMessage());
showExceptionErrorMessage("IOException: " + ioe.getMessage());
}
}
void print()
{
Toolkit toolkit;
toolkit=getToolkit();
String name="Test print job";
Properties properties=new Properties();
PrintJob pj=toolkit.getPrintJob(SimpleTextEditor.this, name,properties);
if(pj==null) showMessage("Print Cancelled");
else
{
String output="Name: "+name+"\nProperties: "+properties.toString();
Dimension pageDim=pj.getPageDimension();
int resolution=pj.getPageResolution();
boolean lastPageFirst=pj.lastPageFirst();
output+="\nPage dimension (in pixels):";
output+="\n height: "+String.valueOf(pageDim.height);
output+="\n width: "+String.valueOf(pageDim.width);
output+="\nResolution (pixels/inch): "+String.valueOf(resolution);
output+="\nLast Page First: "+String.valueOf(lastPageFirst);
//editor.setText(output);
Graphics g = pj.getGraphics();
g.dispose();
pj.end();
}
}
/** clear() puts all editor text into the clipboardstring Transferrable
* object and into the clipboard in case the user wants to paste it
* back later in, then clears the editor. Text is also placed in the
* system clipboard so it can be pasted into another application.
*/
private void clear()
{
String selectedtext = editor.getText();
if (selectedtext != null)
{
clipboardstring = new StringSelection(selectedtext);
clipboard.setContents(clipboardstring,clipboardstring);
}
editor.setText("");
}
/** cut deletes selected text and stores the text into the clipboardstring
* Transferrable object and into the clipboard.
*/
private void cut()
{
String selectedtext = editor.getSelectedText();
if (selectedtext != null)
{
clipboardstring = new StringSelection(selectedtext);
clipboard.setContents(clipboardstring,clipboardstring);
int startmark = editor.getSelectionStart();
int endmark = editor.getSelectionEnd();
editor.replaceRange("",startmark,endmark);
}
}
/** copy() stores the selected text from the editor into the
* clipboardstring Transferrable object and into the clipboard.
*/
private void copy()
{
String selectedtext = editor.getSelectedText();
if (selectedtext != null)
{
clipboardstring = new StringSelection(selectedtext);
clipboard.setContents(clipboardstring,clipboardstring);
}
}
/** paste() inserts the string stored from clear, copy or cut at
* the current caret position.
*/
private void paste()
{
Transferable clipboarddata = clipboard.getContents(clipboardstring);
try{
String text = (String) clipboarddata .getTransferData(DataFlavor.stringFlavor);
editor.insert(text,editor.getCaretPosition());
}catch(Exception e)
{
System.err.println("Exception: " + e.getMessage());
showExceptionErrorMessage("Exception: " + e.getMessage());
}
}
public File getAFileToOpen()
{
File file = null;
File currentdirectory = new File(".");
JFileChooser filechooser = new JFileChooser(currentdirectory);
int replycode = filechooser.showOpenDialog(frame);
if (replycode == JFileChooser.APPROVE_OPTION)
{
file = filechooser.getSelectedFile();
}
return file;
}
public static void main(String[] args)
{
SimpleTextEditor simpletexteditor = new SimpleTextEditor();
simpletexteditor.init();
}
}
/** Initializes the button controls, a textarea for an editor to display
* or enter text, and adds the components to a frame window and displays
* it. A clipboard object is set up so that this application can interface
* with other applications for cut copy and paste operations provided this
* is allowed by the SecuityManager if active. If the security manager
* does not allow it, it creates a clipboard object which will only support
* cut, copy,and paste operations from this application.
*
*/