forked from bimberlabinternal/LabDevKitModules
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimpleButtonConfigFactory.java
More file actions
158 lines (138 loc) · 4.6 KB
/
SimpleButtonConfigFactory.java
File metadata and controls
158 lines (138 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* Copyright (c) 2013-2015 LabKey Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.labkey.api.ldk.table;
import org.labkey.api.data.Container;
import org.labkey.api.data.RenderContext;
import org.labkey.api.data.TableInfo;
import org.labkey.api.data.UserDefinedButtonConfig;
import org.labkey.api.module.Module;
import org.labkey.api.query.DetailsURL;
import org.labkey.api.security.User;
import org.labkey.api.security.permissions.Permission;
import org.labkey.api.view.DisplayElement;
import org.labkey.api.view.NavTree;
import org.labkey.api.view.template.ClientDependency;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.function.Supplier;
/**
* User: bimber
* Date: 5/5/13
*/
public class SimpleButtonConfigFactory implements ButtonConfigFactory
{
private final Module _owner;
private final String _text;
private DetailsURL _url = null;
private String _jsHandler = null;
private Integer _insertPosition = null;
private Class<? extends Permission> _permission = null;
private List<Supplier<ClientDependency>> _clientDependencies = new ArrayList<>();
public SimpleButtonConfigFactory(Module owner, String text, DetailsURL url)
{
_owner = owner;
_text = text;
_url = url;
}
public SimpleButtonConfigFactory(Module owner, String text, String handler)
{
_owner = owner;
_text = text;
_jsHandler = handler;
}
public SimpleButtonConfigFactory(Module owner, String text, String handler, List<Supplier<ClientDependency>> clientDependencies)
{
_owner = owner;
_text = text;
_jsHandler = handler;
_clientDependencies = clientDependencies;
}
@Override
public UserDefinedButtonConfig createBtn(final TableInfo ti)
{
Container c = ti.getUserSchema().getContainer();
UserDefinedButtonConfig btn = new UserDefinedButtonConfig()
{
@Override
public DisplayElement createButton(RenderContext ctx, List<DisplayElement> originalButtons)
{
DisplayElement result = super.createButton(ctx, originalButtons);
result.setVisible(isVisible(ti));
return result;
}
};
btn.setText(_text);
if (_url != null)
btn.setUrl(_url.copy(c).getActionURL().toString());
String onClick = getJsHandler(ti);
if (onClick != null)
btn.setOnClick(onClick);
if (_insertPosition != null)
btn.setInsertPosition(_insertPosition);
if (_permission != null)
btn.setPermission(_permission);
return btn;
}
public void setInsertPosition(Integer insertPosition)
{
_insertPosition = insertPosition;
}
@Override
public NavTree create(TableInfo ti)
{
Container c = ti.getUserSchema().getContainer();
NavTree tree = new NavTree();
tree.setText(_text);
if (_url != null)
tree.setHref(_url.copy(c).getActionURL().toString());
tree.setScript(getJsHandler(ti));
return tree;
}
protected String getJsHandler(TableInfo ti)
{
return _jsHandler;
}
@Override
public boolean isAvailable(TableInfo ti)
{
return _owner == null || ti.getUserSchema().getContainer().getActiveModules().contains(_owner);
}
@Override
public boolean isVisible(TableInfo ti)
{
return true;
}
@Override
public List<Supplier<ClientDependency>> getClientDependencies(Container c, User u)
{
return _clientDependencies;
}
@SafeVarargs
public final void setClientDependencies(Supplier<ClientDependency>... clientDependencies)
{
setClientDependencies(Arrays.asList(clientDependencies));
}
public void setClientDependencies(Collection<Supplier<ClientDependency>> clientDependencies)
{
_clientDependencies.addAll(clientDependencies);
}
public void setPermission(Class<? extends Permission> permission)
{
_permission = permission;
}
}