1 /*
2  * SavePlotAsImageDialog.java
3  *
4  * Copyright (C) 2021 by RStudio, PBC
5  *
6  * Unless you have received this program directly from RStudio pursuant
7  * to the terms of a commercial license agreement with RStudio, then
8  * this program is licensed to you under the terms of version 3 of the
9  * GNU Affero General Public License. This program is distributed WITHOUT
10  * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
11  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
12  * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
13  *
14  */
15 package org.rstudio.studio.client.workbench.exportplot;
16 
17 import org.rstudio.core.client.files.FileSystemItem;
18 import org.rstudio.core.client.widget.Operation;
19 import org.rstudio.core.client.widget.OperationWithInput;
20 import org.rstudio.core.client.widget.ProgressIndicator;
21 import org.rstudio.core.client.widget.ThemedButton;
22 import org.rstudio.studio.client.common.GlobalDisplay;
23 import org.rstudio.studio.client.workbench.exportplot.model.ExportPlotOptions;
24 import org.rstudio.studio.client.workbench.exportplot.model.SavePlotAsImageContext;
25 
26 import com.google.gwt.event.dom.client.ClickEvent;
27 import com.google.gwt.event.dom.client.ClickHandler;
28 import com.google.gwt.user.client.ui.CheckBox;
29 import com.google.gwt.user.client.ui.Widget;
30 
31 public class SavePlotAsImageDialog extends ExportPlotDialog
32 {
SavePlotAsImageDialog( GlobalDisplay globalDisplay, SavePlotAsImageOperation saveOperation, ExportPlotPreviewer previewer, SavePlotAsImageContext context, final ExportPlotOptions options, final OperationWithInput<ExportPlotOptions> onClose)33    public SavePlotAsImageDialog(
34                            GlobalDisplay globalDisplay,
35                            SavePlotAsImageOperation saveOperation,
36                            ExportPlotPreviewer previewer,
37                            SavePlotAsImageContext context,
38                            final ExportPlotOptions options,
39                            final OperationWithInput<ExportPlotOptions> onClose)
40    {
41       super(options, previewer);
42 
43       setText("Save Plot as Image");
44 
45       globalDisplay_ = globalDisplay;
46       saveOperation_ = saveOperation;
47       progressIndicator_ = addProgressIndicator();
48 
49       ThemedButton saveButton = new ThemedButton("Save",
50                                                  new ClickHandler() {
51          public void onClick(ClickEvent event)
52          {
53             attemptSavePlot(false, new Operation() {
54                @Override
55                public void execute()
56                {
57                   onClose.execute(getCurrentOptions(options));
58 
59                   closeDialog();
60                }
61             });
62          }
63       });
64       addOkButton(saveButton);
65       addCancelButton();
66 
67       // file type and target path
68       saveAsTarget_ = new SavePlotAsImageTargetEditor(options.getFormat(),
69                                                       context);
70 
71       // view after size
72       viewAfterSaveCheckBox_ = new CheckBox("View plot after saving");
73       viewAfterSaveCheckBox_.setValue(options.getViewAfterSave());
74       addLeftWidget(viewAfterSaveCheckBox_);
75 
76    }
77 
78    @Override
createTopLeftWidget()79    protected Widget createTopLeftWidget()
80    {
81       return saveAsTarget_;
82    }
83 
84    @Override
focusInitialControl()85    protected void focusInitialControl()
86    {
87       saveAsTarget_.focus();
88    }
89 
90    @Override
getCurrentOptions(ExportPlotOptions previous)91    protected ExportPlotOptions getCurrentOptions(ExportPlotOptions previous)
92    {
93       ExportPlotSizeEditor sizeEditor = getSizeEditor();
94       return ExportPlotOptions.create(sizeEditor.getImageWidth(),
95                                       sizeEditor.getImageHeight(),
96                                       sizeEditor.getKeepRatio(),
97                                       saveAsTarget_.getFormat(),
98                                       viewAfterSaveCheckBox_.getValue(),
99                                       previous.getCopyAsMetafile());
100    }
101 
attemptSavePlot(boolean overwrite, final Operation onCompleted)102    private void attemptSavePlot(boolean overwrite,
103                                 final Operation onCompleted)
104    {
105       // get plot format
106       final String format = saveAsTarget_.getFormat();
107 
108       // validate path
109       FileSystemItem targetPath = saveAsTarget_.getTargetPath();
110       if (targetPath == null)
111       {
112          globalDisplay_.showErrorMessage(
113             "File Name Required",
114             "You must provide a file name for the plot image.",
115             saveAsTarget_);
116          return;
117       }
118 
119       saveOperation_.attemptSave(
120             progressIndicator_,
121             targetPath,
122             format,
123             getSizeEditor(),
124             overwrite,
125             viewAfterSaveCheckBox_.getValue(),
126             onCompleted);
127    }
128 
129    private final GlobalDisplay globalDisplay_;
130    private ProgressIndicator progressIndicator_;
131    private final SavePlotAsImageOperation saveOperation_;
132    private SavePlotAsImageTargetEditor saveAsTarget_;
133    private CheckBox viewAfterSaveCheckBox_;
134 
135 }
136