View Javadoc

1   /*
2    * Copyright 2006 - 2012 Christina Bohk and Roland Ewald
3    *  
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at 
7    *  
8    *  http://www.apache.org/licenses/LICENSE-2.0
9    *  
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
15   */
16  package p3j.gui.panels.matrices;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import net.sf.jeppers.grid.GridModel;
22  import net.sf.jeppers.grid.GridModelListener;
23  import p3j.misc.Misc;
24  import p3j.misc.math.Matrix2D;
25  
26  /**
27   * Model of {@link net.sf.jeppers.grid.JGrid} to integrate {@link Matrix2D}
28   * objects with {@link net.sf.jeppers.grid.JGrid}. The original matrix is
29   * transposed, i.e. columns are regarded as rows and vice versa.
30   * 
31   * Created on January 14, 2007
32   * 
33   * @author Christina Bohk
34   * @author Roland Ewald
35   * 
36   */
37  public class GridMatrixModel implements GridModel {
38  
39  	/** List of model listeners. */
40  	private List<GridModelListener> gridModelListener = new ArrayList<GridModelListener>();
41  
42  	/** Reference to matrix. */
43  	private Matrix2D matrix;
44  
45  	/**
46  	 * Default constructor.
47  	 * 
48  	 * @param mat
49  	 *          the matrix to be edited
50  	 */
51  	public GridMatrixModel(Matrix2D mat) {
52  		matrix = mat;
53  	}
54  
55  	@Override
56  	public void addGridModelListener(GridModelListener listener) {
57  		gridModelListener.add(listener);
58  	}
59  
60  	@Override
61  	public int getColumnCount() {
62  		return matrix.rows();
63  	}
64  
65  	@Override
66  	public int getRowCount() {
67  		return matrix.columns();
68  	}
69  
70  	@Override
71  	public Object getValueAt(int row, int column) {
72  		return Misc.NUMBER_FORMAT.format(matrix.get(column, row));
73  	}
74  
75  	@Override
76  	public boolean isCellEditable(int row, int column) {
77  		return true;
78  	}
79  
80  	@Override
81  	public void removeGridModelListener(GridModelListener listener) {
82  		gridModelListener.remove(listener);
83  	}
84  
85  	@Override
86  	public void setValueAt(Object value, int row, int column) {
87  		if (!value.equals("")) {
88  			matrix.set(column, row, Misc.parseToDouble(value));
89  		} else {
90  			matrix.set(column, row, 0);
91  		}
92  	}
93  
94  }