~berthold-daum/zora/trunk

« back to all changes in this revision

Viewing changes to com.bdaum.zoom.rcp/src/com/bdaum/zoom/rcp/internal/ApplicationWorkbenchAdvisor.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:
26
26
import org.eclipse.core.runtime.IStatus;
27
27
import org.eclipse.core.runtime.OperationCanceledException;
28
28
import org.eclipse.core.runtime.Platform;
 
29
import org.eclipse.core.runtime.Status;
29
30
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
30
31
import org.eclipse.core.runtime.jobs.Job;
31
32
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
98
99
public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor implements
99
100
                IAdaptable {
100
101
 
 
102
        private final class ErrorHandler implements IDbErrorHandler {
 
103
                private final IWorkbenchConfigurer configurer;
 
104
 
 
105
                public ErrorHandler(IWorkbenchConfigurer configurer) {
 
106
                        this.configurer = configurer;
 
107
                }
 
108
 
 
109
                public void fatalError(final String title, final String msg,
 
110
                                IAdaptable adaptable) {
 
111
                        final Shell shell = getShell(adaptable);
 
112
                        syncExec(shell, new Runnable() {
 
113
                                public void run() {
 
114
                                        if (!shell.isDisposed()) {
 
115
                                                AcousticMessageDialog.openError(shell, title, msg);
 
116
                                                shell.getDisplay().dispose();
 
117
                                        }
 
118
                                }
 
119
                        });
 
120
                        System.exit(4);
 
121
                }
 
122
 
 
123
                public void connectionLostWarning(final String title,
 
124
                                final String message, final IAdaptable adaptable) {
 
125
                        final Shell shell = getShell(adaptable);
 
126
                        syncExec(shell, new Runnable() {
 
127
                                public void run() {
 
128
                                        if (!shell.isDisposed()
 
129
                                                        && !AcousticMessageDialog.openQuestion(shell,
 
130
                                                                        title, message))
 
131
                                                return;
 
132
                                        configurer.emergencyClose();
 
133
                                }
 
134
                        });
 
135
                }
 
136
 
 
137
                public void promptForReconnect(final String title,
 
138
                                final String message, final IInputValidator validator,
 
139
                                final IAdaptable adaptable) {
 
140
                        final Shell shell = getShell(adaptable);
 
141
                        syncExec(shell, new Runnable() {
 
142
                                public void run() {
 
143
                                        if (!shell.isDisposed()
 
144
                                                        && new TimedMessageDialog(shell, validator,
 
145
                                                                        title, null, message,
 
146
                                                                        MessageDialog.WARNING, new String[] {
 
147
                                                                                        IDialogConstants.RETRY_LABEL,
 
148
                                                                                        IDialogConstants.ABORT_LABEL },
 
149
                                                                        0, 0, 1000L).open() != 0) {
 
150
                                                shell.getDisplay().dispose();
 
151
                                                System.exit(4);
 
152
                                        }
 
153
                                }
 
154
                        });
 
155
                }
 
156
 
 
157
                public boolean question(String title, String message,
 
158
                                IAdaptable adaptable) {
 
159
                        final Shell shell = getShell(adaptable);
 
160
                        final AcousticMessageDialog dialog = new AcousticMessageDialog(
 
161
                                        shell, title, null, message, MessageDialog.QUESTION,
 
162
                                        new String[] { IDialogConstants.YES_LABEL,
 
163
                                                        IDialogConstants.NO_LABEL }, 0);
 
164
                        syncExec(shell, new Runnable() {
 
165
                                public void run() {
 
166
                                        dialog.open();
 
167
                                }
 
168
                        });
 
169
                        return dialog.getReturnCode() == 0;
 
170
                }
 
171
 
 
172
                public void showError(final String title, final String message,
 
173
                                IAdaptable adaptable) {
 
174
                        final Shell shell = getShell(adaptable);
 
175
                        asyncExec(shell, new Runnable() {
 
176
                                public void run() {
 
177
                                        if (!shell.isDisposed())
 
178
                                                AcousticMessageDialog.openError(shell, title,
 
179
                                                                message);
 
180
                                }
 
181
                        });
 
182
                }
 
183
 
 
184
                public void showWarning(final String title, final String message,
 
185
                                IAdaptable adaptable) {
 
186
                        final Shell shell = getShell(adaptable);
 
187
                        asyncExec(shell, new Runnable() {
 
188
                                public void run() {
 
189
                                        if (!shell.isDisposed())
 
190
                                                AcousticMessageDialog.openWarning(shell, title,
 
191
                                                                message);
 
192
                                }
 
193
                        });
 
194
                }
 
195
 
 
196
                public void showInformation(final String title,
 
197
                                final String message, IAdaptable adaptable) {
 
198
                        showInformation(title, message, adaptable, null);
 
199
                }
 
200
 
 
201
                public void showInformation(final String title,
 
202
                                final String message, IAdaptable adaptable,
 
203
                                final IValidator validator) {
 
204
                        final Shell shell = getShell(adaptable);
 
205
                        asyncExec(shell, new Runnable() {
 
206
                                public void run() {
 
207
                                        if (!shell.isDisposed())
 
208
                                                AcousticMessageDialog.openInformation(shell, title,
 
209
                                                                message, validator);
 
210
                                }
 
211
                        });
 
212
                }
 
213
 
 
214
                public int showMessageDialog(String dialogTitle,
 
215
                                Image dialogTitleImage, String dialogMessage,
 
216
                                int dialogImageType, String[] dialogButtonLabels,
 
217
                                int defaultIndex, IAdaptable adaptable) {
 
218
                        final Shell shell = getShell(adaptable);
 
219
                        final AcousticMessageDialog dialog = new AcousticMessageDialog(
 
220
                                        shell, dialogTitle, dialogTitleImage, dialogMessage,
 
221
                                        dialogImageType, dialogButtonLabels, defaultIndex);
 
222
                        syncExec(shell, new Runnable() {
 
223
                                public void run() {
 
224
                                        dialog.open();
 
225
                                }
 
226
                        });
 
227
                        return dialog.getReturnCode();
 
228
                }
 
229
 
 
230
                public ImportConfiguration showConflictDialog(String title,
 
231
                                String message, Asset asset,
 
232
                                ImportConfiguration currentConfig, boolean multi,
 
233
                                IAdaptable adaptable) {
 
234
                        final Shell shell = getShell(adaptable);
 
235
                        final ConflictDialog dialog = new ConflictDialog(shell, title,
 
236
                                        message, asset, currentConfig, multi);
 
237
                        syncExec(shell, new Runnable() {
 
238
                                public void run() {
 
239
                                        dialog.open();
 
240
                                }
 
241
                        });
 
242
                        return dialog.getCurrentConfig();
 
243
                }
 
244
 
 
245
                public void alarmOnPrompt(String sound) {
 
246
                        IUi ui = Ui.getUi();
 
247
                        if (ui != null)
 
248
                                ui.playSound(sound, PreferenceConstants.ALARMONPROMPT);
 
249
                }
 
250
 
 
251
                public void signalEOJ(String sound) {
 
252
                        IUi ui = Ui.getUi();
 
253
                        if (ui != null)
 
254
                                ui.playSound(sound, PreferenceConstants.ALARMONFINISH);
 
255
                }
 
256
 
 
257
                private void syncExec(Shell shell, Runnable runnable) {
 
258
                        if (shell != null && !shell.isDisposed())
 
259
                                shell.getDisplay().syncExec(runnable);
 
260
                }
 
261
 
 
262
                private void asyncExec(Shell shell, Runnable runnable) {
 
263
                        if (shell != null && !shell.isDisposed())
 
264
                                shell.getDisplay().asyncExec(runnable);
 
265
                }
 
266
 
 
267
                public Shell getShell(IAdaptable adaptable) {
 
268
                        Shell shell = adaptable == null ? null : (Shell) adaptable
 
269
                                        .getAdapter(Shell.class);
 
270
                        if (shell == null) {
 
271
                                final Display display = Display.getDefault();
 
272
                                if (!display.isDisposed()) {
 
273
                                        display.syncExec(new Runnable() {
 
274
                                                public void run() {
 
275
                                                        auxShell = new Shell(display);
 
276
                                                }
 
277
                                        });
 
278
                                        shell = auxShell;
 
279
                                        auxShell = null;
 
280
                                }
 
281
                        }
 
282
                        return shell;
 
283
                }
 
284
 
 
285
                public File showDngDialog(File dngLocation, IAdaptable adaptable) {
 
286
                        Shell shell = getShell(adaptable);
 
287
                        final DNGConverterDialog dialog = new DNGConverterDialog(shell,
 
288
                                        dngLocation);
 
289
                        syncExec(shell, new Runnable() {
 
290
                                public void run() {
 
291
                                        dialog.open();
 
292
                                }
 
293
                        });
 
294
                        return dialog.getResult();
 
295
                }
 
296
 
 
297
                public IRawConverter showRawDialog(IAdaptable adaptable) {
 
298
                        final Shell shell = getShell(adaptable);
 
299
                        boolean result = question(
 
300
                                        Constants.APPLICATION_NAME,
 
301
                                        Messages.getString("ApplicationWorkbenchAdvisor.no_raw_converter"), adaptable); //$NON-NLS-1$
 
302
                        if (result) {
 
303
                                final int ret[] = new int[1];
 
304
                                syncExec(shell, new Runnable() {
 
305
                                        public void run() {
 
306
                                                ret[0] = PreferencesUtil.createPreferenceDialogOn(
 
307
                                                                shell, ImportPreferencePage.ID,
 
308
                                                                new String[0], ImportPreferencePage.RAW)
 
309
                                                                .open();
 
310
                                        }
 
311
                                });
 
312
                                if (ret[0] == Dialog.OK)
 
313
                                        return BatchActivator.getDefault()
 
314
                                                        .getCurrentRawConverter(true);
 
315
                        }
 
316
                        return null;
 
317
                }
 
318
        }
 
319
 
101
320
        private static final String CURRENT_CATALOG = "com.bdaum.zoom.currentCatalog"; //$NON-NLS-1$
102
321
        private static final String CURRENT_VOLUME = "com.bdaum.zoom.currentVolume"; //$NON-NLS-1$
103
322
        private static final String RECENTCATS = "com.bdaum.zoom.recentCats"; //$NON-NLS-1$
118
337
        private Shell auxShell;
119
338
        private long sendToTray = 0L;
120
339
        private boolean initialized;
 
340
        private WorkbenchHelper workbenchHelper;
121
341
 
122
342
        @Override
123
343
        public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(
132
352
 
133
353
        @Override
134
354
        public void initialize(final IWorkbenchConfigurer configurer) {
135
 
                Core.getCore().getDbFactory().setErrorHandler(new IDbErrorHandler() {
136
 
 
137
 
                        public void fatalError(final String title, final String msg,
138
 
                                        IAdaptable adaptable) {
139
 
                                final Shell shell = getShell(adaptable);
140
 
                                syncExec(shell, new Runnable() {
141
 
                                        public void run() {
142
 
                                                if (!shell.isDisposed()) {
143
 
                                                        AcousticMessageDialog.openError(shell, title, msg);
144
 
                                                }
145
 
                                        }
146
 
                                });
147
 
                                System.exit(4);
148
 
                        }
149
 
 
150
 
                        public void connectionLostWarning(final String title,
151
 
                                        final String message, final IAdaptable adaptable) {
152
 
                                final Shell shell = getShell(adaptable);
153
 
                                syncExec(shell, new Runnable() {
154
 
                                        public void run() {
155
 
                                                if (!shell.isDisposed()) {
156
 
                                                        if (AcousticMessageDialog.openQuestion(shell,
157
 
                                                                        title, message))
158
 
                                                                configurer.emergencyClose();
159
 
                                                } else
160
 
                                                        configurer.emergencyClose();
161
 
                                        }
162
 
                                });
163
 
                        }
164
 
 
165
 
                        public void promptForReconnect(final String title,
166
 
                                        final String message, final IInputValidator validator,
167
 
                                        final IAdaptable adaptable) {
168
 
                                final Shell shell = getShell(adaptable);
169
 
                                syncExec(shell, new Runnable() {
170
 
                                        public void run() {
171
 
                                                if (!shell.isDisposed()
172
 
                                                                && new TimedMessageDialog(shell, validator,
173
 
                                                                                title, null, message,
174
 
                                                                                MessageDialog.WARNING, new String[] {
175
 
                                                                                                IDialogConstants.RETRY_LABEL,
176
 
                                                                                                IDialogConstants.ABORT_LABEL },
177
 
                                                                                0, 0, 1000L).open() != 0) {
178
 
                                                        shell.getDisplay().dispose();
179
 
                                                        System.exit(4);
180
 
                                                }
181
 
                                        }
182
 
                                });
183
 
                        }
184
 
 
185
 
                        public boolean question(String title, String message,
186
 
                                        IAdaptable adaptable) {
187
 
                                final Shell shell = getShell(adaptable);
188
 
                                final AcousticMessageDialog dialog = new AcousticMessageDialog(
189
 
                                                shell, title, null, message, MessageDialog.QUESTION,
190
 
                                                new String[] { IDialogConstants.YES_LABEL,
191
 
                                                                IDialogConstants.NO_LABEL }, 0);
192
 
                                syncExec(shell, new Runnable() {
193
 
                                        public void run() {
194
 
                                                dialog.open();
195
 
                                        }
196
 
                                });
197
 
                                return dialog.getReturnCode() == 0;
198
 
                        }
199
 
 
200
 
                        public void showError(final String title, final String message,
201
 
                                        IAdaptable adaptable) {
202
 
                                final Shell shell = getShell(adaptable);
203
 
                                asyncExec(shell, new Runnable() {
204
 
                                        public void run() {
205
 
                                                if (!shell.isDisposed())
206
 
                                                        AcousticMessageDialog.openError(shell, title,
207
 
                                                                        message);
208
 
                                        }
209
 
                                });
210
 
                        }
211
 
 
212
 
                        public void showWarning(final String title, final String message,
213
 
                                        IAdaptable adaptable) {
214
 
                                final Shell shell = getShell(adaptable);
215
 
                                asyncExec(shell, new Runnable() {
216
 
                                        public void run() {
217
 
                                                if (!shell.isDisposed())
218
 
                                                        AcousticMessageDialog.openWarning(shell, title,
219
 
                                                                        message);
220
 
                                        }
221
 
                                });
222
 
                        }
223
 
 
224
 
                        public void showInformation(final String title,
225
 
                                        final String message, IAdaptable adaptable) {
226
 
                                showInformation(title, message, adaptable, null);
227
 
                        }
228
 
 
229
 
                        public void showInformation(final String title,
230
 
                                        final String message, IAdaptable adaptable,
231
 
                                        final IValidator validator) {
232
 
                                final Shell shell = getShell(adaptable);
233
 
                                asyncExec(shell, new Runnable() {
234
 
                                        public void run() {
235
 
                                                if (!shell.isDisposed())
236
 
                                                        AcousticMessageDialog.openInformation(shell, title,
237
 
                                                                        message, validator);
238
 
                                        }
239
 
                                });
240
 
                        }
241
 
 
242
 
                        public int showMessageDialog(String dialogTitle,
243
 
                                        Image dialogTitleImage, String dialogMessage,
244
 
                                        int dialogImageType, String[] dialogButtonLabels,
245
 
                                        int defaultIndex, IAdaptable adaptable) {
246
 
                                final Shell shell = getShell(adaptable);
247
 
                                final AcousticMessageDialog dialog = new AcousticMessageDialog(
248
 
                                                shell, dialogTitle, dialogTitleImage, dialogMessage,
249
 
                                                dialogImageType, dialogButtonLabels, defaultIndex);
250
 
                                syncExec(shell, new Runnable() {
251
 
                                        public void run() {
252
 
                                                dialog.open();
253
 
                                        }
254
 
                                });
255
 
                                return dialog.getReturnCode();
256
 
                        }
257
 
 
258
 
                        public ImportConfiguration showConflictDialog(String title,
259
 
                                        String message, Asset asset, ImportConfiguration currentConfig,
260
 
                                        boolean multi, IAdaptable adaptable) {
261
 
                                final Shell shell = getShell(adaptable);
262
 
                                final ConflictDialog dialog = new ConflictDialog(shell, title,
263
 
                                                message, asset, currentConfig, multi);
264
 
                                syncExec(shell, new Runnable() {
265
 
                                        public void run() {
266
 
                                                dialog.open();
267
 
                                        }
268
 
                                });
269
 
                                return dialog.getCurrentConfig();
270
 
                        }
271
 
 
272
 
                        public void alarmOnPrompt(String sound) {
273
 
                                IUi ui = Ui.getUi();
274
 
                                if (ui != null)
275
 
                                        ui.playSound(sound, PreferenceConstants.ALARMONPROMPT);
276
 
                        }
277
 
 
278
 
                        public void signalEOJ(String sound) {
279
 
                                IUi ui = Ui.getUi();
280
 
                                if (ui != null)
281
 
                                        ui.playSound(sound, PreferenceConstants.ALARMONFINISH);
282
 
                        }
283
 
 
284
 
                        private void syncExec(Shell shell, Runnable runnable) {
285
 
                                if (shell != null && !shell.isDisposed())
286
 
                                        shell.getDisplay().syncExec(runnable);
287
 
                        }
288
 
 
289
 
                        private void asyncExec(Shell shell, Runnable runnable) {
290
 
                                if (shell != null && !shell.isDisposed())
291
 
                                        shell.getDisplay().asyncExec(runnable);
292
 
                        }
293
 
 
294
 
                        public Shell getShell(IAdaptable adaptable) {
295
 
                                Shell shell = adaptable == null ? null : (Shell) adaptable
296
 
                                                .getAdapter(Shell.class);
297
 
                                if (shell == null) {
298
 
                                        final Display display = Display.getDefault();
299
 
                                        if (!display.isDisposed()) {
300
 
                                                display.syncExec(new Runnable() {
301
 
                                                        public void run() {
302
 
                                                                auxShell = new Shell(display);
303
 
                                                        }
304
 
                                                });
305
 
                                                shell = auxShell;
306
 
                                                auxShell = null;
307
 
                                        }
308
 
                                }
309
 
                                return shell;
310
 
                        }
311
 
 
312
 
                        public File showDngDialog(File dngLocation, IAdaptable adaptable) {
313
 
                                Shell shell = getShell(adaptable);
314
 
                                final DNGConverterDialog dialog = new DNGConverterDialog(shell,
315
 
                                                dngLocation);
316
 
                                syncExec(shell, new Runnable() {
317
 
                                        public void run() {
318
 
                                                dialog.open();
319
 
                                        }
320
 
                                });
321
 
                                return dialog.getResult();
322
 
                        }
323
 
 
324
 
                        public IRawConverter showRawDialog(IAdaptable adaptable) {
325
 
                                final Shell shell = getShell(adaptable);
326
 
                                boolean result = question(
327
 
                                                Constants.APPLICATION_NAME,
328
 
                                                Messages.getString("ApplicationWorkbenchAdvisor.no_raw_converter"), adaptable); //$NON-NLS-1$
329
 
                                if (result) {
330
 
                                        final int ret[] = new int[1];
331
 
                                        syncExec(shell, new Runnable() {
332
 
                                                public void run() {
333
 
                                                        ret[0] = PreferencesUtil.createPreferenceDialogOn(shell,
334
 
                                                                        ImportPreferencePage.ID, new String[0], ImportPreferencePage.RAW).open();
335
 
                                                }
336
 
                                        });
337
 
                                        if (ret[0] == Dialog.OK)
338
 
                                                return BatchActivator.getDefault().getCurrentRawConverter(true);
339
 
                                }
340
 
                                return null;
341
 
                        }
342
 
 
343
 
                });
 
355
                Core.getCore().getDbFactory().setErrorHandler(new ErrorHandler(configurer));
344
356
                configurer.setSaveAndRestore(true);
 
357
                workbenchHelper = new WorkbenchHelper(this, true);
345
358
        }
346
359
 
347
360
        @Override
348
361
        public void postStartup() {
349
362
                super.postStartup();
 
363
//              Runtime.getRuntime().addShutdownHook(new Thread() {  // Caused to many problems during emergency shutdown of platform
 
364
//                      @Override
 
365
//                      public void run() {
 
366
//                              if (!getWorkbenchConfigurer().emergencyClosing()) {
 
367
//                                      BatchActivator.setFastExit(true);
 
368
//                                      getWorkbenchConfigurer().emergencyClose();
 
369
//                              }
 
370
//                      }
 
371
//              });
350
372
                final IWorkbench workbench = getWorkbenchConfigurer().getWorkbench();
351
373
                workbench.getPreferenceManager().remove(
352
374
                                "org.eclipse.help.ui.browsersPreferencePage"); //$NON-NLS-1$
541
563
                                setTrayVisible(true, MAINTENANCE);
542
564
                                UiActivator.getDefault().performClosingTasks(
543
565
                                                CatalogListener.TASKBAR);
 
566
                                workbenchHelper.flushWorkbenchState();
 
567
                                System.gc();
544
568
                        }
545
569
                        setTrayVisible(true, RUNNING);
546
570
                } else
573
597
                } catch (InterruptedException e1) {
574
598
                        // ignore
575
599
                }
576
 
                if (!CommonConstants.DEVELOPMENTMODE)
 
600
                if (!CommonConstants.DEVELOPMENTMODE
 
601
                                && !getWorkbenchConfigurer().emergencyClosing())
577
602
                        maintainInstallation();
578
603
        }
579
604
 
651
676
                                coreActivator.setRecentCats(recentList);
652
677
                        }
653
678
                }
654
 
                IStatus status = super.restoreState(memento);
655
679
                coreActivator.setCatFile(catFile);
656
 
                return status;
 
680
                return Status.OK_STATUS;
657
681
        }
658
682
 
659
683
        private File extractCatFile(String[] commandLineArgs) {