1+ /**
2+ * MIT License
3+ *
4+ * Copyright (c) 2017, 2018, 2019 SourceLab.org (https://github.com/SourceLabOrg/kafka-webview/)
5+ *
6+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7+ * of this software and associated documentation files (the "Software"), to deal
8+ * in the Software without restriction, including without limitation the rights
9+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+ * copies of the Software, and to permit persons to whom the Software is
11+ * furnished to do so, subject to the following conditions:
12+ *
13+ * The above copyright notice and this permission notice shall be included in all
14+ * copies or substantial portions of the Software.
15+ *
16+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+ * SOFTWARE.
23+ */
24+
125package org .sourcelab .kafka .webview .ui .manager .ui .datatable ;
226
327import java .util .ArrayList ;
428import java .util .Collections ;
529import java .util .List ;
30+ import java .util .Objects ;
631import java .util .function .Function ;
732
833/**
934 * Provides Action links template.
35+ * @param <T> Record type.
1036 */
1137public class ActionTemplate <T > extends RenderTemplate <T > {
1238 private final List <ActionLink <T >> links ;
@@ -32,17 +58,39 @@ List<Object> getParameters(final T record) {
3258 return params ;
3359 }
3460
61+ /**
62+ * Create new ActionLink Builder instance.
63+ * @param type Record type.
64+ * @param <T> Record type.
65+ * @return new Builder instance.
66+ */
3567 public static <T > Builder <T > newBuilder (Class <T > type ) {
3668 return new Builder <>();
3769 }
3870
71+ /**
72+ * Defines an Action Link.
73+ * @param <T> Record type.
74+ */
3975 public static class ActionLink <T > {
4076 private final Function <T , String > urlFunction ;
4177 private final Function <T , String > labelFunction ;
4278 private final String icon ;
4379 private final boolean isPost ;
4480
45- public ActionLink (final Function <T , String > urlFunction , final Function <T , String > labelFunction , final String icon , final boolean isPost ) {
81+ /**
82+ * Constructor. See Builder instance.
83+ * @param urlFunction Function to generate URL.
84+ * @param labelFunction Function to generate label.
85+ * @param icon Icon to display.
86+ * @param isPost Should the link be a POST or get.
87+ */
88+ public ActionLink (
89+ final Function <T , String > urlFunction ,
90+ final Function <T , String > labelFunction ,
91+ final String icon ,
92+ final boolean isPost
93+ ) {
4694 this .urlFunction = urlFunction ;
4795 this .labelFunction = labelFunction ;
4896 this .icon = icon ;
@@ -65,6 +113,10 @@ public String getIcon() {
65113 return icon ;
66114 }
67115
116+ /**
117+ * Does this action link have an icon.
118+ * @return True if yes, false if not.
119+ */
68120 public boolean hasIcon () {
69121 if (icon == null || icon .trim ().isEmpty ()) {
70122 return false ;
@@ -77,13 +129,23 @@ public boolean isPost() {
77129 }
78130 }
79131
132+ /**
133+ * ActionLink Builder instance.
134+ * @param <T> Record type.
135+ */
80136 public static class Builder <T > {
81137 private List <ActionLink <T >> links = new ArrayList <>();
82138
83139 private Builder () {
84140 }
85141
142+ /**
143+ * Add multiple links.
144+ * @param links Links to add.
145+ * @return Builder instance.
146+ */
86147 public Builder <T > withLinks (List <ActionLink <T >> links ) {
148+ Objects .requireNonNull (links );
87149 this .links .clear ();
88150 this .links .addAll (links );
89151 return this ;
@@ -94,11 +156,58 @@ public Builder<T> withLink(final ActionLink<T> link) {
94156 return this ;
95157 }
96158
159+ /**
160+ * Add a standard Edit link.
161+ * @param type Record type.
162+ * @param urlFunction The url to link to.
163+ * @return Builder instance.
164+ */
165+ public Builder <T > withEditLink (
166+ final Class <T > type ,
167+ final Function <T , String > urlFunction
168+ ) {
169+ return withLink (
170+ ActionTemplate .ActionLink .newBuilder (type )
171+ .withLabelFunction ((record ) -> "Edit" )
172+ .withUrlFunction (urlFunction )
173+ .withIcon ("fa-edit" )
174+ .build ()
175+ );
176+ }
177+
178+ /**
179+ * Add a standard Delete link.
180+ * @param type Record type.
181+ * @param urlFunction The url to link to.
182+ * @return Builder instance.
183+ */
184+ public Builder <T > withDeleteLink (
185+ final Class <T > type ,
186+ final Function <T , String > urlFunction
187+ ) {
188+ return withLink (
189+ ActionTemplate .ActionLink .newBuilder (type )
190+ .withLabelFunction ((record ) -> "Delete" )
191+ .withUrlFunction (urlFunction )
192+ .withIcon ("fa-remove" )
193+ .withIsPost (true )
194+ .build ()
195+ );
196+ }
197+
198+ /**
199+ * Create new ActionTemplate instance from Builder.
200+ * @return ActionType instance.
201+ */
97202 public ActionTemplate <T > build () {
98203 return new ActionTemplate <T >(links );
99204 }
100205 }
101206
207+ /**
208+ * Link builder instance.
209+ * @param <T> Record type.
210+ */
102211 public static class ActionLinkBuilder <T > {
103212 private Function <T , String > urlFunction ;
104213 private Function <T , String > labelFunction ;
0 commit comments