1 /*
2  * ExportPlotDialog.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 com.google.gwt.aria.client.Roles;
18 import org.rstudio.core.client.Size;
19 import org.rstudio.core.client.widget.ModalDialogBase;
20 import org.rstudio.studio.client.workbench.exportplot.model.ExportPlotOptions;
21 
22 import com.google.gwt.user.client.Window;
23 import com.google.gwt.user.client.ui.VerticalPanel;
24 import com.google.gwt.user.client.ui.Widget;
25 
26 public class ExportPlotDialog extends ModalDialogBase
27 {
ExportPlotDialog(ExportPlotOptions options, ExportPlotPreviewer previewer)28    public ExportPlotDialog(ExportPlotOptions options,
29                            ExportPlotPreviewer previewer)
30    {
31       super(Roles.getDialogRole());
32       options_ = options;
33       previewer_ = previewer;
34    }
35 
36    @Override
createMainWidget()37    protected Widget createMainWidget()
38    {
39       VerticalPanel mainPanel = new VerticalPanel();
40 
41       // enforce maximum initial dimensions based on screen size
42       Size maxSize = new Size(Window.getClientWidth() - 100,
43                               Window.getClientHeight() - 200);
44 
45       int width = Math.min(options_.getWidth(), maxSize.width);
46       int height = Math.min(options_.getHeight(), maxSize.height);
47 
48       sizeEditor_ = new ExportPlotSizeEditor(
49                                  width,
50                                  height,
51                                  options_.getKeepRatio(),
52                                  createTopLeftWidget(),
53                                  previewer_,
54                                  new ExportPlotSizeEditor.Observer()
55                                  {
56                                     @Override
57                                     public void onResized(boolean withMouse)
58                                     {
59                                        if (!withMouse)
60                                           center();
61                                     }
62                                  });
63       mainPanel.add(sizeEditor_);
64 
65       Widget bottomWidget = createBottomWidget();
66       if (bottomWidget != null)
67          mainPanel.add(bottomWidget);
68 
69       return mainPanel;
70 
71    }
72 
createTopLeftWidget()73    protected Widget createTopLeftWidget()
74    {
75       return null;
76    }
77 
createBottomWidget()78    protected Widget createBottomWidget()
79    {
80       return null;
81    }
82 
getSizeEditor()83    protected ExportPlotSizeEditor getSizeEditor()
84    {
85       return sizeEditor_;
86    }
87 
getCurrentOptions(ExportPlotOptions previous)88    protected ExportPlotOptions getCurrentOptions(ExportPlotOptions previous)
89    {
90       ExportPlotSizeEditor sizeEditor = getSizeEditor();
91       return ExportPlotOptions.create(sizeEditor.getImageWidth(),
92                                       sizeEditor.getImageHeight(),
93                                       sizeEditor.getKeepRatio(),
94                                       previous.getFormat(),
95                                       previous.getViewAfterSave(),
96                                       previous.getCopyAsMetafile());
97    }
98 
99 
100    @Override
focusInitialControl()101    protected void focusInitialControl()
102    {
103       sizeEditor_.onSizerShown();
104    }
105 
106    private final ExportPlotOptions options_;
107    private final ExportPlotPreviewer previewer_;
108    private ExportPlotSizeEditor sizeEditor_;
109 }
110