Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 54536af5f41b2d1e4dc0862fb5528e6b93a460b0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/**
 */
package org.eclipse.emf.cdo.releng.setup.rcp.presentation;

import org.eclipse.emf.common.ui.URIEditorInput;
import org.eclipse.emf.common.ui.action.WorkbenchWindowActionDelegate;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.edit.ui.action.LoadResourceAction;
import org.eclipse.emf.edit.ui.util.EditUIUtil;

import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.jface.action.GroupMarker;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorDescriptor;
import org.eclipse.ui.IFolderLayout;
import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ContributionItemFactory;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchAdvisor;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;

import java.io.File;
import java.util.Arrays;

/**
 * Customized {@link WorkbenchAdvisor} for the RCP application.
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public final class SetupEditorAdvisor extends WorkbenchAdvisor
{
  /**
   * The default file extension filters for use in dialogs.
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  private static final String[] FILE_EXTENSION_FILTERS = SetupEditor.FILE_EXTENSION_FILTERS.toArray(new String[0]);

  /**
   * This looks up a string in the plugin's plugin.properties file.
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  private static String getString(String key)
  {
    return SetupEditorPlugin.INSTANCE.getString(key);
  }

  /**
   * This looks up a string in plugin.properties, making a substitution.
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  private static String getString(String key, Object s1)
  {
    return org.eclipse.emf.cdo.releng.setup.rcp.presentation.SetupEditorPlugin.INSTANCE.getString(key,
        new Object[] { s1 });
  }

  /**
   * RCP's application
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  public static class Application implements IApplication
  {
    /**
     * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext)
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @generated
     */
    public Object start(IApplicationContext context) throws Exception
    {
      WorkbenchAdvisor workbenchAdvisor = new SetupEditorAdvisor();
      Display display = PlatformUI.createDisplay();
      try
      {
        int returnCode = PlatformUI.createAndRunWorkbench(display, workbenchAdvisor);
        if (returnCode == PlatformUI.RETURN_RESTART)
        {
          return IApplication.EXIT_RESTART;
        }
        else
        {
          return IApplication.EXIT_OK;
        }
      }
      finally
      {
        display.dispose();
      }
    }

    /**
     * @see org.eclipse.equinox.app.IApplication#stop()
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @generated
     */
    public void stop()
    {
      // Do nothing.
    }
  }

  /**
   * RCP's perspective
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  public static class Perspective implements IPerspectiveFactory
  {
    /**
     * Perspective ID
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @generated
     */
    public static final String ID_PERSPECTIVE = "org.eclipse.emf.cdo.releng.setup.rcp.presentation.SetupEditorAdvisorPerspective";

    /**
     * @see org.eclipse.ui.IPerspectiveFactory#createInitialLayout(org.eclipse.ui.IPageLayout)
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @generated NOT
     */
    public void createInitialLayout(IPageLayout layout)
    {
      layout.setEditorAreaVisible(true);
      layout.addPerspectiveShortcut(ID_PERSPECTIVE);

      IFolderLayout bottom = layout.createFolder("bottom", IPageLayout.BOTTOM, (float)0.60, layout.getEditorArea());
      bottom.addView(IPageLayout.ID_PROP_SHEET);
    }
  }

  /**
   * RCP's window advisor
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  public static class WindowAdvisor extends WorkbenchWindowAdvisor
  {
    /**
     * @see WorkbenchWindowAdvisor#WorkbenchWindowAdvisor(org.eclipse.ui.application.IWorkbenchWindowConfigurer)
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @generated
     */
    public WindowAdvisor(IWorkbenchWindowConfigurer configurer)
    {
      super(configurer);
    }

    /**
     * @see org.eclipse.ui.application.WorkbenchWindowAdvisor#preWindowOpen()
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @generated
     */
    @Override
    public void preWindowOpen()
    {
      IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
      configurer.setInitialSize(new Point(600, 450));
      configurer.setShowCoolBar(false);
      configurer.setShowStatusLine(true);
      configurer.setTitle(getString("_UI_Application_title"));
    }

    /**
     * @see org.eclipse.ui.application.WorkbenchWindowAdvisor#createActionBarAdvisor(org.eclipse.ui.application.IActionBarConfigurer)
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @generated
     */
    @Override
    public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer)
    {
      return new WindowActionBarAdvisor(configurer);
    }
  }

  /**
   * RCP's action bar advisor
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  public static class WindowActionBarAdvisor extends ActionBarAdvisor
  {
    /**
     * @see ActionBarAdvisor#ActionBarAdvisor(org.eclipse.ui.application.IActionBarConfigurer)
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @generated
     */
    public WindowActionBarAdvisor(IActionBarConfigurer configurer)
    {
      super(configurer);
    }

    /**
     * @see org.eclipse.ui.application.ActionBarAdvisor#fillMenuBar(org.eclipse.jface.action.IMenuManager)
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @generated NOT
     */
    @Override
    protected void fillMenuBar(IMenuManager menuBar)
    {
      IWorkbenchWindow window = getActionBarConfigurer().getWindowConfigurer().getWindow();
      menuBar.add(createFileMenu(window));
      menuBar.add(createEditMenu(window));
      menuBar.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
      menuBar.add(createHelpMenu(window));
    }

    /**
     * Creates the 'File' menu.
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @generated
     */
    protected IMenuManager createFileMenu(IWorkbenchWindow window)
    {
      IMenuManager menu = new MenuManager(getString("_UI_Menu_File_label"), IWorkbenchActionConstants.M_FILE);
      menu.add(new GroupMarker(IWorkbenchActionConstants.FILE_START));

      IMenuManager newMenu = new MenuManager(getString("_UI_Menu_New_label"), "new");
      newMenu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));

      menu.add(newMenu);
      menu.add(new Separator());
      menu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
      menu.add(new Separator());
      addToMenuAndRegister(menu, ActionFactory.CLOSE.create(window));
      addToMenuAndRegister(menu, ActionFactory.CLOSE_ALL.create(window));
      menu.add(new Separator());
      addToMenuAndRegister(menu, ActionFactory.SAVE.create(window));
      addToMenuAndRegister(menu, ActionFactory.SAVE_AS.create(window));
      addToMenuAndRegister(menu, ActionFactory.SAVE_ALL.create(window));
      menu.add(new Separator());
      addToMenuAndRegister(menu, ActionFactory.QUIT.create(window));
      menu.add(new GroupMarker(IWorkbenchActionConstants.FILE_END));
      return menu;
    }

    /**
     * Creates the 'Edit' menu.
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @generated
     */
    protected IMenuManager createEditMenu(IWorkbenchWindow window)
    {
      IMenuManager menu = new MenuManager(getString("_UI_Menu_Edit_label"), IWorkbenchActionConstants.M_EDIT);
      menu.add(new GroupMarker(IWorkbenchActionConstants.EDIT_START));

      addToMenuAndRegister(menu, ActionFactory.UNDO.create(window));
      addToMenuAndRegister(menu, ActionFactory.REDO.create(window));
      menu.add(new GroupMarker(IWorkbenchActionConstants.UNDO_EXT));
      menu.add(new Separator());

      addToMenuAndRegister(menu, ActionFactory.CUT.create(window));
      addToMenuAndRegister(menu, ActionFactory.COPY.create(window));
      addToMenuAndRegister(menu, ActionFactory.PASTE.create(window));
      menu.add(new GroupMarker(IWorkbenchActionConstants.CUT_EXT));
      menu.add(new Separator());

      addToMenuAndRegister(menu, ActionFactory.DELETE.create(window));
      addToMenuAndRegister(menu, ActionFactory.SELECT_ALL.create(window));
      menu.add(new Separator());

      menu.add(new GroupMarker(IWorkbenchActionConstants.ADD_EXT));

      menu.add(new GroupMarker(IWorkbenchActionConstants.EDIT_END));
      menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
      return menu;
    }

    /**
     * Creates the 'Window' menu.
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @generated
     */
    protected IMenuManager createWindowMenu(IWorkbenchWindow window)
    {
      IMenuManager menu = new MenuManager(getString("_UI_Menu_Window_label"), IWorkbenchActionConstants.M_WINDOW);

      addToMenuAndRegister(menu, ActionFactory.OPEN_NEW_WINDOW.create(window));
      menu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
      menu.add(ContributionItemFactory.OPEN_WINDOWS.create(window));

      return menu;
    }

    /**
     * Creates the 'Help' menu.
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @generated
     */
    protected IMenuManager createHelpMenu(IWorkbenchWindow window)
    {
      IMenuManager menu = new MenuManager(getString("_UI_Menu_Help_label"), IWorkbenchActionConstants.M_HELP);
      // Welcome or intro page would go here
      // Help contents would go here
      // Tips and tricks page would go here
      menu.add(new GroupMarker(IWorkbenchActionConstants.HELP_START));
      menu.add(new GroupMarker(IWorkbenchActionConstants.HELP_END));
      menu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
      return menu;
    }

    /**
     * Adds the specified action to the given menu and also registers the action with the
     * action bar configurer, in order to activate its key binding.
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @generated
     */
    protected void addToMenuAndRegister(IMenuManager menuManager, IAction action)
    {
      menuManager.add(action);
      getActionBarConfigurer().registerGlobalAction(action);
    }
  }

  /**
   * About action for the RCP application.
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  public static class AboutAction extends WorkbenchWindowActionDelegate
  {
    /**
     * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @generated
     */
    public void run(IAction action)
    {
      MessageDialog.openInformation(getWindow().getShell(), getString("_UI_About_title"), getString("_UI_About_text"));
    }
  }

  /**
   * Open action for the objects from the Setup model.
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  public static class OpenAction extends WorkbenchWindowActionDelegate
  {
    /**
     * Opens the editors for the files selected using the file dialog.
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @generated
     */
    public void run(IAction action)
    {
      String[] filePaths = openFilePathDialog(getWindow().getShell(), SWT.OPEN, null);
      if (filePaths.length > 0)
      {
        openEditor(getWindow().getWorkbench(), URI.createFileURI(filePaths[0]));
      }
    }
  }

  /**
   * Open URI action for the objects from the Setup model.
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  public static class OpenURIAction extends WorkbenchWindowActionDelegate
  {
    /**
     * Opens the editors for the files selected using the LoadResourceDialog.
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @generated
     */
    public void run(IAction action)
    {
      LoadResourceAction.LoadResourceDialog loadResourceDialog = new LoadResourceAction.LoadResourceDialog(getWindow()
          .getShell());
      if (Window.OK == loadResourceDialog.open())
      {
        for (URI uri : loadResourceDialog.getURIs())
        {
          openEditor(getWindow().getWorkbench(), uri);
        }
      }
    }
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  public static String[] openFilePathDialog(Shell shell, int style, String[] fileExtensionFilters)
  {
    return openFilePathDialog(shell, style, fileExtensionFilters, (style & SWT.OPEN) != 0, (style & SWT.OPEN) != 0,
        (style & SWT.SAVE) != 0);
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  public static String[] openFilePathDialog(Shell shell, int style, String[] fileExtensionFilters,
      boolean includeGroupFilter, boolean includeAllFilter, boolean addExtension)
  {
    FileDialog fileDialog = new FileDialog(shell, style);
    if (fileExtensionFilters == null)
    {
      fileExtensionFilters = FILE_EXTENSION_FILTERS;
    }

    // If requested, augment the file extension filters by adding a group of all the other filters (*.ext1;*.ext2;...)
    // at the beginning and/or an all files wildcard (*.*) at the end.
    //
    includeGroupFilter &= fileExtensionFilters.length > 1;
    int offset = includeGroupFilter ? 1 : 0;

    if (includeGroupFilter || includeAllFilter)
    {
      int size = fileExtensionFilters.length + offset + (includeAllFilter ? 1 : 0);
      String[] allFilters = new String[size];
      StringBuilder group = includeGroupFilter ? new StringBuilder() : null;

      for (int i = 0; i < fileExtensionFilters.length; i++)
      {
        if (includeGroupFilter)
        {
          if (i != 0)
          {
            group.append(';');
          }
          group.append(fileExtensionFilters[i]);
        }
        allFilters[i + offset] = fileExtensionFilters[i];
      }

      if (includeGroupFilter)
      {
        allFilters[0] = group.toString();
      }
      if (includeAllFilter)
      {
        allFilters[allFilters.length - 1] = "*.*";
      }

      fileDialog.setFilterExtensions(allFilters);
    }
    else
    {
      fileDialog.setFilterExtensions(fileExtensionFilters);
    }
    fileDialog.open();

    String[] filenames = fileDialog.getFileNames();
    String[] result = new String[filenames.length];
    String path = fileDialog.getFilterPath() + File.separator;
    String extension = null;

    // If extension adding requested, get the dotted extension corresponding to the selected filter.
    //
    if (addExtension)
    {
      int i = fileDialog.getFilterIndex();
      if (i != -1 && (!includeAllFilter || i != fileExtensionFilters.length))
      {
        i = includeGroupFilter && i == 0 ? 0 : i - offset;
        String filter = fileExtensionFilters[i];
        int dot = filter.lastIndexOf('.');
        if (dot == 1 && filter.charAt(0) == '*')
        {
          extension = filter.substring(dot);
        }
      }
    }

    // Build the result by adding the selected path and, if needed, auto-appending the extension.
    //
    for (int i = 0; i < filenames.length; i++)
    {
      String filename = path + filenames[i];
      if (extension != null)
      {
        int dot = filename.lastIndexOf('.');
        if (dot == -1 || !Arrays.asList(fileExtensionFilters).contains("*" + filename.substring(dot)))
        {
          filename += extension;
        }
      }
      result[i] = filename;
    }
    return result;
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  public static boolean openEditor(IWorkbench workbench, URI uri)
  {
    IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
    IWorkbenchPage page = workbenchWindow.getActivePage();

    IEditorDescriptor editorDescriptor = EditUIUtil.getDefaultEditor(uri, null);
    if (editorDescriptor == null)
    {
      MessageDialog.openError(workbenchWindow.getShell(), getString("_UI_Error_title"),
          getString("_WARN_No_Editor", uri.lastSegment()));
      return false;
    }
    else
    {
      try
      {
        page.openEditor(new URIEditorInput(uri), editorDescriptor.getId());
      }
      catch (PartInitException exception)
      {
        MessageDialog.openError(workbenchWindow.getShell(), getString("_UI_OpenEditorError_label"),
            exception.getMessage());
        return false;
      }
    }
    return true;
  }

  /**
   * @see org.eclipse.ui.application.WorkbenchAdvisor#getInitialWindowPerspectiveId()
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  @Override
  public String getInitialWindowPerspectiveId()
  {
    return Perspective.ID_PERSPECTIVE;
  }

  /**
   * @see org.eclipse.ui.application.WorkbenchAdvisor#initialize(org.eclipse.ui.application.IWorkbenchConfigurer)
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  @Override
  public void initialize(IWorkbenchConfigurer configurer)
  {
    super.initialize(configurer);
    configurer.setSaveAndRestore(true);
  }

  /**
   * @see org.eclipse.ui.application.WorkbenchAdvisor#createWorkbenchWindowAdvisor(org.eclipse.ui.application.IWorkbenchWindowConfigurer)
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  @Override
  public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer)
  {
    return new WindowAdvisor(configurer);
  }
}

Back to the top