Mercurial > hg > jslab
comparison src/samer/maths/MatrixPanel.java @ 0:bf79fb79ee13
Initial Mercurial check in.
author | samer |
---|---|
date | Tue, 17 Jan 2012 17:50:20 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:bf79fb79ee13 |
---|---|
1 /* | |
2 * MatrixPanel.java | |
3 * | |
4 * Copyright (c) 2000, Samer Abdallah, King's College London. | |
5 * All rights reserved. | |
6 * | |
7 * This software is provided AS iS and WITHOUT ANY WARRANTY; | |
8 * without even the implied warranty of MERCHANTABILITY or | |
9 * FITNESS FOR A PARTICULAR PURPOSE. | |
10 */ | |
11 | |
12 package samer.maths; | |
13 | |
14 import java.awt.*; | |
15 import java.awt.event.*; | |
16 import java.text.*; | |
17 import samer.core.types.*; | |
18 import samer.core.util.*; | |
19 import samer.core.*; | |
20 | |
21 /** | |
22 A text based matrix editor | |
23 */ | |
24 | |
25 public class MatrixPanel extends BaseViewer | |
26 implements FocusListener, ActionListener | |
27 { | |
28 int rowdim, coldim; | |
29 Matrix M; | |
30 Field[][] TF; | |
31 boolean isvalid; | |
32 String errmsg; | |
33 boolean readonly; | |
34 Color validColor, badColor; | |
35 | |
36 public MatrixPanel( Matrix A) { this(A,true); } | |
37 public MatrixPanel( Matrix A, boolean qedit) | |
38 { | |
39 super(A.observable()); | |
40 | |
41 int m = A.getRowDimension(); | |
42 int n = A.getColumnDimension(); | |
43 | |
44 readonly = !qedit; | |
45 | |
46 /* initialize to all 0 */ | |
47 int i, j; | |
48 setLayout(new GridLayout(m,n,2,2)); | |
49 rowdim = m; coldim = n; | |
50 M = A; | |
51 | |
52 TF = new Field[m][n]; | |
53 isvalid = true; | |
54 for (i=0; i < m; i++) { | |
55 for (j=0; j < n; j++){ | |
56 TF[i][j] = new Field(6); | |
57 TF[i][j].setEditable(qedit); | |
58 add(TF[i][j]); | |
59 } | |
60 } | |
61 | |
62 validColor = TF[0][0].getForeground(); | |
63 badColor = Color.red; | |
64 update(null,null); | |
65 } | |
66 | |
67 public void update(java.util.Observable o, Object src) | |
68 { | |
69 if (src!=this) { // &&& | |
70 for (int i=0; i < rowdim; i++) { | |
71 for (int j=0; j< coldim; j++) { | |
72 TF[i][j].setForeground(validColor); | |
73 TF[i][j].setText(numfmt._format(M.get(i,j))); | |
74 TF[i][j].edited=false; | |
75 } | |
76 } | |
77 } | |
78 super.update(o,src); | |
79 } | |
80 | |
81 | |
82 boolean Validate(boolean qcheck) | |
83 { | |
84 /* check validity of all edited entries and change M | |
85 If qcheck is true, check all entries | |
86 */ | |
87 String newtext=""; | |
88 isvalid = true; | |
89 for (int i=0; i < rowdim; i++) { | |
90 for (int j=0; j < coldim; j++) { | |
91 if (TF[i][j].edited || qcheck) { | |
92 try { | |
93 newtext = TF[i][j].getText().trim(); | |
94 double newv = numfmt._parse(newtext); | |
95 M.set(i,j,newv); | |
96 TF[i][j].setForeground(validColor); | |
97 TF[i][j].edited = false; | |
98 TF[i][j].repaint(); | |
99 } catch( Exception ex) { | |
100 TF[i][j].setForeground(badColor); | |
101 TF[i][j].repaint(); | |
102 isvalid = false; | |
103 if (ex instanceof NumberFormatException) | |
104 errmsg = "Invalid number:"+newtext; | |
105 else errmsg = ex.getMessage(); | |
106 Shell.print(errmsg); | |
107 } | |
108 } | |
109 } | |
110 } | |
111 | |
112 if (isvalid) M.changed(this); | |
113 return isvalid; | |
114 } | |
115 | |
116 public void focusGained( FocusEvent e) {} | |
117 public void focusLost( FocusEvent e) { | |
118 if (!readonly) { | |
119 Component c=e.getComponent(); | |
120 if (c instanceof Field) { | |
121 ((Field)c).edited=true; | |
122 Validate(false); | |
123 } | |
124 } | |
125 } | |
126 public void actionPerformed( ActionEvent e) | |
127 { | |
128 // could mean return pressed in a field | |
129 Object c=e.getSource(); | |
130 if (c instanceof Field) { | |
131 ((Field)c).edited=true; | |
132 Validate(false); | |
133 } | |
134 } | |
135 | |
136 static DoubleFormat numfmt=new DoubleFormat(3); | |
137 | |
138 class Field extends TextField | |
139 { | |
140 boolean edited=false; | |
141 | |
142 public Field(int cols) | |
143 { | |
144 super(cols); | |
145 this.addFocusListener(MatrixPanel.this); | |
146 this.addActionListener(MatrixPanel.this); | |
147 } | |
148 } | |
149 } |