Skip to main content
summaryrefslogtreecommitdiffstats
blob: 05305752d99705b709b0fd9ed4b20ffce4719313 (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
 * Copyright (c) 2004 - 2012 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.widgets;

import org.eclipse.emf.cdo.transfer.CDOTransfer;
import org.eclipse.emf.cdo.transfer.CDOTransferElement;
import org.eclipse.emf.cdo.transfer.CDOTransferMapping;
import org.eclipse.emf.cdo.transfer.CDOTransferType;
import org.eclipse.emf.cdo.transfer.ui.TransferTypeContentProvider;

import org.eclipse.net4j.util.ObjectUtil;
import org.eclipse.net4j.util.StringUtil;
import org.eclipse.net4j.util.event.IEvent;
import org.eclipse.net4j.util.event.IListener;
import org.eclipse.net4j.util.ui.StructuredContentProvider;

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

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Eike Stepper
 * @since 4.2
 */
public class TransferDetailsComposite extends Composite implements IListener
{
  private static final String UP = "..";

  private static final Path UP_PATH = new Path(UP);

  private CDOTransfer transfer;

  private CDOTransferType[] transferTypes;

  private CDOTransferMapping mapping;

  private Text sourcePath;

  private Text targetPath;

  private ComboViewer transferType;

  private Text status;

  private Text relativePath;

  private Combo resolution;

  private ListViewer unmappedModels;

  public TransferDetailsComposite(Composite parent, int style, final CDOTransfer transfer)
  {
    super(parent, style);
    this.transfer = transfer;
    this.transfer.addListener(this);
    initTransferTypes(transfer);

    GridLayout gl_composite = new GridLayout(4, false);
    gl_composite.marginWidth = 10;
    setLayout(gl_composite);

    Label sourcePathLabel = new Label(this, SWT.NONE);
    sourcePathLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    sourcePathLabel.setText("Source Path:");

    sourcePath = new Text(this, SWT.BORDER | SWT.READ_ONLY);
    sourcePath.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

    Label transferTypeLabel = new Label(this, SWT.NONE);
    transferTypeLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    transferTypeLabel.setText("Type:");

    transferType = new ComboViewer(this, SWT.NONE);
    transferType.getCombo().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
    transferType.setContentProvider(new TransferTypeContentProvider());
    transferType.setLabelProvider(new LabelProvider());
    transferType.addSelectionChangedListener(new ISelectionChangedListener()
    {
      public void selectionChanged(SelectionChangedEvent event)
      {
        if (mapping != null)
        {
          IStructuredSelection selection = (IStructuredSelection)transferType.getSelection();
          CDOTransferType type = (CDOTransferType)selection.getFirstElement();
          mapping.setTransferType(type);
        }
      }
    });

    Label targetPathLabel = new Label(this, SWT.NONE);
    targetPathLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    targetPathLabel.setText("Target Path:");

    targetPath = new Text(this, SWT.BORDER | SWT.READ_ONLY);
    targetPath.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

    Label statusLabel = new Label(this, SWT.NONE);
    statusLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    statusLabel.setText("Status:");

    status = new Text(this, SWT.BORDER | SWT.READ_ONLY);
    status.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));

    Label relativePathLabel = new Label(this, SWT.NONE);
    relativePathLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    relativePathLabel.setText("Relative Path:");

    GridData gd_pathPane = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
    gd_pathPane.heightHint = 27;

    GridLayout pathPaneLayout = new GridLayout(4, false);
    pathPaneLayout.marginWidth = 0;
    pathPaneLayout.marginHeight = 0;

    Composite pathPane = new Composite(this, SWT.NONE);
    pathPane.setLayoutData(gd_pathPane);
    pathPane.setLayout(pathPaneLayout);

    relativePath = new Text(pathPane, SWT.BORDER);
    relativePath.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
    relativePath.addModifyListener(new ModifyListener()
    {
      public void modifyText(ModifyEvent e)
      {
        if (mapping != null)
        {
          String text = relativePath.getText();
          mapping.setRelativePath(text);
        }
      }
    });

    Button leftButton = new Button(pathPane, SWT.NONE);
    leftButton.setBounds(0, 0, 75, 25);
    leftButton.setText("<");
    leftButton.addSelectionListener(new SelectionAdapter()
    {
      @Override
      public void widgetSelected(SelectionEvent e)
      {
        IPath path = new Path(relativePath.getText());
        if (path.isEmpty() || UP.equals(path.segment(0)))
        {
          path = UP_PATH.append(path);
        }
        else
        {
          path = path.removeFirstSegments(1);
        }

        mapping.setRelativePath(path);
      }
    });

    Button rightButton = new Button(pathPane, SWT.NONE);
    rightButton.setBounds(0, 0, 75, 25);
    rightButton.setText(">");
    rightButton.addSelectionListener(new SelectionAdapter()
    {
      @Override
      public void widgetSelected(SelectionEvent e)
      {
        IPath path = new Path(relativePath.getText());
        if (UP.equals(path.segment(0)))
        {
          path = path.removeFirstSegments(1);
        }
        else if (path.isEmpty())
        {
          path = new Path(mapping.getSource().getName());
        }
        else
        {
          path = new Path("folder").append(path);
        }

        mapping.setRelativePath(path);
      }
    });

    Button renameButton = new Button(pathPane, SWT.NONE);
    renameButton.setText("+");
    renameButton.addSelectionListener(new SelectionAdapter()
    {
      @Override
      public void widgetSelected(SelectionEvent e)
      {
        String sourceName = mapping.getSource().getName();

        IPath path = new Path(relativePath.getText());
        if (path.isEmpty() || UP.equals(path.segment(0)))
        {
          path = new Path(sourceName);
        }

        String name = path.lastSegment();
        int i = 1;
        while (transfer.getTargetSystem().getElement(path) != null) // TODO This condition is still wrong
        {
          path = path.removeLastSegments(1).append(name + i);
        }

        mapping.setRelativePath(path);
      }
    });

    Label resolutionLabel = new Label(this, SWT.NONE);
    resolutionLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    resolutionLabel.setText("Resolution:");
    resolutionLabel.setVisible(false);

    resolution = new Combo(this, SWT.NONE);
    resolution.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
    resolution.setVisible(false);

    Label unmappedModelsLabel = new Label(this, SWT.NONE);
    unmappedModelsLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
    unmappedModelsLabel.setText("Unmapped Models:");

    unmappedModels = new ListViewer(this, SWT.BORDER);
    org.eclipse.swt.widgets.List list = unmappedModels.getList();
    list.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

    unmappedModels.setContentProvider(new UnmappedModelsContentProvider());
    unmappedModels.setLabelProvider(new UnmappedModelsLabelProvider());
    unmappedModels.setInput(transfer);

    GridLayout transformationButtonsPaneLayout = new GridLayout(1, false);
    transformationButtonsPaneLayout.marginWidth = 0;
    transformationButtonsPaneLayout.marginHeight = 0;

    Composite transformationButtonsPane = new Composite(this, SWT.NONE);
    transformationButtonsPane.setLayout(transformationButtonsPaneLayout);
    transformationButtonsPane.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false, 2, 1));

    Button mapSource = new Button(transformationButtonsPane, SWT.NONE);
    mapSource.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
    mapSource.setBounds(0, 0, 75, 25);
    mapSource.setText("Map From Source");
    mapSource.addSelectionListener(new SelectionAdapter()
    {
      @Override
      public void widgetSelected(SelectionEvent e)
      {
        IStructuredSelection selection = (IStructuredSelection)unmappedModels.getSelection();
        Resource resource = (Resource)selection.getFirstElement();

        URI uri = resource.getURI();
        CDOTransferElement element = transfer.getSourceSystem().getElement(uri);
        transfer.map(element);
      }
    });

    Button replaceTarget = new Button(transformationButtonsPane, SWT.NONE);
    replaceTarget.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
    replaceTarget.setBounds(0, 0, 75, 25);
    replaceTarget.setText("Replace With Target");

    Button keepAsIs = new Button(transformationButtonsPane, SWT.NONE);
    keepAsIs.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
    keepAsIs.setBounds(0, 0, 75, 25);
    keepAsIs.setText("Keep As Is");
  }

  @Override
  public void dispose()
  {
    transfer.removeListener(this);
    transfer = null;
    mapping = null;
    super.dispose();
  }

  public CDOTransfer getTransfer()
  {
    return transfer;
  }

  public CDOTransferMapping getMapping()
  {
    return mapping;
  }

  public void setMapping(CDOTransferMapping mapping)
  {
    if (!ObjectUtil.equals(this.mapping, mapping))
    {
      this.mapping = mapping;
      if (mapping != null)
      {
        sourcePath.setText(mapping.getSource().getPath().toString());
        targetPath.setText(mapping.getFullPath().toString());
        status.setText(mapping.getStatus().toString());
        relativePath.setText(mapping.getRelativePath().toString());

        CDOTransferType type = mapping.getTransferType();
        transferType.setInput(type != CDOTransferType.FOLDER ? transferTypes : CDOTransferType.FOLDER);
        transferType.setSelection(new StructuredSelection(type));
        transferType.getCombo().setEnabled(type != CDOTransferType.FOLDER);
      }
      else
      {
        sourcePath.setText(StringUtil.EMPTY);
        targetPath.setText(StringUtil.EMPTY);
        status.setText(StringUtil.EMPTY);
        relativePath.setText(StringUtil.EMPTY);

        transferType.setInput(TransferTypeContentProvider.NO_TANSFER_TYPES);
        transferType.setSelection(StructuredSelection.EMPTY);
      }
    }
  }

  public Text getSourcePath()
  {
    return sourcePath;
  }

  public Text getTargetPath()
  {
    return targetPath;
  }

  public ComboViewer getTransferType()
  {
    return transferType;
  }

  public Text getStatus()
  {
    return status;
  }

  public Text getRelativePath()
  {
    return relativePath;
  }

  public Combo getResolution()
  {
    return resolution;
  }

  public ListViewer getUnmappedModels()
  {
    return unmappedModels;
  }

  @Override
  public boolean setFocus()
  {
    return relativePath.setFocus();
  }

  public void notifyEvent(IEvent event)
  {
    if (event instanceof CDOTransfer.MappingEvent)
    {
      CDOTransfer.MappingEvent e = (CDOTransfer.MappingEvent)event;
      if (ObjectUtil.equals(e.getMapping(), mapping))
      {
        notifyMappingEvent(e);
      }
    }
  }

  protected void notifyMappingEvent(CDOTransfer.MappingEvent event)
  {
    if (event instanceof CDOTransfer.TransferTypeChangedEvent)
    {
      CDOTransfer.TransferTypeChangedEvent e = (CDOTransfer.TransferTypeChangedEvent)event;
      final CDOTransferType newType = e.getNewType();
      getDisplay().asyncExec(new Runnable()
      {
        public void run()
        {
          Object currentType = ((IStructuredSelection)transferType.getSelection()).getFirstElement();
          if (currentType != newType)
          {
            transferType.setSelection(new StructuredSelection(newType));
          }

          unmappedModels.refresh();
        }
      });
    }
    else if (event instanceof CDOTransfer.RelativePathChangedEvent)
    {
      CDOTransfer.RelativePathChangedEvent e = (CDOTransfer.RelativePathChangedEvent)event;
      final String value = e.getNewPath().toString();
      if (!ObjectUtil.equals(value, relativePath.getText()))
      {
        getDisplay().asyncExec(new Runnable()
        {
          public void run()
          {
            relativePath.setText(value);
            status.setText(mapping.getStatus().toString());
          }
        });
      }
    }
  }

  protected void initTransferTypes(final CDOTransfer transfer)
  {
    Set<CDOTransferType> set = new HashSet<CDOTransferType>(CDOTransferType.REGISTRY.values());
    set.remove(CDOTransferType.FOLDER);

    transferTypes = set.toArray(new CDOTransferType[set.size()]);
    Arrays.sort(transferTypes);
  }

  /**
   * @author Eike Stepper
   */
  public static class UnmappedModelsContentProvider extends StructuredContentProvider<CDOTransfer>
  {
    public Object[] getElements(Object inputElement)
    {
      CDOTransfer transfer = getInput();
      Set<Resource> resources = transfer.getModelTransferContext().resolve();
      return resources.toArray(new Resource[resources.size()]);
    }
  }

  /**
   * @author Eike Stepper
   */
  public static class UnmappedModelsLabelProvider extends LabelProvider
  {
    @Override
    public String getText(Object element)
    {
      if (element instanceof Resource)
      {
        Resource resource = (Resource)element;
        return resource.getURI().toString();
      }

      return super.getText(element);
    }
  }
}

Back to the top