1 /* 2 * DocPropMenuItem.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.core.client.widget; 16 17 import java.util.HashMap; 18 19 import org.rstudio.core.client.StringUtil; 20 import org.rstudio.studio.client.RStudioGinjector; 21 import org.rstudio.studio.client.workbench.views.source.model.DocUpdateSentinel; 22 23 import com.google.gwt.event.logical.shared.ValueChangeEvent; 24 import com.google.gwt.event.logical.shared.ValueChangeHandler; 25 26 public class DocPropMenuItem extends CheckableMenuItem 27 { DocPropMenuItem(String label, DocUpdateSentinel docUpdate, boolean defaultChecked, String propName, String targetValue)28 public DocPropMenuItem(String label, DocUpdateSentinel docUpdate, 29 boolean defaultChecked, String propName, String targetValue) 30 { 31 this(label, false, docUpdate, defaultChecked, propName, targetValue); 32 } 33 DocPropMenuItem(String label, boolean html, DocUpdateSentinel docUpdate, boolean defaultChecked, String propName, String targetValue)34 public DocPropMenuItem(String label, boolean html, DocUpdateSentinel docUpdate, 35 boolean defaultChecked, String propName, String targetValue) 36 { 37 super(label, html); 38 docUpdate_ = docUpdate; 39 default_ = defaultChecked; 40 propName_ = propName; 41 targetValue_ = targetValue; 42 docUpdate_.addPropertyValueChangeHandler(propName, 43 new ValueChangeHandler<String>() 44 { 45 @Override 46 public void onValueChange(ValueChangeEvent<String> arg0) 47 { 48 onStateChanged(); 49 } 50 }); 51 onStateChanged(); 52 } 53 54 @Override getLabel()55 public String getLabel() 56 { 57 return getText(); 58 } 59 60 @Override isChecked()61 public boolean isChecked() 62 { 63 if (docUpdate_ == null) 64 return default_; 65 String val = docUpdate_.getProperty(propName_); 66 if (StringUtil.isNullOrEmpty(val)) 67 return default_; 68 else 69 return val == targetValue_; 70 } 71 72 @Override onInvoked()73 public void onInvoked() 74 { 75 HashMap<String, String> props = new HashMap<>(); 76 String target = targetValue_; 77 78 // toggle behavior for boolean values: if our target was true but the 79 // prop is already set to true, set it to false 80 if (target == DocUpdateSentinel.PROPERTY_TRUE && 81 docUpdate_.getBoolProperty(propName_, default_)) 82 target = DocUpdateSentinel.PROPERTY_FALSE; 83 84 props.put(propName_, target); 85 docUpdate_.modifyProperties(props, new ProgressIndicator() 86 { 87 @Override 88 public void onError(String message) 89 { 90 RStudioGinjector.INSTANCE.getGlobalDisplay().showErrorMessage( 91 "Could Not Change Setting", message); 92 } 93 94 @Override 95 public void onCompleted() 96 { 97 onStateChanged(); 98 onUpdateComplete(); 99 } 100 101 @Override 102 public void clearProgress() 103 { 104 } 105 106 @Override 107 public void onProgress(String message, Operation onCancel) 108 { 109 } 110 111 @Override 112 public void onProgress(String message) 113 { 114 } 115 }); 116 } 117 onUpdateComplete()118 protected void onUpdateComplete() 119 { 120 } 121 122 private DocUpdateSentinel docUpdate_; 123 private String propName_; 124 private String targetValue_; 125 private boolean default_; 126 } 127