Skip to main content
summaryrefslogtreecommitdiffstats
blob: ad6ef9adc3a68d736dc715f7b702dc2165bb6738 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*******************************************************************************
 * Copyright (c) 2004, 2006 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jsf.facesconfig.ui.page;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.jst.jsf.facesconfig.emf.FacesConfigPackage;
import org.eclipse.jst.jsf.facesconfig.emf.ManagedBeanType;
import org.eclipse.jst.jsf.facesconfig.emf.ManagedPropertyType;
import org.eclipse.jst.jsf.facesconfig.ui.FacesConfigEditor;
import org.eclipse.jst.jsf.facesconfig.ui.EditorMessages;
import org.eclipse.jst.jsf.facesconfig.ui.page.detail.ManagedBeanDetailsPage;
import org.eclipse.jst.jsf.facesconfig.ui.section.FacesConfigMasterSection;
import org.eclipse.jst.jsf.facesconfig.ui.section.ManagedBeanMasterSection;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.IDetailsPage;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
import org.eclipse.ui.views.contentoutline.IContentOutlinePage;

/**
 * 
 * @author sfshi
 * 
 */
public class ManagedBeanPage extends FacesConfigMasterDetailPage {

    /**
     * The id of this page in the faces-config editor.
     */
	public static final String PAGE_ID = "org.eclipse.jst.jsf.facesconfig.ui.page.ManagedBeanPage"; //$NON-NLS-1$

	private IContentOutlinePage contentOutlinePage;

	/**
	 * @param editor
	 */
	public ManagedBeanPage(FacesConfigEditor editor) {
		super(editor, "ManagedBeanPage", //$NON-NLS-1$
				EditorMessages.FacesConfigEditor_ManagedBeans_TabName);
	}

	public void dispose() {
		if (contentOutlinePage != null) {
			contentOutlinePage.dispose();
			contentOutlinePage = null;
		}
		super.dispose();
	}
	protected FacesConfigMasterSection[] createMasterSections(
			Composite composite, IManagedForm managedForm, FormToolkit toolkit,
			FacesConfigMasterDetailPage page) {
		FacesConfigMasterSection managedBeanMaster = new ManagedBeanMasterSection(
				composite, managedForm, toolkit, page);
		managedBeanMaster.getSection().setExpanded(true);
		GridData gd = new GridData(GridData.FILL_BOTH);
		managedBeanMaster.getSection().setLayoutData(gd);
		return new FacesConfigMasterSection[] { managedBeanMaster };
	}

	public IDetailsPage getPage(Object key) {
		IDetailsPage detailsPage = null;
		if (key instanceof EClass) {
			EClass eClass = (EClass) key;
			if (eClass.getClassifierID() == FacesConfigPackage.MANAGED_BEAN_TYPE) {
				detailsPage = new ManagedBeanDetailsPage(this);
			}
			
			if (detailsPage instanceof ISelectionProvider) {
				((ISelectionProvider) detailsPage)
						.addSelectionChangedListener(this);
			}
		}
		return detailsPage;
	}

	public Object getAdapter(Class adapter) {

		if (adapter == IContentOutlinePage.class) {
			return getOutlinePage();
		}

		return super.getAdapter(adapter);
	}

	private IContentOutlinePage getOutlinePage() {

		if (contentOutlinePage == null) {
			// The content outline is just a tree.
			//
			class ManagedBeanContentOutlinePage extends ContentOutlinePage {
				public void createControl(Composite parent) {
					super.createControl(parent);
					TreeViewer contentOutlineViewer = getTreeViewer();
					contentOutlineViewer.addSelectionChangedListener(this);

					AdapterFactory adapterFactory = (AdapterFactory) getEditor()
							.getAdapter(AdapterFactory.class);
					// Set up the tree viewer.
					contentOutlineViewer
							.setContentProvider(new AdapterFactoryContentProvider(
									adapterFactory));
					contentOutlineViewer
							.setLabelProvider(new AdapterFactoryLabelProvider(
									adapterFactory));
					contentOutlineViewer.addFilter(new ViewerFilter() {

						public boolean select(Viewer viewer,
								Object parentElement, Object element) {
							return FacesConfigPackage.eINSTANCE
									.getManagedBeanType().isInstance(element)
									|| FacesConfigPackage.eINSTANCE
											.getManagedPropertyType()
											.isInstance(element);
						}
					});

					contentOutlineViewer.setSorter(new ViewerSorter());
					contentOutlineViewer.setInput(getInput());
				}

			}

			contentOutlinePage = new ManagedBeanContentOutlinePage();

			// Listen to selection so that we can handle it is a special
			// way.
			//
			contentOutlinePage
					.addSelectionChangedListener(new ISelectionChangedListener() {
						// This ensures that we handle selections correctly.
						public void selectionChanged(SelectionChangedEvent event) {
							setSelection(event.getSelection());
						}
					});

			// this.addSelectionChangedListener((ContentOutlinePage)
			// contentOutlinePage);
		}

		return contentOutlinePage;

	}

	/**
	 * handle the selection changed event from outline page.
	 * 
	 * @param selection
	 */
	public void setSelection(ISelection selection) {

		if (selection instanceof IStructuredSelection) {
			IStructuredSelection ss = (IStructuredSelection) selection;
			ManagedBeanType bean = null;
			if (ss.getFirstElement() instanceof ManagedBeanType) {
				bean = (ManagedBeanType) ss.getFirstElement();
			} else if (ss.getFirstElement() instanceof ManagedPropertyType) {
				bean = (ManagedBeanType) ((ManagedPropertyType) ss
						.getFirstElement()).eContainer();
			}

			if (bean != null) {
				if (!getManagedBeanMasterSection().getSection().isExpanded()) {
					GridData gd = new GridData(GridData.FILL_BOTH);
					getManagedBeanMasterSection().getSection()
							.setLayoutData(gd);
					getManagedBeanMasterSection().getSection()
							.setExpanded(true);
				}
				IStructuredSelection newselection = new StructuredSelection(
						bean);
				getManagedBeanMasterSection().getStructuredViewer()
						.setSelection(newselection);
			}
		}

	}

	private ManagedBeanMasterSection getManagedBeanMasterSection() {
		return (ManagedBeanMasterSection) getFacesConfigMasterSections()[0];
	}
}

Back to the top