PageRenderTime 45ms CodeModel.GetById 38ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/buffer/FoldHandler.java

#
Java | 182 lines | 72 code | 19 blank | 91 comment | 8 complexity | 50aec4f7bb9c815b970a91da6a778c6e 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 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.util.Log;
 29
 30/**
 31 * Interface for obtaining the fold level of a specified label.
 32 * @author Slava Pestov
 33 * @version $Id: FoldHandler.java 4277 2002-06-22 08:33:00Z spestov $
 34 * @since jEdit 4.0pre1
 35 */
 36public abstract class FoldHandler
 37{
 38	//{{{ getName() method
 39	/**
 40	 * Returns the internal name of this FoldHandler
 41	 * @return The internal name of this FoldHandler
 42	 * @since jEdit 4.0pre6
 43	 */
 44	public String getName()
 45	{
 46		return name;
 47	}
 48	//}}}
 49
 50	//{{{ getFoldLevel() method
 51	/**
 52	 * Returns the fold level of the specified line.
 53	 * @param buffer The buffer in question
 54	 * @param lineIndex The line index
 55	 * @param seg A segment the fold handler can use to obtain any
 56	 * text from the buffer, if necessary
 57	 * @return The fold level of the specified line
 58	 * @since jEdit 4.0pre1
 59	 */
 60	public abstract int getFoldLevel(Buffer buffer, int lineIndex, Segment seg);
 61	//}}}
 62
 63	//{{{ equals() method
 64	/**
 65	 * Returns if the specified fold handler is equal to this one.
 66	 * @param o The object
 67	 */
 68	public boolean equals(Object o)
 69	{
 70		// Default implementation... subclasses can extend this.
 71		if(o == null)
 72			return false;
 73		else
 74			return getClass() == o.getClass();
 75	} //}}}
 76
 77	//{{{ registerFoldHandler() method
 78	/**
 79	 * Adds a fold handler to the list of registered handlers
 80	 * @param handler The fold handler to add
 81	 * @since jEdit 4.0pre6
 82	 */
 83	public static void registerFoldHandler(FoldHandler handler)
 84	{
 85		if (getFoldHandler(handler.getName()) != null)
 86		{
 87			Log.log(Log.ERROR, FoldHandler.class, "Cannot register more than one fold handler with the same name");
 88			return;
 89		}
 90
 91		foldHandlers.add(handler);
 92	}
 93	//}}}
 94
 95	//{{{ unregisterFoldHandler() method
 96	/**
 97	 * Removes a fold handler from the list of registered handlers
 98	 * @param handler The fold handler to add
 99	 * @since jEdit 4.1pre2
100	 */
101	public static void unregisterFoldHandler(FoldHandler handler)
102	{
103		foldHandlers.remove(handler);
104	}
105	//}}}
106
107	//{{{ getFoldHandlers() method
108	/**
109	 * Returns an array containing the list of registered fold handlers
110	 * @since jEdit 4.0pre6
111	 */
112	public static FoldHandler[] getFoldHandlers()
113	{
114		FoldHandler[] handlers = new FoldHandler[foldHandlers.size()];
115		return (FoldHandler[])foldHandlers.toArray(handlers);
116	}
117	//}}}
118
119	//{{{ getFoldHandler() method
120	/**
121	 * Returns the fold handler with the specified name, or null if
122	 * there is no registered handler with that name.
123	 * @param name The name of the desired fold handler
124	 * @since jEdit 4.0pre6
125	 */
126	public static FoldHandler getFoldHandler(String name)
127	{
128		FoldHandler handler;
129
130		Iterator i = foldHandlers.iterator();
131		while (i.hasNext())
132		{
133			handler = (FoldHandler)i.next();
134			if (name.equals(handler.getName())) return handler;
135		}
136
137		return null;
138	}
139	//}}}
140
141	//{{{ getFoldModes() method
142	/**
143	 * Returns an array containing the names of all registered fold handlers
144	 * @since jEdit 4.0pre6
145	 */
146	public static String[] getFoldModes()
147	{
148		FoldHandler[] handlers = getFoldHandlers();
149		String[] foldModes = new String[handlers.length];
150
151		for (int i = 0; i < foldModes.length; i++)
152		{
153			foldModes[i] = handlers[i].getName();
154		}
155
156		return foldModes;
157	}
158	//}}}
159
160	//{{{ FoldHandler() constructor
161	protected FoldHandler(String name)
162	{
163		this.name = name;
164	}
165	//}}}
166
167	//{{{ Private members
168	private String name;
169
170	private static ArrayList foldHandlers;
171	//}}}
172
173	//{{{ Static initializer
174	static
175	{
176		foldHandlers = new ArrayList();
177		registerFoldHandler(new DummyFoldHandler());
178		registerFoldHandler(new IndentFoldHandler());
179		registerFoldHandler(new ExplicitFoldHandler());
180	}
181	//}}}
182}