21
21
package com.bdaum.zoom.gps.internal.actions;
23
import java.io.BufferedInputStream;
23
24
import java.io.File;
25
import java.io.FileInputStream;
26
import java.io.FileNotFoundException;
27
import java.io.IOException;
28
import java.io.InputStream;
29
import java.text.ParseException;
30
import java.util.ArrayList;
31
import java.util.Arrays;
32
import java.util.HashMap;
24
33
import java.util.List;
36
import org.eclipse.core.runtime.IAdaptable;
26
37
import org.eclipse.jface.action.IAction;
38
import org.eclipse.jface.preference.PreferenceDialog;
27
39
import org.eclipse.jface.viewers.ISelection;
40
import org.eclipse.osgi.util.NLS;
41
import org.eclipse.swt.custom.BusyIndicator;
28
42
import org.eclipse.swt.widgets.Shell;
43
import org.eclipse.ui.dialogs.PreferencesUtil;
30
45
import com.bdaum.zoom.cat.model.asset.Asset;
31
46
import com.bdaum.zoom.gps.internal.GpsActivator;
47
import com.bdaum.zoom.gps.internal.GpsConfiguration;
48
import com.bdaum.zoom.gps.internal.dialogs.TrackpointDialog;
32
49
import com.bdaum.zoom.gps.internal.operations.GeotagOperation;
50
import com.bdaum.zoom.gps.internal.preferences.GpsPreferencePage;
33
51
import com.bdaum.zoom.job.OperationJob;
52
import com.bdaum.zoom.ui.Ui;
34
53
import com.bdaum.zoom.ui.dialogs.AcousticMessageDialog;
54
import com.bdaum.zoom.ui.gps.IGpsParser;
55
import com.bdaum.zoom.ui.gps.IWaypointCollector;
56
import com.bdaum.zoom.ui.gps.RasterCoordinate;
57
import com.bdaum.zoom.ui.gps.Trackpoint;
58
import com.bdaum.zoom.ui.gps.Waypoint;
35
59
import com.bdaum.zoom.ui.internal.actions.CollectionOperationActionDelegate;
37
61
@SuppressWarnings("restriction")
38
62
public class GeoNamingAction extends CollectionOperationActionDelegate {
64
private static final String[] EMPTY = new String[0];
65
private static final long ONEMINUTE = 60000L;
66
private List<Trackpoint> trackpoints = new ArrayList<Trackpoint>(250);
67
private Map<RasterCoordinate, Waypoint> waypoints;
40
69
public void run(IAction action) {
70
run(action, null, null);
49
78
super.selectionChanged(action, sel);
52
protected void run(IAction action, File[] files) {
81
public void run(IAction action, final File[] files, IAdaptable info) {
53
82
List<Asset> selectedAssets = getSelectedAssets(true);
54
if (selectedAssets.isEmpty() && !AcousticMessageDialog.openConfirm(getShell(),
55
Messages.getString("GeoNamingAction.Geonaming"), //$NON-NLS-1$
56
Messages.getString("GeoNamingAction.Nothing_selected"))) //$NON-NLS-1$
83
if (selectedAssets.isEmpty()
84
&& !AcousticMessageDialog.openConfirm(getShell(),
85
Messages.getString("GeoNamingAction.Geonaming"), //$NON-NLS-1$
86
Messages.getString("GeoNamingAction.Nothing_selected"))) //$NON-NLS-1$
58
88
String[] assetIds = new String[selectedAssets.size()];
60
90
for (Asset asset : selectedAssets)
61
assetIds[i++] = asset.getStringId();
62
OperationJob.executeOperation(new GeotagOperation(files, assetIds, GpsActivator
63
.getDefault().createGpsConfiguration()), this);
91
assetIds[j++] = asset.getStringId();
93
Shell shell = (Shell) info.getAdapter(Shell.class);
94
PreferenceDialog dialog = PreferencesUtil.createPreferenceDialogOn(
95
shell, GpsPreferencePage.ID, EMPTY, "onAction"); //$NON-NLS-1$
96
if (dialog.open() != PreferenceDialog.OK)
99
Trackpoint[] pnts = new Trackpoint[0];
100
final GpsConfiguration gpsConfiguration = GpsActivator.getDefault()
101
.createGpsConfiguration();
103
BusyIndicator.showWhile(getShell().getDisplay(), new Runnable() {
105
for (int i = 0; i < files.length; i++) {
106
if (gpsConfiguration.useWaypoints) {
107
InputStream in = null;
109
IWaypointCollector collector = Ui.getUi()
110
.getWaypointCollector(files[i]);
111
if (collector != null) {
112
if (waypoints == null)
113
waypoints = new HashMap<RasterCoordinate, Waypoint>(
115
in = new BufferedInputStream(
116
new FileInputStream(files[i]));
117
collector.collect(in, waypoints);
120
} catch (FileNotFoundException e) {
123
Messages.getString("GeotagAction.parsing_error"), //$NON-NLS-1$
125
} catch (IOException e) {
128
Messages.getString("GeotagAction.io_error_waypoints"), //$NON-NLS-1$
130
} catch (ParseException e) {
133
Messages.getString("GeotagAction.parsing_error"), //$NON-NLS-1$
139
} catch (IOException e) {
144
InputStream in = null;
146
IGpsParser parser = Ui.getUi().getGpsParser(
148
if (parser == null) {
153
Messages.getString("GeotagAction.no_parser"), //$NON-NLS-1$
157
in = new BufferedInputStream(new FileInputStream(
159
parser.parse(in, trackpoints);
160
} catch (FileNotFoundException e) {
161
// should never happen
162
} catch (ParseException e) {
163
showError(NLS.bind(Messages
164
.getString("GeotagAction.parsing_error"), //$NON-NLS-1$
166
} catch (IOException e) {
169
Messages.getString("GeotagAction.io_error"), //$NON-NLS-1$
175
} catch (IOException e) {
182
if (!trackpoints.isEmpty() && info != null && gpsConfiguration.edit) {
183
pnts = trackpoints.toArray(new Trackpoint[trackpoints.size()]);
185
TrackpointDialog dialog = new TrackpointDialog(getShell(),
186
pnts, gpsConfiguration.tolerance * ONEMINUTE);
187
if (dialog.open() != TrackpointDialog.OK)
189
pnts = dialog.getResult();
192
OperationJob.executeOperation(new GeotagOperation(pnts, assetIds,
193
gpsConfiguration), this);
196
public void showError(String message, Throwable t) {
197
AcousticMessageDialog.openError(getShell(),
198
Messages.getString("GeoNamingAction.error_parsing_gpx"), //$NON-NLS-1$
199
t == null ? message : NLS.bind("{0}: {1}", message, t)); //$NON-NLS-1$
66
202
protected Shell getShell() {