001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.wizards.toc;
011:
012: import java.util.ArrayList;
013:
014: import org.eclipse.core.resources.IProject;
015: import org.eclipse.jface.dialogs.Dialog;
016: import org.eclipse.jface.wizard.WizardPage;
017: import org.eclipse.pde.core.IModel;
018: import org.eclipse.pde.core.plugin.IPluginAttribute;
019: import org.eclipse.pde.core.plugin.IPluginElement;
020: import org.eclipse.pde.core.plugin.IPluginExtension;
021: import org.eclipse.pde.core.plugin.IPluginModelBase;
022: import org.eclipse.pde.core.plugin.IPluginObject;
023: import org.eclipse.pde.core.plugin.ISharedExtensionsModel;
024: import org.eclipse.pde.core.plugin.PluginRegistry;
025: import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase;
026: import org.eclipse.pde.internal.core.util.PDETextHelper;
027: import org.eclipse.pde.internal.ui.IHelpContextIds;
028: import org.eclipse.pde.internal.ui.PDEUIMessages;
029: import org.eclipse.swt.SWT;
030: import org.eclipse.swt.events.SelectionAdapter;
031: import org.eclipse.swt.events.SelectionEvent;
032: import org.eclipse.swt.layout.GridData;
033: import org.eclipse.swt.layout.GridLayout;
034: import org.eclipse.swt.widgets.Button;
035: import org.eclipse.swt.widgets.Composite;
036: import org.eclipse.ui.PlatformUI;
037:
038: /**
039: * RegisterTocWizardPage
040: *
041: */
042: public class RegisterTocWizardPage extends WizardPage implements
043: IRegisterTOCData {
044:
045: public static final int NUM_COLUMNS = 2;
046:
047: public final static String F_PAGE_NAME = "register-toc"; //$NON-NLS-1$
048:
049: public final static String F_TOC_ELEMENT_TOC = "toc"; //$NON-NLS-1$
050:
051: private Button fPrimaryChkBox;
052:
053: protected IModel fTocModel;
054:
055: private ISharedExtensionsModel fExtensionsModel;
056:
057: private IProject fPluginProject;
058:
059: private boolean fDataIsPrimary;
060:
061: /**
062: * @param pageName
063: */
064: public RegisterTocWizardPage(IModel model) {
065: super (F_PAGE_NAME);
066:
067: fTocModel = model;
068: initialize();
069: }
070:
071: /**
072: *
073: */
074: private void initialize() {
075:
076: setTitle(PDEUIMessages.TocWizardPage_title);
077: setDescription(PDEUIMessages.RegisterTocWizardPage_wizardPageDescription);
078:
079: fPrimaryChkBox = null;
080:
081: fDataIsPrimary = true;
082:
083: // Get the project the TOC is stored in
084: fPluginProject = fTocModel.getUnderlyingResource().getProject();
085:
086: initializePluginModel();
087: }
088:
089: /**
090: *
091: */
092: private void initializePluginModel() {
093: IPluginModelBase base = PluginRegistry
094: .findModel(getPluginProject());
095:
096: if (base == null)
097: return;
098: if (base instanceof IBundlePluginModelBase)
099: fExtensionsModel = ((IBundlePluginModelBase) base)
100: .getExtensionsModel();
101: else
102: fExtensionsModel = base;
103: }
104:
105: /**
106: *
107: */
108: private void updateUI() {
109:
110: if (fExtensionsModel != null) {
111: // Find all TOC extensions within the host plug-in
112: IPluginExtension[] extensions = RegisterTocOperation
113: .findTOCExtensions(fExtensionsModel);
114:
115: // Process all TOC elements
116: processTocElements(extensions);
117: }
118: }
119:
120: /**
121: * @param extensions
122: */
123: private void processTocElements(IPluginExtension[] extensions) {
124: // Query cheat sheet extensions for information required to update
125: // the description text and category combo widgets
126: // Linear search: Process all cheat sheet extensions found
127: for (int i = 0; i < extensions.length; i++) {
128: if (extensions[i].getChildCount() == 0) {
129: // Extension has no children, skip to the next extension
130: continue;
131: }
132: IPluginExtension extension = extensions[i];
133: IPluginObject[] pluginObjects = extension.getChildren();
134: // Process all children
135: for (int j = 0; j < pluginObjects.length; j++) {
136: if (pluginObjects[j] instanceof IPluginElement) {
137: IPluginElement element = (IPluginElement) pluginObjects[j];
138: if (element.getName().equals(F_TOC_ELEMENT_TOC)) {
139: // TOC element
140: processTocElement(element, getDataTocFile());
141: }
142:
143: }
144: }
145: }
146: }
147:
148: /**
149: * @param extensions
150: */
151: private void processTocElement(IPluginElement parentElement,
152: String generatedID) {
153: // Get the id attribute
154: IPluginAttribute fileAttribute = parentElement
155: .getAttribute(RegisterTocOperation.F_TOC_ATTRIBUTE_FILE);
156:
157: // Check for the generated ID for this TOC
158: // If a TOC exists with the generated ID already, read its
159: // description and populate the description text accordingly
160: if ((fileAttribute != null)
161: && PDETextHelper.isDefined(fileAttribute.getValue())
162: && generatedID.equals(fileAttribute.getValue())) {
163: // Matching TOC extension found
164: // Process children if any
165: if (parentElement.getChildCount() > 0) {
166: // Update the description text widget
167: updateUIPrimary(parentElement);
168: }
169: }
170: }
171:
172: private void updateUIPrimary(IPluginElement parentElement) {
173: IPluginObject pluginObject = parentElement.getChildren()[0];
174: if (pluginObject instanceof IPluginElement) {
175: IPluginElement element = (IPluginElement) pluginObject;
176: if (element.getName().equals(
177: RegisterTocOperation.F_TOC_ATTRIBUTE_PRIMARY)
178: && PDETextHelper.isDefinedAfterTrim(element
179: .getText())) {
180: // Triggers listener to update data description on load
181: fPrimaryChkBox.setSelection(Boolean.getBoolean(element
182: .getText().trim()));
183: }
184: }
185: }
186:
187: public boolean getDataPrimary() {
188: return fDataIsPrimary;
189: }
190:
191: public String getDataTocFile() {
192: return fTocModel.getUnderlyingResource()
193: .getProjectRelativePath().toPortableString();
194: }
195:
196: public IProject getPluginProject() {
197: return fPluginProject;
198: }
199:
200: /* (non-Javadoc)
201: * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
202: */
203: public void createControl(Composite parent) {
204: createUI(parent);
205: createUIListeners();
206:
207: updateUI();
208: validateUI();
209: }
210:
211: /**
212: * @param parent
213: */
214: private void createUI(Composite parent) {
215: // Create the container
216: Composite container = createUIContainer(parent);
217:
218: // Create the primary checkbox
219: createUIPrimaryChkBox(container);
220:
221: // Set the control for the reciever
222: // Must be done otherwise a null assertion error is generated
223: setControl(container);
224: // Apply the dialog font to all controls using the default font
225: Dialog.applyDialogFont(container);
226: // Provide functionality for the help button
227: PlatformUI.getWorkbench().getHelpSystem().setHelp(container,
228: IHelpContextIds.REGISTER_TOC);
229: }
230:
231: /**
232: * @param parent
233: * @return
234: */
235: private Composite createUIContainer(Composite parent) {
236: Composite container = new Composite(parent, SWT.NONE);
237: GridLayout layout = new GridLayout();
238: layout.numColumns = NUM_COLUMNS;
239: container.setLayout(layout);
240: return container;
241: }
242:
243: private void createUIPrimaryChkBox(Composite parent) {
244: fPrimaryChkBox = new Button(parent, SWT.CHECK);
245: fPrimaryChkBox
246: .setText(PDEUIMessages.RegisterTocWizardPage_labelPrimary);
247: GridData data = new GridData();
248: data.horizontalSpan = NUM_COLUMNS;
249: fPrimaryChkBox.setLayoutData(data);
250: fPrimaryChkBox.setSelection(true);
251: }
252:
253: /**
254: *
255: */
256: private void createUIListeners() {
257: // Create listeners for the primary check box
258: createUIListenersPrimaryChkBox();
259: }
260:
261: /**
262: *
263: */
264: private void createUIListenersPrimaryChkBox() {
265: fPrimaryChkBox.addSelectionListener(new SelectionAdapter() {
266: public void widgetSelected(SelectionEvent e) {
267: fDataIsPrimary = fPrimaryChkBox.getSelection();
268: }
269: });
270: }
271:
272: /**
273: *
274: */
275: private void validateUI() {
276: setPageComplete(true);
277: }
278:
279: /**
280: * @param model
281: * @param extensionPointID
282: * @return
283: */
284: public IPluginExtension[] findExtensions(IPluginModelBase model,
285: String extensionPointID) {
286: IPluginExtension[] extensions = model.getPluginBase()
287: .getExtensions();
288:
289: ArrayList tocExtensions = new ArrayList();
290: for (int i = 0; i < extensions.length; i++) {
291: String point = extensions[i].getPoint();
292: if (extensionPointID.equals(point)) {
293: tocExtensions.add(extensions[i]);
294: }
295: }
296: return (IPluginExtension[]) tocExtensions
297: .toArray(new IPluginExtension[tocExtensions.size()]);
298: }
299: }
|