Project

General

Profile

1
/*
2
 * ClientHtmlHelper.java
3
 *
4
 * Created on June 25, 2007, 9:58 AM
5
 *
6
 * To change this template, choose Tools | Template Manager
7
 * and open the template in the editor.
8
 */
9

    
10
package edu.ucsb.nceas.metacat.clientview;
11

    
12
import java.util.Iterator;
13
import java.util.Map;
14
import java.util.Vector;
15

    
16
/**
17
 *
18
 * @author barteau
19
 */
20
public abstract class ClientHtmlHelper {
21
    private static final String                SELECT_TEMPLATE = "<select name='%1s' style='%2s' size='%3s'>\n%4s</select>\n";
22
    private static final String                OPTION_TEMPLATE = "<option value='%1s'>%2s</option>\n";
23
    private static final String                OPTGRP_TEMPLATE = "<optgroup label='%1s'>%2s</optgroup>";
24
    private static final String                INPUT_TEMPLATE = "<input name='%1s' value='%2s' type='%4s' class='%3s' size='%5s'/>\n";
25
    
26
    /**
27
     * JSP API: A static helper method which takes a map (key, value pairs) and returns
28
     * an XHTML SELECT String.
29
     * @param map The map contianing the key, value pairs to convert into an HTML SELECT
30
     * statement.
31
     * @param name The name to assign the HTML SELECT, which will become the parameter name.
32
     * @param style Any HTML styling text.
33
     * @param size HTML field width.
34
     * @return String, XHTML for a SELECT statement.
35
     */
36
    public static String mapToHtmlSelect(Map map, String name, String style, int size) {
37
        String                      result = "", item, key, optGrp, tmp;
38
        Iterator                    iterIt;
39
        Object                      obj;
40
        Vector                      vector;
41
        Iterator                    completeIterIt;
42
        
43
        iterIt = map.keySet().iterator();
44
        while(iterIt.hasNext()) {
45
            key = (String) iterIt.next();
46
            obj = map.get(key);
47
            if (obj instanceof String) {
48
                item = (String) obj;
49
                tmp = OPTION_TEMPLATE.replaceFirst("%1s", key);
50
                item = tmp.replaceFirst("%2s", item);
51
                result += item;
52
            } else if (obj instanceof Vector) {
53
                vector = (Vector) obj;
54
                optGrp = "";
55
                completeIterIt = vector.iterator();
56
                while (completeIterIt.hasNext()) {
57
                    item = (String) completeIterIt.next();
58
                    tmp = OPTION_TEMPLATE.replaceFirst("%1s", item);
59
                    item = tmp.replaceFirst("%2s", item);
60
                    optGrp += item;
61
                }
62
                tmp = OPTGRP_TEMPLATE.replaceFirst("%1s", key);
63
                item = tmp.replaceFirst("%2s", optGrp);
64
                result += item;
65
            }
66
        }
67
        tmp = SELECT_TEMPLATE.replaceFirst("%1s", name);
68
        tmp = tmp.replaceFirst("%2s", style);
69
        tmp = tmp.replaceFirst("%3s", String.valueOf(size));
70
        result = tmp.replaceFirst("%4s", result);
71
        return(result);
72
    }
73
    
74
}
(2-2/5)