KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > mapping > action > RemoveMappingAction


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2005 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: RemoveMappingAction.java,v 1.3 2005/06/08 06:23:57 nickb Exp $
16  */

17 package org.eclipse.emf.mapping.action;
18
19
20 import java.util.Collection JavaDoc;
21 import java.util.EventObject JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import org.eclipse.jface.action.Action;
25 import org.eclipse.jface.action.IAction;
26 import org.eclipse.jface.resource.ImageDescriptor;
27 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.jface.viewers.ISelectionProvider;
29 import org.eclipse.ui.IWorkbenchPart;
30
31 import org.eclipse.emf.common.command.Command;
32 import org.eclipse.emf.common.command.CommandStackListener;
33 import org.eclipse.emf.common.command.CommandWrapper;
34 import org.eclipse.emf.edit.command.CommandActionDelegate;
35 import org.eclipse.emf.edit.command.CommandParameter;
36 import org.eclipse.emf.edit.domain.EditingDomain;
37 import org.eclipse.emf.edit.ui.action.CommandAction;
38 import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
39 import org.eclipse.emf.mapping.Mapping;
40 import org.eclipse.emf.mapping.MappingPlugin;
41 import org.eclipse.emf.mapping.command.RemoveMappingCommand;
42 import org.eclipse.emf.mapping.domain.MappingDomain;
43 import org.eclipse.emf.mapping.presentation.IComposedSelection;
44 import org.eclipse.emf.mapping.provider.MappingItemProvider;
45
46
47 /**
48  */

49 public class RemoveMappingAction extends CommandAction implements CommandStackListener
50 {
51   public void setActiveWorkbenchPart(IWorkbenchPart workbenchPart)
52   {
53     if (editingDomain != null)
54     {
55       editingDomain.getCommandStack().removeCommandStackListener(this);
56     }
57
58     super.setActiveWorkbenchPart(workbenchPart);
59
60     if (editingDomain != null)
61     {
62       editingDomain.getCommandStack().addCommandStackListener(this);
63     }
64   }
65
66   public void commandStackChanged(EventObject JavaDoc event)
67   {
68     selectionChanged(action, ((ISelectionProvider)workbenchPart).getSelection());
69   }
70
71   public void selectionChanged(IAction action, ISelection selection)
72   {
73     if (selection instanceof IComposedSelection)
74     {
75       super.selectionChanged(action,((IComposedSelection)selection).getCombinedSelection());
76     }
77     else
78     {
79       super.selectionChanged(action, selection);
80     }
81   }
82
83   /**
84    * This returns the image that is used if the command does not provide an override.
85    */

86   protected Object JavaDoc getDefaultImage()
87   {
88     return MappingPlugin.getPlugin().getImage("full/etool16/RemoveOneToOneMapping");
89   }
90
91   public static class CommandDelegate extends CommandWrapper implements CommandActionDelegate
92   {
93     protected MappingDomain mappingDomain;
94     protected Collection JavaDoc collection;
95     public CommandDelegate(EditingDomain editingDomain, CommandParameter commandParameter)
96     {
97       super();
98       mappingDomain = (MappingDomain)editingDomain;
99       collection = commandParameter.getCollection();
100     }
101
102     /**
103      * This returns the icon, if any, of the action.
104      */

105     public Object JavaDoc getImage()
106     {
107       return "Placeholder";
108     }
109
110     public String JavaDoc getText()
111     {
112       return getLabel();
113     }
114
115     /**
116      * This returns the tool tip text, if any, of the action.
117      */

118     public String JavaDoc getToolTipText()
119     {
120       return getDescription();
121     }
122
123     public Command createCommand()
124     {
125       boolean allMappings = true;
126       for (Iterator JavaDoc objects = collection.iterator(); objects.hasNext(); )
127       {
128         Object JavaDoc object = objects.next();
129         if (!(object instanceof Mapping))
130         {
131           allMappings = false;
132           break;
133         }
134       }
135
136       if (allMappings)
137       {
138         return RemoveMappingCommand.create(mappingDomain, collection);
139       }
140       else
141       {
142         Collection JavaDoc mappings = mappingDomain.getMappingRoot().getExactMappings(collection);
143         return RemoveMappingCommand.create(mappingDomain, mappings);
144       }
145     }
146   }
147
148   protected ImageDescriptor objectToImageDescriptor(Object JavaDoc object)
149   {
150     MappingDomain mappingDomain = (MappingDomain)editingDomain;
151
152     ((Action)action).setHoverImageDescriptor
153       (ExtendedImageRegistry.getInstance().getImageDescriptor
154         (MappingItemProvider.getImage(mappingDomain.getMappingRoot(), "full/ctool16/Remove", collection)));
155
156     ((Action)action).setDisabledImageDescriptor
157       (ExtendedImageRegistry.getInstance().getImageDescriptor
158         (MappingItemProvider.getImage(mappingDomain.getMappingRoot(), "full/dtool16/Remove", collection)));
159
160     ImageDescriptor result=
161       ExtendedImageRegistry.getInstance().getImageDescriptor
162         (MappingItemProvider.getImage(mappingDomain.getMappingRoot(), "full/etool16/Remove", collection));
163
164     ((Action)action).setEnabled(!action.isEnabled());
165     ((Action)action).setImageDescriptor(result);
166     ((Action)action).setEnabled(!action.isEnabled());
167
168     return result;
169   }
170
171   /**
172    * Create the command for this action
173    */

174   protected Command createActionCommand(EditingDomain editingDomain, Collection JavaDoc collection)
175   {
176     return editingDomain.createCommand(CommandDelegate.class, new CommandParameter(null, null, collection));
177   }
178 }
179
Popular Tags