samer@0: /* samer@0: * MatrixPanel.java samer@0: * samer@0: * Copyright (c) 2000, Samer Abdallah, King's College London. samer@0: * All rights reserved. samer@0: * samer@0: * This software is provided AS iS and WITHOUT ANY WARRANTY; samer@0: * without even the implied warranty of MERCHANTABILITY or samer@0: * FITNESS FOR A PARTICULAR PURPOSE. samer@0: */ samer@0: samer@0: package samer.maths; samer@0: samer@0: import java.awt.*; samer@0: import java.awt.event.*; samer@0: import java.text.*; samer@0: import samer.core.types.*; samer@0: import samer.core.util.*; samer@0: import samer.core.*; samer@0: samer@0: /** samer@0: A text based matrix editor samer@0: */ samer@0: samer@0: public class MatrixPanel extends BaseViewer samer@0: implements FocusListener, ActionListener samer@0: { samer@0: int rowdim, coldim; samer@0: Matrix M; samer@0: Field[][] TF; samer@0: boolean isvalid; samer@0: String errmsg; samer@0: boolean readonly; samer@0: Color validColor, badColor; samer@0: samer@0: public MatrixPanel( Matrix A) { this(A,true); } samer@0: public MatrixPanel( Matrix A, boolean qedit) samer@0: { samer@0: super(A.observable()); samer@0: samer@0: int m = A.getRowDimension(); samer@0: int n = A.getColumnDimension(); samer@0: samer@0: readonly = !qedit; samer@0: samer@0: /* initialize to all 0 */ samer@0: int i, j; samer@0: setLayout(new GridLayout(m,n,2,2)); samer@0: rowdim = m; coldim = n; samer@0: M = A; samer@0: samer@0: TF = new Field[m][n]; samer@0: isvalid = true; samer@0: for (i=0; i < m; i++) { samer@0: for (j=0; j < n; j++){ samer@0: TF[i][j] = new Field(6); samer@0: TF[i][j].setEditable(qedit); samer@0: add(TF[i][j]); samer@0: } samer@0: } samer@0: samer@0: validColor = TF[0][0].getForeground(); samer@0: badColor = Color.red; samer@0: update(null,null); samer@0: } samer@0: samer@0: public void update(java.util.Observable o, Object src) samer@0: { samer@0: if (src!=this) { // &&& samer@0: for (int i=0; i < rowdim; i++) { samer@0: for (int j=0; j< coldim; j++) { samer@0: TF[i][j].setForeground(validColor); samer@0: TF[i][j].setText(numfmt._format(M.get(i,j))); samer@0: TF[i][j].edited=false; samer@0: } samer@0: } samer@0: } samer@0: super.update(o,src); samer@0: } samer@0: samer@0: samer@0: boolean Validate(boolean qcheck) samer@0: { samer@0: /* check validity of all edited entries and change M samer@0: If qcheck is true, check all entries samer@0: */ samer@0: String newtext=""; samer@0: isvalid = true; samer@0: for (int i=0; i < rowdim; i++) { samer@0: for (int j=0; j < coldim; j++) { samer@0: if (TF[i][j].edited || qcheck) { samer@0: try { samer@0: newtext = TF[i][j].getText().trim(); samer@0: double newv = numfmt._parse(newtext); samer@0: M.set(i,j,newv); samer@0: TF[i][j].setForeground(validColor); samer@0: TF[i][j].edited = false; samer@0: TF[i][j].repaint(); samer@0: } catch( Exception ex) { samer@0: TF[i][j].setForeground(badColor); samer@0: TF[i][j].repaint(); samer@0: isvalid = false; samer@0: if (ex instanceof NumberFormatException) samer@0: errmsg = "Invalid number:"+newtext; samer@0: else errmsg = ex.getMessage(); samer@0: Shell.print(errmsg); samer@0: } samer@0: } samer@0: } samer@0: } samer@0: samer@0: if (isvalid) M.changed(this); samer@0: return isvalid; samer@0: } samer@0: samer@0: public void focusGained( FocusEvent e) {} samer@0: public void focusLost( FocusEvent e) { samer@0: if (!readonly) { samer@0: Component c=e.getComponent(); samer@0: if (c instanceof Field) { samer@0: ((Field)c).edited=true; samer@0: Validate(false); samer@0: } samer@0: } samer@0: } samer@0: public void actionPerformed( ActionEvent e) samer@0: { samer@0: // could mean return pressed in a field samer@0: Object c=e.getSource(); samer@0: if (c instanceof Field) { samer@0: ((Field)c).edited=true; samer@0: Validate(false); samer@0: } samer@0: } samer@0: samer@0: static DoubleFormat numfmt=new DoubleFormat(3); samer@0: samer@0: class Field extends TextField samer@0: { samer@0: boolean edited=false; samer@0: samer@0: public Field(int cols) samer@0: { samer@0: super(cols); samer@0: this.addFocusListener(MatrixPanel.this); samer@0: this.addActionListener(MatrixPanel.this); samer@0: } samer@0: } samer@0: }