Skip to main content
summaryrefslogtreecommitdiffstats
blob: ed75b37f3f657fc4fc033ef5d470768d19fe7cd6 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*******************************************************************************
 * Copyright (c) 2009 Alena Laskavaia 
 * 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:
 *    Alena Laskavaia  - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.codan.core.param;

import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;

/**
 * Default implementation of problem preference. It keeps preference metadata
 * together with preference itself.
 * 
 */
public abstract class AbstractProblemPreference implements IProblemPreference {
	public static final String PARAM = "param"; //$NON-NLS-1$
	protected String key;
	protected String label;
	protected String toolTip = null;
	protected PreferenceType type;
	protected String uiInfo;
	private IProblemPreference parent;

	public PreferenceType getType() {
		return type;
	}

	public String getLabel() {
		return label;
	}

	public String getToolTip() {
		return toolTip;
	}

	public String getKey() {
		return key;
	}

	public String getUiInfo() {
		return uiInfo;
	}

	public void setKey(String key) {
		if (isValidIdentifier(key))
			this.key = key;
		else
			throw new IllegalArgumentException(
					"Key must have java identifier syntax or number, i.e no dots and other funky stuff"); //$NON-NLS-1$
	}

	protected boolean isValidIdentifier(String id) {
		if (id == null)
			return false;
		int n = id.length();
		if (n == 0)
			return false;
		for (int i = 0; i < n; i++)
			if (!Character.isJavaIdentifierPart(id.charAt(i)))
				return false;
		return true;
	}

	public void setLabel(String label) {
		if (label == null)
			throw new NullPointerException("Label cannot be null"); //$NON-NLS-1$
		this.label = label;
	}

	public void setToolTip(String tooltip) {
		this.toolTip = tooltip;
	}

	public void setType(PreferenceType type) {
		if (type == null)
			throw new NullPointerException("Type cannot be null"); //$NON-NLS-1$
		this.type = type;
	}

	public void setUiInfo(String uiinfo) {
		this.uiInfo = uiinfo;
	}

	public Object getValue() {
		throw new UnsupportedOperationException();
	}

	public void setValue(Object value) {
		throw new UnsupportedOperationException();
	}

	@Override
	public Object clone() {
		try {
			return super.clone();
		} catch (CloneNotSupportedException e) {
			return null;
		}
	}

	/**
	 * @param str
	 * @return
	 */
	protected StreamTokenizer getImportTokenizer(String str) {
		ByteArrayInputStream st = new ByteArrayInputStream(str.getBytes());
		StreamTokenizer tokenizer = new StreamTokenizer(new InputStreamReader(
				st));
		tokenizer.resetSyntax();
		tokenizer.quoteChar('"');
		tokenizer.wordChars('_', '_');
		tokenizer.wordChars('-', '-');
		tokenizer.wordChars('.', '.');
		tokenizer.wordChars('0', '9');
		tokenizer.wordChars('a', 'z');
		tokenizer.wordChars('A', 'Z');
		tokenizer.wordChars(128 + 32, 255);
		tokenizer.whitespaceChars(0, ' ');
		tokenizer.commentChar('/');
		return tokenizer;
	}

	public IProblemPreference getParent() {
		return parent;
	}

	/**
	 * @param parent
	 *            the parent to set
	 */
	public void setParent(IProblemPreference parent) {
		this.parent = parent;
	}

	public String getQualifiedKey() {
		if (parent == null)
			return getKey();
		return parent.getQualifiedKey() + "." + getKey(); //$NON-NLS-1$
	}
}

Back to the top