PageRenderTime 158ms CodeModel.GetById 145ms app.highlight 10ms 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
  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
 23package org.gjt.sp.jedit.buffer;
 24
 25import java.util.*;
 26import javax.swing.text.Segment;
 27import org.gjt.sp.jedit.Buffer;
 28import org.gjt.sp.jedit.MiscUtilities;
 29import org.gjt.sp.jedit.ServiceManager;
 30import org.gjt.sp.util.Log;
 31
 32/**
 33 * Interface for obtaining the fold level of a specified line.<p>
 34 *
 35 * Plugins can provide fold handlers by defining entries in their
 36 * <code>services.xml</code> files like so:
 37 *
 38 * <pre>&lt;SERVICE CLASS="org.gjt.sp.jedit.buffer.FoldHandler" NAME="<i>name</i>"&gt;
 39 *    new <i>MyFoldHandler<i>();
 40 *&lt;/SERVICE&gt;</pre>
 41 *
 42 * See {@link org.gjt.sp.jedit.ServiceManager} for details.
 43 *
 44 * @author Slava Pestov
 45 * @version $Id: FoldHandler.java 4918 2003-11-18 20:51:58Z spestov $
 46 * @since jEdit 4.0pre1
 47 */
 48public abstract class FoldHandler
 49{
 50	/**
 51	 * The service type. See {@link org.gjt.sp.jedit.ServiceManager}.
 52	 * @since jEdit 4.2pre1
 53	 */
 54	public static final String SERVICE = "org.gjt.sp.jedit.buffer.FoldHandler";
 55
 56	//{{{ getName() method
 57	/**
 58	 * Returns the internal name of this FoldHandler
 59	 * @return The internal name of this FoldHandler
 60	 * @since jEdit 4.0pre6
 61	 */
 62	public String getName()
 63	{
 64		return name;
 65	}
 66	//}}}
 67
 68	//{{{ getFoldLevel() method
 69	/**
 70	 * Returns the fold level of the specified line.
 71	 * @param buffer The buffer in question
 72	 * @param lineIndex The line index
 73	 * @param seg A segment the fold handler can use to obtain any
 74	 * text from the buffer, if necessary
 75	 * @return The fold level of the specified line
 76	 * @since jEdit 4.0pre1
 77	 */
 78	public abstract int getFoldLevel(Buffer buffer, int lineIndex, Segment seg);
 79	//}}}
 80
 81	//{{{ equals() method
 82	/**
 83	 * Returns if the specified fold handler is equal to this one.
 84	 * @param o The object
 85	 */
 86	public boolean equals(Object o)
 87	{
 88		// Default implementation... subclasses can extend this.
 89		if(o == null)
 90			return false;
 91		else
 92			return getClass() == o.getClass();
 93	} //}}}
 94
 95	//{{{ hashCode() method
 96	public int hashCode()
 97	{
 98		return getClass().hashCode();
 99	} //}}}
100
101	//{{{ registerFoldHandler() method
102	/**
103	 * @deprecated Write a <code>services.xml</code> file instead;
104	 * see {@link org.gjt.sp.jedit.ServiceManager}.
105	 */
106	public static void registerFoldHandler(FoldHandler handler)
107	{
108		if (getFoldHandler(handler.getName()) != null)
109		{
110			Log.log(Log.ERROR, FoldHandler.class, "Cannot register more than one fold handler with the same name");
111			return;
112		}
113
114		foldHandlers.add(handler);
115	}
116	//}}}
117
118	//{{{ unregisterFoldHandler() method
119	/**
120	 * @deprecated Write a <code>services.xml</code> file instead;
121	 * see {@link org.gjt.sp.jedit.ServiceManager}.
122	 */
123	public static void unregisterFoldHandler(FoldHandler handler)
124	{
125		foldHandlers.remove(handler);
126	}
127	//}}}
128
129	//{{{ getFoldHandlers() method
130	/**
131	 * @deprecated Call
132	 * <code>ServiceManager.getServiceNames(
133	 * "org.gjt.sp.jedit.buffer.FoldHandler" );</code> instead. See
134	 * {@link org.gjt.sp.jedit.ServiceManager}.
135	 */
136	public static FoldHandler[] getFoldHandlers()
137	{
138		FoldHandler[] handlers = new FoldHandler[foldHandlers.size()];
139		return (FoldHandler[])foldHandlers.toArray(handlers);
140	}
141	//}}}
142
143	//{{{ getFoldHandler() method
144	/**
145	 * Returns the fold handler with the specified name, or null if
146	 * there is no registered handler with that name.
147	 * @param name The name of the desired fold handler
148	 * @since jEdit 4.0pre6
149	 */
150	public static FoldHandler getFoldHandler(String name)
151	{
152		FoldHandler handler = (FoldHandler)ServiceManager
153			.getService(SERVICE,name);
154		if(handler != null)
155			return handler;
156
157		Iterator i = foldHandlers.iterator();
158		while (i.hasNext())
159		{
160			handler = (FoldHandler)i.next();
161			if (name.equals(handler.getName())) return handler;
162		}
163
164		return null;
165	}
166	//}}}
167
168	//{{{ getFoldModes() method
169	/**
170	 * Returns an array containing the names of all registered fold
171	 * handlers.
172	 *
173	 * @since jEdit 4.0pre6
174	 */
175	public static String[] getFoldModes()
176	{
177		FoldHandler[] handlers = getFoldHandlers();
178		String[] newApi = ServiceManager.getServiceNames(SERVICE);
179		String[] foldModes = new String[handlers.length
180			+ newApi.length];
181		System.arraycopy(newApi,0,foldModes,0,newApi.length);
182
183		for (int i = 0; i < handlers.length; i++)
184		{
185			foldModes[i + newApi.length] = handlers[i].getName();
186		}
187
188		Arrays.sort(foldModes,new MiscUtilities.StringCompare());
189		return foldModes;
190	}
191	//}}}
192
193	//{{{ FoldHandler() constructor
194	protected FoldHandler(String name)
195	{
196		this.name = name;
197	}
198	//}}}
199
200	//{{{ toString() method
201	public String toString()
202	{
203		return name;
204	} //}}}
205
206	//{{{ Private members
207	private String name;
208
209	/**
210	 * @deprecated
211	 */
212	private static ArrayList foldHandlers;
213	//}}}
214
215	//{{{ Static initializer
216	static
217	{
218		foldHandlers = new ArrayList();
219	}
220	//}}}
221}