Skip to content

Commit 055b388

Browse files
authored
Merge pull request #29 from SidaDan/master
Added file filter for drag & drop functionality.
2 parents c93e78f + 189bc7b commit 055b388

File tree

1 file changed

+42
-17
lines changed

1 file changed

+42
-17
lines changed

src/simplejavatexteditor/UI.java

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ public class UI extends JFrame implements ActionListener {
7070
private final Action selectAllAction;
7171

7272
//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");
7575

7676
// setup icons - File Menu
7777
private final ImageIcon newIcon = new ImageIcon("icons/new.png");
@@ -101,14 +101,13 @@ public class UI extends JFrame implements ActionListener {
101101
private boolean edit = false;
102102

103103
public UI() {
104-
try {
104+
try {
105105
ImageIcon image = new ImageIcon("icons/ste.png");
106106
super.setIconImage(image.getImage());
107-
}
108-
catch (Exception ex) {
107+
} catch (Exception ex) {
109108
ex.printStackTrace();
110109
}
111-
110+
112111
// Set the initial size of the window
113112
setSize(800, 500);
114113

@@ -141,7 +140,6 @@ public void keyPressed(KeyEvent ke) {
141140
}
142141
});
143142

144-
145143
JScrollPane scrollPane = new JScrollPane(textArea);
146144
textArea.setWrapStyleWord(true);
147145
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
@@ -525,14 +523,14 @@ else if (e.getSource() == saveFile || e.getSource() == saveButton) {
525523
saveFile();
526524
}// If the source of the event was the "Bold" button
527525
else if (e.getSource() == boldButton) {
528-
if (textArea.getFont().getStyle() == Font.BOLD){
526+
if (textArea.getFont().getStyle() == Font.BOLD) {
529527
textArea.setFont(textArea.getFont().deriveFont(Font.PLAIN));
530528
} else {
531529
textArea.setFont(textArea.getFont().deriveFont(Font.BOLD));
532530
}
533531
}// If the source of the event was the "Italic" button
534532
else if (e.getSource() == italicButton) {
535-
if (textArea.getFont().getStyle() == Font.ITALIC){
533+
if (textArea.getFont().getStyle() == Font.ITALIC) {
536534
textArea.setFont(textArea.getFont().deriveFont(Font.PLAIN));
537535
} else {
538536
textArea.setFont(textArea.getFont().deriveFont(Font.ITALIC));
@@ -605,7 +603,7 @@ private void saveFile() {
605603
}
606604
}
607605
}
608-
DropTargetListener dropTargetListener = new DropTargetListener() {
606+
DropTargetListener dropTargetListener = new DropTargetListener() {
609607

610608
@Override
611609
public void dragEnter(DropTargetDragEvent e) {
@@ -621,6 +619,21 @@ public void dragOver(DropTargetDragEvent e) {
621619

622620
@Override
623621
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+
}
624637
try {
625638
Transferable tr = e.getTransferable();
626639
DataFlavor[] flavors = tr.getTransferDataFlavors();
@@ -630,13 +643,25 @@ public void drop(DropTargetDropEvent e) {
630643

631644
try {
632645
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) {
640665
ex.printStackTrace();
641666
}
642667
e.dropComplete(true);

0 commit comments

Comments
 (0)