KickJava   Java API By Example, From Geeks To Geeks.

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


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: CreateMappingAction.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
23 import org.eclipse.jface.action.Action;
24 import org.eclipse.jface.action.IAction;
25 import org.eclipse.jface.resource.ImageDescriptor;
26 import org.eclipse.jface.viewers.ISelection;
27 import org.eclipse.jface.viewers.ISelectionProvider;
28 import org.eclipse.ui.IWorkbenchPart;
29
30 import org.eclipse.emf.common.command.Command;
31 import org.eclipse.emf.common.command.CommandStackListener;
32 import org.eclipse.emf.common.command.CommandWrapper;
33 import org.eclipse.emf.edit.command.CommandActionDelegate;
34 import org.eclipse.emf.edit.command.CommandParameter;
35 import org.eclipse.emf.edit.domain.EditingDomain;
36 import org.eclipse.emf.edit.ui.action.CommandAction;
37 import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
38 import org.eclipse.emf.mapping.MappingPlugin;
39 import org.eclipse.emf.mapping.command.CreateMappingCommand;
40 import org.eclipse.emf.mapping.domain.MappingDomain;
41 import org.eclipse.emf.mapping.presentation.IComposedSelection;
42 import org.eclipse.emf.mapping.provider.MappingItemProvider;
43
44
45 /**
46  */

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

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

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

117     public String JavaDoc getToolTipText()
118     {
119       return getDescription();
120     }
121   }
122
123   protected ImageDescriptor objectToImageDescriptor(Object JavaDoc object)
124   {
125     MappingDomain mappingDomain = (MappingDomain)editingDomain;
126
127     ((Action)action).setHoverImageDescriptor
128       (ExtendedImageRegistry.getInstance().getImageDescriptor
129         (MappingItemProvider.getImage(mappingDomain.getMappingRoot(), "full/ctool16/Create", collection, true)));
130
131     ((Action)action).setDisabledImageDescriptor
132       (ExtendedImageRegistry.getInstance().getImageDescriptor
133         (MappingItemProvider.getImage(mappingDomain.getMappingRoot(), "full/dtool16/Create", collection, true)));
134
135     ImageDescriptor result=
136       (ExtendedImageRegistry.getInstance().getImageDescriptor
137         (MappingItemProvider.getImage(mappingDomain.getMappingRoot(), "full/etool16/Create", collection, true)));
138
139     ((Action)action).setEnabled(!action.isEnabled());
140     ((Action)action).setImageDescriptor(result);
141     ((Action)action).setEnabled(!action.isEnabled());
142
143     return result;
144   }
145
146   /**
147    * Create the command for this action
148    */

149   protected Command createActionCommand(EditingDomain editingDomain, Collection JavaDoc collection)
150   {
151     return editingDomain.createCommand(DelegateCommand.class, new CommandParameter(null, null, collection));
152   }
153 }
154
Popular Tags