001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.project.ui.internal.property.pages;
016:
017: import java.io.IOException;
018: import java.util.ArrayList;
019: import java.util.List;
020:
021: import net.refractions.udig.project.internal.Layer;
022: import net.refractions.udig.project.ui.internal.Messages;
023: import net.refractions.udig.project.ui.summary.SummaryControl;
024: import net.refractions.udig.project.ui.summary.SummaryData;
025: import net.refractions.udig.ui.ProgressManager;
026:
027: import org.eclipse.jface.viewers.ICellModifier;
028: import org.eclipse.swt.widgets.Composite;
029: import org.eclipse.swt.widgets.Control;
030: import org.eclipse.swt.widgets.TreeItem;
031: import org.eclipse.ui.IWorkbenchPropertyPage;
032: import org.eclipse.ui.dialogs.PropertyPage;
033: import org.geotools.feature.AttributeType;
034: import org.geotools.feature.FeatureType;
035: import org.opengis.referencing.crs.CoordinateReferenceSystem;
036:
037: import com.vividsolutions.jts.geom.Envelope;
038:
039: /**
040: * Shows a summary of the layer
041: * @author Jesse
042: * @since 1.1.0
043: */
044: public class LayerSummary extends PropertyPage implements
045: IWorkbenchPropertyPage {
046:
047: private String newName;
048: private SummaryData nameData;
049: private SummaryControl summaryControl;
050: private String oldName;
051:
052: @Override
053: protected Control createContents(Composite parent) {
054: final Layer layer = (Layer) getElement();
055: final CoordinateReferenceSystem layerCRS = layer.getCRS();
056: Envelope bounds;
057: try {
058: bounds = layer.getBounds(ProgressManager.instance().get(),
059: layerCRS);
060: } catch (IOException e) {
061: bounds = null;
062: }
063:
064: final List<SummaryData> data = new ArrayList<SummaryData>();
065: String name = layer.getName();
066: nameData = new SummaryData(Messages.LayerSummary_name,
067: name == null ? "" : name); //$NON-NLS-1$
068: nameData.setModifier(new NameModifier());
069: data.add(nameData);
070: newName = oldName = nameData.getInfo();
071: data.add(new SummaryData(Messages.LayerSummary_id, layer
072: .getID()));
073: data.add(new SummaryData(Messages.LayerSummary_bounds,
074: bounds == null ? Messages.LayerSummary_unknownBounds
075: : parseBounds(bounds)));
076: data.add(new SummaryData(Messages.LayerSummary_selection, layer
077: .getFilter()));
078: data.add(new SummaryData(Messages.LayerSummary_status, layer
079: .getStatusMessage()));
080: if (layer.getSchema() != null) {
081: FeatureType schema = layer.getSchema();
082: SummaryData schemaData = new SummaryData(
083: Messages.LayerSummary_featureType, schema
084: .getTypeName());
085: SummaryData[] children = new SummaryData[schema
086: .getAttributeCount()];
087:
088: for (int i = 0; i < children.length; i++) {
089: AttributeType attributeType = schema
090: .getAttributeType(i);
091: children[i] = new SummaryData(attributeType.getName(),
092: attributeType.getType().getSimpleName());
093: children[i].setParent(schemaData);
094: SummaryData[] attTypeChildren = new SummaryData[4];
095:
096: attTypeChildren[0] = new SummaryData(
097: Messages.LayerSummary_nillable, attributeType
098: .isNillable());
099: attTypeChildren[0].setParent(children[i]);
100: attTypeChildren[1] = new SummaryData(
101: Messages.LayerSummary_restriction,
102: attributeType.getRestriction());
103: attTypeChildren[1].setParent(children[i]);
104: attTypeChildren[2] = new SummaryData(
105: Messages.LayerSummary_maxOccurs, attributeType
106: .getMaxOccurs());
107: attTypeChildren[2].setParent(children[i]);
108: attTypeChildren[3] = new SummaryData(
109: Messages.LayerSummary_minOccurs, attributeType
110: .getMinOccurs());
111: attTypeChildren[3].setParent(children[i]);
112:
113: children[i].setChildren(attTypeChildren);
114: }
115: schemaData.setChildren(children);
116: data.add(schemaData);
117: }
118:
119: summaryControl = new SummaryControl(data);
120: return summaryControl.createControl(parent);
121: }
122:
123: public static String parseBounds(Envelope env) {
124: String minx = chopDouble(env.getMinX());
125: String maxx = chopDouble(env.getMaxX());
126: String miny = chopDouble(env.getMinY());
127: String maxy = chopDouble(env.getMaxY());
128: return "(" + minx + "," + miny + ") (" + maxx + "," + maxy + ") "; //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$//$NON-NLS-5$
129: }
130:
131: private static String chopDouble(double d) {
132: String s = String.valueOf(d);
133: int end = s.indexOf('.') + 2;
134: while (end > s.length())
135: end--;
136: return s.substring(0, end);
137: }
138:
139: @Override
140: protected void performApply() {
141: summaryControl.applyEdit();
142: final Layer layer = (Layer) getElement();
143:
144: if (!newName.equals(oldName)) {
145: layer.setName(newName);
146: }
147: }
148:
149: @Override
150: public boolean performCancel() {
151: performDefaults();
152: return super .performCancel();
153: }
154:
155: @Override
156: public boolean performOk() {
157: performApply();
158: return super .performOk();
159: }
160:
161: @Override
162: protected void performDefaults() {
163: summaryControl.cancelEdit();
164: final Layer layer = (Layer) getElement();
165: if (!newName.equals(oldName)) {
166: newName = oldName;
167: nameData.setInfo(oldName);
168: summaryControl.refresh(nameData);
169: layer.setName(oldName);
170: }
171: }
172:
173: private class NameModifier implements ICellModifier {
174:
175: public boolean canModify(Object element, String property) {
176: if (element == nameData)
177: return true;
178: return false;
179: }
180:
181: public Object getValue(Object element, String property) {
182: if (!newName.equals(oldName))
183: return newName;
184:
185: return ((SummaryData) element).getInfo();
186: }
187:
188: public void modify(Object element, String property, Object value) {
189: if (((TreeItem) element).getData() == nameData) {
190: newName = (String) value;
191: nameData.setInfo(newName);
192: summaryControl.refresh(nameData);
193: }
194: }
195: }
196:
197: }
|