001: /**
002: *
003: */package net.refractions.udig.project.ui.internal;
004:
005: import java.io.File;
006: import java.io.IOException;
007: import java.lang.reflect.InvocationTargetException;
008: import java.net.MalformedURLException;
009: import java.net.URL;
010: import java.util.ArrayList;
011: import java.util.List;
012:
013: import net.refractions.udig.internal.ui.UDIGDropHandler;
014: import net.refractions.udig.project.IProjectElement;
015: import net.refractions.udig.project.internal.ProjectPlugin;
016: import net.refractions.udig.project.ui.ApplicationGIS;
017: import net.refractions.udig.project.ui.commands.OpenProjectElementCommand;
018: import net.refractions.udig.project.ui.preferences.PreferenceConstants;
019: import net.refractions.udig.ui.IDropAction;
020: import net.refractions.udig.ui.IDropHandlerListener;
021: import net.refractions.udig.ui.PlatformGIS;
022:
023: import org.eclipse.core.runtime.IProgressMonitor;
024: import org.eclipse.core.runtime.Platform;
025: import org.eclipse.emf.common.util.URI;
026: import org.eclipse.emf.ecore.resource.Resource;
027: import org.eclipse.jface.operation.IRunnableWithProgress;
028: import org.eclipse.jface.preference.IPreferenceStore;
029: import org.eclipse.jface.viewers.Viewer;
030: import org.eclipse.swt.widgets.Composite;
031: import org.eclipse.ui.IEditorInput;
032: import org.eclipse.ui.IEditorSite;
033: import org.eclipse.ui.IStartup;
034: import org.eclipse.ui.PartInitException;
035: import org.eclipse.ui.PlatformUI;
036: import org.eclipse.ui.intro.IIntroManager;
037: import org.eclipse.ui.intro.IIntroPart;
038: import org.eclipse.ui.part.EditorPart;
039: import org.geotools.referencing.CRS;
040: import org.opengis.referencing.NoSuchAuthorityCodeException;
041:
042: /**
043: * Loads the maps from the previous session and processes the commandline arguments. If the DND
044: * extension point will be called so that it will act as if the url/string whatever was dropped on
045: * the application. So if a url to shapefile is on the commandline a new map will be openned and
046: * will display the shapefile.
047: *
048: * @author jones
049: */
050: public class StartupOpenMaps implements IStartup {
051:
052: private String[] args;
053:
054: public StartupOpenMaps() {
055: args = Platform.getApplicationArgs();
056: }
057:
058: /**
059: * @see org.eclipse.ui.IStartup#earlyStartup()
060: */
061: public void earlyStartup() {
062:
063: List<URL> urls = new ArrayList<URL>(args.length);
064:
065: for (String string : args) {
066: String urlString = string;
067: try {
068: if (urlString.startsWith("-")) { //$NON-NLS-1$
069: continue;
070: }
071: // try to make string a url
072: if (!urlString.contains(":/")) { //$NON-NLS-1$
073: File file = new File(urlString);
074: if (!file.exists())
075: continue;
076: urlString = file.toURL().toString();
077: }
078: if (urlString.startsWith("file:/")) {//$NON-NLS-1$
079: try {
080: new URL(urlString);
081: } catch (IOException ie) {
082: String tmp = urlString.substring(5);
083: if (tmp.contains(":")) { //$NON-NLS-1$
084: tmp = tmp.substring(tmp.indexOf(":")); //$NON-NLS-1$
085: }
086: File file = new File(tmp);
087: if (!file.exists())
088: continue;
089: }
090: }
091:
092: urls.add(new URL(urlString));
093:
094: } catch (MalformedURLException e) {
095: // ignore
096: }
097: }
098:
099: if (!urls.isEmpty()) {
100: final List<URL> finalurls = urls;
101: final String title = Messages.StartupOpenMaps_openURLDialogTitle;
102: final IRunnableWithProgress openMapRunnable = new IRunnableWithProgress() {
103:
104: public void run(IProgressMonitor monitor)
105: throws InvocationTargetException,
106: InterruptedException {
107: dropURLS(finalurls, monitor);
108: }
109:
110: };
111: PlatformGIS.runInProgressDialog(title, true,
112: openMapRunnable, true);
113: } else {
114: IPreferenceStore p = ProjectUIPlugin.getDefault()
115: .getPreferenceStore();
116: boolean proceed = p
117: .getBoolean(PreferenceConstants.P_OPEN_MAPS_ON_STARTUP);
118: if (!proceed) {
119: return;
120: }
121: int numEditors = p.getInt(MapEditor.ID);
122: if (numEditors == 0)
123: return;
124: final String title = Messages.StartupOpenMaps_openMapDialogTitle;
125: final IRunnableWithProgress openMapRunnable = new IRunnableWithProgress() {
126:
127: public void run(IProgressMonitor monitor)
128: throws InvocationTargetException,
129: InterruptedException {
130: openLastOpenMaps(monitor);
131: }
132:
133: };
134:
135: PlatformGIS.runInProgressDialog(title, true,
136: openMapRunnable, true);
137: }
138: }
139:
140: /**
141: * Opens maps from previous section. Only opens if there are no maps in the arguments, IE no
142: * maps dropped onto uDig icon or udig was not started by clicking on an associated file.
143: */
144: private void openLastOpenMaps(IProgressMonitor monitor) {
145: IPreferenceStore p = ProjectUIPlugin.getDefault()
146: .getPreferenceStore();
147: int numEditors = p.getInt(MapEditor.ID);
148: monitor.beginTask(Messages.StartupOpenMaps_openMapDialogTitle,
149: numEditors * 2 + 2);
150: p.setValue(MapEditor.ID, 0);
151:
152: for (int i = 0; i < numEditors; i++) {
153: monitor.worked(1);
154: String id = MapEditor.ID + ":" + i; //$NON-NLS-1$
155: String name = p.getString(id);
156: if (name == null || name.equals("")) //$NON-NLS-1$
157: continue;
158: p.setValue(id, ""); //$NON-NLS-1$
159: if (!monitor.isCanceled()) {
160: monitor
161: .setTaskName(Messages.StartupOpenMaps_loadingTask
162: + name);
163: URI mapResourceURI = URI.createURI(name);
164: Resource resource = ProjectPlugin.getPlugin()
165: .getProjectRegistry().eResource()
166: .getResourceSet().getResource(mapResourceURI,
167: true);
168:
169: monitor.worked(1);
170: // kick the classloader so that the CRS plugins are loaded correctly.
171: try {
172: CRS.decode("EPSG:4326"); //$NON-NLS-1$
173: } catch (NoSuchAuthorityCodeException e) {
174: throw (RuntimeException) new RuntimeException()
175: .initCause(e);
176: }
177: final Object object = resource.getContents().get(0);
178: if (object instanceof IProjectElement) {
179: OpenProjectElementCommand command = new OpenProjectElementCommand(
180: (IProjectElement) object);
181: monitor
182: .setTaskName(Messages.StartupOpenMaps_OpenTask
183: + ": " //$NON-NLS-1$
184: + ((IProjectElement) object)
185: .getName());
186: ApplicationGIS.getActiveProject().sendSync(command);
187: }
188: }
189: }
190: }
191:
192: private void dropURLS(List<URL> urls, IProgressMonitor monitor) {
193: monitor.beginTask(Messages.StartupOpenMaps_openURLDialogTitle,
194: urls.size() * 1 + 2);
195: monitor.worked(1);
196: Viewer viewer = LayersView.getViewer();
197: if (viewer == null)
198: ProjectUIPlugin.trace(getClass(),
199: "Layers View is not available", (Exception) null); //$NON-NLS-1$
200:
201: UDIGDropHandler dropHandler = new UDIGDropHandler();
202: dropHandler.setTarget(new EditorPart() {
203:
204: @Override
205: public void doSave(IProgressMonitor monitor) {
206: }
207:
208: @Override
209: public void doSaveAs() {
210: }
211:
212: @Override
213: public void init(IEditorSite site, IEditorInput input)
214: throws PartInitException {
215: }
216:
217: @Override
218: public boolean isDirty() {
219: return false;
220: }
221:
222: @Override
223: public boolean isSaveAsAllowed() {
224: return false;
225: }
226:
227: @Override
228: public void createPartControl(Composite parent) {
229: }
230:
231: @Override
232: public void setFocus() {
233: }
234:
235: });
236:
237: closeIntro();
238:
239: for (URL url : urls) {
240: monitor.worked(1);
241: monitor.setTaskName(Messages.StartupOpenMaps_processingTask
242: + ": " + url); //$NON-NLS-1$
243: if (monitor.isCanceled())
244: break;
245: ProcessingURLSListener listener = new ProcessingURLSListener(
246: this );
247: try {
248: dropHandler.addListener(listener);
249: dropHandler.performDrop(url, null);
250: while (!listener.processed && !monitor.isCanceled()) {
251: synchronized (this ) {
252: try {
253: wait(500);
254: } catch (InterruptedException e) {
255: return;
256: }
257: }
258: }
259: } finally {
260: dropHandler.removeListener(listener);
261: }
262: }
263: }
264:
265: private void closeIntro() {
266: PlatformGIS.syncInDisplayThread(new Runnable() {
267:
268: public void run() {
269: IIntroManager introManager = PlatformUI.getWorkbench()
270: .getIntroManager();
271: IIntroPart intro = introManager.getIntro();
272: if (intro != null)
273: introManager.closeIntro(intro);
274: }
275:
276: });
277: }
278:
279: public void testingSetArgs(String[] strings) {
280: if (strings == null)
281: this .args = new String[0];
282: else {
283:
284: this .args = new String[strings.length];
285: System.arraycopy(strings, 0, this .args, 0, args.length);
286: }
287: }
288:
289: private static class ProcessingURLSListener implements
290: IDropHandlerListener {
291: volatile boolean processed = false;
292: StartupOpenMaps lock;
293:
294: public ProcessingURLSListener(StartupOpenMaps maps) {
295: lock = maps;
296: }
297:
298: public void done(IDropAction action, Throwable t) {
299: processed = true;
300:
301: synchronized (lock) {
302: lock.notify();
303: }
304: }
305:
306: public void starting(IDropAction action) {
307: }
308:
309: public void noAction(Object data) {
310: processed = true;
311: synchronized (lock) {
312: lock.notify();
313: }
314: }
315:
316: }
317:
318: }
|