001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032: package com.vividsolutions.jump.workbench.ui;
033:
034: import java.awt.Color;
035: import java.util.Collection;
036: import java.util.Iterator;
037: import java.util.Map;
038:
039: import com.vividsolutions.jts.geom.Geometry;
040: import com.vividsolutions.jump.I18N;
041: import com.vividsolutions.jump.feature.Feature;
042: import com.vividsolutions.jump.workbench.model.Layer;
043:
044: public class FeatureInfoWriter {
045: public static interface Writer {
046: public String toHTML(Feature feature);
047: }
048:
049: public static Writer ATTRIBUTE_WRITER = new Writer() {
050: public String toHTML(Feature feature) {
051: StringBuffer s = new StringBuffer();
052: for (int i = 0; i < feature.getSchema().getAttributeCount(); i++) {
053: if (feature.getAttribute(i) instanceof Geometry) {
054: continue;
055: }
056: s.append("<br><b>"
057: + GUIUtil.escapeHTML(feature.getSchema()
058: .getAttributeName(i), false, false)
059: + ":</b> ");
060: if (feature.getAttribute(i) == null) {
061: //do nothing
062: } else {
063: s.append(GUIUtil.escapeHTML(feature.getAttribute(i)
064: .toString(), false, false));
065: }
066: }
067: return s.toString();
068: }
069: };
070:
071: public static Writer EMPTY_WRITER = new Writer() {
072: public String toHTML(Feature feature) {
073: return "";
074: }
075: };
076:
077: //Row-stripe colour recommended in
078: //Java Look and Feel Design Guidelines: Advanced Topics [Jon Aquino]
079: private final static String BEIGE = "#E6E6E6";
080: private final static String WHITE = "#FFFFFF";
081: private final static String COLOR1 = BEIGE;
082: private final static String COLOR2 = WHITE;
083:
084: private boolean workingAroundJEditorPaneBug = true;
085:
086: public Color sidebarColor(Layer layer) {
087: Color basicColor = layer.getBasicStyle().isRenderingFill() ? layer
088: .getBasicStyle().getFillColor()
089: : layer.getBasicStyle().getLineColor();
090: int alpha = layer.getBasicStyle().getAlpha();
091: return GUIUtil.toSimulatedTransparency(GUIUtil.alphaColor(
092: basicColor, alpha));
093: }
094:
095: public String writeGeom(Map layerToFeaturesMap,
096: Writer featureWriter, Writer attributeWriter) {
097: if (layerToFeaturesMap.isEmpty()) {
098: return "";
099: }
100: StringBuffer stringBuffer = new StringBuffer();
101: for (Iterator i = layerToFeaturesMap.keySet().iterator(); i
102: .hasNext();) {
103: Layer layer = (Layer) i.next();
104: Collection features = (Collection) layerToFeaturesMap
105: .get(layer);
106: stringBuffer.append("<table width=100%>");
107: stringBuffer.append(" <tr>");
108: stringBuffer.append(" <td width=5 bgcolor="
109: + toHTML(sidebarColor(layer)) + ">");
110: stringBuffer.append(" </td>");
111: //I'm setting the column 1 width to 5 and the column 2 width to 100%.
112: //Theoretically column 2 should smush column 1, but this isn't happening.
113: //I hope this behaviour will continue in future Java versions.
114: //(See Dahm, Tom. "HTML Tip: Making a Wild Card Column Width." March 2000.
115: //Available from http://www.netmechanic.com/news/vol3/html_no3.htm.
116: //Internet; accessed 29 October 2002.)
117: //[Jon Aquino]
118: stringBuffer.append(" <td width=100%>");
119: stringBuffer.append(" <table width=100%>");
120: stringBuffer.append(" <tr>");
121: stringBuffer.append(" <td bgcolor=#FFFFCC>");
122: stringBuffer.append(" <B>" + layer.getName()
123: + "</B>");
124: stringBuffer.append(" </td>");
125: stringBuffer.append(" </tr>");
126: String bgcolor = COLOR1;
127: for (Iterator j = features.iterator(); j.hasNext();) {
128: Feature feature = (Feature) j.next();
129: if (!bgcolor.equals(COLOR1)) {
130: bgcolor = COLOR1;
131: } else {
132: bgcolor = COLOR2;
133: }
134: stringBuffer.append(" <tr bgcolor='" + bgcolor
135: + "'>");
136: stringBuffer.append(" <td>");
137: stringBuffer
138: .append(" FID <font color='#3300cc'><b>"
139: + feature.getID() + "</b></font>");
140: if (featureWriter != EMPTY_WRITER) {
141: append(feature, stringBuffer, featureWriter);
142: }
143: if (attributeWriter != EMPTY_WRITER) {
144: stringBuffer.append(" "
145: + attributeWriter.toHTML(feature));
146: }
147: stringBuffer.append(" </td>");
148: stringBuffer.append(" </tr>");
149: }
150: stringBuffer.append(" </table>");
151: stringBuffer.append(" </td>");
152: stringBuffer.append(" </tr>");
153: stringBuffer.append("</table>");
154: }
155: return stringBuffer.toString();
156: }
157:
158: private String pad(String s) {
159: return (s.length() == 1) ? ("0" + s) : s;
160: }
161:
162: private String toHTML(Color color) {
163: String colorString = "#";
164: colorString += pad(Integer.toHexString(color.getRed()));
165: colorString += pad(Integer.toHexString(color.getGreen()));
166: colorString += pad(Integer.toHexString(color.getBlue()));
167: return colorString;
168: }
169:
170: private void append(Feature feature, StringBuffer stringBuffer,
171: Writer featureWriter) {
172: String text = featureWriter.toHTML(feature);
173: if (workingAroundJEditorPaneBug
174: && ((stringBuffer.length() + featureWriter.toHTML(
175: feature).length()) > (32768 - 2000))) {
176: //See http://developer.java.sun.com/developer/bugParade/bugs/4775730.html. [Jon Aquino]
177: text = I18N
178: .get("ui.FeatureInfoWriter.text-representation-of-geometry-is-too-large");
179: }
180: stringBuffer.append(text);
181: }
182:
183: }
|