001: /*******************************************************************************
002: * Copyright (c) 2007 MyGWT.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Darrell Meyer <darrell@mygwt.net> - initial API and implementation
010: *******************************************************************************/package net.mygwt.ui.client.viewer;
011:
012: import java.util.HashMap;
013: import java.util.Map;
014:
015: import net.mygwt.ui.client.MyDOM;
016: import net.mygwt.ui.client.util.Format;
017:
018: /**
019: * A label provider for a <code>TemplateViewer</code>. This label provider
020: * differs from other label providers as it returns a HTML fragment to be used
021: * to generate the element. Typically, index or key based substitution is used
022: * to generate the HTML for each element.
023: *
024: * @see TemplateViewer
025: */
026: public abstract class TemplateLabelProvider implements
027: IBaseLabelProvider {
028:
029: /**
030: * The HTML fragment used to create the container element. Optionally, the
031: * container element can be specified by assigning a class name of
032: * "my-container" to the element. This allows the container to be decorated.
033: * Default value is "<div></div>".
034: */
035: protected String containerTemplate = "<div class='my-container'></div>";
036:
037: /**
038: * The HTML template used to render each element. All elements MUST be
039: * assigned an id. Default value is "<div id='{id}'>{text}</div>".
040: */
041: protected String template = "<div id='{id}'>{text}</div>";
042:
043: /**
044: * Creates a new label provider.
045: */
046: public TemplateLabelProvider() {
047:
048: }
049:
050: /**
051: * Creates a new label provider with the given template.
052: *
053: * @param template the template
054: */
055: public TemplateLabelProvider(String template) {
056: this .template = template;
057: }
058:
059: /**
060: * Returns the style that is added to the selected element.
061: *
062: * @return the select style
063: */
064: public abstract String getSelectStyle();
065:
066: /**
067: * Returns the style that is added to a element under the mouse. Default
068: * implementation returns <code>null</code>.
069: *
070: * @return the over style
071: */
072: public String getOverStyle() {
073: return null;
074: }
075:
076: /**
077: * Returns the value to be displayed in the form field for the element.
078: *
079: * @param elem the element
080: * @return the value
081: */
082: public String getValue(Object elem) {
083: return elem.toString();
084: }
085:
086: /**
087: * Returns the template.
088: *
089: * @return the template
090: */
091: public String getTemplate() {
092: return template;
093: }
094:
095: /**
096: * Sets the template.
097: *
098: * @param template the html template
099: */
100: public void setTemplate(String template) {
101: this .template = template;
102: }
103:
104: /**
105: * Returns the HTML fragment used to create the container component.
106: * Optionally, the container element can be specified by assigning a class
107: * name of "my-container" to the element. This allows the container to be
108: * decorated. Default value is "<div></div>".
109: *
110: * @return the container html
111: */
112: protected String render() {
113: return containerTemplate;
114: }
115:
116: /**
117: * Renders the current element.
118: *
119: * @param elem the model element
120: * @return the tempalte html
121: */
122: protected String renderElement(Object elem) {
123: return Format.substitute(template, getParameters(elem));
124: }
125:
126: /**
127: * Returns the paramters to be used when rendering an element.
128: *
129: * @param elem the element
130: * @return the parameters
131: */
132: protected Map getParameters(Object elem) {
133: Map params = new HashMap();
134: params.put("id", MyDOM.getUniqueID());
135: params.put("text", elem.toString());
136: return params;
137: }
138:
139: }
|