001: /*******************************************************************************
002: * Copyright (c) 2003, 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.imports;
011:
012: import java.util.ArrayList;
013:
014: import org.eclipse.jface.dialogs.IDialogSettings;
015: import org.eclipse.jface.viewers.IStructuredContentProvider;
016: import org.eclipse.jface.viewers.TableViewer;
017: import org.eclipse.jface.wizard.WizardPage;
018: import org.eclipse.pde.core.IModelProviderEvent;
019: import org.eclipse.pde.core.IModelProviderListener;
020: import org.eclipse.pde.core.plugin.IFragment;
021: import org.eclipse.pde.core.plugin.IFragmentModel;
022: import org.eclipse.pde.core.plugin.IPlugin;
023: import org.eclipse.pde.core.plugin.IPluginImport;
024: import org.eclipse.pde.core.plugin.IPluginLibrary;
025: import org.eclipse.pde.core.plugin.IPluginModel;
026: import org.eclipse.pde.core.plugin.IPluginModelBase;
027: import org.eclipse.pde.internal.core.ClasspathUtilCore;
028: import org.eclipse.pde.internal.core.PDECore;
029: import org.eclipse.pde.internal.ui.PDEPlugin;
030: import org.eclipse.pde.internal.ui.PDEUIMessages;
031: import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
032: import org.eclipse.pde.internal.ui.wizards.ListUtil;
033: import org.eclipse.swt.SWT;
034: import org.eclipse.swt.layout.GridData;
035: import org.eclipse.swt.layout.GridLayout;
036: import org.eclipse.swt.widgets.Button;
037: import org.eclipse.swt.widgets.Composite;
038: import org.eclipse.swt.widgets.Label;
039: import org.eclipse.swt.widgets.Table;
040: import org.eclipse.swt.widgets.TableItem;
041:
042: public abstract class BaseImportWizardSecondPage extends WizardPage
043: implements IModelProviderListener {
044:
045: protected static final String SETTINGS_ADD_FRAGMENTS = "addFragments"; //$NON-NLS-1$
046: protected static final String SETTINGS_AUTOBUILD = "autobuild"; //$NON-NLS-1$
047:
048: protected PluginImportWizardFirstPage fPage1;
049: protected IPluginModelBase[] fModels = new IPluginModelBase[0];
050: private String fLocation;
051: protected Button fAddFragmentsButton;
052: private Button fAutoBuildButton;
053: protected TableViewer fImportListViewer;
054: private boolean fRefreshNeeded = true;
055:
056: class ContentProvider extends DefaultContentProvider implements
057: IStructuredContentProvider {
058: public Object[] getElements(Object element) {
059: return new Object[0];
060: }
061: }
062:
063: public BaseImportWizardSecondPage(String pageName,
064: PluginImportWizardFirstPage page) {
065: super (pageName);
066: fPage1 = page;
067: PDEPlugin.getDefault().getLabelProvider().connect(this );
068: PDECore.getDefault().getModelManager()
069: .getExternalModelManager().addModelProviderListener(
070: this );
071: }
072:
073: protected Composite createImportList(Composite parent) {
074: Composite container = new Composite(parent, SWT.NONE);
075: GridLayout layout = new GridLayout();
076: layout.marginWidth = 0;
077: layout.marginHeight = 0;
078: container.setLayout(layout);
079: container.setLayoutData(new GridData(GridData.FILL_BOTH));
080:
081: Label label = new Label(container, SWT.NONE);
082: label
083: .setText(PDEUIMessages.ImportWizard_DetailedPage_importList);
084:
085: Table table = new Table(container, SWT.BORDER | SWT.MULTI
086: | SWT.V_SCROLL);
087: GridData gd = new GridData(GridData.FILL_BOTH);
088: gd.widthHint = 225;
089: table.setLayoutData(gd);
090:
091: fImportListViewer = new TableViewer(table);
092: fImportListViewer.setLabelProvider(PDEPlugin.getDefault()
093: .getLabelProvider());
094: fImportListViewer.setContentProvider(new ContentProvider());
095: fImportListViewer.setInput(PDECore.getDefault()
096: .getModelManager().getExternalModelManager());
097: fImportListViewer.setComparator(ListUtil.PLUGIN_COMPARATOR);
098: return container;
099: }
100:
101: protected Composite createComputationsOption(Composite parent,
102: int span) {
103: Composite composite = new Composite(parent, SWT.NONE);
104: composite.setLayout(new GridLayout());
105: GridData gd = new GridData();
106: gd.horizontalSpan = span;
107: composite.setLayoutData(gd);
108:
109: fAddFragmentsButton = new Button(composite, SWT.CHECK);
110: fAddFragmentsButton
111: .setText(PDEUIMessages.ImportWizard_SecondPage_addFragments);
112: fAddFragmentsButton.setLayoutData(new GridData(
113: GridData.FILL_HORIZONTAL));
114: if (getDialogSettings().get(SETTINGS_ADD_FRAGMENTS) != null)
115: fAddFragmentsButton.setSelection(getDialogSettings()
116: .getBoolean(SETTINGS_ADD_FRAGMENTS));
117: else
118: fAddFragmentsButton.setSelection(true);
119:
120: if (!PDEPlugin.getWorkspace().isAutoBuilding()) {
121: fAutoBuildButton = new Button(composite, SWT.CHECK);
122: fAutoBuildButton
123: .setText(PDEUIMessages.BaseImportWizardSecondPage_autobuild);
124: fAutoBuildButton.setLayoutData(new GridData(
125: GridData.FILL_HORIZONTAL));
126: fAutoBuildButton.setSelection(getDialogSettings()
127: .getBoolean(SETTINGS_AUTOBUILD));
128: }
129: return composite;
130:
131: }
132:
133: public void dispose() {
134: PDEPlugin.getDefault().getLabelProvider().disconnect(this );
135: PDECore.getDefault().getModelManager()
136: .getExternalModelManager().removeModelProviderListener(
137: this );
138: }
139:
140: public void setVisible(boolean visible) {
141: super .setVisible(visible);
142: if (visible && isRefreshNeeded()) {
143: fModels = fPage1.getModels();
144: refreshPage();
145: }
146: }
147:
148: protected abstract void refreshPage();
149:
150: protected boolean isRefreshNeeded() {
151: if (fRefreshNeeded) {
152: fRefreshNeeded = false;
153: fLocation = fPage1.getDropLocation();
154: return true;
155: }
156: String currLocation = fPage1.getDropLocation();
157: if (fLocation == null || !fLocation.equals(currLocation)) {
158: fLocation = fPage1.getDropLocation();
159: return true;
160: }
161: return fPage1.isRefreshNeeded();
162: }
163:
164: private IPluginModelBase findModel(String id) {
165: for (int i = 0; i < fModels.length; i++) {
166: String modelId = fModels[i].getPluginBase().getId();
167: if (modelId != null && modelId.equals(id))
168: return fModels[i];
169: }
170: return null;
171: }
172:
173: private IFragmentModel[] findFragments(IPlugin plugin) {
174: ArrayList result = new ArrayList();
175: for (int i = 0; i < fModels.length; i++) {
176: if (fModels[i] instanceof IFragmentModel) {
177: IFragment fragment = ((IFragmentModel) fModels[i])
178: .getFragment();
179: if (plugin.getId().equalsIgnoreCase(
180: fragment.getPluginId())) {
181: result.add(fModels[i]);
182: }
183: }
184: }
185: return (IFragmentModel[]) result
186: .toArray(new IFragmentModel[result.size()]);
187: }
188:
189: protected void addPluginAndDependencies(IPluginModelBase model,
190: ArrayList selected, boolean addFragments) {
191:
192: boolean containsVariable = false;
193: if (!selected.contains(model)) {
194: selected.add(model);
195: boolean hasextensibleAPI = ClasspathUtilCore
196: .hasExtensibleAPI(model);
197: if (!addFragments && !hasextensibleAPI
198: && model instanceof IPluginModel) {
199: IPluginLibrary[] libraries = model.getPluginBase()
200: .getLibraries();
201: for (int i = 0; i < libraries.length; i++) {
202: if (ClasspathUtilCore
203: .containsVariables(libraries[i].getName())) {
204: containsVariable = true;
205: break;
206: }
207: }
208: }
209: addDependencies(model, selected, addFragments
210: || containsVariable || hasextensibleAPI);
211: }
212: }
213:
214: protected void addDependencies(IPluginModelBase model,
215: ArrayList selected, boolean addFragments) {
216:
217: IPluginImport[] required = model.getPluginBase().getImports();
218: if (required.length > 0) {
219: for (int i = 0; i < required.length; i++) {
220: IPluginModelBase found = findModel(required[i].getId());
221: if (found != null) {
222: addPluginAndDependencies(found, selected,
223: addFragments);
224: }
225: }
226: }
227:
228: if (addFragments) {
229: if (model instanceof IPluginModel) {
230: IFragmentModel[] fragments = findFragments(((IPluginModel) model)
231: .getPlugin());
232: for (int i = 0; i < fragments.length; i++) {
233: addPluginAndDependencies(fragments[i], selected,
234: addFragments);
235: }
236: } else {
237: IFragment fragment = ((IFragmentModel) model)
238: .getFragment();
239: IPluginModelBase found = findModel(fragment
240: .getPluginId());
241: if (found != null) {
242: addPluginAndDependencies(found, selected,
243: addFragments);
244: }
245: }
246: }
247: }
248:
249: public IPluginModelBase[] getModelsToImport() {
250: TableItem[] items = fImportListViewer.getTable().getItems();
251: ArrayList result = new ArrayList();
252: for (int i = 0; i < items.length; i++) {
253: result.add(items[i].getData());
254: }
255: return (IPluginModelBase[]) result
256: .toArray(new IPluginModelBase[result.size()]);
257: }
258:
259: public void storeSettings() {
260: IDialogSettings settings = getDialogSettings();
261: settings.put(SETTINGS_ADD_FRAGMENTS, fAddFragmentsButton
262: .getSelection());
263: if (fAutoBuildButton != null)
264: settings.put(SETTINGS_AUTOBUILD, fAutoBuildButton
265: .getSelection());
266: }
267:
268: /* (non-Javadoc)
269: * @see org.eclipse.pde.core.IModelProviderListener#modelsChanged(org.eclipse.pde.core.IModelProviderEvent)
270: */
271: public void modelsChanged(IModelProviderEvent event) {
272: fRefreshNeeded = true;
273: }
274:
275: public boolean forceAutoBuild() {
276: return fAutoBuildButton != null
277: && getDialogSettings().getBoolean(SETTINGS_AUTOBUILD);
278: }
279:
280: }
|