Skip to content

Commit 214efdf

Browse files
authored
Migrate remaining renderInputHtml() and renderDetailsCellContents() overrides (#6467)
1 parent 193c2a2 commit 214efdf

26 files changed

+369
-654
lines changed

api/src/org/labkey/api/data/AbstractExcelDisplayColumn.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import org.labkey.api.view.ActionURL;
55
import org.labkey.api.writer.HtmlWriter;
66

7-
import java.io.IOException;
87
import java.io.Writer;
98

109
public abstract class AbstractExcelDisplayColumn extends DisplayColumn
@@ -44,24 +43,12 @@ public void renderDetailsCellContents(RenderContext ctx, HtmlWriter out)
4443
throw new UnsupportedOperationException("This is for excel only.");
4544
}
4645

47-
@Override
48-
public void renderDetailsCellContents(RenderContext ctx, Writer oldWriter, HtmlWriter out)
49-
{
50-
throw new UnsupportedOperationException("This is for excel only.");
51-
}
52-
5346
@Override
5447
public void renderInputHtml(RenderContext ctx, HtmlWriter out, Object value)
5548
{
5649
throw new UnsupportedOperationException("This is for excel only.");
5750
}
5851

59-
@Override
60-
protected void renderInputHtml(RenderContext ctx, Writer oldWriter, HtmlWriter out, Object value) throws IOException
61-
{
62-
throw new UnsupportedOperationException("This is for excel only.");
63-
}
64-
6552
@Override
6653
public HtmlString getTitle(RenderContext ctx)
6754
{

api/src/org/labkey/api/data/DataColumn.java

Lines changed: 83 additions & 93 deletions
Large diffs are not rendered by default.

api/src/org/labkey/api/data/DisplayColumn.java

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ public boolean shouldRenderInCurrentRow(RenderContext ctx)
144144
/*
145145
Note: DataRegion plus its subclasses and the vast majority of DisplayColumn (and subclasses) have been rewritten
146146
to use HtmlWriter, DOM, and builders instead of String-based HTML generation. They also no longer throw
147-
IOException. The three deprecated methods below that take both Writer and HtmlWriter are temporary, present only
148-
until their overrides are migrated to use HtmlWriter, DOM, and builders, and adjusted to override the
149-
corresponding non-Writer variant. Once migrated, the deprecated methods will be removed and the non-deprecated
150-
variants will be made abstract.
147+
IOException. The deprecated renderGridCellContents() variant below that take both Writer and HtmlWriter is
148+
temporary, present only until its overrides are migrated to use HtmlWriter, DOM, and builders, and adjusted to
149+
override the corresponding non-Writer variant. Once migrated, the deprecated method will be removed and the
150+
non-deprecated variant will be made abstract.
151151
*/
152152

153153
public void renderGridCellContents(RenderContext ctx, HtmlWriter out)
@@ -169,43 +169,9 @@ protected void renderGridCellContents(RenderContext ctx, Writer oldWriter, HtmlW
169169
throw new IllegalStateException("Must override renderGridCellContents()");
170170
}
171171

172-
public void renderDetailsCellContents(RenderContext ctx, HtmlWriter out)
173-
{
174-
try
175-
{
176-
renderDetailsCellContents(ctx, out.unwrap(), out);
177-
}
178-
catch (IOException e)
179-
{
180-
throw new RuntimeException(e);
181-
}
182-
}
183-
184-
// No callers (other than just above)
185-
@Deprecated
186-
protected void renderDetailsCellContents(RenderContext ctx, Writer oldWriter, HtmlWriter out) throws IOException
187-
{
188-
throw new IllegalStateException("Must override renderDetailsCellContents()");
189-
}
172+
public abstract void renderDetailsCellContents(RenderContext ctx, HtmlWriter out);
190173

191-
public void renderInputHtml(RenderContext ctx, HtmlWriter out, Object value)
192-
{
193-
try
194-
{
195-
renderInputHtml(ctx, out.unwrap(), out, value);
196-
}
197-
catch (IOException e)
198-
{
199-
throw new RuntimeException(e);
200-
}
201-
}
202-
203-
// No callers (other than just above)
204-
@Deprecated
205-
protected void renderInputHtml(RenderContext ctx, Writer oldWriter, HtmlWriter out, Object value) throws IOException
206-
{
207-
throw new IllegalStateException("Must override renderInputHtml()");
208-
}
174+
public abstract void renderInputHtml(RenderContext ctx, HtmlWriter out, Object value);
209175

210176
public @Nullable HtmlString getTitle(RenderContext ctx)
211177
{

api/src/org/labkey/api/data/MVDisplayColumn.java

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@
2020
import org.labkey.api.util.HtmlString;
2121
import org.labkey.api.util.HtmlStringBuilder;
2222
import org.labkey.api.util.PageFlowUtil;
23+
import org.labkey.api.util.element.Select.SelectBuilder;
2324
import org.labkey.api.writer.HtmlWriter;
2425

25-
import java.io.IOException;
26-
import java.io.Writer;
2726
import java.util.Set;
2827

2928
import static org.labkey.api.util.DOM.Attribute.style;
@@ -180,57 +179,37 @@ public void renderInputHtml(RenderContext ctx, HtmlWriter out, Object value)
180179
{
181180
DIV(at(style,"margin-top:5px")).appendTo(out);
182181
super.renderInputHtml(ctx, out, value);
183-
try
184-
{
185-
renderMVPicker(ctx, out);
186-
}
187-
catch (IOException e)
188-
{
189-
throw new RuntimeException(e);
190-
}
182+
renderMVPicker(ctx, out);
191183
}
192184

193-
private void renderMVPicker(RenderContext ctx, HtmlWriter out) throws IOException
185+
private void renderMVPicker(RenderContext ctx, HtmlWriter out)
194186
{
195-
Writer oldWriter = out.unwrap();
196187
String formFieldName = ctx.getForm().getFormFieldName(mvIndicatorColumn);
197188
String selectedMvIndicator = getMvIndicator(ctx);
198189
Set<String> mvIndicators = MvUtil.getMvIndicators(ctx.getContainer());
199-
oldWriter.write("Missing Value Indicator:&nbsp;");
200-
oldWriter.write("<select style=\"margin-bottom:5px; margin-top:2px\"");
201-
outputName(ctx, oldWriter, formFieldName);
202-
if (isDisabledInput())
203-
oldWriter.write(" DISABLED");
204-
oldWriter.write(">\n");
205-
oldWriter.write("<option value=\"\"></option>");
206-
for (String mvIndicator : mvIndicators)
207-
{
208-
oldWriter.write(" <option value=\"");
209-
oldWriter.write(mvIndicator);
210-
oldWriter.write("\"");
211-
if (null != selectedMvIndicator && mvIndicator.equals(selectedMvIndicator))
212-
oldWriter.write(" selected ");
213-
oldWriter.write(" >");
214-
oldWriter.write(mvIndicator);
215-
oldWriter.write("</option>\n");
216-
}
217-
oldWriter.write("</select>");
218-
// disabled inputs are not posted with the form, so we output a hidden form element:
219-
//if (isDisabledInput())
220-
// renderHiddenFormInput(ctx, out, formFieldName, value);
221-
}
222-
223-
private void outputName(RenderContext ctx, Writer out, String formFieldName) throws IOException
224-
{
225-
out.write(" name=\"");
226-
out.write(PageFlowUtil.filter(formFieldName));
227-
out.write("\"");
228-
229190
String setFocusId = (String)ctx.get("setFocusId");
230191
if (null != setFocusId)
231192
{
232-
out.write(" id=\"" + PageFlowUtil.filter(setFocusId) + "\"");
233193
ctx.remove("setFocusId");
234194
}
195+
196+
out.write("Missing Value Indicator:");
197+
out.write(HtmlString.NBSP);
198+
199+
new SelectBuilder()
200+
.addStyle("margin-bottom:5px")
201+
.addStyle("margin-top:2px")
202+
.className(null)
203+
.name(formFieldName)
204+
.id(setFocusId)
205+
.disabled(isDisabledInput())
206+
.addOption("")
207+
.addOptions(mvIndicators)
208+
.selected(selectedMvIndicator)
209+
.appendTo(out);
210+
211+
// disabled inputs are not posted with the form, so we output a hidden form element:
212+
//if (isDisabledInput())
213+
// renderHiddenFormInput(ctx, out, formFieldName, value);
235214
}
236215
}

api/src/org/labkey/api/data/SimpleDisplayColumn.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ public Object getValue(RenderContext ctx)
110110
}
111111

112112
@Override
113-
public void renderDetailsCellContents(RenderContext ctx, Writer oldWriter, HtmlWriter out) throws IOException
113+
public void renderDetailsCellContents(RenderContext ctx, HtmlWriter out)
114114
{
115115
Object value = getValue(ctx);
116116
if (value != null)
117-
oldWriter.write(value.toString());
117+
out.write(value.toString());
118118
}
119119

120120
@Override
@@ -167,12 +167,6 @@ public void renderInputHtml(RenderContext ctx, HtmlWriter out, Object value)
167167
throw new UnsupportedOperationException("Non Bound columns not editable for " + this);
168168
}
169169

170-
@Override
171-
public void renderInputHtml(RenderContext ctx, Writer oldWriter, HtmlWriter out, Object value) throws IOException
172-
{
173-
throw new UnsupportedOperationException("Non Bound columns not editable for " + this);
174-
}
175-
176170
@Override
177171
public @NotNull HtmlString getTitle(RenderContext ctx)
178172
{

api/src/org/labkey/api/query/PropertiesDisplayColumn.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ public boolean isEditable()
279279
}
280280

281281
@Override
282-
public void renderInputHtml(RenderContext ctx, Writer oldWriter, HtmlWriter out, Object value)
282+
public void renderInputHtml(RenderContext ctx, HtmlWriter out, Object value)
283283
{
284284
// no-op
285285
}

api/src/org/labkey/api/study/actions/StudyPickerColumn.java

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,19 @@
2626
import org.labkey.api.security.permissions.ReadPermission;
2727
import org.labkey.api.study.Study;
2828
import org.labkey.api.study.publish.StudyPublishService;
29+
import org.labkey.api.util.DOM.Renderable;
2930
import org.labkey.api.util.HtmlString;
3031
import org.labkey.api.util.PageFlowUtil;
3132
import org.labkey.api.util.element.Input;
3233
import org.labkey.api.util.element.Option.OptionBuilder;
3334
import org.labkey.api.util.element.Select.SelectBuilder;
3435
import org.labkey.api.writer.HtmlWriter;
3536

36-
import java.io.IOException;
37-
import java.io.Writer;
3837
import java.util.Set;
3938

39+
import static org.labkey.api.util.DOM.TD;
40+
import static org.labkey.api.util.DOM.cl;
41+
4042
public class StudyPickerColumn extends UploadWizardAction.InputDisplayColumn
4143
{
4244
ColumnInfo _colInfo;
@@ -69,29 +71,25 @@ public void renderDetailsCaptionCell(RenderContext ctx, HtmlWriter out, @Nullabl
6971
if (null == _caption)
7072
return;
7173

72-
Writer oldWriter = out.unwrap();
73-
try
74-
{
75-
oldWriter.write("<td class=\"" + (cls != null ? cls : "lk-form-label") + "\">");
76-
oldWriter.write(getTitle(ctx).toString());
77-
int mode = ctx.getMode();
78-
if (mode == DataRegion.MODE_INSERT || mode == DataRegion.MODE_UPDATE)
79-
{
80-
if (_colInfo != null)
74+
TD(
75+
cl(cls != null ? cls : "lk-form-label"),
76+
getTitle(ctx),
77+
(Renderable) ret -> {
78+
int mode = ctx.getMode();
79+
if (mode == DataRegion.MODE_INSERT || mode == DataRegion.MODE_UPDATE)
8180
{
82-
String helpPopupText = ((_colInfo.getFriendlyTypeName() != null) ? "Type: " + _colInfo.getFriendlyTypeName() + "\n" : "") +
83-
((_colInfo.getDescription() != null) ? "Description: " + _colInfo.getDescription() + "\n" : "");
84-
PageFlowUtil.popupHelp(HtmlString.of(helpPopupText), _colInfo.getName());
85-
if (!_colInfo.isNullable())
86-
oldWriter.write(" *");
81+
if (_colInfo != null)
82+
{
83+
String helpPopupText = ((_colInfo.getFriendlyTypeName() != null) ? "Type: " + _colInfo.getFriendlyTypeName() + "\n" : "") +
84+
((_colInfo.getDescription() != null) ? "Description: " + _colInfo.getDescription() + "\n" : "");
85+
out.write(PageFlowUtil.popupHelp(HtmlString.of(helpPopupText), _colInfo.getName()));
86+
if (!_colInfo.isNullable())
87+
out.write(" *");
88+
}
8789
}
90+
return ret;
8891
}
89-
oldWriter.write("</td>");
90-
}
91-
catch (IOException e)
92-
{
93-
throw new RuntimeException(e);
94-
}
92+
).appendTo(out);
9593
}
9694

9795
protected boolean isDisabledInput()

api/src/org/labkey/api/util/element/Input.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.labkey.api.util.element;
1717

1818
import org.apache.commons.lang3.StringUtils;
19+
import org.jetbrains.annotations.NotNull;
1920
import org.jetbrains.annotations.Nullable;
2021
import org.labkey.api.data.RenderContext;
2122
import org.labkey.api.util.HasHtmlString;
@@ -122,6 +123,7 @@ public static State get(String stateName)
122123
private final @Nullable HtmlString _value;
123124
private final Integer _tabIndex;
124125
private final List<String> _styles;
126+
private final List<InputBuilder.DataAttribute> _dataAttributes;
125127

126128
protected Input(InputBuilder<?> builder)
127129
{
@@ -169,6 +171,7 @@ protected Input(InputBuilder<?> builder)
169171
_needsWrapping = builder._needsWrapping == null || builder._needsWrapping;
170172
_tabIndex = builder._tabIndex;
171173
_styles = builder._styles;
174+
_dataAttributes = builder._dataAttributes;
172175
}
173176

174177
public String getAutoComplete()
@@ -406,6 +409,11 @@ public List<String> getStyles()
406409
return _styles;
407410
}
408411

412+
private List<InputBuilder.DataAttribute> getDataAttributes()
413+
{
414+
return _dataAttributes;
415+
}
416+
409417
@Override
410418
public void render(RenderContext ctx, Writer out) throws IOException
411419
{
@@ -566,6 +574,7 @@ protected void doInput(Appendable sb) throws IOException
566574
sb.append(" tabIndex=\"").append(h(_tabIndex)).append("\"");
567575

568576
doStyles(sb);
577+
doDataAttributes(sb);
569578
if (!HtmlString.isBlank(getValue()))
570579
sb.append(" value=\"").append(h(getValue())).append("\"");
571580
doInputEvents(id);
@@ -610,6 +619,20 @@ protected void doStyles(Appendable sb) throws IOException
610619
}
611620
}
612621

622+
protected void doDataAttributes(Appendable sb)
623+
{
624+
getDataAttributes().forEach(attr -> {
625+
try
626+
{
627+
sb.append(" data-").append(attr.name()).append("=\"").append(h(attr.value())).append("\"");
628+
}
629+
catch (IOException io)
630+
{
631+
UnexpectedException.rethrow(io);
632+
}
633+
});
634+
}
635+
613636
protected void doInputEvents(String id)
614637
{
615638
var pageConfig = HttpView.currentPageConfig();
@@ -760,6 +783,9 @@ public static class InputBuilder<T extends InputBuilder<T>> implements HasHtmlSt
760783
private Integer _tabIndex;
761784

762785
private final List<String> _styles = new LinkedList<>();
786+
private final List<DataAttribute> _dataAttributes = new LinkedList<>();
787+
788+
private record DataAttribute(String name, String value) {}
763789

764790
public InputBuilder()
765791
{
@@ -1061,6 +1087,13 @@ public T addStyles(List<String> styles)
10611087
return (T)this;
10621088
}
10631089

1090+
// Add an arbitrary "data-" attribute. Name should not include "data-"... the builder will add prefix.
1091+
public T addDataAttribute(@NotNull String name, @NotNull String value)
1092+
{
1093+
_dataAttributes.add(new DataAttribute(name, value));
1094+
return (T)this;
1095+
}
1096+
10641097
public Input build()
10651098
{
10661099
return new Input(this);

api/src/org/labkey/api/util/element/Select.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ protected void doInput(Appendable sb) throws IOException
7171

7272
doStyles(sb);
7373

74+
doDataAttributes(sb);
75+
7476
doInputEvents(id);
7577

7678
if (isDisabled())

0 commit comments

Comments
 (0)