| 1 |
/** |
| 2 |
* Copyright (c) 2005 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Common Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/cpl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM - Initial API and implementation |
| 10 |
*/ |
| 11 |
package org.eclipse.emf.databinding.edit.internal; |
| 12 |
|
| 13 |
import java.util.ArrayList; |
| 14 |
import java.util.Collections; |
| 15 |
import java.util.List; |
| 16 |
|
| 17 |
import org.eclipse.core.databinding.observable.list.ListDiff; |
| 18 |
import org.eclipse.core.databinding.observable.list.ListDiffVisitor; |
| 19 |
|
| 20 |
import org.eclipse.emf.common.command.Command; |
| 21 |
import org.eclipse.emf.common.command.CompoundCommand; |
| 22 |
import org.eclipse.emf.databinding.internal.EMFResourceContentProperty; |
| 23 |
import org.eclipse.emf.ecore.resource.Resource; |
| 24 |
import org.eclipse.emf.edit.command.AddCommand; |
| 25 |
import org.eclipse.emf.edit.command.MoveCommand; |
| 26 |
import org.eclipse.emf.edit.command.RemoveCommand; |
| 27 |
import org.eclipse.emf.edit.command.ReplaceCommand; |
| 28 |
import org.eclipse.emf.edit.domain.EditingDomain; |
| 29 |
|
| 30 |
/** |
| 31 |
* @since 2.6 |
| 32 |
*/ |
| 33 |
public class EMFEditResourceContentProperty extends EMFResourceContentProperty |
| 34 |
{ |
| 35 |
private final EditingDomain editingDomain; |
| 36 |
|
| 37 |
public EMFEditResourceContentProperty(EditingDomain editingDomain) |
| 38 |
{ |
| 39 |
this.editingDomain = editingDomain; |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
protected void doSetList(Object source, List list, ListDiff diff) |
| 44 |
{ |
| 45 |
final ListVisitorImpl visitor = new ListVisitorImpl((Resource)source); |
| 46 |
diff.accept(visitor); |
| 47 |
editingDomain.getCommandStack().execute |
| 48 |
(new CompoundCommand() |
| 49 |
{ |
| 50 |
protected int index = 0; |
| 51 |
|
| 52 |
@Override |
| 53 |
protected boolean prepare() |
| 54 |
{ |
| 55 |
for (int size = visitor.commands.size(); index < size; ++index) |
| 56 |
{ |
| 57 |
if (visitor.commands.get(index).canExecute()) |
| 58 |
{ |
| 59 |
return true; |
| 60 |
} |
| 61 |
} |
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
public void execute() |
| 67 |
{ |
| 68 |
for (int size = visitor.commands.size(); index < size; ++index) |
| 69 |
{ |
| 70 |
appendAndExecute(visitor.commands.get(index)); |
| 71 |
} |
| 72 |
} |
| 73 |
}); |
| 74 |
} |
| 75 |
|
| 76 |
private class ListVisitorImpl extends ListDiffVisitor |
| 77 |
{ |
| 78 |
private Resource resource; |
| 79 |
protected final List<Command> commands = new ArrayList<Command>(); |
| 80 |
|
| 81 |
private ListVisitorImpl(Resource resource) |
| 82 |
{ |
| 83 |
this.resource = resource; |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public void handleAdd(int index, Object element) |
| 88 |
{ |
| 89 |
commands.add(new AddCommand(editingDomain, resource.getContents(), element, index)); |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public void handleMove(int oldIndex, int newIndex, Object element) |
| 94 |
{ |
| 95 |
commands.add(new MoveCommand(editingDomain, resource.getContents(), element, newIndex)); |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public void handleReplace(int index, Object oldElement, Object newElement) |
| 100 |
{ |
| 101 |
commands.add(new ReplaceCommand(editingDomain, resource.getContents(), oldElement, Collections.singleton(newElement))); |
| 102 |
} |
| 103 |
|
| 104 |
@Override |
| 105 |
public void handleRemove(int index, Object element) |
| 106 |
{ |
| 107 |
commands.add(new RemoveCommand(editingDomain, resource.getContents(), element)); |
| 108 |
} |
| 109 |
} |
| 110 |
} |