Skip to main content
summaryrefslogtreecommitdiffstats
blob: 600f37bb302eaf4844da3c4e8fd3ac73fa8a517f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*******************************************************************************
 * Copyright (c) 2004, 2010 Helen Bershadskaya and others. 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Helen Bershadskaya - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.provisional.commons.ui.dialogs;

import java.util.HashMap;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.wizard.IWizard;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.mylyn.internal.provisional.commons.ui.CommonImages;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;

/**
 * Wizard dialog for displaying repository settings page. Necessary so we can add a validate button in the button bar.
 * 
 * @author Helen Bershadskaya
 */
public class ValidatableWizardDialog extends EnhancedWizardDialog {

	private static final String VALIDATE_BUTTON_KEY = "validate"; //$NON-NLS-1$

	private Button validateServerButton;

	private static final int VALIDATE_BUTTON_ID = 2000;

	/**
	 * @see WizardDialog#WizardDialog(Shell, IWizard)
	 */
	public ValidatableWizardDialog(Shell parentShell, IWizard newWizard) {
		super(parentShell, newWizard);
		setHelpAvailable(false);
	}

	@Override
	protected void createExtraButtons(Composite composite) {
		validateServerButton = createButton(composite, VALIDATE_BUTTON_ID,
				Messages.ValidatableWizardDialog_Validate_Button_Label, false);
		validateServerButton.setImage(CommonImages.getImage(CommonImages.VALIDATE));
		validateServerButton.setVisible(false);
		setButtonLayoutData(validateServerButton);
	}

	@Override
	public void updateExtraButtons() {
		IValidatable validatable = getValidatablePage();
		if (validatable != null && validatable.needsValidation()) {
			if (!validateServerButton.isVisible()) {
				validateServerButton.setVisible(true);
			}
			validateServerButton.setEnabled(validatable.canValidate());
		} else {
			if (validateServerButton != null && validateServerButton.isVisible()) {
				validateServerButton.setVisible(false);
			}
		}
	}

	private IValidatable getValidatablePage() {
		IValidatable validatable = null;
		IWizardPage currentPage = getCurrentPage();
		if (currentPage instanceof IValidatable) {
			validatable = (IValidatable) currentPage;
		} else if (currentPage instanceof IAdaptable) {
			validatable = (IValidatable) ((IAdaptable) currentPage).getAdapter(IValidatable.class);
		}
		return validatable;
	}

	@Override
	protected boolean handleExtraButtonPressed(int buttonId) {
		if (buttonId == VALIDATE_BUTTON_ID) {
			IValidatable validatable = getValidatablePage();
			if (validatable != null) {
				validatable.validate();
				return true;
			}
		}
		return false;
	}

	/**
	 * Modeled after WizardDialog.saveAndSetEnabledState(), but that one is private, so create our own
	 */
	@Override
	protected HashMap<String, Boolean> saveAndSetEnabledStateMylyn() {
		HashMap<String, Boolean> savedEnabledState = null;
		if (getShell() != null) {
			savedEnabledState = new HashMap<String, Boolean>();
			if (validateServerButton != null && validateServerButton.getShell() == getShell()) {
				savedEnabledState.put(VALIDATE_BUTTON_KEY, validateServerButton.getEnabled());
				validateServerButton.setEnabled(false);
			}
		}
		return savedEnabledState;
	}

	/**
	 * Modeled after WizardDialog.restoreEnabledState() and WizardDialog.restoreUIState() -- couldn't override those
	 * since they are private, so create our own. Currently only single button to work with, so don't create two
	 * separate methods
	 */
	@Override
	protected void restoreEnabledStateMylyn(HashMap<String, Boolean> savedEnabledState) {
		if (savedEnabledState != null) {
			Boolean savedValidateEnabledState = savedEnabledState.get(VALIDATE_BUTTON_KEY);
			if (validateServerButton != null && savedValidateEnabledState != null) {
				validateServerButton.setEnabled(savedValidateEnabledState);
			}
		}
	}

}

Back to the top