001: package com.vividsolutions.jump.workbench.datasource;
002:
003: import java.awt.event.WindowAdapter;
004: import java.awt.event.WindowEvent;
005: import java.util.ArrayList;
006: import java.util.Arrays;
007: import java.util.Collection;
008: import java.util.Iterator;
009:
010: import org.apache.log4j.Logger;
011:
012: import com.vividsolutions.jts.util.Assert;
013: import com.vividsolutions.jump.I18N;
014: import com.vividsolutions.jump.coordsys.CoordinateSystemRegistry;
015: import com.vividsolutions.jump.feature.FeatureCollection;
016: import com.vividsolutions.jump.io.datasource.Connection;
017: import com.vividsolutions.jump.io.datasource.DataSourceQuery;
018: import com.vividsolutions.jump.task.TaskMonitor;
019: import com.vividsolutions.jump.util.CollectionUtil;
020: import com.vividsolutions.jump.util.StringUtil;
021: import com.vividsolutions.jump.workbench.WorkbenchContext;
022: import com.vividsolutions.jump.workbench.model.StandardCategoryNames;
023: import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory;
024: import com.vividsolutions.jump.workbench.plugin.MultiEnableCheck;
025: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
026: import com.vividsolutions.jump.workbench.plugin.ThreadedBasePlugIn;
027: import com.vividsolutions.jump.workbench.ui.GUIUtil;
028: import com.vividsolutions.jump.workbench.ui.WorkbenchFrame;
029: import com.vividsolutions.jump.workbench.ui.plugin.PersistentBlackboardPlugIn;
030:
031: public abstract class AbstractLoadDatasetPlugIn extends
032: AbstractLoadSaveDatasetPlugIn {
033: public void run(TaskMonitor monitor, PlugInContext context)
034: throws Exception {
035: //Seamus Thomas Carroll [mailto:carrolls@cpsc.ucalgary.ca]
036: //was concerned when he noticed that #getDataSourceQueries
037: //was being called twice. So call it once only. [Jon Aquino 2004-02-05]
038: Assert.isTrue(!getDataSourceQueries().isEmpty());
039:
040: boolean exceptionsEncountered = false;
041: for (Iterator i = getDataSourceQueries().iterator(); i
042: .hasNext();) {
043: DataSourceQuery dataSourceQuery = (DataSourceQuery) i
044: .next();
045: ArrayList exceptions = new ArrayList();
046: Assert.isTrue(dataSourceQuery.getDataSource().isReadable());
047: monitor.report(I18N
048: .get("datasource.LoadDatasetPlugIn.loading")
049: + " " + dataSourceQuery.toString() + "...");
050:
051: Connection connection = dataSourceQuery.getDataSource()
052: .getConnection();
053: try {
054: FeatureCollection dataset = dataSourceQuery
055: .getDataSource().installCoordinateSystem(
056: connection.executeQuery(dataSourceQuery
057: .getQuery(), exceptions,
058: monitor),
059: CoordinateSystemRegistry
060: .instance(context
061: .getWorkbenchContext()
062: .getBlackboard()));
063: if (dataset != null) {
064: context.getLayerManager().addLayer(
065: chooseCategory(context),
066: dataSourceQuery.toString(), dataset)
067: .setDataSourceQuery(dataSourceQuery)
068: .setFeatureCollectionModified(false);
069: }
070: } finally {
071: connection.close();
072: }
073: if (!exceptions.isEmpty()) {
074: if (!exceptionsEncountered) {
075: context.getOutputFrame().createNewDocument();
076: exceptionsEncountered = true;
077: }
078: reportExceptions(exceptions, dataSourceQuery, context);
079: }
080: }
081: if (exceptionsEncountered) {
082: context
083: .getWorkbenchFrame()
084: .warnUser(
085: I18N
086: .get("datasource.LoadDatasetPlugIn.problems-were-encountered"));
087: }
088: }
089:
090: private void reportExceptions(ArrayList exceptions,
091: DataSourceQuery dataSourceQuery, PlugInContext context) {
092: context
093: .getOutputFrame()
094: .addHeader(
095: 1,
096: exceptions.size()
097: + " "
098: + I18N
099: .get("datasource.LoadDatasetPlugIn.problem")
100: + StringUtil.s(exceptions.size())
101: + " "
102: + I18N
103: .get("datasource.LoadDatasetPlugIn.loading")
104: + " "
105: + dataSourceQuery.toString()
106: + "."
107: + ((exceptions.size() > 10) ? " "
108: + I18N
109: .get("datasource.LoadDatasetPlugIn.first-and-last-five")
110: : ""));
111: context.getOutputFrame().addText(
112: I18N.get("datasource.LoadDatasetPlugIn.see-view-log"));
113: context.getOutputFrame().append("<ul>");
114:
115: Collection exceptionsToReport = exceptions.size() <= 10 ? exceptions
116: : CollectionUtil.concatenate(Arrays
117: .asList(new Collection[] {
118: exceptions.subList(0, 5),
119: exceptions.subList(
120: exceptions.size() - 5,
121: exceptions.size()) }));
122: for (Iterator j = exceptionsToReport.iterator(); j.hasNext();) {
123: Exception exception = (Exception) j.next();
124: context.getWorkbenchFrame().log(
125: StringUtil.stackTrace(exception));
126: context.getOutputFrame().append("<li>");
127: context.getOutputFrame().append(
128: GUIUtil.escapeHTML(WorkbenchFrame
129: .toMessage(exception), true, true));
130: context.getOutputFrame().append("</li>");
131: }
132: context.getOutputFrame().append("</ul>");
133: }
134:
135: private String chooseCategory(PlugInContext context) {
136: return context.getLayerNamePanel().getSelectedCategories()
137: .isEmpty() ? StandardCategoryNames.WORKING : context
138: .getLayerNamePanel().getSelectedCategories().iterator()
139: .next().toString();
140: }
141:
142: public static MultiEnableCheck createEnableCheck(
143: final WorkbenchContext workbenchContext) {
144: EnableCheckFactory checkFactory = new EnableCheckFactory(
145: workbenchContext);
146: return new MultiEnableCheck().add(checkFactory
147: .createWindowWithLayerManagerMustBeActiveCheck());
148: }
149:
150: }
|