blob: 29c56783abe2b13e9b91f1c33e8c2fd28e68dcae [file] [log] [blame]
csalter47f8bd82006-01-23 05:53:23 +00001/*******************************************************************************
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 *******************************************************************************/
csalterce820002006-01-10 07:53:46 +000012package org.eclipse.wst.xsd.ui.internal.refactor.wizard;
13
14import org.eclipse.emf.ecore.resource.ResourceSet;
15import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
16import org.eclipse.jface.action.IAction;
17import org.eclipse.jface.action.IMenuCreator;
18import org.eclipse.jface.text.ITextSelection;
19import org.eclipse.jface.viewers.ISelection;
20import org.eclipse.jface.viewers.IStructuredSelection;
21import org.eclipse.swt.events.MenuAdapter;
22import org.eclipse.swt.events.MenuEvent;
23import org.eclipse.swt.widgets.Control;
24import org.eclipse.swt.widgets.Menu;
25import org.eclipse.swt.widgets.MenuItem;
26import org.eclipse.ui.IEditorActionDelegate;
27import org.eclipse.ui.IEditorPart;
28import org.eclipse.ui.IObjectActionDelegate;
29import org.eclipse.ui.IWorkbenchPart;
30
31public abstract class RefactorGroupActionDelegate implements IObjectActionDelegate, IEditorActionDelegate, IMenuCreator {
32
33 protected ISelection fSelection;
34 private IAction fDelegateAction;
35 // whether to re-fill the menu (reset on selection change)
36 private boolean fFillMenu = true;
37 protected IWorkbenchPart workbenchPart;
38 protected ResourceSet resourceSet = new ResourceSetImpl();
39
40
41 public RefactorGroupActionDelegate() {
42
43 }
44
45 /*
46 * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
47 */
48 public void setActivePart(IAction action, IWorkbenchPart targetPart) {
49 workbenchPart = targetPart;
50 }
51 /* (non-Javadoc)
52 * @see org.eclipse.jface.action.IMenuCreator#dispose()
53 */
54 public void dispose() {
55 // nothing to do
56 }
57 /* (non-Javadoc)
58 * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Control)
59 */
60 public Menu getMenu(Control parent) {
61 // never called
62 return null;
63 }
64 /* (non-Javadoc)
65 * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Menu)
66 */
67 public Menu getMenu(Menu parent) {
68 //Create the new menu. The menu will get filled when it is about to be shown. see fillMenu(Menu).
69 Menu menu = new Menu(parent);
70 /**
71 * Add listener to repopulate the menu each time
72 * it is shown because MenuManager.update(boolean, boolean)
73 * doesn't dispose pulldown ActionContribution items for each popup menu.
74 */
75 menu.addMenuListener(new MenuAdapter() {
76 public void menuShown(MenuEvent e) {
77 if (fFillMenu) {
78 Menu m = (Menu)e.widget;
79 MenuItem[] items = m.getItems();
80 for (int i=0; i < items.length; i++) {
81 items[i].dispose();
82 }
83 fillMenu(m);
84 fFillMenu = false;
85 }
86 }
87 });
88 return menu;
89 }
90
91 /*
92 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
93 */
94 public void run(IAction action) {
95 // Never called because we become a menu.
96 }
97
98 /*
99 * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
100 */
101 public void selectionChanged(IAction action, ISelection selection) {
102 fDelegateAction = action;
103 updateWith(selection);
104
105 }
106
107 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
108 workbenchPart = targetEditor;
109 fDelegateAction = action;
110 if (targetEditor != null && targetEditor.getEditorSite() != null && targetEditor.getEditorSite().getSelectionProvider() != null) {
111 updateWith(targetEditor.getEditorSite().getSelectionProvider().getSelection());
112 }
113
114 }
115
116 public void updateWith(ISelection selection) {
117 fSelection = selection;
118 if (fDelegateAction != null) {
119 boolean enable = false;
120 if (selection != null) {
121 if (selection instanceof ITextSelection) {
122 //if (((ITextSelection) selection).getLength() > 0) {
123 enable = true;
124 //}
125 }
126 else if(selection instanceof IStructuredSelection ){
127 enable = !selection.isEmpty();
128 }
129 }
130 // enable action
131 fDelegateAction.setEnabled(enable);
132
133 // fill submenu
134 fFillMenu = true;
135 fDelegateAction.setMenuCreator(this);
136
137
138 }
139
140 }
141
142
143 protected abstract void fillMenu(Menu menu);
144
145
146}