Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2c8007daa59a6e647428d387d7859bce725bda0e (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
/*
 * Copyright (c) 2012, 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.transfer.ui;

import org.eclipse.emf.cdo.transfer.CDOTransfer;
import org.eclipse.emf.cdo.transfer.CDOTransferElement;
import org.eclipse.emf.cdo.transfer.CDOTransferSystem;
import org.eclipse.emf.cdo.transfer.ui.swt.TransferComposite;
import org.eclipse.emf.cdo.ui.shared.SharedIcons;

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

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.net4j.util.om.monitor.SubProgressMonitor;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.List;

/**
 * A dialog that contains a {@link TransferComposite}.
 *
 * @author Eike Stepper
 */
public class TransferDialog extends TitleAreaDialog
{
  private final CDOTransfer transfer;

  private TransferComposite transferComposite;

  public TransferDialog(Shell parentShell, CDOTransfer transfer)
  {
    super(parentShell);
    setShellStyle(SWT.RESIZE | SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
    this.transfer = transfer;
  }

  public final CDOTransfer getTransfer()
  {
    return transfer;
  }

  public final TransferComposite getTransferComposite()
  {
    return transferComposite;
  }

  @Override
  protected Control createDialogArea(Composite parent)
  {
    setTitle("Transfer from " + transfer.getSourceSystem() + " to " + transfer.getTargetSystem());
    setTitleImage(SharedIcons.getImage(SharedIcons.WIZBAN_TRANSFER));

    Composite area = (Composite)super.createDialogArea(parent);

    Composite container = new Composite(area, SWT.NONE);
    container.setLayoutData(new GridData(GridData.FILL_BOTH));
    container.setLayout(new FillLayout());

    transferComposite = new TransferComposite(container, transfer);
    return area;
  }

  @Override
  protected void createButtonsForButtonBar(Composite parent)
  {
    createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
    createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
  }

  @Override
  protected void okPressed()
  {
    UIUtil.runWithProgress(new IRunnableWithProgress()
    {
      public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException
      {
        try
        {
          transfer.perform(monitor);
        }
        catch (OperationCanceledException ex)
        {
          throw new InterruptedException();
        }
        catch (RuntimeException ex)
        {
          throw ex;
        }
        catch (Exception ex)
        {
          throw new InvocationTargetException(ex);
        }
      }
    });

    super.okPressed();
  }

  @Override
  protected Point getInitialSize()
  {
    return new Point(1000, 800);
  }

  public static boolean open(Shell shell, final List<CDOTransferElement> sourceElements,
      final CDOTransferElement targetElement)
  {
    final CDOTransferSystem sourceSystem = sourceElements.get(0).getSystem();
    final CDOTransferSystem targetSystem = targetElement.getSystem();

    final CDOTransfer transfer = new CDOTransfer(sourceSystem, targetSystem);
    transfer.setTargetPath(targetElement.getPath());

    initializeTransfer(transfer, sourceElements);

    TransferDialog dialog = new TransferDialog(shell, transfer);
    return dialog.open() == TransferDialog.OK;
  }

  public static void initializeTransfer(final CDOTransfer transfer, final Collection<CDOTransferElement> sourceElements)
  {
    UIUtil.runWithProgress(new IRunnableWithProgress()
    {
      public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException
      {
        try
        {
          CDOTransferSystem sourceSystem = transfer.getSourceSystem();
          CDOTransferSystem targetSystem = transfer.getTargetSystem();
          monitor.beginTask("Initialize transfer from " + sourceSystem + " to " + targetSystem, sourceElements.size());

          for (CDOTransferElement sourceElement : sourceElements)
          {
            transfer.map(sourceElement, new SubProgressMonitor(monitor, 1));
          }
        }
        catch (OperationCanceledException ex)
        {
          throw new InterruptedException();
        }
        catch (RuntimeException ex)
        {
          throw ex;
        }
        catch (Exception ex)
        {
          throw new InvocationTargetException(ex);
        }
        finally
        {
          monitor.done();
        }
      }
    });
  }
}

Back to the top