1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.team.internal.ccvs.ui.wizards;
15 
16 import org.eclipse.jface.dialogs.IDialogSettings;
17 import org.eclipse.jface.wizard.Wizard;
18 import org.eclipse.jface.wizard.WizardDialog;
19 import org.eclipse.swt.graphics.Point;
20 import org.eclipse.swt.graphics.Rectangle;
21 import org.eclipse.swt.widgets.Shell;
22 
23 /**
24  * Persists the size of the wizard dialog.
25  */
26 public class ResizableWizard extends Wizard {
27 
28 	private final int DEFAULT_WIDTH;
29 	private final int DEFAULT_HEIGHT;
30 
31 	private static final String BOUNDS_HEIGHT_KEY = "width"; //$NON-NLS-1$
32 	private static final String BOUNDS_WIDTH_KEY = "height"; //$NON-NLS-1$
33 
34 	final String fSectionName;
35 
ResizableWizard(String sectionName, IDialogSettings settings)36 	public ResizableWizard(String sectionName, IDialogSettings settings) {
37 		this(sectionName, settings, 300, 400);
38 	}
39 
ResizableWizard(String sectionName, IDialogSettings settings, int defaultWidth, int defaultHeight)40 	protected ResizableWizard(String sectionName, IDialogSettings settings, int defaultWidth, int defaultHeight) {
41 		DEFAULT_WIDTH= defaultWidth;
42 		DEFAULT_HEIGHT= defaultHeight;
43 		fSectionName= sectionName;
44 		setDialogSettings(settings);
45 	}
46 
open(Shell shell, ResizableWizard wizard)47 	protected static int open(Shell shell, ResizableWizard wizard) {
48 		final WizardDialog dialog= new WizardDialog(shell, wizard);
49 		dialog.setMinimumPageSize(wizard.loadSize());
50 		return dialog.open();
51 	}
52 
saveSize()53 	public void saveSize() {
54 		final Rectangle bounds= getContainer().getCurrentPage().getControl().getParent().getClientArea();
55 		final IDialogSettings settings= getDialogSettings();
56 		if (settings == null)
57 			return;
58 
59 		IDialogSettings section= settings.getSection(fSectionName);
60 		if (section == null)
61 			section= settings.addNewSection(fSectionName);
62 
63 		section.put(BOUNDS_WIDTH_KEY, bounds.width);
64 		section.put(BOUNDS_HEIGHT_KEY, bounds.height);
65 	}
66 
loadSize()67 	public Point loadSize() {
68 		final Point size= new Point(DEFAULT_WIDTH, DEFAULT_HEIGHT);
69 
70 		final IDialogSettings settings= getDialogSettings();
71 		if (settings == null)
72 			return size;
73 
74 		final IDialogSettings section= settings.getSection(fSectionName);
75 		if (section == null)
76 			return size;
77 
78 		try {
79 			size.x= section.getInt(BOUNDS_WIDTH_KEY);
80 			size.y= section.getInt(BOUNDS_HEIGHT_KEY);
81 		} catch (NumberFormatException e) {
82 		}
83 		return size;
84 	}
85 
86 
performFinish()87 	public boolean performFinish() {
88 		saveSize();
89 		return true;
90 	}
91 }
92