PageRenderTime 35ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 1ms

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/buffer/FoldHandler.java

#
Java | 221 lines | 87 code | 22 blank | 112 comment | 10 complexity | d5dbf4e7147c5390153ae05d84a78687 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * FoldHandler.java - Fold handler interface
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2001, 2003 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package org.gjt.sp.jedit.buffer;
  23. import java.util.*;
  24. import javax.swing.text.Segment;
  25. import org.gjt.sp.jedit.Buffer;
  26. import org.gjt.sp.jedit.MiscUtilities;
  27. import org.gjt.sp.jedit.ServiceManager;
  28. import org.gjt.sp.util.Log;
  29. /**
  30. * Interface for obtaining the fold level of a specified line.<p>
  31. *
  32. * Plugins can provide fold handlers by defining entries in their
  33. * <code>services.xml</code> files like so:
  34. *
  35. * <pre>&lt;SERVICE CLASS="org.gjt.sp.jedit.buffer.FoldHandler" NAME="<i>name</i>"&gt;
  36. * new <i>MyFoldHandler<i>();
  37. *&lt;/SERVICE&gt;</pre>
  38. *
  39. * See {@link org.gjt.sp.jedit.ServiceManager} for details.
  40. *
  41. * @author Slava Pestov
  42. * @version $Id: FoldHandler.java 4918 2003-11-18 20:51:58Z spestov $
  43. * @since jEdit 4.0pre1
  44. */
  45. public abstract class FoldHandler
  46. {
  47. /**
  48. * The service type. See {@link org.gjt.sp.jedit.ServiceManager}.
  49. * @since jEdit 4.2pre1
  50. */
  51. public static final String SERVICE = "org.gjt.sp.jedit.buffer.FoldHandler";
  52. //{{{ getName() method
  53. /**
  54. * Returns the internal name of this FoldHandler
  55. * @return The internal name of this FoldHandler
  56. * @since jEdit 4.0pre6
  57. */
  58. public String getName()
  59. {
  60. return name;
  61. }
  62. //}}}
  63. //{{{ getFoldLevel() method
  64. /**
  65. * Returns the fold level of the specified line.
  66. * @param buffer The buffer in question
  67. * @param lineIndex The line index
  68. * @param seg A segment the fold handler can use to obtain any
  69. * text from the buffer, if necessary
  70. * @return The fold level of the specified line
  71. * @since jEdit 4.0pre1
  72. */
  73. public abstract int getFoldLevel(Buffer buffer, int lineIndex, Segment seg);
  74. //}}}
  75. //{{{ equals() method
  76. /**
  77. * Returns if the specified fold handler is equal to this one.
  78. * @param o The object
  79. */
  80. public boolean equals(Object o)
  81. {
  82. // Default implementation... subclasses can extend this.
  83. if(o == null)
  84. return false;
  85. else
  86. return getClass() == o.getClass();
  87. } //}}}
  88. //{{{ hashCode() method
  89. public int hashCode()
  90. {
  91. return getClass().hashCode();
  92. } //}}}
  93. //{{{ registerFoldHandler() method
  94. /**
  95. * @deprecated Write a <code>services.xml</code> file instead;
  96. * see {@link org.gjt.sp.jedit.ServiceManager}.
  97. */
  98. public static void registerFoldHandler(FoldHandler handler)
  99. {
  100. if (getFoldHandler(handler.getName()) != null)
  101. {
  102. Log.log(Log.ERROR, FoldHandler.class, "Cannot register more than one fold handler with the same name");
  103. return;
  104. }
  105. foldHandlers.add(handler);
  106. }
  107. //}}}
  108. //{{{ unregisterFoldHandler() method
  109. /**
  110. * @deprecated Write a <code>services.xml</code> file instead;
  111. * see {@link org.gjt.sp.jedit.ServiceManager}.
  112. */
  113. public static void unregisterFoldHandler(FoldHandler handler)
  114. {
  115. foldHandlers.remove(handler);
  116. }
  117. //}}}
  118. //{{{ getFoldHandlers() method
  119. /**
  120. * @deprecated Call
  121. * <code>ServiceManager.getServiceNames(
  122. * "org.gjt.sp.jedit.buffer.FoldHandler" );</code> instead. See
  123. * {@link org.gjt.sp.jedit.ServiceManager}.
  124. */
  125. public static FoldHandler[] getFoldHandlers()
  126. {
  127. FoldHandler[] handlers = new FoldHandler[foldHandlers.size()];
  128. return (FoldHandler[])foldHandlers.toArray(handlers);
  129. }
  130. //}}}
  131. //{{{ getFoldHandler() method
  132. /**
  133. * Returns the fold handler with the specified name, or null if
  134. * there is no registered handler with that name.
  135. * @param name The name of the desired fold handler
  136. * @since jEdit 4.0pre6
  137. */
  138. public static FoldHandler getFoldHandler(String name)
  139. {
  140. FoldHandler handler = (FoldHandler)ServiceManager
  141. .getService(SERVICE,name);
  142. if(handler != null)
  143. return handler;
  144. Iterator i = foldHandlers.iterator();
  145. while (i.hasNext())
  146. {
  147. handler = (FoldHandler)i.next();
  148. if (name.equals(handler.getName())) return handler;
  149. }
  150. return null;
  151. }
  152. //}}}
  153. //{{{ getFoldModes() method
  154. /**
  155. * Returns an array containing the names of all registered fold
  156. * handlers.
  157. *
  158. * @since jEdit 4.0pre6
  159. */
  160. public static String[] getFoldModes()
  161. {
  162. FoldHandler[] handlers = getFoldHandlers();
  163. String[] newApi = ServiceManager.getServiceNames(SERVICE);
  164. String[] foldModes = new String[handlers.length
  165. + newApi.length];
  166. System.arraycopy(newApi,0,foldModes,0,newApi.length);
  167. for (int i = 0; i < handlers.length; i++)
  168. {
  169. foldModes[i + newApi.length] = handlers[i].getName();
  170. }
  171. Arrays.sort(foldModes,new MiscUtilities.StringCompare());
  172. return foldModes;
  173. }
  174. //}}}
  175. //{{{ FoldHandler() constructor
  176. protected FoldHandler(String name)
  177. {
  178. this.name = name;
  179. }
  180. //}}}
  181. //{{{ toString() method
  182. public String toString()
  183. {
  184. return name;
  185. } //}}}
  186. //{{{ Private members
  187. private String name;
  188. /**
  189. * @deprecated
  190. */
  191. private static ArrayList foldHandlers;
  192. //}}}
  193. //{{{ Static initializer
  194. static
  195. {
  196. foldHandlers = new ArrayList();
  197. }
  198. //}}}
  199. }