Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ab0a1f400159c9a6b41772efabe44b3c589c34ef (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
package org.eclipse.emf.cdo.internal.ui.actions;

import org.eclipse.emf.cdo.CDOTransaction;
import org.eclipse.emf.cdo.CDOView;

import org.eclipse.emf.common.ui.dialogs.ResourceDialog;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchPage;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Eike Stepper
 */
public class ImportResourceAction extends ViewAction
{
  public static final String ID = "import-resource";

  private static final String TITLE = "Import Resource";

  private URI sourceURI;

  private String targetPath;

  public ImportResourceAction(IWorkbenchPage page, CDOView view)
  {
    super(page, TITLE + INTERACTIVE, "Import a CDO resource", null, view);
    setId(ID);
  }

  @Override
  protected void preRun() throws Exception
  {
    ImportResourceDialog dialog = new ImportResourceDialog(getShell(), TITLE, SWT.OPEN);
    if (dialog.open() == ImportResourceDialog.OK)
    {
      List<URI> uris = dialog.getURIs();
      if (uris.size() == 1)
      {
        sourceURI = uris.get(0);
        targetPath = dialog.getTargetPath();
      }
      else
      {
        MessageDialog.openError(getShell(), TITLE, "A single URI must be entered!");
        cancel();
      }
    }
    else
    {
      cancel();
    }
  }

  @Override
  protected void doRun() throws Exception
  {
    CDOTransaction transaction = getTransaction();

    // Source ResourceSet
    ResourceSet sourceSet = new ResourceSetImpl();
    Map<String, Object> map = sourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap();
    map.put("*", new XMIResourceFactoryImpl());
    sourceSet.setPackageRegistry(transaction.getSession().getPackageRegistry());

    // Source Resource
    Resource source = sourceSet.getResource(sourceURI, true);
    List<EObject> sourceContents = new ArrayList<EObject>(source.getContents());

    // Target Resource
    Resource target = transaction.createResource(targetPath);
    EList<EObject> targetContents = target.getContents();

    // Move contents over
    for (EObject root : sourceContents)
    {
      targetContents.add(root);
    }
  }

  /**
   * @author Eike Stepper
   */
  public static class ImportResourceDialog extends ResourceDialog
  {
    private String targetPath = "/";

    private Text targetText;

    public ImportResourceDialog(Shell parent, String title, int style)
    {
      super(parent, title, style);
    }

    public String getTargetPath()
    {
      return targetPath;
    }

    public void setTargetPath(String targetPath)
    {
      this.targetPath = targetPath;
    }

    @Override
    protected Control createDialogArea(Composite parent)
    {
      Composite composite = (Composite)super.createDialogArea(parent);

      Label separatorLabel1 = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
      {
        FormData data = new FormData();
        data.top = new FormAttachment(uriField, (int)(1.5 * CONTROL_OFFSET));
        data.left = new FormAttachment(0, -CONTROL_OFFSET);
        data.right = new FormAttachment(100, CONTROL_OFFSET);
        separatorLabel1.setLayoutData(data);
      }

      Label label = new Label(composite, SWT.NONE);
      label.setText("Target path:");
      {
        FormData data = new FormData();
        data.top = new FormAttachment(separatorLabel1, CONTROL_OFFSET);
        data.left = new FormAttachment(0, CONTROL_OFFSET);
        data.right = new FormAttachment(100, -CONTROL_OFFSET);
        label.setLayoutData(data);
      }

      targetText = new Text(composite, SWT.BORDER);
      {
        FormData data = new FormData();
        data.top = new FormAttachment(label, CONTROL_OFFSET);
        data.left = new FormAttachment(0, CONTROL_OFFSET);
        data.right = new FormAttachment(100, -CONTROL_OFFSET);
        targetText.setLayoutData(data);
        targetText.setText(targetPath);
        targetText.addModifyListener(new ModifyListener()
        {
          public void modifyText(ModifyEvent e)
          {
            targetPath = targetText.getText();
          }
        });
      }

      Label separatorLabel2 = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
      {
        FormData data = new FormData();
        data.top = new FormAttachment(targetText, (int)(1.5 * CONTROL_OFFSET));
        data.left = new FormAttachment(0, -CONTROL_OFFSET);
        data.right = new FormAttachment(100, CONTROL_OFFSET);
        separatorLabel2.setLayoutData(data);
      }

      return composite;
    }
  }
}

Back to the top