csalter | 47f8bd8 | 2006-01-23 05:53:23 +0000 | [diff] [blame] | 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 | *******************************************************************************/ |
csalter | ce82000 | 2006-01-10 07:53:46 +0000 | [diff] [blame] | 12 | package org.eclipse.wst.xsd.ui.internal.refactor.wizard; |
| 13 | |
| 14 | import org.eclipse.emf.ecore.resource.ResourceSet; |
| 15 | import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
| 16 | import org.eclipse.jface.action.IAction; |
| 17 | import org.eclipse.jface.action.IMenuCreator; |
| 18 | import org.eclipse.jface.text.ITextSelection; |
| 19 | import org.eclipse.jface.viewers.ISelection; |
| 20 | import org.eclipse.jface.viewers.IStructuredSelection; |
| 21 | import org.eclipse.swt.events.MenuAdapter; |
| 22 | import org.eclipse.swt.events.MenuEvent; |
| 23 | import org.eclipse.swt.widgets.Control; |
| 24 | import org.eclipse.swt.widgets.Menu; |
| 25 | import org.eclipse.swt.widgets.MenuItem; |
| 26 | import org.eclipse.ui.IEditorActionDelegate; |
| 27 | import org.eclipse.ui.IEditorPart; |
| 28 | import org.eclipse.ui.IObjectActionDelegate; |
| 29 | import org.eclipse.ui.IWorkbenchPart; |
| 30 | |
| 31 | public 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 | } |