001: /*
002: * Created on Mar 25, 2005
003: *
004: * TODO To change the template for this generated file go to
005: * Window - Preferences - Java - Code Style - Code Templates
006: */
007: package net.refractions.udig.project.ui.operations.example;
008:
009: import java.io.IOException;
010: import java.util.ArrayList;
011: import java.util.List;
012:
013: import net.refractions.udig.catalog.IGeoResource;
014: import net.refractions.udig.catalog.IGeoResourceInfo;
015: import net.refractions.udig.project.ui.internal.Messages;
016: import net.refractions.udig.project.ui.internal.ProjectUIPlugin;
017: import net.refractions.udig.project.ui.summary.SummaryData;
018: import net.refractions.udig.project.ui.summary.SummaryDialog;
019: import net.refractions.udig.ui.operations.IOp;
020:
021: import org.eclipse.core.runtime.IProgressMonitor;
022: import org.eclipse.jface.dialogs.Dialog;
023: import org.eclipse.jface.dialogs.MessageDialog;
024: import org.eclipse.swt.widgets.Display;
025: import org.geotools.data.FeatureSource;
026: import org.geotools.data.FeatureStore;
027: import org.geotools.data.Query;
028: import org.geotools.data.wms.WebMapServer;
029: import org.geotools.feature.FeatureIterator;
030:
031: import com.vividsolutions.jts.geom.Envelope;
032:
033: /**
034: * An Operation that can operate on either GeoResources or on FeatureSources. If the object is a
035: * georesource the operation prints out a summary about the resource. if the object is a
036: * FeatureSource the number of features are printed.
037: *
038: * @author jones
039: */
040: public class MultiTargetOp implements IOp {
041:
042: /**
043: * @see net.refractions.udig.ui.operations.IOp#op(java.lang.Object)
044: */
045: public void op(final Display display, Object target,
046: IProgressMonitor monitor) throws Exception {
047: if (target instanceof IGeoResource) {
048: IGeoResource resource = (IGeoResource) target;
049: op(display, monitor, resource);
050: } else if (target instanceof FeatureSource) {
051: FeatureSource source = (FeatureSource) target;
052: op(display, monitor, source);
053: }
054: }
055:
056: /**
057: * @param display
058: * @param monitor
059: * @param resource
060: * @throws IOException
061: */
062: private void op(final Display display, IProgressMonitor monitor,
063: IGeoResource resource) throws IOException {
064: IGeoResourceInfo info = resource.getInfo(monitor);
065: Envelope bounds = info.getBounds();
066: final List<SummaryData> data = new ArrayList<SummaryData>();
067: String crs;
068: if (info.getCRS() != null)
069: crs = info.getCRS().getName().toString();
070: else
071: crs = Messages.MultiTargetOp_unknown;
072: crs = crs.replace('\n', ' ');
073:
074: try {
075: data.add(new SummaryData(Messages.MultiTargetOp_name, info
076: .getName()));
077: data.add(new SummaryData(Messages.MultiTargetOp_title, info
078: .getTitle()));
079: data.add(new SummaryData(Messages.MultiTargetOp_bounds,
080: LayerSummary.parseBounds(bounds)));
081: data.add(new SummaryData(Messages.MultiTargetOp_crs, crs));
082: data.add(new SummaryData(
083: Messages.MultiTargetOp_featuresource, resource
084: .canResolve(FeatureSource.class)));
085: data.add(new SummaryData(
086: Messages.MultiTargetOp_featurestore, resource
087: .canResolve(FeatureStore.class)));
088: data.add(new SummaryData(Messages.MultiTargetOp_wms,
089: resource.canResolve(WebMapServer.class)));
090: boolean first = false;
091: for (String word : info.getKeywords()) {
092: if (first)
093: data.add(new SummaryData(
094: Messages.MultiTargetOp_keywords, word));
095: else
096: data.add(new SummaryData(null, word));
097: }
098: } catch (Exception e) {
099: display.asyncExec(new Runnable() {
100: public void run() {
101: MessageDialog.openError(display.getActiveShell(),
102: Messages.MultiTargetOp_resource_summary,
103: Messages.MultiTargetOp_error);
104: }
105: });
106: ProjectUIPlugin.log(null, e);
107:
108: }
109: display.asyncExec(new Runnable() {
110: public void run() {
111: Dialog d = new SummaryDialog(display.getActiveShell(),
112: Messages.MultiTargetOp_resource_summary, data);
113: d.setBlockOnOpen(true);
114: d.open();
115: }
116: });
117: }
118:
119: private void op(final Display display, IProgressMonitor monitor,
120: FeatureSource source) throws IOException {
121: int tmp = 0;
122: tmp = source.getCount(Query.ALL);
123: if (tmp == -1) {
124: FeatureIterator iter = source.getFeatures().features();
125:
126: try {
127: while (iter.hasNext()) {
128: try {
129: iter.next();
130: } catch (Exception e) {
131: // do nothing
132: }
133: tmp++;
134: }
135: } finally {
136: iter.close();
137: }
138: }
139: final int features = tmp;
140: display.asyncExec(new Runnable() {
141: public void run() {
142: MessageDialog
143: .openInformation(
144: display.getActiveShell(),
145: "Number of features", //$NON-NLS-1$
146: Messages.MultiTargetOp_number
147: + (features == -1 ? Messages.MultiTargetOp_expensive
148: : String
149: .valueOf(features)));
150: }
151: });
152: }
153:
154: }
|