KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > cheatsheets > composite > explorer > TreeExplorerMenu


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal.cheatsheets.composite.explorer;
13
14 /**
15  * Creates the context menu for a task explorer
16  */

17
18 import org.eclipse.jface.action.Action;
19 import org.eclipse.jface.action.IMenuListener;
20 import org.eclipse.jface.action.IMenuManager;
21 import org.eclipse.jface.action.MenuManager;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.swt.widgets.Menu;
25 import org.eclipse.ui.internal.cheatsheets.composite.model.TaskStateUtilities;
26 import org.eclipse.ui.internal.provisional.cheatsheets.ICompositeCheatSheetTask;
27 import org.eclipse.ui.internal.provisional.cheatsheets.TaskExplorer;
28
29 public class TreeExplorerMenu {
30     
31     private TaskExplorer explorer;
32
33     public TreeExplorerMenu(TaskExplorer explorer) {
34         this.explorer = explorer;
35         MenuManager menuMgr = new MenuManager(null);
36         menuMgr.setRemoveAllWhenShown(true);
37         menuMgr.addMenuListener(new IMenuListener() {
38             public void menuAboutToShow(IMenuManager manager) {
39                 fillContextMenu(manager);
40             }
41         });
42         Menu menu = menuMgr.createContextMenu(explorer.getControl());
43         explorer.getControl().setMenu(menu);
44     }
45
46     protected void fillContextMenu(IMenuManager manager) {
47         ISelection selection = explorer.getSelectionProvider().getSelection();
48         ICompositeCheatSheetTask selectedTask = null;
49         if (selection instanceof IStructuredSelection) {
50             IStructuredSelection structuredSelection = (IStructuredSelection)selection;
51             if (structuredSelection.size() == 1) {
52             selectedTask = (ICompositeCheatSheetTask)(structuredSelection).getFirstElement();
53             }
54         }
55         if (selectedTask == null) return;
56         
57         // Start
58
StartAction startAction = new StartAction(selectedTask);
59         startAction.setEnabled(TaskStateUtilities.isStartEnabled(selectedTask));
60         manager.add(startAction);
61         
62         // Skip
63
SkipAction skipAction = new SkipAction(selectedTask);
64         skipAction.setEnabled(TaskStateUtilities.isSkipEnabled(selectedTask));
65         manager.add(skipAction);
66         
67         // Restart
68
Action restartAction;
69         if (selectedTask.getParent() == null) {
70             restartAction = new RestartAllAction(selectedTask.getCompositeCheatSheet());
71         } else {
72             restartAction = new ResetTaskAction(selectedTask);
73             restartAction.setEnabled(selectedTask.getState() != ICompositeCheatSheetTask.NOT_STARTED);
74         }
75         manager.add(restartAction);
76     }
77
78 }
79
Popular Tags