001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.samples;
011:
012: import java.lang.reflect.InvocationTargetException;
013: import java.util.HashSet;
014:
015: import org.eclipse.core.resources.IFile;
016: import org.eclipse.core.runtime.CoreException;
017: import org.eclipse.core.runtime.IConfigurationElement;
018: import org.eclipse.core.runtime.IExecutableExtension;
019: import org.eclipse.core.runtime.OperationCanceledException;
020: import org.eclipse.core.runtime.Platform;
021: import org.eclipse.jface.dialogs.IDialogConstants;
022: import org.eclipse.jface.dialogs.MessageDialog;
023: import org.eclipse.jface.viewers.IStructuredSelection;
024: import org.eclipse.jface.wizard.Wizard;
025: import org.eclipse.osgi.util.NLS;
026: import org.eclipse.pde.internal.ui.PDEPlugin;
027: import org.eclipse.pde.internal.ui.PDEPluginImages;
028: import org.eclipse.pde.internal.ui.PDEUIMessages;
029: import org.eclipse.swt.widgets.Shell;
030: import org.eclipse.ui.INewWizard;
031: import org.eclipse.ui.IWorkbench;
032: import org.eclipse.ui.IWorkbenchPage;
033: import org.eclipse.ui.PlatformUI;
034: import org.eclipse.ui.activities.IWorkbenchActivitySupport;
035: import org.eclipse.ui.dialogs.IOverwriteQuery;
036: import org.eclipse.ui.ide.IDE;
037:
038: public class SampleWizard extends Wizard implements INewWizard,
039: IExecutableExtension {
040: private IConfigurationElement[] samples;
041: private IConfigurationElement selection;
042: private ProjectNamesPage namesPage;
043: private ReviewPage lastPage;
044:
045: private boolean sampleEditorNeeded = true;
046: private boolean switchPerspective = true;
047: private boolean selectRevealEnabled = true;
048: private boolean activitiesEnabled = true;
049:
050: private class ImportOverwriteQuery implements IOverwriteQuery {
051: public String queryOverwrite(String file) {
052: String[] returnCodes = { YES, NO, ALL, CANCEL };
053: int returnVal = openDialog(file);
054: return returnVal < 0 ? CANCEL : returnCodes[returnVal];
055: }
056:
057: private int openDialog(final String file) {
058: final int[] result = { IDialogConstants.CANCEL_ID };
059: getShell().getDisplay().syncExec(new Runnable() {
060: public void run() {
061: String title = PDEUIMessages.SampleWizard_title;
062: String msg = NLS.bind(
063: PDEUIMessages.SampleWizard_overwrite, file);
064: String[] options = { IDialogConstants.YES_LABEL,
065: IDialogConstants.NO_LABEL,
066: IDialogConstants.YES_TO_ALL_LABEL,
067: IDialogConstants.CANCEL_LABEL };
068: MessageDialog dialog = new MessageDialog(
069: getShell(), title, null, msg,
070: MessageDialog.QUESTION, options, 0);
071: result[0] = dialog.open();
072: }
073: });
074: return result[0];
075: }
076: }
077:
078: /**
079: * The default constructor.
080: *
081: */
082: public SampleWizard() {
083: PDEPlugin.getDefault().getLabelProvider().connect(this );
084: setDefaultPageImageDescriptor(PDEPluginImages.DESC_NEWEXP_WIZ);
085: samples = Platform.getExtensionRegistry()
086: .getConfigurationElementsFor(
087: "org.eclipse.pde.ui.samples"); //$NON-NLS-1$
088: namesPage = new ProjectNamesPage(this );
089: lastPage = new ReviewPage(this );
090: setNeedsProgressMonitor(true);
091: setWindowTitle(PDEUIMessages.ShowSampleAction_title);
092: }
093:
094: public void dispose() {
095: PDEPlugin.getDefault().getLabelProvider().disconnect(this );
096: super .dispose();
097: }
098:
099: public IConfigurationElement[] getSamples() {
100: return samples;
101: }
102:
103: /**
104: *
105: */
106: public void addPages() {
107: if (selection == null) {
108: addPage(new SelectionPage(this ));
109: }
110: addPage(namesPage);
111: addPage(lastPage);
112: }
113:
114: /**
115: *
116: */
117: public boolean performFinish() {
118: try {
119: String perspId = selection.getAttribute("perspectiveId"); //$NON-NLS-1$
120: IWorkbenchPage page = PDEPlugin.getActivePage();
121: if (perspId != null && switchPerspective) {
122: PlatformUI.getWorkbench().showPerspective(perspId,
123: page.getWorkbenchWindow());
124: }
125: SampleOperation op = new SampleOperation(selection,
126: namesPage.getProjectNames(),
127: new ImportOverwriteQuery());
128: getContainer().run(true, true, op);
129: IFile sampleManifest = op.getSampleManifest();
130: if (selectRevealEnabled) {
131: selectReveal(getShell());
132: }
133: if (activitiesEnabled)
134: enableActivities();
135: if (sampleEditorNeeded && sampleManifest != null)
136: IDE.openEditor(page, sampleManifest, true);
137: } catch (InvocationTargetException e) {
138: PDEPlugin.logException(e);
139: return false;
140: } catch (InterruptedException e) {
141: //PDEPlugin.logException(e);
142: return false;
143: } catch (CoreException e) {
144: PDEPlugin.logException(e);
145: return false;
146: } catch (OperationCanceledException e) {
147: return false;
148: }
149: return true;
150: }
151:
152: public void selectReveal(Shell shell) {
153: /*
154: shell.getDisplay().asyncExec(new Runnable() {
155: public void run() {
156: doSelectReveal();
157: }
158: });
159: */
160: }
161:
162: /*private void doSelectReveal() {
163: if (selection == null || createdProjects==null)
164: return;
165: String viewId = selection.getAttribute("targetViewId"); //$NON-NLS-1$
166: if (viewId == null)
167: return;
168: IWorkbenchWindow window = PlatformUI.getWorkbench()
169: .getActiveWorkbenchWindow();
170: if (window == null)
171: return;
172: IWorkbenchPage page = window.getActivePage();
173: if (page == null)
174: return;
175: IViewPart view = page.findView(viewId);
176: if (view == null || !(view instanceof ISetSelectionTarget))
177: return;
178: ISetSelectionTarget target = (ISetSelectionTarget) view;
179: IConfigurationElement[] projects = selection.getChildren("project"); //$NON-NLS-1$
180:
181: ArrayList items = new ArrayList();
182: for (int i = 0; i < projects.length; i++) {
183: String path = projects[i].getAttribute("selectReveal"); //$NON-NLS-1$
184: if (path == null)
185: continue;
186: IResource resource = createdProjects[i].findMember(path);
187: if (resource.exists())
188: items.add(resource);
189: }
190: if (items.size() > 0)
191: target.selectReveal(new StructuredSelection(items));
192: }
193: */
194: public void enableActivities() {
195: IConfigurationElement[] elements = selection
196: .getChildren("activity"); //$NON-NLS-1$
197: HashSet activitiesToEnable = new HashSet();
198: IWorkbenchActivitySupport workbenchActivitySupport = PlatformUI
199: .getWorkbench().getActivitySupport();
200:
201: for (int i = 0; i < elements.length; i++) {
202: IConfigurationElement element = elements[i];
203: String id = element.getAttribute("id"); //$NON-NLS-1$
204: if (id == null)
205: continue;
206: activitiesToEnable.add(id);
207: }
208: HashSet set = new HashSet(workbenchActivitySupport
209: .getActivityManager().getEnabledActivityIds());
210: set.addAll(activitiesToEnable);
211: workbenchActivitySupport.setEnabledActivityIds(set);
212: }
213:
214: /**
215: *
216: */
217: public void setInitializationData(IConfigurationElement config,
218: String propertyName, Object data) throws CoreException {
219: String variable = data != null && data instanceof String ? data
220: .toString() : null;
221: if (variable != null) {
222: for (int i = 0; i < samples.length; i++) {
223: IConfigurationElement element = samples[i];
224: String id = element.getAttribute("id"); //$NON-NLS-1$
225: if (id != null && id.equals(variable)) {
226: setSelection(element);
227: break;
228: }
229: }
230: }
231: }
232:
233: public void init(IWorkbench workbench,
234: IStructuredSelection selection) {
235: }
236:
237: /**
238: * @return Returns the selection.
239: */
240: public IConfigurationElement getSelection() {
241: return selection;
242: }
243:
244: /**
245: * @param selection
246: * The selection to set.
247: */
248: public void setSelection(IConfigurationElement selection) {
249: this .selection = selection;
250: }
251:
252: /**
253: * @return Returns the sampleEditorNeeded.
254: */
255: public boolean isSampleEditorNeeded() {
256: return sampleEditorNeeded;
257: }
258:
259: /**
260: * @param sampleEditorNeeded
261: * The sampleEditorNeeded to set.
262: */
263: public void setSampleEditorNeeded(boolean sampleEditorNeeded) {
264: this .sampleEditorNeeded = sampleEditorNeeded;
265: }
266:
267: /**
268: * @return Returns the switchPerspective.
269: * @todo Generated comment
270: */
271: public boolean isSwitchPerspective() {
272: return switchPerspective;
273: }
274:
275: /**
276: * @param switchPerspective The switchPerspective to set.
277: * @todo Generated comment
278: */
279: public void setSwitchPerspective(boolean switchPerspective) {
280: this .switchPerspective = switchPerspective;
281: }
282:
283: /**
284: * @return Returns the selectRevealEnabled.
285: * @todo Generated comment
286: */
287: public boolean isSelectRevealEnabled() {
288: return selectRevealEnabled;
289: }
290:
291: /**
292: * @param selectRevealEnabled The selectRevealEnabled to set.
293: * @todo Generated comment
294: */
295: public void setSelectRevealEnabled(boolean selectRevealEnabled) {
296: this .selectRevealEnabled = selectRevealEnabled;
297: }
298:
299: /**
300: * @return Returns the activitiesEnabled.
301: * @todo Generated comment
302: */
303: public boolean getActivitiesEnabled() {
304: return activitiesEnabled;
305: }
306:
307: /**
308: * @param activitiesEnabled The activitiesEnabled to set.
309: * @todo Generated comment
310: */
311: public void setActivitiesEnabled(boolean activitiesEnabled) {
312: this.activitiesEnabled = activitiesEnabled;
313: }
314: }
|