FileDocCategorySizeDatePackage
UITextAreaImpl.javaAPI DocAzureus 3.0.3.44936Wed Jul 11 21:06:46 BST 2007org.gudy.azureus2.pluginsimpl.local.ui.components

UITextAreaImpl

public class UITextAreaImpl extends UIComponentImpl implements UITextArea
author
parg

Fields Summary
private int
max_size
PrintWriter
pw
File
file
boolean
useFile
org.gudy.azureus2.core3.util.AEMonitor
file_mon
LinkedList
delay_text
int
delay_size
org.gudy.azureus2.core3.util.FrequencyLimitedDispatcher
dispatcher
Constructors Summary
public UITextAreaImpl()

	
	
	
	
		setText("");
	
Methods Summary
public voidaddPropertyChangeListener(UIPropertyChangeListener l)

		if (useFile) {
			if (pw != null) {
				try {
					file_mon.enter();

					pw.close();
					pw = null;
				} finally {
					file_mon.exit();
				}
			}

			useFile = false;
			setText(getFileText());
		}

		super.addPropertyChangeListener(l);
	
public voidappendText(java.lang.String text)

		if (useFile && pw != null) {
			try {
				file_mon.enter();
				pw.print(text);
				pw.flush();
				return;
			} finally {
				file_mon.exit();
			}
		}

		synchronized( this ){
			
			delay_text.addLast( text );
			
			delay_size += text.length();
			
			while( delay_size > max_size ){
		
				if ( delay_text.size() == 0 ){
					
					break;
				}
				
				String	s = (String)delay_text.removeFirst();
				
				delay_size -= s.length();
			}
		}
		
		dispatcher.dispatch();
	
protected voiddelayAppend()

		String	str = getText();

		String	text;
		
		synchronized( this ){

			if ( delay_text.size() == 1 ){
				
				text = (String)delay_text.get(0);
				
			}else{
				
				StringBuffer sb = new StringBuffer( delay_size );
				
				Iterator	it = delay_text.iterator();
				
				while( it.hasNext()){
				
					sb.append((String)it.next());
				}
				
				text = sb.toString();
			}
			
			delay_text.clear();
			delay_size = 0;
		}
		
		if ( str == null ){
			
			setText( text );
			
		}else{
			
			setText( str + text );
		}
	
private java.lang.StringgetFileText()

		boolean recreate = pw != null;

		try {
			file_mon.enter();

			if (recreate) {
				pw.close();
			}

			String text = null;
			try {
				text = FileUtil.readFileEndAsString(file, max_size);
			} catch (IOException e) {
				e.printStackTrace();
			}

			if (text == null) {
				text = "";
			}

			if (recreate) {
				try {
					FileWriter fr = new FileWriter(file, true);
					pw = new PrintWriter(fr);
				} catch (IOException e) {
					useFile = false;
					e.printStackTrace();
				}
			}
			return text;
		} finally {
			file_mon.exit();
		}
	
public java.lang.StringgetText()

		if (useFile && pw != null) {
			return getFileText();
		}

		return((String)getProperty( PT_VALUE ));
	
public voidsetMaximumSize(int _max_size)

		max_size	= _max_size;
	
public voidsetText(java.lang.String text)

		if (useFile) {
			try {
				file_mon.enter();
				if (pw == null) {
					try {
						file = AETemporaryFileHandler.createTempFile();

						FileWriter fr = new FileWriter(file);
						pw = new PrintWriter(fr);

						pw.print(text);
						pw.flush();

						return;
					} catch (IOException e) {
					}
				}
			} finally {
				file_mon.exit();
			}
		}
		
		// has property change listener, or error while doing file (fallthrough)
		
		if ( text.length() > max_size ){
				
			int	size_to_show = max_size - 10000;
			
			if ( size_to_show < 0 ){
				
				size_to_show	= max_size;
			}
			
			text = text.substring( text.length() - size_to_show );
		}
		
		setProperty( PT_VALUE, text );