view src/samer/maths/MatrixPanel.java @ 8:5e3cbbf173aa tip

Reorganise some more
author samer
date Fri, 05 Apr 2019 22:41:58 +0100
parents bf79fb79ee13
children
line wrap: on
line source
/*
 *	MatrixPanel.java	
 *
 *	Copyright (c) 2000, Samer Abdallah, King's College London.
 *	All rights reserved.
 *
 *	This software is provided AS iS and WITHOUT ANY WARRANTY; 
 *	without even the implied warranty of MERCHANTABILITY or 
 *	FITNESS FOR A PARTICULAR PURPOSE.
 */

package samer.maths;

import java.awt.*;
import java.awt.event.*;
import java.text.*;
import samer.core.types.*;
import samer.core.util.*;
import samer.core.*;

/**
	A text based matrix editor
  */

public class MatrixPanel extends BaseViewer
	implements FocusListener, ActionListener
{
	int			rowdim, coldim;
	Matrix		M;
	Field[][]	TF;
	boolean		isvalid;
	String		errmsg;
	boolean		readonly;
	Color			validColor, badColor;

	public MatrixPanel( Matrix A) { this(A,true); }
	public MatrixPanel( Matrix A, boolean qedit) 
	{
		super(A.observable());

		int m = A.getRowDimension();
		int n = A.getColumnDimension();

		readonly = !qedit;

		/* initialize to all 0 */
		int i, j;
		setLayout(new GridLayout(m,n,2,2));
		rowdim = m; coldim = n;
		M = A;

		TF = new Field[m][n];
		isvalid = true;
		for (i=0; i < m; i++) {
			for (j=0; j < n; j++){
				TF[i][j] = new Field(6);
				TF[i][j].setEditable(qedit);
				add(TF[i][j]);
			}
		}

		validColor = TF[0][0].getForeground();
		badColor   = Color.red;
		update(null,null);
	}

	public void update(java.util.Observable o, Object src) 
	{
		if (src!=this) { // &&&
			for (int i=0; i < rowdim; i++) {
				for (int j=0; j< coldim; j++) {
					TF[i][j].setForeground(validColor);
					TF[i][j].setText(numfmt._format(M.get(i,j)));
					TF[i][j].edited=false;
				}
			}
		}
		super.update(o,src);
	}


	boolean Validate(boolean qcheck)
	{
		/* check validity of all edited entries and change M
		   If qcheck is true, check all entries
		*/
		String newtext="";
		isvalid = true;
		for (int i=0; i < rowdim; i++) {
			for (int j=0; j < coldim; j++) {
				if (TF[i][j].edited || qcheck) {
					try {
						newtext = TF[i][j].getText().trim();
						double newv = numfmt._parse(newtext);
						M.set(i,j,newv);
						TF[i][j].setForeground(validColor);
						TF[i][j].edited = false;
						TF[i][j].repaint();
					} catch( Exception ex) {
						TF[i][j].setForeground(badColor);
						TF[i][j].repaint();
						isvalid = false;
						if (ex instanceof NumberFormatException) 
							errmsg = "Invalid number:"+newtext;
						else errmsg = ex.getMessage();
						Shell.print(errmsg);
					}
				}
			}
		}

		if (isvalid) M.changed(this);
		return isvalid;
	} 
      
	public void focusGained( FocusEvent e) {}
	public void focusLost( FocusEvent e) { 
		if (!readonly) { 
			Component c=e.getComponent();
			if (c instanceof Field) {
				((Field)c).edited=true; 
				Validate(false); 
			}
		}
	}
	public void actionPerformed( ActionEvent e)
	{
		// could mean return pressed in a field
		Object c=e.getSource();
		if (c instanceof Field) {
			((Field)c).edited=true; 
			Validate(false); 
		}
	}

	static DoubleFormat numfmt=new DoubleFormat(3);

	class Field extends TextField
	{
		boolean edited=false;

		public Field(int cols)
		{ 
			super(cols); 
			this.addFocusListener(MatrixPanel.this);
			this.addActionListener(MatrixPanel.this);
		}
	}  
}