Project

General

Profile

« Previous | Next » 

Revision 3320

Added by barteau over 17 years ago

Initial checkin. The "Client View HTML Helper", to provide HTML specific methods for client views. This is an API class for the clientview package.

View differences:

src/edu/ucsb/nceas/metacat/clientview/ClientHtmlHelper.java
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='%1$s' style='%2$s' size='%3$s'>\n%4$s</select>\n";
22
    private static final String                OPTION_TEMPLATE = "<option value='%1$s'>%2$s</option>\n";
23
    private static final String                OPTGRP_TEMPLATE = "<optgroup label='%1$s'>%2$s</optgroup>";
24
    private static final String                INPUT_TEMPLATE = "<input name='%1$s' value='%2$s' type='%4$s' class='%3$s' size='%5$s'/>\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;
38
        Iterator<String>            iterIt;
39
        Object                      obj;
40
        Vector                      vector;
41
        Iterator                    completeIterIt;
42
        
43
        iterIt = map.keySet().iterator();
44
        while(iterIt.hasNext()) {
45
            key = iterIt.next();
46
            obj = map.get(key);
47
            if (obj instanceof String) {
48
                item = (String) obj;
49
                item = String.format(OPTION_TEMPLATE, key, item);
50
                result += item;
51
            } else if (obj instanceof Vector) {
52
                vector = (Vector) obj;
53
                optGrp = "";
54
                completeIterIt = vector.iterator();
55
                while (completeIterIt.hasNext()) {
56
                    item = (String) completeIterIt.next();
57
                    item = String.format(OPTION_TEMPLATE, item, item);
58
                    optGrp += item;
59
                }
60
                item = String.format(OPTGRP_TEMPLATE, key, optGrp);
61
                result += item;
62
            }
63
        }
64
        result = String.format(SELECT_TEMPLATE, name, style, size, result);
65
        return(result);
66
    }
67
    
68
}
0 69

  

Also available in: Unified diff