/plugins/Optional/tags/release-0-2-3/optional/PluginOptionGroup.java

# · Java · 265 lines · 185 code · 40 blank · 40 comment · 31 complexity · d24523ca22293b8e8fc6f86c5152ad80 MD5 · raw file

  1. /*
  2. * PluginOptionGroup.java - Plugin options model
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2003 Slava Pestov
  7. * Copyright (C) 2006 Alan Ezust
  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 optional;
  24. import javax.swing.event.EventListenerList;
  25. import javax.swing.event.TreeModelEvent;
  26. import javax.swing.event.TreeModelListener;
  27. import javax.swing.tree.TreeModel;
  28. import javax.swing.tree.TreePath;
  29. import org.gjt.sp.jedit.EditPlugin;
  30. import org.gjt.sp.jedit.OptionGroup;
  31. import org.gjt.sp.jedit.jEdit;
  32. import org.gjt.sp.jedit.options.PluginOptions.NoPluginsPane;
  33. /**
  34. * Refactored from PluginOptions.java - this class
  35. * contains only the OptionGroup
  36. * and none of the GUI code.
  37. * NOTE: This version does not show optionpanes from plugins that
  38. * use the deprecated (pre 4.2) plugin APIs.
  39. * @since jedit4.3pre3
  40. * @todo - add this to jEdit core and refactor the PluginOptions.java to use this some day?
  41. *
  42. */
  43. // {{{ PluginOptionGroup class
  44. public class PluginOptionGroup extends OptionGroup
  45. {
  46. // {{{ PluginOptionGroup()
  47. public PluginOptionGroup()
  48. {
  49. super("Plugin Options");
  50. createOptionTreeModel();
  51. } // }}}
  52. // {{{ createOptionTreeModel()
  53. public OptionTreeModel createOptionTreeModel() {
  54. OptionTreeModel paneTreeModel = new OptionTreeModel();
  55. OptionGroup rootGroup = (OptionGroup) paneTreeModel.getRoot();
  56. // initialize the Plugins branch of the options tree
  57. setSort(true);
  58. // Query plugins for option panes
  59. EditPlugin[] plugins = jEdit.getPlugins();
  60. for(int i = 0; i < plugins.length; i++)
  61. {
  62. EditPlugin ep = plugins[i];
  63. if(ep instanceof EditPlugin.Broken)
  64. continue;
  65. String className = ep.getClassName();
  66. String optionPane = jEdit.getProperty(
  67. "plugin." + className + ".option-pane");
  68. if(optionPane != null)
  69. addOptionPane(optionPane);
  70. else
  71. {
  72. String options = jEdit.getProperty(
  73. "plugin." + className
  74. + ".option-group");
  75. if(options != null)
  76. {
  77. addOptionGroup(
  78. new OptionGroup(
  79. "plugin." + className,
  80. jEdit.getProperty("plugin."
  81. + className + ".name"),
  82. options)
  83. );
  84. }
  85. }
  86. }
  87. // only add the Plugins branch if there are OptionPanes
  88. if (getMemberCount() == 0)
  89. addOptionPane(new NoPluginsPane());
  90. rootGroup.addOptionGroup(this);
  91. return paneTreeModel;
  92. } // }}}
  93. // {{{ class OptionTreeModel
  94. public class OptionTreeModel implements TreeModel
  95. {
  96. private OptionGroup root = new OptionGroup(null);
  97. private EventListenerList listenerList = new EventListenerList();
  98. public void addTreeModelListener(TreeModelListener l)
  99. {
  100. listenerList.add(TreeModelListener.class, l);
  101. }
  102. public void removeTreeModelListener(TreeModelListener l)
  103. {
  104. listenerList.remove(TreeModelListener.class, l);
  105. }
  106. public Object getChild(Object parent, int index)
  107. {
  108. if (parent instanceof OptionGroup)
  109. {
  110. return ((OptionGroup)parent).getMember(index);
  111. }
  112. else
  113. {
  114. return null;
  115. }
  116. }
  117. public int getChildCount(Object parent)
  118. {
  119. if (parent instanceof OptionGroup)
  120. {
  121. return ((OptionGroup)parent).getMemberCount();
  122. }
  123. else
  124. {
  125. return 0;
  126. }
  127. }
  128. public int getIndexOfChild(Object parent, Object child)
  129. {
  130. if (parent instanceof OptionGroup)
  131. {
  132. return ((OptionGroup)parent)
  133. .getMemberIndex(child);
  134. }
  135. else
  136. {
  137. return -1;
  138. }
  139. }
  140. public Object getRoot()
  141. {
  142. return root;
  143. }
  144. public boolean isLeaf(Object node)
  145. {
  146. return !(node instanceof OptionGroup);
  147. }
  148. public void valueForPathChanged(TreePath path, Object newValue)
  149. {
  150. // this model may not be changed by the TableCellEditor
  151. }
  152. protected void fireNodesChanged(Object source, Object[] path,
  153. int[] childIndices, Object[] children)
  154. {
  155. Object[] listeners = listenerList.getListenerList();
  156. TreeModelEvent modelEvent = null;
  157. for (int i = listeners.length - 2; i >= 0; i -= 2)
  158. {
  159. if (listeners[i] != TreeModelListener.class)
  160. continue;
  161. if (modelEvent == null)
  162. {
  163. modelEvent = new TreeModelEvent(source,
  164. path, childIndices, children);
  165. }
  166. ((TreeModelListener)listeners[i + 1])
  167. .treeNodesChanged(modelEvent);
  168. }
  169. }
  170. protected void fireNodesInserted(Object source, Object[] path,
  171. int[] childIndices, Object[] children)
  172. {
  173. Object[] listeners = listenerList.getListenerList();
  174. TreeModelEvent modelEvent = null;
  175. for (int i = listeners.length - 2; i >= 0; i -= 2)
  176. {
  177. if (listeners[i] != TreeModelListener.class)
  178. continue;
  179. if (modelEvent == null)
  180. {
  181. modelEvent = new TreeModelEvent(source,
  182. path, childIndices, children);
  183. }
  184. ((TreeModelListener)listeners[i + 1])
  185. .treeNodesInserted(modelEvent);
  186. }
  187. }
  188. protected void fireNodesRemoved(Object source, Object[] path,
  189. int[] childIndices, Object[] children)
  190. {
  191. Object[] listeners = listenerList.getListenerList();
  192. TreeModelEvent modelEvent = null;
  193. for (int i = listeners.length - 2; i >= 0; i -= 2)
  194. {
  195. if (listeners[i] != TreeModelListener.class)
  196. continue;
  197. if (modelEvent == null)
  198. {
  199. modelEvent = new TreeModelEvent(source,
  200. path, childIndices, children);
  201. }
  202. ((TreeModelListener)listeners[i + 1])
  203. .treeNodesRemoved(modelEvent);
  204. }
  205. }
  206. protected void fireTreeStructureChanged(Object source,
  207. Object[] path, int[] childIndices, Object[] children)
  208. {
  209. Object[] listeners = listenerList.getListenerList();
  210. TreeModelEvent modelEvent = null;
  211. for (int i = listeners.length - 2; i >= 0; i -= 2)
  212. {
  213. if (listeners[i] != TreeModelListener.class)
  214. continue;
  215. if (modelEvent == null)
  216. {
  217. modelEvent = new TreeModelEvent(source,
  218. path, childIndices, children);
  219. }
  220. ((TreeModelListener)listeners[i + 1])
  221. .treeStructureChanged(modelEvent);
  222. }
  223. }
  224. } //}}}
  225. } // }}}