PageRenderTime 8ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/OptionGroup.java

#
Java | 203 lines | 108 code | 25 blank | 70 comment | 9 complexity | 0b264700473f9f99f2060ed1e46f6a82 MD5 | raw file
  1/*
  2 * OptionGroup.java - Option pane group
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2000 mike dillon
  7 * Portions copyright (C) 2003 Slava Pestov
  8 *
  9 * This program is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU General Public License
 11 * as published by the Free Software Foundation; either version 2
 12 * of the License, or any later version.
 13 *
 14 * This program is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 17 * GNU General Public License for more details.
 18 *
 19 * You should have received a copy of the GNU General Public License
 20 * along with this program; if not, write to the Free Software
 21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 22 */
 23
 24package org.gjt.sp.jedit;
 25
 26import java.util.*;
 27
 28/**
 29 * A set of option panes shown in one branch in the options dialog.<p>
 30 *
 31 * Plugins should not create instances of this class directly. See
 32 * {@link EditPlugin} for information on how jEdit obtains and constructs
 33 * option pane instances.
 34 *
 35 * @author Mike Dillon
 36 * @version $Id: OptionGroup.java 14444 2009-01-24 06:19:57Z shlomy $
 37 */
 38public class OptionGroup
 39{
 40	
 41	// {{{ data members
 42	protected final String name;
 43	protected final String label;
 44	protected final Vector<Object> members;
 45	private boolean sort;
 46	// }}}
 47	
 48	//{{{ OptionGroup constructor
 49	/**
 50	 * Creates an option group.
 51	 * @param name The internal name of the option group, used to key a
 52	 * property <code>options.<i>name</i>.label</code> which is the
 53	 * label displayed in the options dialog.
 54	 * @see jEdit#getProperty(String)
 55	 */
 56	public OptionGroup(String name)
 57	{
 58		this.name = name;
 59		label = jEdit.getProperty("options." + name + ".label");
 60		members = new Vector<Object>();
 61	} //}}}
 62
 63	//{{{ OptionGroup constructor
 64	/**
 65	 * Creates an option group.
 66	 * @param label The label
 67	 * @param options A whitespace-separated list of option pane names
 68	 * @since jEdit 4.2pre2
 69	 */
 70	public OptionGroup(String name, String label, String options)
 71	{
 72		this.name = name;
 73		this.label = label;
 74		members = new Vector<Object>();
 75
 76		StringTokenizer st = new StringTokenizer(options);
 77		while(st.hasMoreTokens())
 78		{
 79			String pane = st.nextToken();
 80			addOptionPane(pane);
 81		}
 82	} //}}}
 83
 84	//{{{ getName() method
 85	public String getName()
 86	{
 87		return name;
 88	} //}}}
 89
 90	//{{{ getLabel() method
 91	/**
 92	 * Returns the option group's human-readable label.
 93	 * @since jEdit 4.2pre1
 94	 */
 95	public String getLabel()
 96	{
 97		return label;
 98	} //}}}
 99
100	//{{{ addOptionGroup() method
101	public void addOptionGroup(OptionGroup group)
102	{
103		insertionSort(group.getLabel(),group);
104	} //}}}
105
106	//{{{ addOptionPane() method
107	public void addOptionPane(OptionPane pane)
108	{
109		String label = jEdit.getProperty("options."
110			+ pane.getName() + ".label","NO LABEL PROPERTY: "
111			+ pane.getName());
112
113		insertionSort(label,pane);
114	} //}}}
115
116	//{{{ addOptionPane() method
117	public void addOptionPane(String pane)
118	{
119		String label = jEdit.getProperty("options."
120			+ pane + ".label","NO LABEL PROPERTY: "
121			+ pane);
122
123		insertionSort(label,pane);
124	} //}}}
125
126	//{{{ getMembers() method
127	public Enumeration<Object> getMembers()
128	{
129		return members.elements();
130	} //}}}
131
132	//{{{ getMember() method
133	public Object getMember(int index)
134	{
135		return (index >= 0 && index < members.size())
136			? members.elementAt(index) : null;
137	} //}}}
138
139	//{{{ getMemberIndex() method
140	public int getMemberIndex(Object member)
141	{
142		return members.indexOf(member);
143	} //}}}
144
145	//{{{ getMemberCount() method
146	public int getMemberCount()
147	{
148		return members.size();
149	} //}}}
150
151	//{{{ setSort() method
152	/**
153	 * Sets if the members of this group should be sorted.
154	 * @since jEdit 4.2pre3
155	 */
156	public void setSort(boolean sort)
157	{
158		this.sort = sort;
159	} //}}}
160
161	//{{{ Private members
162
163
164	//{{{ insertionSort() method
165	private void insertionSort(String newLabel, Object newObj)
166	{
167		if(sort)
168		{
169			for(int i = 0; i < members.size(); i++)
170			{
171				Object obj = members.elementAt(i);
172				String label;
173				if(obj instanceof OptionPane)
174				{
175					String name = ((OptionPane)obj).getName();
176					label = jEdit.getProperty("options."
177						+ name + ".label","NO LABEL PROPERTY: "
178						+ name);
179				}
180				else if(obj instanceof String)
181				{
182					label = jEdit.getProperty("options."
183						+ obj + ".label","NO LABEL PROPERTY: "
184						+ obj);
185				}
186				else if(obj instanceof OptionGroup)
187					label = ((OptionGroup)obj).getLabel();
188				else
189					throw new InternalError();
190
191				if(newLabel.compareToIgnoreCase(label) < 0)
192				{
193					members.insertElementAt(newObj,i);
194					return;
195				}
196			}
197		}
198
199		members.addElement(newObj);
200	} //}}}
201
202	//}}}
203}