All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xworker.swt.custom.StyledTextInputStreamRunnable Maven / Gradle / Ivy

/*******************************************************************************
* Copyright 2007-2013 See AUTHORS file.
 * 
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* 
*   http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package xworker.swt.custom;

import java.io.InputStream;

import org.eclipse.swt.custom.StyledText;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 读取InputStream并把数据显示到StyledText的线程。
 * 
 * @author Administrator
 *
 */
public class StyledTextInputStreamRunnable implements Runnable{
	private static Logger logger = LoggerFactory.getLogger(StyledTextInputStreamRunnable.class);
	
	StyledText text;
	InputStream inputStream;
	boolean autoScroolToBottom;
	String charset;
	
	public StyledTextInputStreamRunnable(StyledText text, InputStream inputStream, boolean autoScroolToBottom, String charset){
		this.text = text;
		this.inputStream = inputStream;
		this.autoScroolToBottom = autoScroolToBottom;
		this.charset = charset;
	}
	
	public void setAutoScroolToBottom(boolean autoScroolToBottom){
		this.autoScroolToBottom = autoScroolToBottom;
	}
	
	public void run(){
		try{
			byte[] bytes = new byte[1024];
			int length = -1;
			while((length = inputStream.read(bytes)) != -1){
				final String str = new String(bytes, 0, length, charset);
				if(text == null || text.isDisposed()){
					break;
				}
				
				text.getDisplay().asyncExec(new Runnable(){
					public void run(){
						text.append(str);
						
						if(autoScroolToBottom){
							 text.setTopIndex(text.getLineCount() - 1);
						}
					}
				});			
			}
		}catch(Exception e){
			logger.info("get data form inputStream error", e);
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy