package com.c2kernel.gui.tabs.outcome.form.field; import java.awt.Component; import java.util.Enumeration; import java.util.StringTokenizer; import javax.swing.DefaultComboBoxModel; import javax.swing.JComboBox; import javax.swing.text.JTextComponent; import org.exolab.castor.types.AnyNode; import org.exolab.castor.xml.schema.AttributeDecl; import org.exolab.castor.xml.schema.ElementDecl; import org.exolab.castor.xml.schema.Facet; import org.exolab.castor.xml.schema.SimpleType; import com.c2kernel.gui.tabs.outcome.form.StructuralException; import com.c2kernel.scripting.Script; import com.c2kernel.utils.Logger; /******************************************************************************* * * $Revision: 1.4 $ $Date: 2005/08/16 13:59:56 $ * * Copyright (C) 2003 CERN - European Organization for Nuclear Research All * rights reserved. ******************************************************************************/ public class ComboField extends StringEditField { JComboBox comboField; ListOfValues vals; DefaultComboBoxModel comboModel; AnyNode listNode; public ComboField(SimpleType type, AnyNode listNode) { super(); comboField = new JComboBox(); content = type; this.listNode = listNode; createLOV(); } public String getDefaultValue() { if (vals.getDefaultKey() != null) return vals.get(vals.getDefaultKey()).toString(); else return ""; } public String getText() { return vals.get(comboModel.getSelectedItem()).toString(); } public JTextComponent makeTextField() { // not used by this control return null; } public void setText(String text) { comboModel.setSelectedItem(text); } public Component getControl() { return comboField; } private void createLOV() { vals = new ListOfValues(); if (listNode != null) { // schema instructions for list building String lovType = listNode.getLocalName(); String param = listNode.getFirstChild().getStringValue(); if (lovType.equals("ScriptList")) populateLOVFromScript(param); if (lovType.equals("PathList")) populateLOVFromLDAP(param); } // handle enumerations // TODO: should be ANDed with above results if (content.hasFacet(Facet.ENUMERATION)) { //ListOfValues andList = new ListOfValues(); Enumeration enums = content.getFacets(Facet.ENUMERATION); while (enums.hasMoreElements()) { Facet thisEnum = (Facet)enums.nextElement(); vals.put(thisEnum.getValue(), thisEnum.getValue(), false); } } comboModel = new DefaultComboBoxModel(vals.keySet().toArray()); comboModel.setSelectedItem(vals.getDefaultKey()); comboField.setModel(comboModel); } /** * @param param */ private void populateLOVFromLDAP(String param) { // TODO '/root/path;prop=val;prop=val' } private void populateLOVFromScript(String scriptName) { try { StringTokenizer tok = new StringTokenizer(scriptName, "_"); if (tok.countTokens() != 2) throw new Exception("Invalid LOVScript name"); Script lovscript = new Script(tok.nextToken(), Integer.parseInt(tok.nextToken())); lovscript.setInputParamValue("LOV", vals); lovscript.execute(); } catch (Exception ex) { Logger.exceptionDialog(ex); } } public void setDecl(AttributeDecl model) throws StructuralException { super.setDecl(model); createLOV(); } public void setDecl(ElementDecl model) throws StructuralException { super.setDecl(model); createLOV(); } /** * */ public void setEditable(boolean editable) { comboField.setEditable(editable); } }