Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 55c39576e2c978ef1240466c4c70611abee77c3c (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
/***************************************************************************
 * Copyright (c) 2004 - 2008 Eike Stepper, Germany.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *    Eike Stepper - initial API and implementation
 **************************************************************************/
package org.eclipse.emf.cdo.internal.ui.actions;

import org.eclipse.emf.cdo.CDOSession;
import org.eclipse.emf.cdo.internal.ui.bundle.OM;
import org.eclipse.emf.cdo.util.CDOPackageRegistry;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchPage;

import java.util.List;

/**
 * @author Eike Stepper
 */
public abstract class RegisterPackagesAction extends SessionAction
{
  private List<EPackage> ePackages;

  private int errors;

  public RegisterPackagesAction(IWorkbenchPage page, String text, String toolTipText, ImageDescriptor image,
      CDOSession session)
  {
    super(page, text, toolTipText, image, session);
  }

  @Override
  protected void preRun() throws Exception
  {
    ePackages = getEPackages(getPage(), getSession());
    if (ePackages == null)
    {
      cancel();
    }
  }

  @Override
  protected void doRun() throws Exception
  {
    errors = 0;
    CDOPackageRegistry packageRegistry = getSession().getPackageRegistry();
    for (EPackage ePackage : ePackages)
    {
      Resource resource = ePackage.eResource();
      URI uri = resource == null ? null : resource.getURI();

      try
      {
        packageRegistry.putEPackage(ePackage);
      }
      catch (RuntimeException ex)
      {
        ++errors;
        if (uri == null)
        {
          OM.LOG.error(ex);
        }
        else
        {
          OM.LOG.error("Failed to register package " + uri, ex);
        }
      }
    }

    postRegistration(ePackages);
    if (errors != 0)
    {
      final String label = String.valueOf(errors) + (errors == 1 ? " package has" : " packages have");
      final Shell shell = getShell();
      shell.getDisplay().asyncExec(new Runnable()
      {
        public void run()
        {
          MessageDialog.openError(shell, getText(), label
              + " not been registered due to errors.\nSee the log for details.");
        }
      });
    }
  }

  protected void postRegistration(List<EPackage> ePackages)
  {
  }

  protected abstract List<EPackage> getEPackages(IWorkbenchPage page, CDOSession session);
}

Back to the top