1 /*
2  * RmdStringOption.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.rmarkdown.ui;
16 
17 import org.rstudio.studio.client.rmarkdown.model.RmdTemplateFormatOption;
18 
19 import com.google.gwt.dom.client.Style.Display;
20 import com.google.gwt.dom.client.Style.Unit;
21 import com.google.gwt.user.client.ui.HTMLPanel;
22 import com.google.gwt.user.client.ui.TextBox;
23 
24 public class RmdStringOption extends RmdNullableOption
25 {
26 
RmdStringOption(RmdTemplateFormatOption option, String initialValue)27    public RmdStringOption(RmdTemplateFormatOption option, String initialValue)
28    {
29       super(option, initialValue);
30       defaultValue_ = option.getDefaultValue();
31 
32       HTMLPanel panel = new HTMLPanel("");
33       txtValue_ = new TextBox();
34       if (initialValue != "null")
35          txtValue_.setValue(initialValue);
36       txtValue_.getElement().getStyle().setDisplay(Display.BLOCK);
37       txtValue_.getElement().getStyle().setMarginLeft(20, Unit.PX);
38       txtValue_.getElement().getStyle().setMarginTop(3, Unit.PX);
39       txtValue_.setWidth("75%");
40       panel.add(getOptionLabelWidget(txtValue_.getElement()));
41       panel.add(txtValue_);
42 
43       updateNull();
44       initWidget(panel);
45    }
46 
47    @Override
valueIsDefault()48    public boolean valueIsDefault()
49    {
50       if (getValue() == null)
51          return defaultValue_ == "null";
52       else
53          return defaultValue_ == txtValue_.getValue();
54    }
55 
56    @Override
getValue()57    public String getValue()
58    {
59       if (valueIsNull())
60          return null;
61       else if (getOption().isNullable() && txtValue_.getValue().trim().isEmpty())
62          return null;
63       else
64          return txtValue_.getValue().trim();
65    }
66 
67    @Override
updateNull()68    public void updateNull()
69    {
70       txtValue_.setEnabled(!valueIsNull());
71    }
72 
73    private TextBox txtValue_;
74 
75    private String defaultValue_;
76 }
77