Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8d4748938a763e9619c890a180953840738d84b4 (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*******************************************************************************
 * Copyright (c) 2004, 2005 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.section;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.help.HelpSystem;
import org.eclipse.help.IContext;
import org.eclipse.help.IHelpResource;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jst.jsf.facesconfig.ui.EditorPlugin;
import org.eclipse.jst.jsf.facesconfig.ui.page.IFacesConfigPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.SectionPart;
import org.eclipse.ui.forms.events.HyperlinkAdapter;
import org.eclipse.ui.forms.events.HyperlinkEvent;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ImageHyperlink;
import org.eclipse.ui.forms.widgets.Section;

/**
 * The abstract implementation of IFacesConfigSection.
 * 
 * 
 * @author jchoi, xgzhang, sfshi
 * 
 * @version 1.0
 */
public abstract class AbstractFacesConfigSection extends SectionPart implements
		IFacesConfigSection, ISelectionProvider, ISelectionChangedListener {
	/** tool kit */
	private FormToolkit toolkit;

	/** help context id */
	private String helpContextId = null;

	/** help tool tip string */
	private String helpTooltip = null;

	/** model object */
	private Object input = null;

	private IFacesConfigPage page;

	private List selectionChangedListeners = new ArrayList();

	/** help image */
	private final static Image HELP_IMAGE = EditorPlugin.getDefault().getImage(
			"help.gif"); //$NON-NLS-1$

	/**
	 * Constructor with help option.

	 * @param parent 
	 * @param managedForm
	 * @param page 
	 * @param toolkit
	 * @param helpContextId
	 * @param helpTooltip
	 */
	public AbstractFacesConfigSection(Composite parent,
			IManagedForm managedForm, IFacesConfigPage page,
			FormToolkit toolkit, String helpContextId, String helpTooltip) {
		super(parent, toolkit, ExpandableComposite.TITLE_BAR
				| ExpandableComposite.TWISTIE | Section.DESCRIPTION
				| ExpandableComposite.EXPANDED);
		super.initialize(managedForm);
		this.page = page;
		this.toolkit = toolkit;
		this.helpContextId = helpContextId;
		this.helpTooltip = helpTooltip;
	}

	/**
	 * 
	 */
	public void initialize() {
		if (helpContextId != null) {
			createTextClientWithHelp();
		}

		getSection().setLayout(new GridLayout());

		Composite clientContainer = toolkit.createComposite(getSection());
		GridData gd = new GridData(GridData.FILL_BOTH);
		clientContainer.setLayoutData(gd);

		createContents(clientContainer, toolkit);
		getSection().setClient(clientContainer);

	}

	/**
	 * 
	 * 
	 */
	private void createTextClientWithHelp() {
		ImageHyperlink helpImage = new ImageHyperlink(getSection(), SWT.NULL);

		toolkit.adapt(helpImage, true, true);

		helpImage.setImage(HELP_IMAGE); //$NON-NLS-1$    	

		helpImage.setBackground(getSection().getTitleBarGradientBackground());
		if (helpTooltip != null) {
			helpImage.setToolTipText(helpTooltip);
		}

		helpImage.addHyperlinkListener(new HyperlinkAdapter() {
			public void linkActivated(HyperlinkEvent e) {
				IContext context = HelpSystem.getContext(helpContextId);
				if (context != null) {
					IHelpResource[] topics = context.getRelatedTopics();
					if (topics != null && topics.length == 1) {
						EditorPlugin.getDefault().getWorkbench()
								.getHelpSystem().displayHelpResource(
										topics[0].getHref());
					} else {
						EditorPlugin.getDefault().getWorkbench()
								.getHelpSystem().displayHelp(helpContextId);
					}
				}
			}
		});

		getSection().setTextClient(helpImage);
	}

	/**
	 * The sub calss should implement this method to create and fill the
	 * contents in the section
	 * 
	 * @param container
	 * @param toolkit_
	 */
	protected abstract void createContents(Composite container,
			FormToolkit toolkit_);

	/**
	 * get the input object of this section.
	 */
	public Object getInput() {
		return input;
	}

	/**
	 * set input object for this section.
	 */
	public void setInput(Object newInput) {
		if (input != null)
			removeAdaptersFromInput(input);

		input = newInput;

		if (newInput != null)
			addAdaptersOntoInput(newInput);
		// if (this.getSection().isExpanded())
		refreshAll();
	}

	/**
	 * Remove adapters from the input object when the section changes it's
	 * input. Sub classes should overwrite this method to remove the adapters
	 * that they added on.
	 * 
	 * @param oldInput
	 */
	protected void removeAdaptersFromInput(Object oldInput) {
	    // do nothing; subs should override
	}

	/**
	 * Add adapters onto the input object when the section has new input. Sub
	 * classes should their own adapters.
	 * 
	 * @param newInput
	 */
	protected void addAdaptersOntoInput(Object newInput) {
        // do nothing; subs should override
	}

	/**
	 * @return the page that this section lies in.
	 */
	public IFacesConfigPage getPage() {
		return page;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.viewers.ISelectionProvider#getSelection()
	 */
	public ISelection getSelection() {
		return StructuredSelection.EMPTY;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.viewers.ISelectionProvider#setSelection(org.eclipse.jface.viewers.ISelection)
	 */
	public void setSelection(ISelection selection) {
	    // do nothing: no selection change
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.viewers.ISelectionProvider#addSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
	 */
	public void addSelectionChangedListener(ISelectionChangedListener listener) {
		selectionChangedListeners.add(listener);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.viewers.ISelectionProvider#removeSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
	 */
	public void removeSelectionChangedListener(
			ISelectionChangedListener listener) {
		selectionChangedListeners.remove(listener);
	}

	/**
	 * transfer the selection changed event to detail part.
	 */
	public void selectionChanged(SelectionChangedEvent event) {

		for (Iterator listeners = selectionChangedListeners.iterator(); listeners
				.hasNext();) {
			ISelectionChangedListener listener = (ISelectionChangedListener) listeners
					.next();
			listener.selectionChanged(new SelectionChangedEvent(this,
					event != null ? event.getSelection()
							: StructuredSelection.EMPTY));
		}
	}

	/**
	 * the convenient method to get the AdapterFactory instance of the editor;
	 * 
	 * @return the AdapterFactory instance.
	 */
	public AdapterFactory getAdapterFactory() {
		return (AdapterFactory) getPage().getEditor().getAdapter(
				AdapterFactory.class);
	}

	/**
	 * the convenient method to get the EditingDomain instance of the editor;
	 * 
	 * @return the EditingDomain instance.
	 */
	public EditingDomain getEditingDomain() {
		return (EditingDomain) getPage().getEditor().getAdapter(
				EditingDomain.class);
	}
    
    public void clearAll()
    {
        // do nothing, sub-classes may choose to override to clear their contents
    }
}

Back to the top