We defined our own MultiSelectDisplayComponent and renderer. Then we used it in a Data Utility to display a multi-select list of values.
MultiSelectDisplayComponent.java:
import com.ptc.core.components.rendering.guicomponents.*;
import java.util.ArrayList;
public class MultiSelectDisplayComponent extends AttributeGuiComponent implements PrintableComponent
{
private static final long serialVersionUID = 1L;
String attributeName;
int numberOfDisplayLines = 0;
ArrayList<String> keyValues;
ArrayList<String> displayValues;
ArrayList<String> selectedValues;
public MultiSelectDisplayComponent( String attrName, ArrayList<String> keys, ArrayList<String> displayV, ArrayList<String> selectedV, int numLines)
{
attributeName = attrName;
keyValues = keys;
numberOfDisplayLines = numLines;
selectedValues = selectedV;
displayValues = displayV;
setRenderer(new MultiSelectDisplayComponentRenderer());
}
public String getPrintableValue()
{
return "";
}
}
----------------------------------------------------------------
MultiSelectDisplayComponentRenderer.java:
import com.ptc.core.components.rendering.*;
import java.io.PrintWriter;
import java.util.Vector;
import wt.util.WTContext;
import java.lang.reflect.Method;
import wt.util.WTStringUtilities;
import wt.fc.EnumeratedType;
public class MultiSelectDisplayComponentRenderer extends AbstractRenderer
{
private static final long serialVersionUID = 1L;
public MultiSelectDisplayComponentRenderer()
{
}
protected void renderObject(Object obj, PrintWriter pw, RenderingContext rc)
throws RenderingException
{
renderObject((MultiSelectDisplayComponent)obj, pw, rc);
}
protected void renderObject(MultiSelectDisplayComponent comp, PrintWriter pw, RenderingContext rc)
throws RenderingException
{
write(pw, buildMultiSelectComponent(comp));
}
protected String buildMultiSelectComponent(MultiSelectDisplayComponent dispComp)
{
//System.out.println("+++ In MultiSelectDisplayComponentRenderer.buildMultiSelectComponent()");
String htmlString;
Vector<String> theList = new Vector<String>();
String s1 = null;
int numLines = dispComp.numberOfDisplayLines;
if (numLines <= 1 ) numLines = 4;
if (dispComp.selectedValues != null) {
for (int j=0; j<dispComp.selectedValues.size(); j++){
theList.add(dispComp.selectedValues.get(j));
}
}
htmlString = "<select ";
htmlString = htmlString + "name= \"" + dispComp.attributeName + "\" id = \"" + dispComp.attributeName + "\"";
if (dispComp.isRequired()) {
htmlString = htmlString + " class = \"required \"";
}
htmlString = htmlString + " multiple size=" + numLines;
htmlString = htmlString + ">\n";
try {
for (int k=0;k< dispComp.keyValues.size(); k++) {
if ( dispComp.selectedValues == null ) {
String stringV = dispComp.keyValues.get(k);
String displayV = dispComp.displayValues.get(k);
htmlString +=" <OPTION VALUE=\"" + stringV + "\">" + displayV + "\n";
} else {
boolean selected;
String stringV = dispComp.keyValues.get(k);
String displayV = dispComp.displayValues.get(k);
if ( dispComp.selectedValues != null) {
selected = dispComp.selectedValues.contains(stringV);
} else {
selected = false;
}
if (selected) {
htmlString +=" <OPTION VALUE=\"" + stringV + "\" selected>" + displayV + "\n";
} else {
htmlString +=" <OPTION VALUE=\"" + stringV + "\">" + displayV + "\n";
}
}
}
} catch ( Exception ex ) {
ex.printStackTrace();
}
htmlString += "</select>";
//System.out.println("+++ Leaving MultiSelectDisplayComponentRenderer.buildMultiSelectComponent()");
return htmlString;
}
protected boolean isValidForObject(Object obj)
{
return obj instanceof MultiSelectDisplayComponent;
}
}
------------------------------------------------------------------
Usage in Data Utility.getDataValue method:
MultiSelectDisplayComponent multiSelectComp = new MultiSelectDisplayComponent(compId, keyValues, displayValues, selectedValues, listSize);
multiSelectComp.setRequired(isRequired);
return multiSelectComp;