001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.module.builders;
011:
012: import java.text.SimpleDateFormat;
013: import java.util.*;
014:
015: import org.mmbase.module.core.MMObjectNode;
016: import org.mmbase.module.corebuilders.InsRel;
017: import org.mmbase.util.*;
018: import org.mmbase.util.logging.*;
019:
020: /**
021: * @javadoc
022: * @application Tools
023: * @author David van Zeventer
024: * @version $Id: AnnotRel.java,v 1.22 2005/10/06 17:46:39 michiel Exp $
025: */
026: public class AnnotRel extends InsRel {
027:
028: // Defining possible annotation types
029: public final static int HOURS = 0;
030: public final static int MINUTES = 1;
031: public final static int SECONDS = 2;
032: public final static int MILLIS = 3;
033: /*
034: public final static int LINES = 4;
035: public final static int WORDS = 5;
036: public final static int CHARS = 6;
037: public final static int PIXELS = 7;
038: public final static int ROWS = 8;
039: public final static int COLS = 9;
040: */
041:
042: private static final Logger log = Logging
043: .getLoggerInstance(AnnotRel.class);
044:
045: /**
046: * Sets defaults for a node.
047: * Initializes all numeric fields to 0, and sets the annotation type to {@link #MILLIS}.
048: * @param node The node to set the defaults of.
049: */
050: public void setDefaults(MMObjectNode node) {
051: super .setDefaults(node);
052: // Set the default value for pos and length to 0 (0:0:0.0)
053: node.setValue("pos", 0);
054: node.setValue("end", 0);
055: node.setValue("length", 0);
056: // All time values are default stored in milliseconds.
057: node.setValue("type", MILLIS);
058: }
059:
060: /**
061: * What should a GUI display for this node/field combo.
062: * Displays the pos, end, and length fields as time-values,
063: * and the annotation type field as a descriptive string.
064: * @param node The node to display
065: * @param field the name field of the field to display
066: * @return the display of the node's field as a <code>String</code>, null if not specified
067: */
068: public String getGUIIndicator(String field, MMObjectNode node) {
069: if (field.equals("pos")) {
070: int time = node.getIntValue("pos");
071: return RelativeTime.convertIntToTime(time);
072: } else if (field.equals("end")) {
073: int time = node.getIntValue("end");
074: return RelativeTime.convertIntToTime(time);
075: } else if (field.equals("length")) {
076: int time = node.getIntValue("length");
077: return RelativeTime.convertIntToTime(time);
078: } else if (field.equals("type")) {
079: int val = node.getIntValue("type");
080: if (val == HOURS) {
081: return "Hours";
082: } else if (val == MINUTES) {
083: return "Minuten"; // return "Minutes";
084: } else if (val == SECONDS) {
085: return "Seconden"; // return "Seconds";
086: } else if (val == MILLIS) {
087: return "Milliseconden"; // return "Milliseconds";
088: }
089:
090: /*
091: else if (val==LINES) {
092: return "Regels";
093: } else if (val==WORDS) {
094: return "Woorden";
095: } else if (val==CHARS) {
096: return "Karakters";
097: } else if (val==PIXELS) {
098: return "Pixels";
099: } else if (val==ROWS) {
100: return "Rijen";
101: } else if (val==COLS) {
102: return "Kolommen";
103: }
104: */
105: }
106: return null;
107: }
108:
109: /**
110: * The hook that passes all form related pages to the correct handler.
111: * This method is not supported.
112: * @param sp The PageInfo
113: * @param cmds the commands (PRC-CMD) to process
114: * @param vars variables (PRC-VAR) to use
115: * @return the result value as a <code>String</code>
116: */
117: public boolean process(PageInfo sp, Hashtable cmds, Hashtable vars) {
118: log.debug("process: This method isn't implemented yet.");
119: return false;
120: }
121:
122: /**
123: * Obtains a string value by performing the provided command.
124: * This method is not supported.
125: * @param sp The PageInfo
126: * @param command the command to execute
127: * @return the result value as a <code>String</code>
128: */
129: public String replace(PageInfo sp, StringTokenizer command) {
130: log.debug("replace: This method isn't implemented yet.");
131: return "";
132: }
133:
134: /**
135: * Provides additional functionality when setting field values.
136: * This method makes sure that the pos, end, and length values have the
137: * correct value.
138: * @param node the node whose fields are changed
139: * @param field the fieldname that is changed
140: * @return <code>true</code> if the call was handled.
141: */
142: public boolean setValue(MMObjectNode node, String field) {
143: if (field.equals("end")) {
144: int pos = node.getIntValue("pos");
145: int end = node.getIntValue("end");
146: if (end != -1)
147: node.setValue("length", (end - pos));
148: } else if (field.equals("pos")) {
149: int pos = node.getIntValue("pos");
150: int end = node.getIntValue("end");
151: if (end != -1)
152: node.setValue("length", (end - pos));
153: } else if (field.equals("length")) {
154: // extra check needed to make sure we don't create a loop !
155: // XXX: ???
156: int pos = node.getIntValue("pos");
157: int end = node.getIntValue("end");
158: int len = node.getIntValue("length");
159: }
160: return true;
161: }
162:
163: public Object getValue(MMObjectNode node, String field) {
164: if (field.equals("ms_pos")) {
165: int pos = node.getIntValue("pos");
166: //format to pos is in ms
167: return new SimpleDateFormat("hh:mm.0")
168: .format(new Date(pos));
169: } else if (field.equals("ms_length")) {
170: int len = node.getIntValue("length");
171: return new SimpleDateFormat("hh:mm").format(new Date(len));
172: } else if (field.equals("end")) {
173: int pos = node.getIntValue("pos");
174: int len = node.getIntValue("length");
175: int end = pos + len;
176: return ("" + end);
177: }
178: return (null);
179: }
180: }
|