PageRenderTime 15ms CodeModel.GetById 2ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/jedit/OptionGroup.java

#
Java | 199 lines | 109 code | 22 blank | 68 comment | 9 complexity | fb1b184eddb934837f54571e62c485fe 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.*;
 27import org.gjt.sp.util.Log;
 28
 29/**
 30 * A set of option panes shown in one branch in the options dialog.<p>
 31 *
 32 * Plugins should not create instances of this class anymore. See
 33 * {@link EditPlugin} for information on how jEdit obtains and constructs
 34 * option pane instances.
 35 *
 36 * @author Mike Dillon
 37 * @version $Id: OptionGroup.java 4815 2003-06-27 20:02:06Z spestov $
 38 */
 39public class OptionGroup
 40{
 41	//{{{ OptionGroup constructor
 42	/**
 43	 * Creates an option group.
 44	 * @param name The internal name of the option group, used to key a
 45	 * property <code>options.<i>name</i>.label</code> which is the
 46	 * label displayed in the options dialog.
 47	 * @see jEdit#getProperty(String)
 48	 */
 49	public OptionGroup(String name)
 50	{
 51		this.name = name;
 52		label = jEdit.getProperty("options." + name + ".label");
 53		members = new Vector();
 54	} //}}}
 55
 56	//{{{ OptionGroup constructor
 57	/**
 58	 * Creates an option group.
 59	 * @param label The label
 60	 * @param options A whitespace-separated list of option pane names
 61	 * @since jEdit 4.2pre2
 62	 */
 63	public OptionGroup(String name, String label, String options)
 64	{
 65		this.name = name;
 66		this.label = label;
 67		members = new Vector();
 68
 69		StringTokenizer st = new StringTokenizer(options);
 70		while(st.hasMoreTokens())
 71		{
 72			String pane = st.nextToken();
 73			addOptionPane(pane);
 74		}
 75	} //}}}
 76
 77	//{{{ getName() method
 78	public String getName()
 79	{
 80		return name;
 81	} //}}}
 82
 83	//{{{ getLabel() method
 84	/**
 85	 * Returns the option group's human-readable label.
 86	 * @since jEdit 4.2pre1
 87	 */
 88	public String getLabel()
 89	{
 90		return label;
 91	} //}}}
 92
 93	//{{{ addOptionGroup() method
 94	public void addOptionGroup(OptionGroup group)
 95	{
 96		insertionSort(group.getLabel(),group);
 97	} //}}}
 98
 99	//{{{ addOptionPane() method
100	public void addOptionPane(OptionPane pane)
101	{
102		String label = jEdit.getProperty("options."
103			+ pane.getName() + ".label","NO LABEL PROPERTY: "
104			+ pane.getName());
105
106		insertionSort(label,pane);
107	} //}}}
108
109	//{{{ addOptionPane() method
110	public void addOptionPane(String pane)
111	{
112		String label = jEdit.getProperty("options."
113			+ pane + ".label","NO LABEL PROPERTY: "
114			+ pane);
115
116		insertionSort(label,pane);
117	} //}}}
118
119	//{{{ getMembers() method
120	public Enumeration getMembers()
121	{
122		return members.elements();
123	} //}}}
124
125	//{{{ getMember() method
126	public Object getMember(int index)
127	{
128		return (index >= 0 && index < members.size())
129			? members.elementAt(index) : null;
130	} //}}}
131
132	//{{{ getMemberIndex() method
133	public int getMemberIndex(Object member)
134	{
135		return members.indexOf(member);
136	} //}}}
137
138	//{{{ getMemberCount() method
139	public int getMemberCount()
140	{
141		return members.size();
142	} //}}}
143
144	//{{{ setSort() method
145	/**
146	 * Sets if the members of this group should be sorted.
147	 * @since jEdit 4.2pre3
148	 */
149	public void setSort(boolean sort)
150	{
151		this.sort = sort;
152	} //}}}
153
154	//{{{ Private members
155	private String name;
156	private String label;
157	private Vector members;
158	private boolean sort;
159
160	//{{{ insertionSort() method
161	private void insertionSort(String newLabel, Object newObj)
162	{
163		if(sort)
164		{
165			for(int i = 0; i < members.size(); i++)
166			{
167				Object obj = members.elementAt(i);
168				String label;
169				if(obj instanceof OptionPane)
170				{
171					String name = ((OptionPane)obj).getName();
172					label = jEdit.getProperty("options."
173						+ name + ".label","NO LABEL PROPERTY: "
174						+ name);
175				}
176				else if(obj instanceof String)
177				{
178					label = jEdit.getProperty("options."
179						+ obj + ".label","NO LABEL PROPERTY: "
180						+ obj);
181				}
182				else if(obj instanceof OptionGroup)
183					label = ((OptionGroup)obj).getLabel();
184				else
185					throw new InternalError();
186
187				if(newLabel.compareTo(label) < 0)
188				{
189					members.insertElementAt(newObj,i);
190					return;
191				}
192			}
193		}
194
195		members.addElement(newObj);
196	} //}}}
197
198	//}}}
199}