1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package p3j.gui.panels.matrices;
17
18 import java.awt.BorderLayout;
19 import java.awt.Component;
20 import java.awt.Point;
21 import java.awt.event.MouseAdapter;
22 import java.awt.event.MouseEvent;
23
24 import javax.swing.ImageIcon;
25 import javax.swing.JMenuItem;
26 import javax.swing.JPanel;
27 import javax.swing.JPopupMenu;
28 import javax.swing.JTabbedPane;
29
30 import net.sf.jeppers.grid.JGrid;
31 import net.sf.jeppers.grid.JScrollGrid;
32
33 import org.math.plot.Plot2DPanel;
34 import org.math.plot.Plot3DPanel;
35
36 import p3j.misc.gui.GUI;
37 import p3j.misc.math.Matrix2D;
38 import p3j.pppm.parameters.ParameterAssignment;
39
40
41
42
43
44
45
46
47
48
49 public class EditMatrixPanel extends JTabbedPane {
50
51
52 private static final long serialVersionUID = -1060345291806349111L;
53
54
55 private JPanel panel;
56
57
58 private JPanel plotPanel = new JPanel(new BorderLayout());
59
60
61 private JGrid matrixGrid;
62
63
64 private Matrix2D matrix;
65
66
67 private ParameterAssignment paramAssignment;
68
69
70
71
72 private JMenuItem cutMenu = new JMenuItem("Cut");
73 {
74 cutMenu.setIcon(new ImageIcon(this.getClass().getResource(
75 "/p3j/icons/cut.gif")));
76 cutMenu.setAccelerator(GridBehaviourAdapter.getCutKeyStroke());
77 }
78
79
80 private JMenuItem copyMenu = new JMenuItem("Copy");
81 {
82 copyMenu.setIcon(new ImageIcon(this.getClass().getResource(
83 "/p3j/icons/copy.gif")));
84 copyMenu.setAccelerator(GridBehaviourAdapter.getCopyKeyStroke());
85 }
86
87
88 private JMenuItem pasteMenu = new JMenuItem("Paste");
89 {
90 pasteMenu.setIcon(new ImageIcon(this.getClass().getResource(
91 "/p3j/icons/paste.gif")));
92 pasteMenu.setAccelerator(GridBehaviourAdapter.getPasteKeyStroke());
93 }
94
95
96
97
98 private final JPopupMenu popupMenu = new JPopupMenu();
99 {
100 popupMenu.add(copyMenu);
101 popupMenu.add(pasteMenu);
102 popupMenu.add(cutMenu);
103 }
104
105
106
107
108
109
110
111 public EditMatrixPanel(ParameterAssignment pAssignment) {
112 panel = new JPanel(GUI.getStdBorderLayout());
113 matrix = pAssignment.getMatrixValue();
114 paramAssignment = pAssignment;
115 initialize();
116 }
117
118
119
120
121 private void initialize() {
122
123
124
125 matrixGrid = new JGrid(matrix.columns(), matrix.rows());
126 matrixGrid.setGridModel(new GridMatrixModel(matrix));
127
128 matrixGrid.addMouseListener(new MouseAdapter() {
129 @Override
130 public void mouseReleased(MouseEvent e) {
131 if (e.isPopupTrigger()) {
132 Point p = new Point(e.getX(), e.getY());
133 int startRow = matrixGrid.rowAtPoint(p);
134 int startCol = matrixGrid.columnAtPoint(p);
135 matrixGrid.getSelectionModel().setSelectionRange(startRow, startCol,
136 startRow, startCol);
137 popupMenu.show(e.getComponent(), e.getX(), e.getY());
138 }
139 }
140 });
141
142 GridBehaviourAdapter behaviourAdapter = new GridBehaviourAdapter(matrixGrid);
143 cutMenu.addActionListener(behaviourAdapter);
144 cutMenu.setActionCommand("Cut");
145 copyMenu.addActionListener(behaviourAdapter);
146 copyMenu.setActionCommand("Copy");
147 pasteMenu.addActionListener(behaviourAdapter);
148 pasteMenu.setActionCommand("Paste");
149
150 JScrollGrid scrollGrid = new JScrollGrid(matrixGrid);
151
152 scrollGrid.setColumnHeader(new GridHeader(matrixGrid, matrix.getRowLabel(),
153 true, true));
154 scrollGrid.setRowHeader(new GridHeader(matrixGrid, matrix.getColumnLabel(),
155 false, false));
156
157 panel.add(scrollGrid, BorderLayout.CENTER);
158 plotPanel.add(getPlotPanel(), BorderLayout.CENTER);
159
160 add("Data", panel);
161 add("Plot", plotPanel);
162 }
163
164 @Override
165 public void setSelectedIndex(int index) {
166
167 if (index == 1) {
168 plotPanel.removeAll();
169 plotPanel.add(getPlotPanel(), BorderLayout.CENTER);
170 }
171 super.setSelectedIndex(index);
172 }
173
174
175
176
177
178
179 private Component getPlotPanel() {
180 if (matrix.columns() == 1) {
181 return create2DChartFromColumn();
182 }
183 if (matrix.rows() == 1) {
184 return create2DChartFromRow();
185 }
186 return create3DDiagram();
187 }
188
189 private Component create3DDiagram() {
190 double[] x = new double[matrix.rows()];
191 double[] y = new double[matrix.columns()];
192 double[][] matVals = new double[matrix.rows()][matrix.columns()];
193 for (int i = 0; i < matrix.rows(); i++) {
194 x[i] = i;
195 }
196 for (int i = 0; i < matrix.columns(); i++) {
197 y[i] = i;
198 }
199 for (int i = 0; i < matrix.rows(); i++) {
200 for (int j = 0; j < matrix.columns(); j++) {
201 matVals[i][j] = matrix.getQuick(i, j);
202 }
203 }
204 Plot3DPanel plot3DPanel = new Plot3DPanel();
205 plot3DPanel.setAxeLabel(0, matrix.getColumnLabel());
206 plot3DPanel.setAxeLabel(1, matrix.getRowLabel());
207 plot3DPanel.addGridPlot("twast", y, x, matVals);
208 return plot3DPanel;
209 }
210
211 private Component create2DChartFromRow() {
212 Plot2DPanel plot2DPanel = new Plot2DPanel();
213 double[][] plotData = new double[1][matrix.columns()];
214 for (int i = 0; i < matrix.columns(); i++) {
215 plotData[0][i] = matrix.getQuick(0, i);
216 }
217 plot2DPanel.addLinePlot(paramAssignment.getName(), plotData);
218 plot2DPanel.setAxeLabel(0, matrix.getColumnLabel());
219 return plot2DPanel;
220 }
221
222 private Component create2DChartFromColumn() {
223 Plot2DPanel plot2DPanel = new Plot2DPanel();
224 double[][] plotData = new double[1][matrix.rows()];
225 for (int i = 0; i < matrix.rows(); i++) {
226 plotData[0][i] = matrix.getQuick(i, 0);
227 }
228 plot2DPanel.addLinePlot(paramAssignment.getName(), plotData);
229 plot2DPanel.setAxeLabel(0, matrix.getRowLabel());
230 return plot2DPanel;
231 }
232 }