1 /* 2 * PanimrrorEditRawDialog.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 16 17 package org.rstudio.studio.client.panmirror.dialogs; 18 19 20 import com.google.gwt.aria.client.Roles; 21 22 import org.rstudio.core.client.ElementIds; 23 import org.rstudio.core.client.StringUtil; 24 import org.rstudio.core.client.widget.ModalDialog; 25 import org.rstudio.core.client.widget.OperationWithInput; 26 import org.rstudio.core.client.widget.ThemedButton; 27 import org.rstudio.studio.client.RStudioGinjector; 28 import org.rstudio.studio.client.common.GlobalDisplay; 29 import org.rstudio.studio.client.panmirror.dialogs.model.PanmirrorRawFormatProps; 30 import org.rstudio.studio.client.panmirror.dialogs.model.PanmirrorRawFormatResult; 31 32 import com.google.gwt.core.client.GWT; 33 import com.google.gwt.uibinder.client.UiBinder; 34 import com.google.gwt.uibinder.client.UiField; 35 import com.google.gwt.user.client.Command; 36 import com.google.gwt.user.client.ui.Label; 37 import com.google.gwt.user.client.ui.TextBox; 38 import com.google.gwt.user.client.ui.Widget; 39 import com.google.inject.Inject; 40 41 42 public class PanmirrorEditRawDialog extends ModalDialog<PanmirrorRawFormatResult> 43 { PanmirrorEditRawDialog( PanmirrorRawFormatProps raw, String[] outputFormats, boolean inline, OperationWithInput<PanmirrorRawFormatResult> operation)44 public PanmirrorEditRawDialog( 45 PanmirrorRawFormatProps raw, 46 String[] outputFormats, 47 boolean inline, 48 OperationWithInput<PanmirrorRawFormatResult> operation) 49 { 50 super("Raw " + (inline ? "Inline" : "Block"), Roles.getDialogRole(), operation, () -> { 51 // cancel returns null 52 operation.execute(null); 53 }); 54 55 RStudioGinjector.INSTANCE.injectMembers(this); 56 mainWidget_ = GWT.<Binder>create(Binder.class).createAndBindUi(this); 57 58 inline_ = inline; 59 60 rawFormatSelect_.setFormats(outputFormats, raw.format); 61 rawFormatSelect_.setValue(StringUtil.notNull(raw.format)); 62 63 rawContent_.setValue(raw.content); 64 PanmirrorDialogsUtil.setFullWidthStyles(rawContent_); 65 66 if (!inline_) 67 { 68 rawFormatSelect_.addStyleName(RES.styles().spaced()); 69 rawContentLabel_.setVisible(false); 70 rawContent_.setVisible(false); 71 } 72 73 // make remove button available if we are editing an existing format 74 if (!rawFormatSelect_.getValue().equals("")) 75 { 76 ThemedButton removeFormatButton = new ThemedButton("Remove Format"); 77 addLeftButton(removeFormatButton, ElementIds.VISUAL_MD_RAW_FORMAT_REMOVE_BUTTON); 78 removeFormatButton.addClickHandler((event) -> { 79 PanmirrorRawFormatResult input = collectInput(); 80 input.action = "remove"; 81 validateAndGo(input, new Command() 82 { 83 @Override 84 public void execute() 85 { 86 closeDialog(); 87 if (operation != null) 88 operation.execute(input); 89 onSuccess(); 90 } 91 }); 92 }); 93 } 94 } 95 96 @Inject initialize(GlobalDisplay globalDisplay)97 void initialize(GlobalDisplay globalDisplay) 98 { 99 globalDisplay_ = globalDisplay; 100 } 101 102 @Override createMainWidget()103 protected Widget createMainWidget() 104 { 105 return mainWidget_; 106 } 107 108 @Override focusInitialControl()109 protected void focusInitialControl() 110 { 111 if (rawFormatSelect_.getValue().length() > 0) 112 { 113 rawContent_.setFocus(true); 114 } 115 else 116 { 117 rawFormatSelect_.getListBox().setFocus(true); 118 } 119 } 120 121 @Override collectInput()122 protected PanmirrorRawFormatResult collectInput() 123 { 124 PanmirrorRawFormatResult result = new PanmirrorRawFormatResult(); 125 result.raw = new PanmirrorRawFormatProps(); 126 result.raw.format = rawFormatSelect_.getValue(); 127 result.raw.content = rawContent(); 128 result.action = "edit"; 129 return result; 130 } 131 132 133 @Override validate(PanmirrorRawFormatResult result)134 protected boolean validate(PanmirrorRawFormatResult result) 135 { 136 if (inline_ && rawContent().length() == 0) 137 { 138 globalDisplay_.showErrorMessage( 139 "No Content Specified", 140 "You must provide content to apply the raw format to.", 141 rawContent_); 142 143 return false; 144 } 145 else 146 { 147 return true; 148 } 149 } 150 rawContent()151 private String rawContent() 152 { 153 return rawContent_.getValue().trim(); 154 } 155 156 private final boolean inline_; 157 158 private GlobalDisplay globalDisplay_; 159 160 private static PanmirrorDialogsResources RES = PanmirrorDialogsResources.INSTANCE; 161 162 interface Binder extends UiBinder<Widget, PanmirrorEditRawDialog> {} 163 164 private Widget mainWidget_; 165 @UiField PanmirrorRawFormatSelect rawFormatSelect_; 166 @UiField TextBox rawContent_; 167 @UiField Label rawContentLabel_; 168 169 170 } 171