KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > mapping > command > PasteFromClipboardOverrideCommand


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: PasteFromClipboardOverrideCommand.java,v 1.2 2005/06/08 06:21:43 nickb Exp $
16  */

17 package org.eclipse.emf.mapping.command;
18
19
20 import java.util.Collection JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import org.eclipse.emf.common.command.Command;
24 import org.eclipse.emf.common.command.CompoundCommand;
25 import org.eclipse.emf.edit.command.AddCommand;
26 import org.eclipse.emf.edit.command.CopyCommand;
27 import org.eclipse.emf.edit.command.CreateCopyCommand;
28 import org.eclipse.emf.edit.command.PasteFromClipboardCommand;
29 import org.eclipse.emf.mapping.domain.MappingDomain;
30
31
32 /**
33  * This command overrides the PasteFromClipboardCommand for cross-domain (optimized) copies.
34  */

35 public class PasteFromClipboardOverrideCommand extends PasteFromClipboardCommand
36 {
37   public PasteFromClipboardOverrideCommand(MappingDomain domain, PasteFromClipboardCommand command)
38   {
39     super(domain, command.getOwner(), command.getFeature(), command.getIndex(), domain.getOptimizeCopy());
40   }
41
42   protected boolean optimizedCanExecute()
43   {
44     Collection JavaDoc collection = domain.getClipboard();
45     if (collection == null)
46     {
47       return false;
48     }
49
50     // We'll try adding a shallow copy of the clipboard contents, instead of a full copy.
51
// Note: we can't just try adding the clipboard contents itself, because the copy may be a
52
// different type then what's on the clipboard (e.g. EJB Field -> RDB Column).
53
//
54
CopyCommand.Helper copyHelper = new CopyCommand.Helper();
55     CompoundCommand shallowCopyCommand = new CompoundCommand(CompoundCommand.MERGE_COMMAND_ALL);
56     for (Iterator JavaDoc objects = collection.iterator(); objects.hasNext(); )
57     {
58       if (!shallowCopyCommand.appendAndExecute(CreateCopyCommand.create(domain, objects.next(), copyHelper)))
59       {
60         shallowCopyCommand.dispose();
61         return false;
62       }
63     }
64
65     Command addCommand = AddCommand.create(domain, owner, feature, shallowCopyCommand.getResult());
66     boolean result = addCommand.canExecute();
67
68     shallowCopyCommand.dispose();
69     addCommand.dispose();
70
71     return result;
72   }
73
74 }
75
Popular Tags