/*
 This file is part of the software framework JStatCom, 
 a library for building applications for data based analysis.

 Copyright (C) 2000-2005  Markus Kraetzig

 This library is free software; you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as
 published by the Free Software Foundation; either version 2.1 of the
 License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 USA
 */

package com.jstatcom.component;

import javax.swing.JLabel;
import javax.swing.JTextArea;

import com.jstatcom.model.JSCTypeDef;
import com.jstatcom.model.Scope;
import com.jstatcom.model.Symbol;
import com.jstatcom.model.SymbolEvent;
import com.jstatcom.model.SymbolListener;
import com.jstatcom.model.SymbolTable;

/**
 * This text area displays the string value of a given data
 * object defined by a symbol. It listens to changes in the
 * symbol and adjusts its value automatically.
 * 
 * @author Markus Kraetzig
 */
public class SymbolDisplay extends JTextArea implements SymbolListener {
	private Symbol symbol = null;
	private JSCTypeDef symbolDef = null;
	private Scope symbolScope = Scope.GLOBAL;

	/**
	 * Creates a SymbolDisplay that looks like a 
	 * JLabel except that it can display multiple lines.
	 */
	public SymbolDisplay() {
		super();
		setSize(100, 20);
		setText("{}");
		setOpaque(false);
		setEditable(false);
		setSelectedTextColor(getForeground());
		setFont(new JLabel().getFont());
	}

	/**
	 * Updates the displayed text if changes happen.
	 * 
	 * @param evt the event
	 */
	public void valueChanged(SymbolEvent evt) {
		symbol = evt.getSource();
		setSymbolText();
	}

	/**
	 * Overrides addNotify to retrieve
	 * data objects as soon as the component hierarchy is
	 * established. 
	 * 
	 */
	public void addNotify() {
		super.addNotify();
		setSelectionColor(getParent().getBackground());
		setDataForSymbolName();
	}

	/**
	 * Gets the data stored in the respective symboltable
	 * for the symbol identifier set in symbolName.
	 * 
	 * @return the data for the symbol name
	 */
	private void setDataForSymbolName() {

		SymbolTable sTable = null;
		if (symbolDef != null)
			sTable = symbolScope.getSymbolTable(this);
		Symbol newSymbol = null;
		if (sTable != null && symbolDef != null)
			newSymbol = sTable.get(symbolDef);

		// special cases
		if (newSymbol == symbol)
			return;

		if (symbol != null)
			symbol.removeSymbolListener(this);

		symbol = newSymbol;

		if (symbol != null)
			symbol.addSymbolListener(this);

		setSymbolText();
	}

	/**
	 * Gets the type definition of the symbol that is currently displayed.
	 * 
	 * @return the symbol def for the data object
	 */
	public JSCTypeDef getSymbolDef() {
		return symbolDef;
	}

	/**
	 * Sets the type definition of the symbol to be displayed.
	 * 
	 * @param newSymbolDef the symbol def for the data object
	 */
	public void setSymbolDef(JSCTypeDef newSymbolDef) {
		symbolDef = newSymbolDef;
		setDataForSymbolName();
	}

	/**
	 * Sets the text for this label.
	 * 
	 */
	private void setSymbolText() {
		if (symbol != null)
			setText(symbol.display());
		else
			setText("{}");

	}

	/**
	 * Gets the scope of the displayed symbol.
	 * 
	 * @return scope
	 */
	public Scope getSymbolScope() {
		return symbolScope;
	}

	/**
	 * Sets the scope of the displayed symbol.
	 * 
	 * @param scope 
	 * @throws IllegalArgumentException if (scope == null)
	 */
	public void setSymbolScope(Scope scope) {
		if (scope == null)
			throw new IllegalArgumentException("Argument was null.");
		symbolScope = scope;
		setDataForSymbolName();
	}

}
 

Ads help maintain this website.