File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1111import file .PanelIO ;
1212import ui .PaintPanel ;
1313
14+ import javax .swing .JFileChooser ;
15+ import java .io .File ;
16+
1417public 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 );
Original file line number Diff line number Diff line change 1515import file .PanelIO ;
1616import ui .PaintPanel ;
1717
18+ import javax .swing .JFileChooser ;
19+ import java .io .File ;
20+
1821public 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 );
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments