Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 966592904c5f309a4d59e729981aedbb860a0a4b (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
/*
 * 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.common.branch.CDOBranchPoint;
import org.eclipse.emf.cdo.explorer.CDOExplorerUtil;
import org.eclipse.emf.cdo.explorer.checkouts.CDOCheckout;
import org.eclipse.emf.cdo.explorer.checkouts.CDOCheckoutManager;
import org.eclipse.emf.cdo.explorer.repositories.CDORepository;
import org.eclipse.emf.cdo.explorer.repositories.CDORepositoryElement;
import org.eclipse.emf.cdo.explorer.ui.bundle.OM;
import org.eclipse.emf.cdo.explorer.ui.checkouts.CDOCheckoutContentProvider;

import org.eclipse.net4j.util.AdapterUtil;
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.dialogs.IPageChangedListener;
import org.eclipse.jface.dialogs.PageChangedEvent;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.IWizardContainer;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.ui.IImportWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.views.IViewDescriptor;

import java.util.Properties;

/**
 * @author Eike Stepper
 */
public class CheckoutWizard extends Wizard implements IImportWizard, IPageChangedListener
{
  private CDORepositoryElement selectedElement;

  private CheckoutRepositoryPage repositoryPage;

  private CheckoutTypePage typePage;

  private CheckoutBranchPointPage branchPointPage;

  private CheckoutRootObjectPage rootObjectPage;

  private CheckoutLabelPage labelPage;

  public CheckoutWizard()
  {
    setWindowTitle("New Checkout");
  }

  public final CheckoutRepositoryPage getRepositoryPage()
  {
    return repositoryPage;
  }

  public final CheckoutTypePage getTypePage()
  {
    return typePage;
  }

  public final CheckoutBranchPointPage getBranchPointPage()
  {
    return branchPointPage;
  }

  public final CheckoutRootObjectPage getRootObjectPage()
  {
    return rootObjectPage;
  }

  public final CheckoutLabelPage getLabelPage()
  {
    return labelPage;
  }

  public void init(IWorkbench workbench, IStructuredSelection selection)
  {
    if (selection.size() == 1)
    {
      Object element = selection.getFirstElement();
      selectedElement = AdapterUtil.adapt(element, CDORepositoryElement.class);
    }
  }

  @Override
  public void setContainer(IWizardContainer wizardContainer)
  {
    if (getContainer() instanceof WizardDialog)
    {
      ((WizardDialog)getContainer()).removePageChangedListener(this);
    }

    super.setContainer(wizardContainer);

    if (getContainer() instanceof WizardDialog)
    {
      ((WizardDialog)getContainer()).addPageChangedListener(this);
    }
  }

  public void pageChanged(PageChangedEvent event)
  {
    Object page = event.getSelectedPage();
    if (page instanceof CheckoutWizardPage)
    {
      CheckoutWizardPage checkoutWizardPage = (CheckoutWizardPage)page;
      checkoutWizardPage.pageActivated();
    }
  }

  @Override
  public void addPages()
  {
    addPage(repositoryPage = new CheckoutRepositoryPage());
    addPage(typePage = new CheckoutTypePage());
    addPage(branchPointPage = new CheckoutBranchPointPage());
    addPage(rootObjectPage = new CheckoutRootObjectPage());
    addPage(labelPage = new CheckoutLabelPage());

    if (selectedElement != null)
    {
      CDORepository repository = selectedElement.getRepository();
      if (repository != null)
      {
        repositoryPage.setRepository(repository);
        repositoryPage.skip();

        int branchID = selectedElement.getBranchID();
        long timeStamp = selectedElement.getTimeStamp();

        if (timeStamp != CDOBranchPoint.UNSPECIFIED_DATE)
        {
          typePage.setType(CDOCheckout.TYPE_ONLINE_HISTORICAL);
        }

        branchPointPage.setBranchPoint(branchID, timeStamp);
      }
    }
  }

  @Override
  public boolean performFinish()
  {
    final Properties properties = new Properties();
    repositoryPage.fillProperties(properties);
    typePage.fillProperties(properties);
    branchPointPage.fillProperties(properties);
    rootObjectPage.fillProperties(properties);
    labelPage.fillProperties(properties);

    new Job("Checkout")
    {
      @Override
      protected IStatus run(IProgressMonitor monitor)
      {
        try
        {
          CDOCheckoutManager checkoutManager = CDOExplorerUtil.getCheckoutManager();
          CDOCheckout checkout = checkoutManager.addCheckout(properties);
          checkout.open();

          showInProjectExplorer(checkout);
        }
        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", "An error occured while creating the checkout.", status);
            }
          });

          return Status.OK_STATUS;
        }
        finally
        {
        }

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

    return true;
  }

  public static void showInProjectExplorer(final CDOCheckout checkout)
  {
    UIUtil.getDisplay().asyncExec(new Runnable()
    {
      public void run()
      {
        IWorkbench workbench = PlatformUI.getWorkbench();

        IViewDescriptor viewDescriptor = workbench.getViewRegistry()
            .find(CDOCheckoutContentProvider.PROJECT_EXPLORER_ID);
        if (viewDescriptor != null)
        {
          IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
          if (window != null)
          {
            IWorkbenchPage page = window.getActivePage();
            if (page != null)
            {
              try
              {
                page.showView(viewDescriptor.getId());

                CDOCheckoutContentProvider checkoutContentProvider = CDOCheckoutContentProvider
                    .getInstance(CDOCheckoutContentProvider.PROJECT_EXPLORER_ID);
                if (checkoutContentProvider != null)
                {
                  checkoutContentProvider.selectObjects(checkout);
                }
              }
              catch (Exception ex)
              {
                OM.LOG.error(ex);
              }
            }
          }
        }
      }
    });
  }
}

Back to the top