~berthold-daum/zora/trunk

« back to all changes in this revision

Viewing changes to com.bdaum.zoom.gps/src/com/bdaum/zoom/gps/internal/actions/GeoNamingAction.java

  • Committer: bdaum
  • Date: 2015-12-26 10:21:51 UTC
  • Revision ID: berthold.daum@bdaum.de-20151226102151-44f1j5113167thb9
VersionĀ 2.4.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 
21
21
package com.bdaum.zoom.gps.internal.actions;
22
22
 
 
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;
 
34
import java.util.Map;
25
35
 
 
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;
29
44
 
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;
36
60
 
37
61
@SuppressWarnings("restriction")
38
62
public class GeoNamingAction extends CollectionOperationActionDelegate {
39
63
 
 
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;
 
68
 
40
69
        public void run(IAction action) {
41
 
                run(action, null);
 
70
                run(action, null, null);
42
71
        }
43
72
 
44
73
        @Override
49
78
                        super.selectionChanged(action, sel);
50
79
        }
51
80
 
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$
57
87
                        return;
58
88
                String[] assetIds = new String[selectedAssets.size()];
59
 
                int i = 0;
 
89
                int j = 0;
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();
 
92
                if (info != null) {
 
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)
 
97
                                return;
 
98
                }
 
99
                Trackpoint[] pnts = new Trackpoint[0];
 
100
                final GpsConfiguration gpsConfiguration = GpsActivator.getDefault()
 
101
                                .createGpsConfiguration();
 
102
                if (files != null) {
 
103
                        BusyIndicator.showWhile(getShell().getDisplay(), new Runnable() {
 
104
                                public void run() {
 
105
                                        for (int i = 0; i < files.length; i++) {
 
106
                                                if (gpsConfiguration.useWaypoints) {
 
107
                                                        InputStream in = null;
 
108
                                                        try {
 
109
                                                                IWaypointCollector collector = Ui.getUi()
 
110
                                                                                .getWaypointCollector(files[i]);
 
111
                                                                if (collector != null) {
 
112
                                                                        if (waypoints == null)
 
113
                                                                                waypoints = new HashMap<RasterCoordinate, Waypoint>(
 
114
                                                                                                201);
 
115
                                                                        in = new BufferedInputStream(
 
116
                                                                                        new FileInputStream(files[i]));
 
117
                                                                        collector.collect(in, waypoints);
 
118
                                                                        in.close();
 
119
                                                                }
 
120
                                                        } catch (FileNotFoundException e) {
 
121
                                                                showError(
 
122
                                                                                NLS.bind(
 
123
                                                                                                Messages.getString("GeotagAction.parsing_error"), //$NON-NLS-1$
 
124
                                                                                                files[i]), e);
 
125
                                                        } catch (IOException e) {
 
126
                                                                showError(
 
127
                                                                                NLS.bind(
 
128
                                                                                                Messages.getString("GeotagAction.io_error_waypoints"), //$NON-NLS-1$
 
129
                                                                                                files[i]), e);
 
130
                                                        } catch (ParseException e) {
 
131
                                                                showError(
 
132
                                                                                NLS.bind(
 
133
                                                                                                Messages.getString("GeotagAction.parsing_error"), //$NON-NLS-1$
 
134
                                                                                                files[i]), e);
 
135
                                                        } finally {
 
136
                                                                if (in != null)
 
137
                                                                        try {
 
138
                                                                                in.close();
 
139
                                                                        } catch (IOException e) {
 
140
                                                                                // do nothing
 
141
                                                                        }
 
142
                                                        }
 
143
                                                }
 
144
                                                InputStream in = null;
 
145
                                                try {
 
146
                                                        IGpsParser parser = Ui.getUi().getGpsParser(
 
147
                                                                        files[i]);
 
148
                                                        if (parser == null) {
 
149
                                                                GpsActivator
 
150
                                                                                .getDefault()
 
151
                                                                                .logError(
 
152
                                                                                                NLS.bind(
 
153
                                                                                                                Messages.getString("GeotagAction.no_parser"), //$NON-NLS-1$
 
154
                                                                                                                files[i]), null);
 
155
                                                                continue;
 
156
                                                        }
 
157
                                                        in = new BufferedInputStream(new FileInputStream(
 
158
                                                                        files[i]));
 
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$
 
165
                                                                        files[i]), e);
 
166
                                                } catch (IOException e) {
 
167
                                                        showError(
 
168
                                                                        NLS.bind(
 
169
                                                                                        Messages.getString("GeotagAction.io_error"), //$NON-NLS-1$
 
170
                                                                                        files[i]), e);
 
171
                                                } finally {
 
172
                                                        if (in != null)
 
173
                                                                try {
 
174
                                                                        in.close();
 
175
                                                                } catch (IOException e) {
 
176
                                                                        // do nothing
 
177
                                                                }
 
178
                                                }
 
179
                                        }
 
180
                                }
 
181
                        });
 
182
                        if (!trackpoints.isEmpty() && info != null && gpsConfiguration.edit) {
 
183
                                pnts = trackpoints.toArray(new Trackpoint[trackpoints.size()]);
 
184
                                Arrays.sort(pnts);
 
185
                                TrackpointDialog dialog = new TrackpointDialog(getShell(),
 
186
                                                pnts, gpsConfiguration.tolerance * ONEMINUTE);
 
187
                                if (dialog.open() != TrackpointDialog.OK)
 
188
                                        return;
 
189
                                pnts = dialog.getResult();
 
190
                        }
 
191
                }
 
192
                OperationJob.executeOperation(new GeotagOperation(pnts, assetIds,
 
193
                                gpsConfiguration), this);
 
194
        }
 
195
 
 
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$
64
200
        }
65
201
 
66
202
        protected Shell getShell() {