Skip to content

Commit d1cd0cb

Browse files
authored
Merge pull request #25 from UCSDOalads/XYGBranch
Modify the file save and file open actions by using JFileChooser
2 parents 6d27fb5 + 59fcc6b commit d1cd0cb

3 files changed

Lines changed: 35 additions & 6 deletions

File tree

src/actions/FileOpen.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
import file.PanelIO;
1212
import ui.PaintPanel;
1313

14+
import javax.swing.JFileChooser;
15+
import java.io.File;
16+
1417
public class FileOpen extends PaintAction {
1518

1619
public FileOpen(PaintPanel panel) {
@@ -24,7 +27,18 @@ public boolean canPerformAction() {
2427

2528
@Override
2629
public void performAction() {
27-
String filePath = JOptionPane.showInputDialog("Please input file path");
30+
JFileChooser fileChooser = new JFileChooser();
31+
fileChooser.setDialogTitle("Select the file to open");
32+
int userSelection = fileChooser.showOpenDialog(panel);
33+
34+
String filePath;
35+
if (userSelection == JFileChooser.APPROVE_OPTION) {
36+
File fileToSave = fileChooser.getSelectedFile();
37+
filePath = fileToSave.getAbsolutePath();
38+
} else {
39+
return;
40+
}
41+
2842
PanelIO io = new PanelIO();
2943
try {
3044
io.constructPanelFromDocument(panel, filePath, true);

src/actions/FileSaveAs.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
import file.PanelIO;
1616
import ui.PaintPanel;
1717

18+
import javax.swing.JFileChooser;
19+
import java.io.File;
20+
1821
public class FileSaveAs extends PaintAction {
1922

2023
public FileSaveAs(PaintPanel panel) {
@@ -25,10 +28,25 @@ public FileSaveAs(PaintPanel panel) {
2528
public boolean canPerformAction() {
2629
return true;
2730
}
28-
31+
32+
/**
33+
* Use a JFileChooser to save the file
34+
* @author xy gong
35+
*/
2936
@Override
3037
public void performAction() {
31-
String filePath = JOptionPane.showInputDialog("Please input file path");
38+
JFileChooser fileChooser = new JFileChooser();
39+
fileChooser.setDialogTitle("Specify a file to save");
40+
int userSelection = fileChooser.showSaveDialog(panel);
41+
42+
String filePath;
43+
if (userSelection == JFileChooser.APPROVE_OPTION) {
44+
File fileToSave = fileChooser.getSelectedFile();
45+
filePath = fileToSave.getAbsolutePath();
46+
} else {
47+
return;
48+
}
49+
3250
PanelIO io = new PanelIO();
3351
try {
3452
io.constructDocumentFromPanel(panel, filePath);

src/ui/MainFrame.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ public MainFrame(){
2424
ActionsMenuBar menuBar = new ActionsMenuBar(paintPanel);
2525
toolBar.getSelectTool().addSelectionToolListener(menuBar);
2626

27-
28-
29-
3027
//set background
3128
paintPanel.setBackground(Color.WHITE);
3229

0 commit comments

Comments
 (0)