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.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   * Panel that contains a matrix to be edited.
42   * 
43   * Created: August 26, 2008
44   * 
45   * @author Christina Bohk
46   * @author Roland Ewald
47   * 
48   */
49  public class EditMatrixPanel extends JTabbedPane {
50  
51  	/** Serialization ID. */
52  	private static final long serialVersionUID = -1060345291806349111L;
53  
54  	/** The panel to hold the matrix editing components. */
55  	private JPanel panel;
56  
57  	/** The panel to contain the plot results. */
58  	private JPanel plotPanel = new JPanel(new BorderLayout());
59  
60  	/** Table to display the matrix. */
61  	private JGrid matrixGrid;
62  
63  	/** Reference to matrix that should be edited. */
64  	private Matrix2D matrix;
65  
66  	/** Parameter assignment to be edited. */
67  	private ParameterAssignment paramAssignment;
68  
69  	// Definition of pop-up menu
70  
71  	/** Cut pop-up menu. */
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  	/** Copy pop-up menu. */
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  	/** Paste pop-up menu. */
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  	 * Pop up - Menu for cut/copy/paste.
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 	 * Default constructor.
107 	 * 
108 	 * @param pAssignment
109 	 *          the parameter assignment to be edited
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 	 * This method initializes the panel.
120 	 */
121 	private void initialize() {
122 
123 		// Careful: the matrix grid is initialized to view a *transposed* matrix!
124 		// TODO: Remove double-transposition of matrices.
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 		// Creates data plot if necessary.
167 		if (index == 1) {
168 			plotPanel.removeAll();
169 			plotPanel.add(getPlotPanel(), BorderLayout.CENTER);
170 		}
171 		super.setSelectedIndex(index);
172 	}
173 
174 	/**
175 	 * Creates either a 2D- or a 3D diagram.
176 	 * 
177 	 * @return the plot component
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 }