001: /**
002: *
003: */package newprocess.diagram.cust.annotations.contributor;
004:
005: import java.util.Iterator;
006: import java.util.Vector;
007:
008: import newprocess.diagram.cust.annotations.actions.FavouriteAnnotationAction;
009: import newprocess.diagram.cust.annotations.actions.LinkToAction;
010: import newprocess.diagram.cust.annotations.actions.ManageAnnotationAction;
011: import newprocess.diagram.cust.annotations.actions.ProgramAnnotationAction;
012: import newprocess.diagram.cust.annotations.commands.ReadCommand;
013: import newprocess.diagram.cust.annotations.utils.FeatureUtil;
014: import newprocess.diagram.cust.annotations.utils.PropertiesPageUtil;
015:
016: import org.eclipse.core.resources.IProject;
017: import org.eclipse.emf.common.util.EList;
018: import org.eclipse.gef.EditPart;
019: import org.eclipse.gef.GraphicalViewer;
020: import org.eclipse.jface.action.ActionContributionItem;
021: import org.eclipse.jface.action.IMenuListener;
022: import org.eclipse.jface.action.IMenuManager;
023: import org.eclipse.jface.action.MenuManager;
024: import org.eclipse.jface.action.Separator;
025: import org.eclipse.jface.resource.ImageDescriptor;
026: import org.eclipse.jface.viewers.ISelection;
027: import org.eclipse.jface.viewers.IStructuredSelection;
028: import org.eclipse.ui.PlatformUI;
029: import org.eclipse.ui.ide.IDE.SharedImages;
030:
031: /**
032: * @author sh
033: *
034: */
035: public class ContextMenuListener implements IMenuListener {
036: private IProject project = null;
037: private GraphicalViewer viewer = null;
038: private EditPart selectedEditPart = null;
039: private IStructuredSelection structuredSelection = null;
040:
041: /**
042: * Constructor
043: */
044: public ContextMenuListener(GraphicalViewer viewer, IProject project) {
045: this .project = project;
046: this .viewer = viewer;
047: }
048:
049: /* (non-Javadoc)
050: * @see org.eclipse.jface.action.IMenuListener#menuAboutToShow(org.eclipse.jface.action.IMenuManager)
051: */
052: public void menuAboutToShow(IMenuManager manager) {
053: // set the selected EditPart
054: selectionChanged();
055:
056: // fill the context menu
057: if (selectedEditPart == null)
058: return;
059: fillContextMenu(manager);
060: }
061:
062: /**
063: * fill the context Menu with entries
064: */
065: private void fillContextMenu(IMenuManager manager) {
066: manager.setRemoveAllWhenShown(true);
067: manager.remove("AnnotationsMenuID");
068:
069: // add a submenu for the Annotations
070: IMenuManager submenuManager = new MenuManager("Annotations",
071: "AnnotationsMenuID");
072: manager.insertAfter("additions", submenuManager);
073:
074: // insert some entries specified in the properties sheet page
075: createProgramsEntries(submenuManager);
076:
077: // insert the "Link to..." entry
078: createLinkToEntry(submenuManager);
079:
080: // insert the "Edit..." entry
081: createEditEntry(submenuManager);
082:
083: // insert the favourite annotations
084: createFavouriteEntries(submenuManager);
085: }
086:
087: /**
088: * create the program entries specified
089: * in the properties sheet page
090: */
091: private void createProgramsEntries(IMenuManager submenuManager) {
092: // in the first release the applications
093: // "MSWord", "MSExcel", "Websites"
094: // are specified
095: submenuManager.add(new Separator("programs"));
096: PropertiesPageUtil propertiesUtil = new PropertiesPageUtil(
097: project.getName());
098: Vector<ProgramAnnotationAction> actions = propertiesUtil
099: .createProgramActions(structuredSelection);
100: for (Iterator iter = actions.iterator(); iter.hasNext();) {
101: ProgramAnnotationAction element = (ProgramAnnotationAction) iter
102: .next();
103: submenuManager.add(new ActionContributionItem(element));
104: }
105: }
106:
107: /**
108: * create the "Link to..." entry
109: */
110: private void createLinkToEntry(IMenuManager submenuManager) {
111: submenuManager.add(new Separator("management"));
112: // create the images
113: ImageDescriptor linkToImage = PlatformUI.getWorkbench()
114: .getSharedImages().getImageDescriptor(
115: SharedImages.IMG_OBJS_BKMRK_TSK);
116: LinkToAction linkAction = new LinkToAction("Link to...",
117: linkToImage, selectedEditPart, project.toString());
118: submenuManager.add(new ActionContributionItem(linkAction));
119: }
120:
121: /**
122: * create the "Edit..." entry
123: */
124: private void createEditEntry(IMenuManager submenuManager) {
125: // create the images
126: ImageDescriptor manageImage = PlatformUI.getWorkbench()
127: .getSharedImages().getImageDescriptor(
128: SharedImages.IMG_OBJS_BKMRK_TSK);
129: ManageAnnotationAction manageAction = new ManageAnnotationAction(
130: "Edit...", manageImage, selectedEditPart, project
131: .getName());
132: submenuManager.add(new ActionContributionItem(manageAction));
133: }
134:
135: /**
136: * create the items to be shown in the
137: * Favourites List of the context menu
138: */
139: private void createFavouriteEntries(IMenuManager submenuManager) {
140: // get the stored Annotations
141: ReadCommand readCom = new ReadCommand();
142: EList annotations = readCom.getValues(selectedEditPart);
143: if (annotations == null)
144: return;
145:
146: // create an action for each annotation
147: FavouriteAnnotationAction favAction = null;
148: submenuManager.add(new Separator("favourites"));
149: for (Iterator iter = annotations.iterator(); iter.hasNext();) {
150: String element = (String) iter.next();
151: favAction = createFavouriteAction(element);
152: submenuManager.add(new ActionContributionItem(favAction));
153: }
154: }
155:
156: /**
157: * creates the actions for the
158: * favourite entries
159: */
160: private FavouriteAnnotationAction createFavouriteAction(String path) {
161: // create the images
162: ImageDescriptor favouriteImage = PlatformUI.getWorkbench()
163: .getSharedImages().getImageDescriptor(
164: SharedImages.IMG_OBJS_BKMRK_TSK);
165: // create an action for each entry
166: FavouriteAnnotationAction favAction = new FavouriteAnnotationAction(
167: project.getName(), path, favouriteImage);
168: return favAction;
169: }
170:
171: /**
172: * set the specified editPart
173: */
174: private void selectionChanged() {
175: ISelection selection = viewer.getSelection();
176: if (selection == null) {
177: selectedEditPart = null;
178: return;
179: }
180: if (selection instanceof IStructuredSelection) {
181: structuredSelection = (IStructuredSelection) selection;
182: if (!selection.isEmpty()) {
183: Object object = structuredSelection.getFirstElement();
184: if (object instanceof EditPart) {
185: // check if the given EditPart supports the Annotation mechanism
186: FeatureUtil featureUtil = new FeatureUtil();
187: boolean isSupported = featureUtil
188: .isAttributeSupported((EditPart) object);
189: if (isSupported)
190: selectedEditPart = (EditPart) object;
191: else
192: selectedEditPart = null;
193: }
194: }
195: }
196: }
197: }
|