summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/preferences/ProfileNameInputValidator.java
blob: 38e38b1e4746962b55530e0dc9d6c09cde1a1422 (plain)
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
/*******************************************************************************
 * Copyright (c) 2012 BMW Car IT GmbH.
 * 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:
 * BMW Car IT - initial API and implementation
 *******************************************************************************/
package org.yocto.sdk.ide.preferences;

import org.eclipse.jface.dialogs.IInputValidator;
import org.yocto.sdk.ide.YoctoProfileElement;
import org.yocto.sdk.ide.YoctoSDKMessages;

public class ProfileNameInputValidator implements IInputValidator {
	private static final String WARNING_CONTAINS_COMMA = "Preferences.Profile.Validator.InvalidName.Comma";
	private static final String WARNING_CONTAINS_DOUBLEQUOTE = "Preferences.Profile.Validator.InvalidName.Quote";
	private static final String PROFILE_NAME_IS_EMPTY = "Preferences.Profile.Validator.InvalidName.Empty";
	private static final String WARNING_ALREADY_EXISTS = "Preferences.Profile.Validator.InvalidName.Exists";

	private final String selectedItem;
	private final YoctoProfileElement profileSetting;

	public ProfileNameInputValidator(YoctoProfileElement profileSetting) {
		this(profileSetting, "");
	}

	public ProfileNameInputValidator(YoctoProfileElement profileSetting, String selectedItem) {
		this.selectedItem = selectedItem;
		this.profileSetting = profileSetting;
	}

	@Override
	public String isValid(String newText) {
		if (newText.contains(",")) {
			return YoctoSDKMessages.getString(WARNING_CONTAINS_COMMA);
		}

		if (newText.contains("\"")) {
			return YoctoSDKMessages.getString(WARNING_CONTAINS_DOUBLEQUOTE);
		}

		if (newText.isEmpty()) {
			return YoctoSDKMessages.getString(PROFILE_NAME_IS_EMPTY);
		}

		if (selectedItemEquals(newText)) {
			return null;
		}

		if (profileSetting.contains(newText)) {
			return YoctoSDKMessages.getString(WARNING_ALREADY_EXISTS);
		}

		return null;
	}

	private boolean selectedItemEquals(String newText) {
		return !selectedItem.isEmpty() && newText.equals(selectedItem);
	}
}