@@ -70,8 +70,8 @@ public class UI extends JFrame implements ActionListener {
70
70
private final Action selectAllAction ;
71
71
72
72
//setup icons - Bold and Italic
73
- private final ImageIcon boldIcon = new ImageIcon ("icons/bold.png" );
74
- private final ImageIcon italicIcon = new ImageIcon ("icons/italic.png" );
73
+ private final ImageIcon boldIcon = new ImageIcon ("icons/bold.png" );
74
+ private final ImageIcon italicIcon = new ImageIcon ("icons/italic.png" );
75
75
76
76
// setup icons - File Menu
77
77
private final ImageIcon newIcon = new ImageIcon ("icons/new.png" );
@@ -101,14 +101,13 @@ public class UI extends JFrame implements ActionListener {
101
101
private boolean edit = false ;
102
102
103
103
public UI () {
104
- try {
104
+ try {
105
105
ImageIcon image = new ImageIcon ("icons/ste.png" );
106
106
super .setIconImage (image .getImage ());
107
- }
108
- catch (Exception ex ) {
107
+ } catch (Exception ex ) {
109
108
ex .printStackTrace ();
110
109
}
111
-
110
+
112
111
// Set the initial size of the window
113
112
setSize (800 , 500 );
114
113
@@ -141,7 +140,6 @@ public void keyPressed(KeyEvent ke) {
141
140
}
142
141
});
143
142
144
-
145
143
JScrollPane scrollPane = new JScrollPane (textArea );
146
144
textArea .setWrapStyleWord (true );
147
145
scrollPane .setVerticalScrollBarPolicy (JScrollPane .VERTICAL_SCROLLBAR_ALWAYS );
@@ -525,14 +523,14 @@ else if (e.getSource() == saveFile || e.getSource() == saveButton) {
525
523
saveFile ();
526
524
}// If the source of the event was the "Bold" button
527
525
else if (e .getSource () == boldButton ) {
528
- if (textArea .getFont ().getStyle () == Font .BOLD ){
526
+ if (textArea .getFont ().getStyle () == Font .BOLD ) {
529
527
textArea .setFont (textArea .getFont ().deriveFont (Font .PLAIN ));
530
528
} else {
531
529
textArea .setFont (textArea .getFont ().deriveFont (Font .BOLD ));
532
530
}
533
531
}// If the source of the event was the "Italic" button
534
532
else if (e .getSource () == italicButton ) {
535
- if (textArea .getFont ().getStyle () == Font .ITALIC ){
533
+ if (textArea .getFont ().getStyle () == Font .ITALIC ) {
536
534
textArea .setFont (textArea .getFont ().deriveFont (Font .PLAIN ));
537
535
} else {
538
536
textArea .setFont (textArea .getFont ().deriveFont (Font .ITALIC ));
@@ -605,7 +603,7 @@ private void saveFile() {
605
603
}
606
604
}
607
605
}
608
- DropTargetListener dropTargetListener = new DropTargetListener () {
606
+ DropTargetListener dropTargetListener = new DropTargetListener () {
609
607
610
608
@ Override
611
609
public void dragEnter (DropTargetDragEvent e ) {
@@ -621,6 +619,21 @@ public void dragOver(DropTargetDragEvent e) {
621
619
622
620
@ Override
623
621
public void drop (DropTargetDropEvent e ) {
622
+ if (edit ) {
623
+ Object [] options = {"Save" , "No Save" , "Return" };
624
+ int n = JOptionPane .showOptionDialog (UI .this , "Do you want to save the file at first ?" , "Question" ,
625
+ JOptionPane .YES_NO_CANCEL_OPTION , JOptionPane .QUESTION_MESSAGE , null , options , options [2 ]);
626
+ if (n == 0 ) {// save
627
+ UI .this .saveFile ();
628
+ edit = false ;
629
+ } else if (n == 1 ) {
630
+ edit = false ;
631
+ FEdit .clear (textArea );
632
+ } else if (n == 2 ) {
633
+ e .rejectDrop ();
634
+ return ;
635
+ }
636
+ }
624
637
try {
625
638
Transferable tr = e .getTransferable ();
626
639
DataFlavor [] flavors = tr .getTransferDataFlavors ();
@@ -630,13 +643,25 @@ public void drop(DropTargetDropEvent e) {
630
643
631
644
try {
632
645
String fileName = tr .getTransferData (flavors [i ]).toString ().replace ("[" , "" ).replace ("]" , "" );
633
- FileInputStream fis = new FileInputStream (new File (fileName ));
634
- byte [] ba = new byte [fis .available ()];
635
- fis .read (ba );
636
- textArea .setText (new String (ba ));
637
- fis .close ();
638
- }
639
- catch (Exception ex ) {
646
+ String [] extensionFilter = {".txt" , ".dat" , ".log" , ".xml" , ".mf" , ".html" }; // allowed file filter extentions for drag and drop
647
+ boolean extensionAllowed = false ;
648
+ for (int j = 0 ; j < extensionFilter .length ; j ++) {
649
+ if (fileName .endsWith (extensionFilter [j ])) {
650
+ extensionAllowed = true ;
651
+ break ;
652
+ }
653
+ }
654
+ if (!extensionAllowed ) {
655
+ JOptionPane .showMessageDialog (UI .this , "This file is not allowed for drag & drop" , "Error" , JOptionPane .ERROR_MESSAGE );
656
+
657
+ } else {
658
+ FileInputStream fis = new FileInputStream (new File (fileName ));
659
+ byte [] ba = new byte [fis .available ()];
660
+ fis .read (ba );
661
+ textArea .setText (new String (ba ));
662
+ fis .close ();
663
+ }
664
+ } catch (Exception ex ) {
640
665
ex .printStackTrace ();
641
666
}
642
667
e .dropComplete (true );
0 commit comments