justinchen | 01a4f7c | 2006-06-21 22:53:17 +0000 | [diff] [blame^] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2006 Oracle Corporation. |
| 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 | * Oracle - initial API and implementation |
| 10 | * |
| 11 | ********************************************************************************/ |
justinchen | 488258a | 2006-04-27 23:47:21 +0000 | [diff] [blame] | 12 | package org.eclipse.jst.jsf.ui.tests.util; |
| 13 | |
| 14 | import java.util.Iterator; |
| 15 | |
| 16 | import org.eclipse.core.runtime.IAdaptable; |
| 17 | import org.eclipse.jface.preference.IPreferenceNode; |
| 18 | import org.eclipse.jface.preference.PreferenceDialog; |
| 19 | import org.eclipse.jface.preference.PreferenceManager; |
| 20 | import org.eclipse.jface.viewers.ISelection; |
| 21 | import org.eclipse.jface.viewers.StructuredSelection; |
| 22 | import org.eclipse.swt.widgets.Shell; |
| 23 | import org.eclipse.ui.PlatformUI; |
| 24 | import org.eclipse.ui.internal.WorkbenchPlugin; |
| 25 | import org.eclipse.ui.internal.dialogs.PropertyDialog; |
| 26 | import org.eclipse.ui.internal.dialogs.PropertyPageContributorManager; |
| 27 | import org.eclipse.ui.internal.dialogs.PropertyPageManager; |
| 28 | import org.eclipse.ui.model.IWorkbenchAdapter; |
| 29 | |
| 30 | /** |
| 31 | * Borrowed from UITestHelper in org.eclipse.wst.internet.monitor.ui.tests |
| 32 | * |
| 33 | * @author YCHEN3 |
| 34 | */ |
| 35 | public class JSFUITestHelper { |
| 36 | private static class PreferenceDialogWrapper extends PreferenceDialog { |
| 37 | public PreferenceDialogWrapper(Shell parentShell, PreferenceManager manager) { |
| 38 | super(parentShell, manager); |
| 39 | } |
| 40 | protected boolean showPage(IPreferenceNode node) { |
| 41 | return super.showPage(node); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | private static class PropertyDialogWrapper extends PropertyDialog { |
| 46 | public PropertyDialogWrapper(Shell parentShell, PreferenceManager manager, ISelection selection) { |
| 47 | super(parentShell, manager, selection); |
| 48 | } |
| 49 | protected boolean showPage(IPreferenceNode node) { |
| 50 | return super.showPage(node); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public static PreferenceDialog getPreferenceDialog(String id) { |
| 55 | PreferenceDialogWrapper dialog = null; |
| 56 | PreferenceManager manager = WorkbenchPlugin.getDefault().getPreferenceManager(); |
| 57 | if (manager != null) { |
| 58 | dialog = new PreferenceDialogWrapper(getShell(), manager); |
| 59 | dialog.create(); |
| 60 | |
| 61 | for (Iterator iterator = manager.getElements(PreferenceManager.PRE_ORDER).iterator(); |
| 62 | iterator.hasNext();) |
| 63 | { |
| 64 | IPreferenceNode node = (IPreferenceNode)iterator.next(); |
| 65 | if ( node.getId().equals(id) ) { |
| 66 | dialog.showPage(node); |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | return dialog; |
| 72 | } |
| 73 | |
| 74 | public static PropertyDialog getPropertyDialog(String id, IAdaptable element) { |
| 75 | PropertyDialogWrapper dialog = null; |
| 76 | |
| 77 | PropertyPageManager manager = new PropertyPageManager(); |
| 78 | String title = ""; |
| 79 | String name = ""; |
| 80 | |
| 81 | // load pages for the selection |
| 82 | // fill the manager with contributions from the matching contributors |
| 83 | PropertyPageContributorManager.getManager().contribute(manager, element); |
| 84 | |
| 85 | IWorkbenchAdapter adapter = (IWorkbenchAdapter)element.getAdapter(IWorkbenchAdapter.class); |
| 86 | if (adapter != null) { |
| 87 | name = adapter.getLabel(element); |
| 88 | } |
| 89 | |
| 90 | // testing if there are pages in the manager |
| 91 | Iterator pages = manager.getElements(PreferenceManager.PRE_ORDER).iterator(); |
| 92 | if (!pages.hasNext()) |
| 93 | return null; |
| 94 | |
| 95 | title = "Title: " + name; |
| 96 | dialog = new PropertyDialogWrapper(getShell(), manager, new StructuredSelection(element)); |
| 97 | dialog.create(); |
| 98 | dialog.getShell().setText(title); |
| 99 | for (Iterator iterator = manager.getElements(PreferenceManager.PRE_ORDER).iterator(); |
| 100 | iterator.hasNext();) { |
| 101 | IPreferenceNode node = (IPreferenceNode)iterator.next(); |
| 102 | if ( node.getId().equals(id) ) { |
| 103 | dialog.showPage(node); |
| 104 | break; |
| 105 | } |
| 106 | } |
| 107 | return dialog; |
| 108 | } |
| 109 | |
| 110 | protected static Shell getShell() { |
| 111 | return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); |
| 112 | } |
| 113 | |
| 114 | } // end of UITestHelper |