001: // /////////////////////////////
002: // Makumba, Makumba tag library
003: // Copyright (C) 2000-2003 http://www.makumba.org
004: //
005: // This library is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU Lesser General Public
007: // License as published by the Free Software Foundation; either
008: // version 2.1 of the License, or (at your option) any later version.
009: //
010: // This library is distributed in the hope that it will be useful,
011: // but WITHOUT ANY WARRANTY; without even the implied warranty of
012: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: // Lesser General Public License for more details.
014: //
015: // You should have received a copy of the GNU Lesser General Public
016: // License along with this library; if not, write to the Free Software
017: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: //
019: // -------------
020: // $Id: ptrEditor.java 2119 2007-11-22 01:12:14Z cristian_bogdan $
021: // $Name$
022: /////////////////////////////////////
023:
024: package org.makumba.forms.html;
025:
026: import java.util.Dictionary;
027: import java.util.Iterator;
028: import java.util.Vector;
029:
030: import org.makumba.Transaction;
031: import org.makumba.Pointer;
032: import org.makumba.commons.formatters.FieldFormatter;
033: import org.makumba.commons.formatters.RecordFormatter;
034: import org.makumba.providers.DataDefinitionProvider;
035: import org.makumba.providers.TransactionProvider;
036:
037: public class ptrEditor extends choiceEditor {
038:
039: private static final class SingletonHolder {
040: static final FieldEditor singleton = new ptrEditor();
041: }
042:
043: /** Don't use this, use getInstance() */
044: protected ptrEditor() {
045: }
046:
047: public static FieldFormatter getInstance() {
048: return SingletonHolder.singleton;
049: }
050:
051: public void onStartup(RecordFormatter rf, int fieldIndex) {
052: ((RecordEditor) rf).db[fieldIndex] = ((RecordEditor) rf).database;
053: ((RecordEditor) rf).query[fieldIndex] = "SELECT choice as choice, choice."
054: + rf.dd.getFieldDefinition(fieldIndex).getTitleField()
055: + " as title FROM "
056: + rf.dd.getFieldDefinition(fieldIndex).getPointedType()
057: .getName() + " choice ORDER BY title";
058: }
059:
060: public Object getOptions(RecordFormatter rf, int fieldIndex,
061: Dictionary formatParams) {
062: ChoiceSet c = (ChoiceSet) formatParams.get(ChoiceSet.PARAMNAME);
063: if (c != null)
064: return c;
065:
066: Vector v = null;
067:
068: Transaction dbc = new TransactionProvider()
069: .getConnectionTo(((RecordEditor) rf).db[fieldIndex]);
070: try {
071: v = dbc.executeQuery(((RecordEditor) rf).query[fieldIndex],
072: null);
073: } finally {
074: dbc.close();
075: }
076: c = new ChoiceSet();
077: if (nullOption != null) {
078: c.add("", nullOption, false, false);
079: }
080: for (Iterator i = v.iterator(); i.hasNext();) {
081: Dictionary d = (Dictionary) i.next();
082: Object ttl = d.get("title");
083: if (ttl == null) {
084: Pointer ptr = (Pointer) d.get("choice");
085: // FIXME ? maybe just displayed the field as untitled?
086: // ttl = "untitled [" + ptr.toExternalForm() + "]";
087: throw new org.makumba.ProgrammerError(
088: "Object "
089: + ptr
090: + " (external ID: "
091: + ptr.toExternalForm()
092: + ") has a null value for the title-field ("
093: + ptr.getType()
094: + "."
095: + new DataDefinitionProvider()
096: .getDataDefinition(
097: ptr.getType())
098: .getTitleFieldName()
099: + "), and can't be displayed in the drop-down.\nEither make sure you have no null values in this field, or use a different field for the title display, using the '!title=' directive in the MDD.");
100: }
101: c.add(d.get("choice"), ttl.toString(), false, false);
102: }
103: return c;
104: }
105:
106: public int getOptionsLength(RecordFormatter rf, int fieldIndex,
107: Object opts) {
108: return ((ChoiceSet) opts).size();
109: }
110:
111: public Object getOptionValue(RecordFormatter rf, int fieldIndex,
112: Object options, int i) {
113: return ((ChoiceSet.Choice) ((ChoiceSet) options).get(i))
114: .getValue();
115: }
116:
117: public String formatOptionValue(RecordFormatter rf, int fieldIndex,
118: Object val) {
119: if (val == Pointer.Null)
120: return "";
121: if (val instanceof String) {
122: // for reloaded forms (via ControllerFilter) check needed
123: // val is String when a null option is selected in mak:input (with mak:option)
124: return (String) val;
125: }
126: return ((Pointer) val).toExternalForm();
127: }
128:
129: public String formatOptionValue(RecordFormatter rf, int fieldIndex,
130: Object opts, int i, Object val) {
131: return formatOptionValue(rf, fieldIndex, val);
132: }
133:
134: public String formatOptionTitle(RecordFormatter rf, int fieldIndex,
135: Object options, int i) {
136: return ""
137: + ((ChoiceSet.Choice) ((ChoiceSet) options).get(i))
138: .getTitle();
139: }
140:
141: public Object readFrom(RecordFormatter rf, int fieldIndex,
142: org.makumba.commons.attributes.HttpParameters p,
143: String suffix) {
144: Object o = super .readFrom(rf, fieldIndex, p, suffix);
145: if ("".equals(o))
146: return null;
147: return o;
148: }
149:
150: public String getMultiple(RecordFormatter rf, int fieldIndex) {
151: return "";
152: }
153:
154: public boolean isMultiple(RecordFormatter rf, int fieldIndex) {
155: return false;
156: }
157:
158: public int getDefaultSize(RecordFormatter rf, int fieldIndex) {
159: return 1;
160: }
161: }
|