PageRenderTime 37ms CodeModel.GetById 29ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/XML/xml/gui/PropertyUtil.java

#
Java | 83 lines | 27 code | 9 blank | 47 comment | 4 complexity | 51c189affd16054df48adc79c2480222 MD5 | raw file
 1/*
 2 * PropertyUtil.java - Utility methods for accessing properties
 3 *
 4 * Copyright 2002 Greg Merrill
 5 * 			 2004 Robert McKinnon
 6 *
 7 * This program is free software; you can redistribute it and/or
 8 * modify it under the terms of the GNU General Public License
 9 * as published by the Free Software Foundation; either version 2
10 * of the License, or any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20*/
21
22package xml.gui;
23
24import org.gjt.sp.jedit.jEdit;
25
26import java.util.ArrayList;
27import java.util.List;
28
29/**
30 * Utility methods for accessing properties.
31 * copied from XSLTPlugin: xslt/PropertyUtil.java
32 * @author Greg Merrill
33 * @author Robert McKinnon
34 */
35public class PropertyUtil {
36
37	/**
38	 * Returns a List of property values whose keys are identical excepct for
39	 * a trailing "." and index.
40	 *
41	 * @param key key of the enumerated property, excluding the trailing "." and
42	 *            index.  I.e., if you have properties "file.0=file0.txt" and
43	 *            "file.1=file1.txt", calling getEnumeratedProperty("file", properties)
44	 *            would return a list containing "file0.txt" and "file1.txt".
45	 */
46	public static List<String> getEnumeratedProperty(String key) {
47		List<String> values = new ArrayList<String>();
48		int i = 0;
49		String value;
50		while ((value = jEdit.getProperty(calculateKey(key, i++))) != null) {
51			values.add(value);
52		}
53		return values;
54	}
55
56	/**
57	 * Sets a series of property values whose keys are identical excepct for
58	 * a trailing "." and index.
59	 *
60	 * @param key    key of the enumerated property (see
61	 *               {@link #getEnumeratedProperty})
62	 * @param values values to be assigned to the enumerated property, in order.
63	 *               All members of this List must be Strings.
64	 */
65	public static void setEnumeratedProperty(String key, List<String> values) {
66		List currentValues = getEnumeratedProperty(key);
67		for (int i = 0; i < currentValues.size(); i++) {
68			jEdit.setProperty(calculateKey(key, i), null);
69		}
70		for (int i = 0; i < values.size(); i++) {
71			jEdit.setProperty(calculateKey(key, i), values.get(i));
72		}
73	}
74
75	/**
76	 * @return indexed property key
77	 */
78	private static String calculateKey(String key, int index) {
79		return key + "." + index;
80	}
81
82}
83