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.datasource;
033:
034: import com.vividsolutions.jts.util.Assert;
035:
036: import com.vividsolutions.jump.I18N;
037: import com.vividsolutions.jump.coordsys.CoordinateSystemRegistry;
038: import com.vividsolutions.jump.feature.FeatureCollection;
039: import com.vividsolutions.jump.io.datasource.*;
040: import com.vividsolutions.jump.io.datasource.Connection;
041: import com.vividsolutions.jump.task.TaskMonitor;
042: import com.vividsolutions.jump.util.CollectionUtil;
043: import com.vividsolutions.jump.util.StringUtil;
044: import com.vividsolutions.jump.workbench.WorkbenchContext;
045: import com.vividsolutions.jump.workbench.model.StandardCategoryNames;
046: import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory;
047: import com.vividsolutions.jump.workbench.plugin.MultiEnableCheck;
048: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
049: import com.vividsolutions.jump.workbench.plugin.ThreadedBasePlugIn;
050: import com.vividsolutions.jump.workbench.ui.GUIUtil;
051: import com.vividsolutions.jump.workbench.ui.WorkbenchFrame;
052: import com.vividsolutions.jump.workbench.ui.images.IconLoader;
053: import com.vividsolutions.jump.workbench.ui.plugin.PersistentBlackboardPlugIn;
054: import com.vividsolutions.jump.util.Blackboard;
055:
056: import java.awt.event.ComponentAdapter;
057: import java.awt.event.WindowAdapter;
058: import java.awt.event.WindowEvent;
059: import java.util.ArrayList;
060: import java.util.Arrays;
061: import java.util.Collection;
062: import java.util.Iterator;
063:
064: import org.apache.log4j.Logger;
065:
066: import javax.swing.ImageIcon;
067: import javax.swing.SwingUtilities;
068: import javax.swing.JFileChooser;
069: import java.awt.event.*;
070:
071: /**
072: * Prompts the user to pick a dataset to load.
073: * @see DataSourceQueryChooserDialog
074: */
075: public class LoadDatasetPlugIn extends ThreadedBasePlugIn {
076: private static String LAST_FORMAT_KEY = LoadDatasetPlugIn.class
077: .getName()
078: + " - LAST FORMAT";
079:
080: private DataSourceQueryChooserDialog getDialog(PlugInContext context) {
081: Blackboard blackboard = context.getWorkbenchContext()
082: .getWorkbench().getBlackboard();
083: String KEY = getClass().getName() + " - DIALOG";
084:
085: if (null == blackboard.get(KEY)) {
086: DataSourceQueryChooserDialog dlg = new DataSourceQueryChooserDialog(
087: DataSourceQueryChooserManager.get(blackboard)
088: .getLoadDataSourceQueryChoosers(), context
089: .getWorkbenchFrame(), getName(), true);
090: blackboard.put(KEY, dlg);
091:
092: //
093: // JJ - If a user double clicks on a file then we want the same behaviour
094: // as if they hit the OK button.
095: // I'm not too famialiar with how people use the LoadDatasetPlugIn.
096: // I have the impression that developers can add/remove choosers -
097: // hence this "if" stuff.
098: //
099: Object obj = blackboard
100: .get(LoadFileDataSourceQueryChooser.FILE_CHOOSER_KEY);
101: if (obj != null && obj instanceof JFileChooser) {
102: JFileChooser chooser = (JFileChooser) obj;
103: chooser
104: .addActionListener(new DoubleClickActionListener(
105: dlg));
106: }
107: }
108:
109: return (DataSourceQueryChooserDialog) blackboard.get(KEY);
110: }
111:
112: class DoubleClickActionListener implements ActionListener {
113: private DataSourceQueryChooserDialog dlg;
114:
115: public DoubleClickActionListener(
116: DataSourceQueryChooserDialog dlg) {
117: this .dlg = dlg;
118: }
119:
120: public void actionPerformed(ActionEvent e) {
121: //
122: // The dailog's setOKPressed method calls a isInputValid method
123: // which calls actionPerformed and we get into an infinite loop
124: // unless we add some extra logic here and carefully order the
125: // method calls in setOKPressed.
126: //
127: if (!dlg.wasOKPressed()) {
128: dlg.setOKPressed();
129: }
130: }
131: }
132:
133: public String getName() {
134: //Suggest that multiple datasets may be loaded [Jon Aquino 11/10/2003]
135: return I18N.get("datasource.LoadDatasetPlugIn.load-dataset");
136: }
137:
138: public void initialize(final PlugInContext context)
139: throws Exception {
140: //Give other plug-ins a chance to add DataSourceQueryChoosers
141: //before the dialog is realized. [Jon Aquino]
142: context.getWorkbenchFrame().addWindowListener(
143: new WindowAdapter() {
144: public void windowOpened(WindowEvent e) {
145: String format = (String) PersistentBlackboardPlugIn
146: .get(context.getWorkbenchContext())
147: .get(LAST_FORMAT_KEY);
148: if (format != null) {
149: getDialog(context)
150: .setSelectedFormat(format);
151: }
152: }
153: });
154: }
155:
156: public boolean execute(PlugInContext context) throws Exception {
157: GUIUtil.centreOnWindow(getDialog(context));
158: getDialog(context).setVisible(true);
159:
160: if (getDialog(context).wasOKPressed()) {
161: PersistentBlackboardPlugIn.get(
162: context.getWorkbenchContext()).put(LAST_FORMAT_KEY,
163: getDialog(context).getSelectedFormat());
164: }
165:
166: return getDialog(context).wasOKPressed();
167: }
168:
169: public void run(TaskMonitor monitor, PlugInContext context)
170: throws Exception {
171: //Seamus Thomas Carroll [mailto:carrolls@cpsc.ucalgary.ca]
172: //was concerned when he noticed that #getDataSourceQueries
173: //was being called twice. So call it once only. [Jon Aquino 2004-02-05]
174: Collection dataSourceQueries = getDialog(context)
175: .getCurrentChooser().getDataSourceQueries();
176: Assert.isTrue(!dataSourceQueries.isEmpty());
177:
178: boolean exceptionsEncountered = false;
179: for (Iterator i = dataSourceQueries.iterator(); i.hasNext();) {
180: DataSourceQuery dataSourceQuery = (DataSourceQuery) i
181: .next();
182: ArrayList exceptions = new ArrayList();
183: Assert.isTrue(dataSourceQuery.getDataSource().isReadable());
184: monitor.report("Loading " + dataSourceQuery.toString()
185: + "...");
186:
187: Connection connection = dataSourceQuery.getDataSource()
188: .getConnection();
189: try {
190: FeatureCollection dataset = dataSourceQuery
191: .getDataSource().installCoordinateSystem(
192: connection.executeQuery(dataSourceQuery
193: .getQuery(), exceptions,
194: monitor),
195: CoordinateSystemRegistry
196: .instance(context
197: .getWorkbenchContext()
198: .getBlackboard()));
199: if (dataset != null) {
200: context.getLayerManager().addLayer(
201: chooseCategory(context),
202: dataSourceQuery.toString(), dataset)
203: .setDataSourceQuery(dataSourceQuery)
204: .setFeatureCollectionModified(false);
205: }
206: } finally {
207: connection.close();
208: }
209: if (!exceptions.isEmpty()) {
210: if (!exceptionsEncountered) {
211: context.getOutputFrame().createNewDocument();
212: exceptionsEncountered = true;
213: }
214: reportExceptions(exceptions, dataSourceQuery, context);
215: }
216: }
217: if (exceptionsEncountered) {
218: context
219: .getWorkbenchFrame()
220: .warnUser(
221: I18N
222: .get("datasource.LoadDatasetPlugIn.problems-were-encountered"));
223: }
224: }
225:
226: private void reportExceptions(ArrayList exceptions,
227: DataSourceQuery dataSourceQuery, PlugInContext context) {
228: context
229: .getOutputFrame()
230: .addHeader(
231: 1,
232: exceptions.size()
233: + " "
234: + I18N
235: .get("datasource.LoadDatasetPlugIn.problem")
236: + StringUtil.s(exceptions.size())
237: + " "
238: + I18N
239: .get("datasource.LoadDatasetPlugIn.loading")
240: + " "
241: + dataSourceQuery.toString()
242: + "."
243: + ((exceptions.size() > 10) ? I18N
244: .get("datasource.LoadDatasetPlugIn.first-and-last-five")
245: : ""));
246: context.getOutputFrame().addText(
247: I18N.get("datasource.LoadDatasetPlugIn.see-view-log"));
248: context.getOutputFrame().append("<ul>");
249:
250: Collection exceptionsToReport = exceptions.size() <= 10 ? exceptions
251: : CollectionUtil.concatenate(Arrays
252: .asList(new Collection[] {
253: exceptions.subList(0, 5),
254: exceptions.subList(
255: exceptions.size() - 5,
256: exceptions.size()) }));
257: for (Iterator j = exceptionsToReport.iterator(); j.hasNext();) {
258: Exception exception = (Exception) j.next();
259: context.getWorkbenchFrame().log(
260: StringUtil.stackTrace(exception));
261: context.getOutputFrame().append("<li>");
262: context.getOutputFrame().append(
263: GUIUtil.escapeHTML(WorkbenchFrame
264: .toMessage(exception), true, true));
265: context.getOutputFrame().append("</li>");
266: }
267: context.getOutputFrame().append("</ul>");
268: }
269:
270: private String chooseCategory(PlugInContext context) {
271: return context.getLayerNamePanel().getSelectedCategories()
272: .isEmpty() ? StandardCategoryNames.WORKING : context
273: .getLayerNamePanel().getSelectedCategories().iterator()
274: .next().toString();
275: }
276:
277: //[sstein 26.08.2006] added for toolbar
278: public static MultiEnableCheck createEnableCheck(
279: final WorkbenchContext workbenchContext) {
280: EnableCheckFactory checkFactory = new EnableCheckFactory(
281: workbenchContext);
282:
283: return new MultiEnableCheck().add(checkFactory
284: .createWindowWithLayerManagerMustBeActiveCheck());
285: }
286:
287: //[sstein 26.08.2006] added for toolbar
288: public static ImageIcon getIcon() {
289: return IconLoader.icon("Plus.gif");
290: }
291: }
|