1 /*
2  * EnvironmentObjectDisplay.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 package org.rstudio.studio.client.workbench.views.environment.view;
17 
18 import java.util.List;
19 
20 import org.rstudio.core.client.SafeHtmlUtil;
21 import org.rstudio.core.client.cellview.ScrollingDataGrid;
22 import org.rstudio.core.client.theme.res.ThemeStyles;
23 import org.rstudio.studio.client.workbench.views.environment.EnvironmentPane;
24 
25 import com.google.gwt.cell.client.FieldUpdater;
26 import com.google.gwt.safehtml.shared.SafeHtml;
27 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
28 import com.google.gwt.text.shared.AbstractSafeHtmlRenderer;
29 import com.google.gwt.user.cellview.client.Column;
30 
31 public abstract class EnvironmentObjectDisplay
32                       extends ScrollingDataGrid<RObjectEntry>
33 {
34    public interface Host
35    {
enableClickableObjects()36       public boolean enableClickableObjects();
useStatePersistence()37       public boolean useStatePersistence();
getFilterText()38       public String getFilterText();
getSortColumn()39       public int getSortColumn();
setSortColumn(int col)40       public void setSortColumn(int col);
toggleAscendingSort()41       public void toggleAscendingSort();
getAscendingSort()42       boolean getAscendingSort();
getShowInternalFunctions()43       boolean getShowInternalFunctions();
setShowInternalFunctions(boolean hide)44       void setShowInternalFunctions(boolean hide);
fillEntryContents(RObjectEntry entry, int idx, boolean drawProgress)45       public void fillEntryContents(RObjectEntry entry, int idx,
46                                     boolean drawProgress);
47    }
48 
EnvironmentObjectDisplay(Host host, EnvironmentObjectsObserver observer, String environmentName)49    public EnvironmentObjectDisplay(Host host,
50                                    EnvironmentObjectsObserver observer,
51                                    String environmentName)
52    {
53       super(EnvironmentObjects.MAX_ENVIRONMENT_OBJECTS,
54             RObjectEntry.KEY_PROVIDER);
55 
56       observer_ = observer;
57       host_ = host;
58       environmentStyle_ = EnvironmentResources.INSTANCE.environmentStyle();
59       environmentStyle_.ensureInjected();
60       environmentName_ = environmentName;
61       filterRenderer_ = new AbstractSafeHtmlRenderer<String>()
62       {
63          @Override
64          public SafeHtml render(String str)
65          {
66             SafeHtmlBuilder sb = new SafeHtmlBuilder();
67             SafeHtmlUtil.highlightSearchMatch(sb, str, host_.getFilterText(),
68                   ThemeStyles.INSTANCE.filterMatch());
69             return sb.toSafeHtml();
70          }
71       };
72    }
73 
getSelectedObjects()74    public abstract List<String> getSelectedObjects();
clearSelection()75    public abstract void clearSelection();
76 
setEnvironmentName(String environmentName)77    public void setEnvironmentName(String environmentName)
78    {
79       environmentName_ = environmentName;
80    }
81 
82    // attaches a handler to a column that invokes the associated object
attachClickToInvoke(Column<RObjectEntry, String> column)83    protected void attachClickToInvoke(Column<RObjectEntry, String> column)
84    {
85       column.setFieldUpdater(new FieldUpdater<RObjectEntry, String>()
86       {
87          @Override
88          public void update(int index, RObjectEntry object, String value)
89          {
90             boolean isClickable =
91                   host_.enableClickableObjects() &&
92                   (object.isPromise() ||
93                    object.getCategory() != RObjectEntry.Categories.Value);
94 
95             if (isClickable)
96                observer_.viewObject(object.isPromise() ?
97                   "force" : "View", object.rObject.getName());
98          }
99       });
100    }
101 
selectionEnabled()102    protected boolean selectionEnabled()
103    {
104       return environmentName_ == EnvironmentPane.GLOBAL_ENVIRONMENT_NAME;
105    }
106 
107    protected AbstractSafeHtmlRenderer<String> filterRenderer_;
108    protected EnvironmentObjectsObserver observer_;
109    protected Host host_;
110    protected EnvironmentStyle environmentStyle_;
111    protected String environmentName_ = "";
112 }
113