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.jdt.internal.ui.preferences;
011:
012: import java.lang.reflect.InvocationTargetException;
013:
014: import org.eclipse.core.runtime.Assert;
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.core.runtime.IAdaptable;
017: import org.eclipse.core.runtime.IProgressMonitor;
018:
019: import org.eclipse.swt.SWT;
020: import org.eclipse.swt.layout.GridData;
021: import org.eclipse.swt.layout.GridLayout;
022: import org.eclipse.swt.widgets.Composite;
023: import org.eclipse.swt.widgets.Control;
024: import org.eclipse.swt.widgets.Label;
025: import org.eclipse.swt.widgets.Shell;
026:
027: import org.eclipse.jface.dialogs.IMessageProvider;
028: import org.eclipse.jface.dialogs.ProgressMonitorDialog;
029: import org.eclipse.jface.operation.IRunnableContext;
030: import org.eclipse.jface.operation.IRunnableWithProgress;
031: import org.eclipse.jface.wizard.IWizard;
032: import org.eclipse.jface.wizard.IWizardContainer;
033: import org.eclipse.jface.wizard.IWizardPage;
034:
035: import org.eclipse.ui.PlatformUI;
036: import org.eclipse.ui.dialogs.PropertyPage;
037:
038: import org.eclipse.jdt.core.IClasspathEntry;
039: import org.eclipse.jdt.core.IJavaProject;
040: import org.eclipse.jdt.core.JavaModelException;
041:
042: import org.eclipse.jdt.internal.ui.actions.ActionMessages;
043: import org.eclipse.jdt.internal.ui.packageview.ClassPathContainer;
044: import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
045: import org.eclipse.jdt.internal.ui.wizards.buildpaths.ClasspathContainerWizard;
046:
047: /**
048: * Wraps a PropertyPage around a ClasspathContainerWizard.
049: * It is required, that the wizard consists of exactly one page.
050: */
051: public class ClasspathContainerPreferencePage extends PropertyPage {
052:
053: private static final class PropertyPageWizardContainer implements
054: IWizardContainer {
055:
056: private final IWizard fWizard;
057: private final PropertyPage fPage;
058: private String fMessage;
059:
060: private PropertyPageWizardContainer(PropertyPage page,
061: IWizard wizard) {
062: Assert.isLegal(wizard.getPageCount() == 1);
063:
064: fPage = page;
065: fWizard = wizard;
066: }
067:
068: public IWizardPage getCurrentPage() {
069: return fWizard.getPages()[0];
070: }
071:
072: public Shell getShell() {
073: return fPage.getShell();
074: }
075:
076: public void showPage(IWizardPage page) {
077: }
078:
079: public void updateButtons() {
080: fPage.setValid(fWizard.canFinish());
081: }
082:
083: public void updateMessage() {
084: IWizardPage page = getCurrentPage();
085:
086: String message = fPage.getMessage();
087: if (message != null && fMessage == null)
088: fMessage = message;
089:
090: if (page.getErrorMessage() != null) {
091: fPage.setMessage(page.getErrorMessage(), ERROR);
092: } else if (page instanceof IMessageProvider) {
093: IMessageProvider messageProvider = (IMessageProvider) page;
094: if (messageProvider.getMessageType() != IMessageProvider.NONE) {
095: fPage.setMessage(messageProvider.getMessage(),
096: messageProvider.getMessageType());
097: } else {
098: if (messageProvider.getMessage() != null
099: && fMessage == null)
100: fMessage = messageProvider.getMessage();
101:
102: fPage.setMessage(fMessage, NONE);
103: }
104: } else {
105: fPage.setErrorMessage(null);
106: }
107: }
108:
109: public void updateTitleBar() {
110: IWizardPage page = getCurrentPage();
111: String name = page.getTitle();
112: if (name == null)
113: name = page.getName();
114:
115: fPage.setMessage(name);
116: }
117:
118: public void updateWindowTitle() {
119: }
120:
121: public void run(boolean fork, boolean cancelable,
122: IRunnableWithProgress runnable)
123: throws InvocationTargetException, InterruptedException {
124: ProgressMonitorDialog dialog = new ProgressMonitorDialog(
125: getShell());
126: dialog.run(fork, cancelable, runnable);
127: }
128: }
129:
130: private ClasspathContainerWizard fWizard;
131: private IJavaProject fJavaProject;
132: private IClasspathEntry fEntry;
133: private Composite fWizardPageContainer;
134:
135: public ClasspathContainerPreferencePage() {
136: noDefaultAndApplyButton();
137: }
138:
139: /**
140: * {@inheritDoc}
141: */
142: public void setElement(IAdaptable element) {
143: super .setElement(element);
144:
145: ClassPathContainer container = (ClassPathContainer) element;
146: fJavaProject = container.getJavaProject();
147: fEntry = container.getClasspathEntry();
148: }
149:
150: protected Control createContents(final Composite parent) {
151: fWizardPageContainer = new Composite(parent, SWT.NONE);
152: fWizardPageContainer.setLayoutData(new GridData(SWT.FILL,
153: SWT.FILL, true, true));
154: GridLayout layout = new GridLayout(1, false);
155: layout.marginHeight = 0;
156: layout.marginWidth = 0;
157: fWizardPageContainer.setLayout(layout);
158:
159: createWizardPageContent(fWizardPageContainer);
160:
161: return fWizardPageContainer;
162: }
163:
164: private void createWizardPageContent(Composite parent) {
165: fWizard = createWizard();
166: if (fWizard == null)
167: return;
168:
169: Composite messageComposite = new Composite(parent, SWT.NONE);
170: messageComposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP,
171: true, false));
172: GridLayout layout = new GridLayout(1, false);
173: layout.marginHeight = 0;
174: messageComposite.setLayout(layout);
175:
176: Label messageLabel = new Label(messageComposite, SWT.WRAP);
177: messageLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
178: true, true));
179:
180: IWizardPage page = fWizard.getPages()[0];
181: fWizard.createPageControls(parent);
182:
183: if (page.getControl() == null)
184: page.createControl(parent);
185:
186: Control pageControl = page.getControl();
187: if (pageControl.getLayoutData() == null)
188: pageControl.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
189: true, true));
190:
191: setPageName(page);
192: setDescription(page, messageLabel);
193:
194: page.setVisible(true);
195:
196: setValid(fWizard.canFinish());
197: }
198:
199: private void setPageName(IWizardPage page) {
200: String name = page.getTitle();
201: if (name == null)
202: name = page.getName();
203:
204: setMessage(name);
205: }
206:
207: private void setDescription(IWizardPage page, Label messageLabel) {
208: String description = null;
209: if (page.getDescription() != null) {
210: description = page.getDescription();
211: } else if (page instanceof IMessageProvider) {
212: IMessageProvider messageProvider = (IMessageProvider) page;
213: if (messageProvider.getMessageType() == IMessageProvider.NONE) {
214: description = messageProvider.getMessage();
215: }
216: }
217:
218: if (description != null) {
219: messageLabel.setText(description);
220: } else {
221: messageLabel.setVisible(false);
222: }
223: }
224:
225: private ClasspathContainerWizard createWizard() {
226: try {
227: IJavaProject project = fJavaProject;
228: IClasspathEntry[] entries = project.getRawClasspath();
229: ClasspathContainerWizard result = new ClasspathContainerWizard(
230: fEntry, project, entries);
231:
232: result.addPages();
233: PropertyPageWizardContainer wizardContainer = new PropertyPageWizardContainer(
234: this , result);
235: wizardContainer.updateButtons();
236: wizardContainer.updateMessage();
237: result.setContainer(wizardContainer);
238:
239: return result;
240: } catch (JavaModelException e) {
241: String title = ActionMessages.ConfigureContainerAction_error_title;
242: String message = ActionMessages.ConfigureContainerAction_error_creationfailed_message;
243: ExceptionHandler.handle(e, getShell(), title, message);
244: }
245:
246: return null;
247: }
248:
249: /**
250: * {@inheritDoc}
251: */
252: public boolean performOk() {
253: fWizard.performFinish();
254: applyContainerChange();
255: fWizard.dispose();
256:
257: return super .performOk();
258: }
259:
260: /**
261: * {@inheritDoc}
262: */
263: public boolean performCancel() {
264: fWizard.performCancel();
265: fWizard.dispose();
266:
267: return super .performCancel();
268: }
269:
270: /**
271: * {@inheritDoc}
272: */
273: protected void performApply() {
274: fWizard.performFinish();
275: applyContainerChange();
276: fWizard.dispose();
277:
278: rebuildWizardPage();
279:
280: super .performApply();
281: }
282:
283: /**
284: * {@inheritDoc}
285: */
286: protected void performDefaults() {
287: fWizard.performCancel();
288: fWizard.dispose();
289:
290: rebuildWizardPage();
291:
292: super .performDefaults();
293: }
294:
295: /**
296: * Rebuilds the wizard page based on the current classpath entry
297: */
298: private void rebuildWizardPage() {
299: Control[] children = fWizardPageContainer.getChildren();
300: for (int i = 0; i < children.length; i++) {
301: children[i].dispose();
302: }
303:
304: createWizardPageContent(fWizardPageContainer);
305: fWizardPageContainer.getParent().layout(true, true);
306: }
307:
308: /**
309: * Apply the changes to the classpath
310: */
311: private void applyContainerChange() {
312: IClasspathEntry[] created = fWizard.getNewEntries();
313: if (created == null || created.length != 1)
314: return;
315:
316: IClasspathEntry result = created[0];
317: if (result == null || result.equals(fEntry))
318: return;
319:
320: try {
321: IClasspathEntry[] entries = fJavaProject.getRawClasspath();
322:
323: int idx = indexInClasspath(entries, fEntry);
324: if (idx == -1)
325: return;
326:
327: final IClasspathEntry[] newEntries = new IClasspathEntry[entries.length];
328: System.arraycopy(entries, 0, newEntries, 0, entries.length);
329: newEntries[idx] = result;
330:
331: IRunnableContext context = new ProgressMonitorDialog(
332: getShell());
333: context = PlatformUI.getWorkbench().getProgressService();
334: context.run(true, true, new IRunnableWithProgress() {
335: public void run(IProgressMonitor monitor)
336: throws InvocationTargetException,
337: InterruptedException {
338: try {
339: fJavaProject.setRawClasspath(newEntries,
340: fJavaProject.getOutputLocation(),
341: monitor);
342: } catch (CoreException e) {
343: throw new InvocationTargetException(e);
344: }
345: }
346: });
347:
348: fEntry = result;
349:
350: } catch (JavaModelException e) {
351: String title = ActionMessages.ConfigureContainerAction_error_title;
352: String message = ActionMessages.ConfigureContainerAction_error_creationfailed_message;
353: ExceptionHandler.handle(e, getShell(), title, message);
354: } catch (InvocationTargetException e) {
355: String title = ActionMessages.ConfigureContainerAction_error_title;
356: String message = ActionMessages.ConfigureContainerAction_error_applyingfailed_message;
357: ExceptionHandler.handle(e, getShell(), title, message);
358: } catch (InterruptedException e) {
359: // user cancelled
360: }
361: }
362:
363: protected static int indexInClasspath(IClasspathEntry[] entries,
364: IClasspathEntry entry) {
365: for (int i = 0; i < entries.length; i++) {
366: if (entries[i].equals(entry)) {
367: return i;
368: }
369: }
370: return -1;
371: }
372:
373: }
|