001: /*
002: * OptionGroup.java - Option pane group
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2000 mike dillon
007: * Portions copyright (C) 2003 Slava Pestov
008: *
009: * This program is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; either version 2
012: * of the License, or any later version.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU General Public License for more details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with this program; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
022: */
023:
024: package org.gjt.sp.jedit;
025:
026: import java.util.*;
027:
028: /**
029: * A set of option panes shown in one branch in the options dialog.<p>
030: *
031: * Plugins should not create instances of this class directly. See
032: * {@link EditPlugin} for information on how jEdit obtains and constructs
033: * option pane instances.
034: *
035: * @author Mike Dillon
036: * @version $Id: OptionGroup.java 8133 2006-11-25 19:50:58Z ezust $
037: */
038: public class OptionGroup {
039:
040: // {{{ data members
041: protected final String name;
042: protected final String label;
043: protected final Vector members;
044: private boolean sort;
045:
046: // }}}
047:
048: //{{{ OptionGroup constructor
049: /**
050: * Creates an option group.
051: * @param name The internal name of the option group, used to key a
052: * property <code>options.<i>name</i>.label</code> which is the
053: * label displayed in the options dialog.
054: * @see jEdit#getProperty(String)
055: */
056: public OptionGroup(String name) {
057: this .name = name;
058: label = jEdit.getProperty("options." + name + ".label");
059: members = new Vector();
060: } //}}}
061:
062: //{{{ OptionGroup constructor
063: /**
064: * Creates an option group.
065: * @param label The label
066: * @param options A whitespace-separated list of option pane names
067: * @since jEdit 4.2pre2
068: */
069: public OptionGroup(String name, String label, String options) {
070: this .name = name;
071: this .label = label;
072: members = new Vector();
073:
074: StringTokenizer st = new StringTokenizer(options);
075: while (st.hasMoreTokens()) {
076: String pane = st.nextToken();
077: addOptionPane(pane);
078: }
079: } //}}}
080:
081: //{{{ getName() method
082: public String getName() {
083: return name;
084: } //}}}
085:
086: //{{{ getLabel() method
087: /**
088: * Returns the option group's human-readable label.
089: * @since jEdit 4.2pre1
090: */
091: public String getLabel() {
092: return label;
093: } //}}}
094:
095: //{{{ addOptionGroup() method
096: public void addOptionGroup(OptionGroup group) {
097: insertionSort(group.getLabel(), group);
098: } //}}}
099:
100: //{{{ addOptionPane() method
101: public void addOptionPane(OptionPane pane) {
102: String label = jEdit.getProperty("options." + pane.getName()
103: + ".label", "NO LABEL PROPERTY: " + pane.getName());
104:
105: insertionSort(label, pane);
106: } //}}}
107:
108: //{{{ addOptionPane() method
109: public void addOptionPane(String pane) {
110: String label = jEdit.getProperty("options." + pane + ".label",
111: "NO LABEL PROPERTY: " + pane);
112:
113: insertionSort(label, pane);
114: } //}}}
115:
116: //{{{ getMembers() method
117: public Enumeration getMembers() {
118: return members.elements();
119: } //}}}
120:
121: //{{{ getMember() method
122: public Object getMember(int index) {
123: return (index >= 0 && index < members.size()) ? members
124: .elementAt(index) : null;
125: } //}}}
126:
127: //{{{ getMemberIndex() method
128: public int getMemberIndex(Object member) {
129: return members.indexOf(member);
130: } //}}}
131:
132: //{{{ getMemberCount() method
133: public int getMemberCount() {
134: return members.size();
135: } //}}}
136:
137: //{{{ setSort() method
138: /**
139: * Sets if the members of this group should be sorted.
140: * @since jEdit 4.2pre3
141: */
142: public void setSort(boolean sort) {
143: this .sort = sort;
144: } //}}}
145:
146: //{{{ Private members
147:
148: //{{{ insertionSort() method
149: private void insertionSort(String newLabel, Object newObj) {
150: if (sort) {
151: for (int i = 0; i < members.size(); i++) {
152: Object obj = members.elementAt(i);
153: String label;
154: if (obj instanceof OptionPane) {
155: String name = ((OptionPane) obj).getName();
156: label = jEdit.getProperty("options." + name
157: + ".label", "NO LABEL PROPERTY: " + name);
158: } else if (obj instanceof String) {
159: label = jEdit.getProperty("options." + obj
160: + ".label", "NO LABEL PROPERTY: " + obj);
161: } else if (obj instanceof OptionGroup)
162: label = ((OptionGroup) obj).getLabel();
163: else
164: throw new InternalError();
165:
166: if (newLabel.compareToIgnoreCase(label) < 0) {
167: members.insertElementAt(newObj, i);
168: return;
169: }
170: }
171: }
172:
173: members.addElement(newObj);
174: } //}}}
175:
176: //}}}
177: }
|