⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 buffer.java

📁 用java 编写的源码开放的文本编辑器。有很多有用的特性
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
			}			contentInserted(offset,len,integerArray);		}		finally		{			writeUnlock();		}	} //}}}	//{{{ insert() method	/**	 * Inserts a string into the buffer.	 * @param offset The offset	 * @param seg The segment	 * @since jEdit 4.0pre1	 */	public void insert(int offset, Segment seg)	{		if(seg.count == 0)			return;		if(isReadOnly())			throw new RuntimeException("buffer read-only");		try		{			writeLock();			if(offset < 0 || offset > contentMgr.getLength())				throw new ArrayIndexOutOfBoundsException(offset);			contentMgr.insert(offset,seg);			integerArray.clear();			for(int i = 0; i < seg.count; i++)			{				if(seg.array[seg.offset + i] == '\n')					integerArray.add(i);			}			if(!getFlag(UNDO_IN_PROGRESS))			{				undoMgr.contentInserted(offset,seg.count,					seg.toString(),!getFlag(DIRTY));			}			contentInserted(offset,seg.count,integerArray);		}		finally		{			writeUnlock();		}	} //}}}	//{{{ remove() method	/**	 * Removes the specified rang efrom the buffer.	 * @param offset The start offset	 * @param length The number of characters to remove	 */	public void remove(int offset, int length)	{		if(length == 0)			return;		if(isReadOnly())			throw new RuntimeException("buffer read-only");		try		{			writeLock();			if(offset < 0 || length < 0				|| offset + length > contentMgr.getLength())				throw new ArrayIndexOutOfBoundsException(offset + ":" + length);			int startLine = offsetMgr.getLineOfOffset(offset);			contentMgr.getText(offset,length,seg);			int numLines = 0;			for(int i = 0; i < seg.count; i++)			{				if(seg.array[seg.offset + i] == '\n')					numLines++;			}			if(!getFlag(UNDO_IN_PROGRESS))			{				undoMgr.contentRemoved(offset,length,					seg.toString(),!getFlag(DIRTY));			}			contentMgr.remove(offset,length);			offsetMgr.contentRemoved(startLine,offset,numLines,length);			if(numLines > 0)			{				for(int i = 0; i < inUseFVMs.length; i++)				{					if(inUseFVMs[i] != null)						inUseFVMs[i]._invalidate(startLine);				}			}			fireContentRemoved(startLine,offset,numLines,length);			setDirty(true);		}		finally		{			writeUnlock();		}	} //}}}	//}}}	//{{{ Undo	//{{{ undo() method	/**	 * Undoes the most recent edit.	 *	 * @since jEdit 4.0pre1	 */	public void undo(JEditTextArea textArea)	{		if(undoMgr == null)			return;		if(!isEditable())		{			textArea.getToolkit().beep();			return;		}		try		{			writeLock();			setFlag(UNDO_IN_PROGRESS,true);			if(!undoMgr.undo(textArea))				textArea.getToolkit().beep();			fireTransactionComplete();		}		finally		{			setFlag(UNDO_IN_PROGRESS,false);			writeUnlock();		}	} //}}}	//{{{ redo() method	/**	 * Redoes the most recently undone edit. Returns true if the redo was	 * successful.	 *	 * @since jEdit 2.7pre2	 */	public void redo(JEditTextArea textArea)	{		if(undoMgr == null)			return;		if(!isEditable())		{			Toolkit.getDefaultToolkit().beep();			return;		}		try		{			writeLock();			setFlag(UNDO_IN_PROGRESS,true);			if(!undoMgr.redo(textArea))				textArea.getToolkit().beep();			fireTransactionComplete();		}		finally		{			setFlag(UNDO_IN_PROGRESS,false);			writeUnlock();		}	} //}}}	//{{{ isTransactionInProgress() method	/**	 * Returns if an undo or compound edit is currently in progress. If this	 * method returns true, then eventually a	 * {@link org.gjt.sp.jedit.buffer.BufferChangeListener#transactionComplete(Buffer)}	 * buffer event will get fired.	 * @since jEdit 4.0pre6	 */	public boolean isTransactionInProgress()	{		return getFlag(UNDO_IN_PROGRESS) || insideCompoundEdit();	} //}}}	//{{{ beginCompoundEdit() method	/**	 * Starts a compound edit. All edits from now on until	 * {@link #endCompoundEdit()} are called will be merged	 * into one. This can be used to make a complex operation	 * undoable in one step. Nested calls to	 * {@link #beginCompoundEdit()} behave as expected,	 * requiring the same number of {@link #endCompoundEdit()}	 * calls to end the edit.	 * @see #endCompoundEdit()	 */	public void beginCompoundEdit()	{		// Why?		//if(getFlag(TEMPORARY))		//	return;		try		{			writeLock();			undoMgr.beginCompoundEdit();		}		finally		{			writeUnlock();		}	} //}}}	//{{{ endCompoundEdit() method	/**	 * Ends a compound edit. All edits performed since	 * {@link #beginCompoundEdit()} was called can now	 * be undone in one step by calling {@link #undo(JEditTextArea)}.	 * @see #beginCompoundEdit()	 */	public void endCompoundEdit()	{		// Why?		//if(getFlag(TEMPORARY))		//	return;		try		{			writeLock();			undoMgr.endCompoundEdit();			if(!insideCompoundEdit())				fireTransactionComplete();		}		finally		{			writeUnlock();		}	}//}}}	//{{{ insideCompoundEdit() method	/**	 * Returns if a compound edit is currently active.	 * @since jEdit 3.1pre1	 */	public boolean insideCompoundEdit()	{		return undoMgr.insideCompoundEdit();	} //}}}	//}}}	//{{{ Buffer events	//{{{ addBufferChangeListener() method	/**	 * Adds a buffer change listener.	 * @param listener The listener	 * @since jEdit 4.0pre1	 */	public void addBufferChangeListener(BufferChangeListener listener)	{		bufferListeners.addElement(listener);	} //}}}	//{{{ removeBufferChangeListener() method	/**	 * Removes a buffer change listener.	 * @param listener The listener	 * @since jEdit 4.0pre1	 */	public void removeBufferChangeListener(BufferChangeListener listener)	{		bufferListeners.removeElement(listener);	} //}}}	//{{{ getBufferChangeListeners() method	/**	 * Returns an array of registered buffer change listeners.	 * @param listener The listener	 * @since jEdit 4.1pre3	 */	public BufferChangeListener[] getBufferChangeListeners()	{		return (BufferChangeListener[])bufferListeners			.toArray(new BufferChangeListener[			bufferListeners.size()]);	} //}}}	//}}}	//{{{ Property methods	//{{{ propertiesChanged() method	/**	 * Reloads settings from the properties. This should be called	 * after the <code>syntax</code> or <code>folding</code>	 * buffer-local properties are changed.	 */	public void propertiesChanged()	{		// Need to reset properties that were cached defaults,		// since the defaults might have changed.		Iterator iter = properties.values().iterator();		while(iter.hasNext())		{			PropValue value = (PropValue)iter.next();			if(value.defaultValue)				iter.remove();		}		textMode = "text".equals(mode.getName());		setTokenMarker(mode.getTokenMarker());		String folding = getStringProperty("folding");		FoldHandler handler = FoldHandler.getFoldHandler(folding);		if(handler != null)		{			setFoldHandler(handler);		}		else		{			if (folding != null)				Log.log(Log.WARNING, this, path + ": invalid 'folding' property: " + folding); 			setFoldHandler(new DummyFoldHandler());		}		if(!isTemporary() && firstTimeDone)			EditBus.send(new BufferUpdate(this,null,BufferUpdate.PROPERTIES_CHANGED));		firstTimeDone = true;	} //}}}	//{{{ getTabSize() method	/**	 * Returns the tab size used in this buffer. This is equivalent	 * to calling <code>getProperty("tabSize")</code>.	 * This method is thread-safe.	 */	public int getTabSize()	{		return getIntegerProperty("tabSize",8);	} //}}}	//{{{ getIndentSize() method	/**	 * Returns the indent size used in this buffer. This is equivalent	 * to calling <code>getProperty("indentSize")</code>.	 * This method is thread-safe.	 * @since jEdit 2.7pre1	 */	public final int getIndentSize()	{		return getIntegerProperty("indentSize",8);	} //}}}	//{{{ getProperty() method	/**	 * Returns the value of a buffer-local property.<p>	 *	 * Using this method is generally discouraged, because it returns an	 * <code>Object</code> which must be cast to another type	 * in order to be useful, and this can cause problems if the object	 * is of a different type than what the caller expects.<p>	 *	 * The following methods should be used instead:	 * <ul>	 * <li>{@link #getStringProperty(String)}</li>	 * <li>{@link #getBooleanProperty(String)}</li>	 * <li>{@link #getIntegerProperty(String,int)}</li>	 * <li>{@link #getRegexpProperty(String,int,gnu.regexp.RESyntax)}</li>	 * </ul>	 *	 * This method is thread-safe.	 *	 * @param name The property name. For backwards compatibility, this	 * is an <code>Object</code>, not a <code>String</code>.	 */	public synchronized Object getProperty(Object name)	{		// First try the buffer-local properties		PropValue o = (PropValue)properties.get(name);		if(o != null)			return o.value;		// For backwards compatibility		if(!(name instanceof String))			return null;		// Now try mode.<mode>.<property>		if(mode != null)		{			Object retVal = mode.getProperty((String)name);			if(retVal == null)				return null;			properties.put(name,new PropValue(retVal,true));			return retVal;		}		else		{			// Now try buffer.<property>			String value = jEdit.getProperty("buffer." + name);			if(value == null)				return null;			// Try returning it as an integer first			Object retVal;			try			{				retVal = new Integer(value);			}			catch(NumberFormatException nf)			{				retVal = value;			}			properties.put(name,new PropValue(retVal,true));			return retVal;		}	} //}}}	//{{{ setProperty() method	/**	 * Sets the value of a buffer-local property.	 * @param name The property name	 * @param value The property value	 * @since jEdit 4.0pre1	 */	public void setProperty(String name, Object value)	{		if(value == null)			properties.remove(name);		else		{			PropValue test = (PropValue)properties.get(name);			if(test == null)				properties.put(name,new PropValue(value,false));			else if(test.value.equals(value))			{				// do nothing			}			else			{				test.value = value;				test.defaultValue = false;			}		}	} //}}}	//{{{ unsetProperty() method	/**	 * Clears the value of a buffer-local property.	 * @param name The property name	 * @since jEdit 4.0pre1	 */	public void unsetProperty(String name)	{		properties.remove(name);	} //}}}	//{{{ getStringProperty() method	/**	 * Returns the value of a string property. This method is thread-safe.	 * @param name The property name	 * @since jEdit 4.0pre1	 */	public String getStringProperty(String name)	{		Object obj = getProperty(name);		if(obj != null)			return obj.toString();		else			return null;	} //}}}	//{{{ setStringProperty() method	/**	 * Sets a string property.	 * @param name The property name	 * @param value The value	 * @since jEdit 4.0pre1	 */	public void setStringProperty(String name, String value)	{		setProperty(name,value);	} //}}}	//{{{ getBooleanProperty() method	/**	 * Returns the value of a boolean property. This method is thread-safe.	 * @param name The property name	 * @since jEdit 4.0pre1	 */	public boolean getBooleanProperty(String name)	{		Object obj = getProperty(name);		if(obj instanceof Boolean)			return ((Boolean)obj).booleanValue();		else if("true".equals(obj) || "on".equals(obj) || "yes".equals(obj))			return true;		else			return false;	} //}}}	//{{{ setBooleanProperty() method	/**	 * Sets a boolean property.	 * @param name The property name	 * @param value The value	 * @since jEdit 4.0pre1	 */	public void setBooleanProperty(String name, boolean value)	{		setProperty(name,value ? Boolean.TRUE : Boolean.FALSE);	} //}}}	//{{{ getIntegerProperty() method	/**	 * Returns the value of an integer property. This method is thread-safe.	 * @param name The property name	 * @since jEdit 4.0pre1	 */	public int getIntegerProperty(String name, int defaultValue)	{		boolean defaultValueFlag;		Object obj;		PropValue value = (PropValue)properties.get(name);		if(value != null)		{			obj = value.value;			defaultValueFlag = value.defaultValue;		}		else		{			obj = getProperty(name);			// will be cached from now on...			defaultValueFlag = true;		}		if(obj == null)			return defaultValue;		else if(obj instanceof Number)			return ((Number)obj).intValue();		else		{			try			{				int returnValue = Integer.parseInt(					obj.toString().trim());				properties.put(name,new PropValue(					new Integer(returnValue),					defaultValueFlag));				return returnValue;			}			catch(Exception e)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -