The following method is from a class that extends DefaultComboBoxModel
:
public void addGroups() {
removeAllElements();
ListModel listModel = ((AutoDataProvider) dataProvider).getXmlJobCustom().getJobList();
ImageIcon imageIcon;
GroupModel groupModel;
for (int i = 0;i < listModel.getSize();i++) {
XmlJobCustomElem elementAt = (XmlJobCustomElem) listModel.getElementAt(i);
String group = elementAt.getGroup();
if(StringUtils.isNotEmpty(group)) {
if (group.equalsIgnoreCase("WebServices")){
imageIcon = Res.getIcon(Constants.ICO + "$WS");
}
else if (group.equalsIgnoreCase("file transfer") || group.equalsIgnoreCase("transfert de fichiers")){
imageIcon = Res.getIcon(Constants.ICO + "$FT");
}
else if (group.equalsIgnoreCase("json")){
imageIcon = Res.getIcon(Constants.ICO + "$JSON");
}
else if (group.toLowerCase().contains("aws")){
imageIcon = Res.getIcon(Constants.ICO + "$AWS");
} else {
imageIcon = Res.getDefIcon(Res.getUserLocalPath() + "/" + XmlJobCustom.ICONS_DIR + "/" + XmlJobCustom.ICONS + elementAt.getId(), Constants.ICO + "$Job.Type." + Job.JOB_CUSTOM);
}
groupModel = new GroupModel(imageIcon, group);
if (getIndexOf(groupModel) ==-1 ){
addElement(groupModel);
}
} else if (StringUtils.isEmpty(group)){
imageIcon = Res.getIcon(Constants.ICO + "$Job.Type" +"."+ 0);
groupModel = new GroupModel(imageIcon, "Default");
if (getIndexOf(groupModel) == -1 ){
addElement(groupModel);
}
}
}
}
Explanation: What I'm doing here is analyse the information that I'm getting from an xml file that contains several elements. Here's an example:
<Job id="job password" name="job password" group="AWS S3">
<params>
<param id="env" name="environment" desc="Python environment" default="default" mandatory="yes"/>
<param id="passwordid" name="passwd" type="password" desc="passwd" mandatory="yes"/>
</params>
<actions>
<action id="start">
<params>
<param prefix="env">env</param>
<param prefix="--connector">Connector</param>
</params>
</action>
</actions>
</Job>
Focus on the first line. Do you see the parameter group
? That's what the search is based on. If it's empty, it means the element belongs to the default
group. If it's not empty, it means I should add it as a new group to the JComboBox of groups with its icon (each element of the JComboBox has an Icon and a String that refers to its name).
Everything is working fine except for one little thing. The elements aren't sorted in alphabetical order which is not practical.
I thought about using Collections.sort(), but the problem here is that I don't have a list or an Array. I have a DefaultComboBoxModel
that has no methods to sort its elements that way.
Any suggestions?