KickJava   Java API By Example, From Geeks To Geeks.

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


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

44 public class CreateOneSidedMappingAction extends CommandAction implements CommandStackListener
45 {
46   public void setActiveWorkbenchPart(IWorkbenchPart workbenchPart)
47   {
48     if (editingDomain != null)
49     {
50       editingDomain.getCommandStack().removeCommandStackListener(this);
51     }
52
53     super.setActiveWorkbenchPart(workbenchPart);
54
55     if (editingDomain != null)
56     {
57       editingDomain.getCommandStack().addCommandStackListener(this);
58     }
59   }
60
61   public void commandStackChanged(EventObject JavaDoc event)
62   {
63     selectionChanged(action, ((ISelectionProvider)workbenchPart).getSelection());
64   }
65
66   /**
67    * This returns the image that is used if the command does not provide an override.
68    */

69   protected Object JavaDoc getDefaultImage()
70   {
71     return MappingPlugin.getPlugin().getImage("full/etool16/CreateOneToOneMapping");
72   }
73
74   public static class DelegateCommand extends CommandWrapper implements CommandActionDelegate
75   {
76     protected MappingDomain mappingDomain;
77     protected Collection JavaDoc collection;
78
79     public DelegateCommand(EditingDomain editingDomain, CommandParameter commandParameter)
80     {
81       super(CreateMappingCommand.create((MappingDomain)editingDomain, commandParameter.getCollection()));
82       mappingDomain = (MappingDomain)editingDomain;
83       collection = commandParameter.getCollection();
84     }
85
86     /**
87      * This returns the icon, if any, of the action.
88      */

89     public Object JavaDoc getImage()
90     {
91       return "Placeholder";
92     }
93
94     public String JavaDoc getText()
95     {
96       return getLabel();
97     }
98
99     /**
100      * This returns the tool tip text, if any, of the action.
101      */

102     public String JavaDoc getToolTipText()
103     {
104       return getDescription();
105     }
106   }
107
108
109   protected ImageDescriptor objectToImageDescriptor(Object JavaDoc object)
110   {
111     MappingDomain mappingDomain = (MappingDomain)editingDomain;
112
113     ((Action)action).setHoverImageDescriptor
114       (ExtendedImageRegistry.getInstance().getImageDescriptor
115         (MappingItemProvider.getImage(mappingDomain.getMappingRoot(), "full/ctool16/Create", collection, true)));
116
117     ((Action)action).setDisabledImageDescriptor
118       (ExtendedImageRegistry.getInstance().getImageDescriptor
119         (MappingItemProvider.getImage(mappingDomain.getMappingRoot(), "full/dtool16/Create", collection, true)));
120
121     ((Action)action).setEnabled(!action.isEnabled());
122     ((Action)action).setEnabled(!action.isEnabled());
123
124     ImageDescriptor result=
125       ExtendedImageRegistry.getInstance().getImageDescriptor
126         (MappingItemProvider.getImage(mappingDomain.getMappingRoot(), "full/etool16/Create", collection, true));
127
128     ((Action)action).setEnabled(!action.isEnabled());
129     ((Action)action).setImageDescriptor(result);
130     ((Action)action).setEnabled(!action.isEnabled());
131
132     return result;
133   }
134
135   /**
136    * Create the command for this action
137    */

138   protected Command createActionCommand(EditingDomain editingDomain, Collection JavaDoc collection)
139   {
140     return editingDomain.createCommand(DelegateCommand.class, new CommandParameter(null, null, collection));
141   }
142 }
143
Popular Tags