htiextension/com.nokia.s60tools.hticonnection/src/com/nokia/s60tools/hticonnection/ui/views/main/MainTextViewer.java
changeset 0 61163b28edca
equal deleted inserted replaced
-1:000000000000 0:61163b28edca
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 package com.nokia.s60tools.hticonnection.ui.views.main;
       
    19 
       
    20 import org.eclipse.jface.text.BadLocationException;
       
    21 import org.eclipse.jface.text.DocumentEvent;
       
    22 import org.eclipse.jface.text.IDocument;
       
    23 import org.eclipse.jface.text.IDocumentListener;
       
    24 import org.eclipse.jface.text.TextViewer;
       
    25 import org.eclipse.swt.SWT;
       
    26 import org.eclipse.swt.graphics.Font;
       
    27 import org.eclipse.swt.graphics.FontData;
       
    28 import org.eclipse.swt.graphics.Point;
       
    29 import org.eclipse.swt.widgets.Composite;
       
    30 
       
    31 /**
       
    32  * Viewer for showing text output.
       
    33  */
       
    34 public class MainTextViewer extends TextViewer implements IDocumentListener {
       
    35 	
       
    36 	/**
       
    37 	 * View font
       
    38 	 */
       
    39 	private String FONT = "Arial"; //$NON-NLS-1$
       
    40 	/**
       
    41 	 * View font size
       
    42 	 */
       
    43 	private final int FONT_SIZE = 10;
       
    44 	/**
       
    45 	 * Scroll lock state.
       
    46 	 */
       
    47 	private boolean scrollLock = false;
       
    48 	
       
    49 	/**
       
    50 	 * Constructor.
       
    51 	 * @param parent Parent composite.
       
    52 	 * @param styles Style options.
       
    53 	 */
       
    54 	public MainTextViewer(Composite parent, int styles){
       
    55 		super(parent, styles);		
       
    56 		setEditable(false);
       
    57 
       
    58 		IDocument document = LogDocument.getInstance();
       
    59 		document.addDocumentListener(this);
       
    60 		
       
    61 		// Creating input for viewer.
       
    62 		setInput(document);
       
    63 	}
       
    64 	
       
    65 	/**
       
    66 	 * Initializing settings for viewer.
       
    67 	 */
       
    68 	public void initializeSettings(){
       
    69 		// Setting correct font.
       
    70 		Font font = new Font(getControl().getDisplay(),
       
    71 				new FontData(FONT, FONT_SIZE, SWT.NORMAL));
       
    72 		getTextWidget().setFont(font);
       
    73 		
       
    74 		// Setting view to the end of document.
       
    75 		setTopIndex(getBottomIndex());
       
    76 	}
       
    77 	
       
    78 	/**
       
    79 	 * Sets scroll lock on or off.
       
    80 	 * @param scrollLockState True if setting scroll lock on. False if setting it off.
       
    81 	 */
       
    82 	public void setScrollLock(boolean scrollLockState){
       
    83 		scrollLock = scrollLockState;
       
    84 	}
       
    85 	
       
    86 	/**
       
    87 	 * Clears text from console.
       
    88 	 */
       
    89 	public void clearScreen(){
       
    90 		// Clearing screen and.
       
    91 		getDocument().set(""); //$NON-NLS-1$
       
    92 	}
       
    93 
       
    94 	/**
       
    95 	 * Selects all text from console.
       
    96 	 */
       
    97 	public void selectAllText() {
       
    98 		IDocument document = getDocument();
       
    99 		setSelectedRange(0, document.getLength());
       
   100 	}
       
   101 
       
   102 	/* (non-Javadoc)
       
   103 	 * @see org.eclipse.jface.text.IDocumentListener#documentAboutToBeChanged(org.eclipse.jface.text.DocumentEvent)
       
   104 	 */
       
   105 	public void documentAboutToBeChanged(DocumentEvent event) {
       
   106 		// Not needed.
       
   107 	}
       
   108 
       
   109 	/* (non-Javadoc)
       
   110 	 * @see org.eclipse.jface.text.IDocumentListener#documentChanged(org.eclipse.jface.text.DocumentEvent)
       
   111 	 */
       
   112 	public void documentChanged(DocumentEvent event) {
       
   113 		if(!scrollLock){
       
   114 			// Scrolling text down to correct location.
       
   115 			setTopIndex(getDocument().getNumberOfLines());
       
   116 		}
       
   117 	}
       
   118 
       
   119 	/**
       
   120 	 * Returns currently selected text.
       
   121 	 * @return Currently selected text or null if getting selection failed.
       
   122 	 */
       
   123 	public String getSelectedText() {
       
   124 		IDocument document = getDocument();
       
   125 		Point range = getSelectedRange();
       
   126 		try {
       
   127 			// Getting text.
       
   128 			return document.get(range.x, range.y);
       
   129 		} catch (BadLocationException e) {
       
   130 			// Returning null to mean empty string if get fails.
       
   131 			e.printStackTrace();
       
   132 		}
       
   133 		return null;
       
   134 	}
       
   135 }