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