Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 78dc864993853bde6722adb23bb6da0063d13fd9 (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
/*
 * Copyright (c) 2015 Eike Stepper (Berlin, Germany) and others.
 * 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.explorer.ui.checkouts.wizards;

import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.common.commit.CDOCommitInfo;
import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.eresource.CDOResourceFolder;
import org.eclipse.emf.cdo.eresource.CDOResourceNode;
import org.eclipse.emf.cdo.explorer.CDOExplorerUtil;
import org.eclipse.emf.cdo.explorer.checkouts.CDOCheckout;
import org.eclipse.emf.cdo.explorer.ui.bundle.OM;
import org.eclipse.emf.cdo.explorer.ui.checkouts.CDOCheckoutContentProvider;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.view.CDOView;

import org.eclipse.net4j.util.ui.UIUtil;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;

/**
 * @author Eike Stepper
 */
public abstract class NewWizard extends Wizard implements INewWizard
{
  private final String resourceType;

  private final String title;

  private IStructuredSelection selection;

  private CDOCheckoutContentProvider contentProvider;

  private NewWizardPage page;

  protected NewWizard(String resourceType, String title)
  {
    this.resourceType = resourceType;
    this.title = title;
  }

  public void init(IWorkbench workbench, IStructuredSelection selection)
  {
    this.selection = selection;
  }

  public void setContentProvider(CDOCheckoutContentProvider contentProvider)
  {
    this.contentProvider = contentProvider;
  }

  public final String getResourceType()
  {
    return resourceType;
  }

  public final String getTitle()
  {
    return title;
  }

  @Override
  public void addPages()
  {
    page = new NewWizardPage(resourceType, title, selection);
    addPage(page);
  }

  @Override
  public boolean performFinish()
  {
    final Object parent = page.getParent();
    final String name = page.getName();
    final String error = "An error occured while creating the " + title.toLowerCase() + ".";

    new Job(title)
    {
      @Override
      protected IStatus run(IProgressMonitor monitor)
      {
        CDOResourceNode newResourceNode = createNewResourceNode();
        newResourceNode.setName(name);

        CDOCheckout checkout;
        CDOResourceNode parentResourceNode;

        if (parent instanceof CDOCheckout)
        {
          checkout = (CDOCheckout)parent;
          parentResourceNode = (CDOResourceNode)checkout.getRootObject();
        }
        else
        {
          parentResourceNode = (CDOResourceNode)parent;
          checkout = CDOExplorerUtil.getCheckout(parentResourceNode);
        }

        CDOTransaction transaction = checkout.openTransaction();

        CDOCommitInfo commitInfo = null;
        CDOID newID = null;

        try
        {
          CDOResourceNode txParent = transaction.getObject(parentResourceNode);
          if (txParent instanceof CDOResourceFolder)
          {
            ((CDOResourceFolder)txParent).getNodes().add(newResourceNode);
          }
          else
          {
            transaction.getRootResource().getContents().add(newResourceNode);
          }

          commitInfo = transaction.commit();
          newID = newResourceNode.cdoID();
        }
        catch (Exception ex)
        {
          OM.LOG.error(ex);

          final IStatus status = new Status(IStatus.ERROR, OM.BUNDLE_ID, ex.getMessage(), ex);
          UIUtil.getDisplay().asyncExec(new Runnable()
          {
            public void run()
            {
              ErrorDialog.openError(getShell(), "Error", error, status);
            }
          });

          return Status.OK_STATUS;
        }
        finally
        {
          transaction.close();
        }

        if (commitInfo != null && contentProvider != null)
        {
          CDOView view = checkout.getView();
          if (!view.waitForUpdate(commitInfo.getTimeStamp(), 10000))
          {
            OM.LOG.error(error + " Did not receive an update");
            return Status.OK_STATUS;
          }

          CDOObject newObject = view.getObject(newID);
          contentProvider.selectObjects(newObject);
        }

        return Status.OK_STATUS;
      }
    }.schedule();

    return true;
  }

  protected abstract CDOResourceNode createNewResourceNode();
}

Back to the top