1 package edu.psu.geovista.app.matrix;
2
3 /***
4 * <p>Title: Studio applications</p>
5 * <p>Description: </p>
6 * <p>Copyright: Copyright (c) 2002</p>
7 * <p>Company: GeoVSITA Center</p>
8 * @author Xiping Dai
9 * @version 1.0
10 */
11
12 import java.awt.*;
13 import java.lang.*;
14 import java.util.*;
15 import java.awt.event.*;
16 import java.awt.image.*;
17 import java.awt.geom.AffineTransform;
18 import javax.swing.event.*;
19 import javax.swing.*;
20 import java.io.*;
21 import edu.psu.geovista.symbolization.*;
22 import edu.psu.geovista.category.MultiClassDistributions;
23 import edu.psu.geovista.ui.event.*;
24
25
26 public class DistributionMatrix extends AbstractMatrix
27 implements ListSelectionListener
28 {
29
30 private transient int[] rowInitOrder;
31 private transient int[] columnInitOrder;
32 private transient int[] rowOrder;
33 private transient int[] columnOrder;
34 private String[] variableNames;
35 private String[] observNames;
36 private int selectedObv;
37 protected transient EventListenerList listenerList = new EventListenerList();
38 private SPTagButton[] columnButton;
39 private JComboBox attCombo;
40 private int yIdx=0;
41 private static ImageIcon leftRightArrow;
42 private static ImageIcon topDownArrow;
43 private static Insets nullInsets;
44 private String[] varTags;
45 private Vector[] dataVector;
46 private MultiClassDistributions distributions[];
47
48 public DistributionMatrix()
49 {
50 super();
51 this.setSize(panelWidthPixels, panelHeightPixels);
52 }
53
54 public void setDataVector(Vector[] dataVector){
55 this.dataVector = dataVector;
56 init();
57 }
58
59 public void setVariableNames(String[] variableNames){
60 this.variableNames = variableNames;
61 int numLen;
62 numLen = this.variableNames.length;
63 //check if there are enough attributes to display in matrix from specified beginning.
64 if (plottedBegin >= numLen) {
65 System.err.println("There aren't enough attributes to display! Please reset the begin display attribute or reload a more attribute data file.");
66 return;
67 }
68 plotNumber = (numLen <= maxNumArrays) ? numLen : maxNumArrays;
69 if ((plottedBegin + plotNumber) > numLen) {
70 plotNumber = numLen - plottedBegin;
71 }
72 this.plottedAttributes = new int[plotNumber];
73 for (int i = plottedBegin; i < plottedBegin + plotNumber; i++) {
74 plottedAttributes[i - plottedBegin] = i;
75 }
76 }
77
78 protected synchronized void init () {
79 //System.out.println("get in init()...");
80 if (!this.recreate) {
81 return; // maybe display error message.
82 }
83 this.removeAll();
84
85 attList = new JList(this.variableNames);
86 attCombo = new JComboBox(this.variableNames);
87
88 varTags = new String[plotNumber];
89 if (this.dataVector != null) {
90 this.distributions = new MultiClassDistributions[plotNumber];
91 matrixLayout = new GridBagLayout();
92 c = new SPGridBagConstraints();
93 c.fill = GridBagConstraints.BOTH;
94 this.createMatrix();
95 Container parent = getParent();
96 if (parent != null) {
97 parent.validate();
98 }
99 else {
100 validate();
101 }
102 }
103 }
104
105 //Set up elements in matrix.
106 protected void createMatrix () {
107 //System.out.println("create SP:");
108 this.setLayout(matrixLayout);
109 Dimension size = getSize();
110 Dimension plotSize = new Dimension(size.width/plotNumber, size.height);
111 JButton configButton = new JButton("m");
112 SPTagButton rowButton;
113 columnButton = new SPTagButton[plotNumber];
114 Dimension tagDm = new Dimension(0, 0);
115 try {
116 for (int i = 0; i < 2; i++) {
117 for (int j = 0; j < plotNumber + 1; j++) {
118 if ((i == 0) && (j == 0)) {
119 c.weightx = 0.0;
120 c.weighty = 0.0;
121 c.gridwidth = 1;
122 c.gridheight = 1;
123 //configButton = new JButton("M");
124 configButton.addActionListener(new java.awt.event.ActionListener() {
125
126 /***
127 * Set up the attributes for plotted in SPM.
128 * @param e
129 */
130 public void actionPerformed (ActionEvent e) {
131 try {
132 configButton_actionPerformed(e);
133 } catch (Exception exception) {}
134 }
135 });
136 c.column = j;
137 c.row = i;
138 matrixLayout.setConstraints(configButton, c);
139 add(configButton);
140 }
141 else if ((i == 0) && (j != 0)) {
142 c.weightx = 1.0;
143 c.weighty = 0.0;
144 c.gridwidth = 4;
145 c.gridheight = 1;
146 if (j == plotNumber)
147 c.gridwidth = GridBagConstraints.REMAINDER; //end row
148 varTags[j-1] = this.variableNames[plottedAttributes[j - 1]];
149 columnButton[j-1] = new SPTagButton(varTags[j-1]);
150 columnButton[j-1].addMouseListener(columnButton[j-1]);
151 columnButton[j-1].addMouseMotionListener(columnButton[j-1]);
152 c.column = j;
153 c.row = i;
154 matrixLayout.setConstraints(columnButton[j-1], c);
155 add(columnButton[j-1]);
156
157 }
158 else if ((i != 0) && (j == 0)) {
159 c.weightx = 0.0;
160 c.weighty = 1.0;
161 c.gridwidth = 1;
162 c.gridheight = 4;
163 rowButton = new SPTagButton("s");
164 //JLabel rowLabel = new JLabel();
165 AffineTransform trans = new AffineTransform();
166 trans.rotate(-Math.PI/2);
167 Font font = new Font("", Font.BOLD, 12);
168 font = font.deriveFont(trans);
169 //rowLabel.setFont(font);
170 //BufferedImage labelImage = new BufferedImage(rowLabel.getWidth()+1,rowLabel.getHeight()+1,BufferedImage.TYPE_INT_ARGB);
171 //Graphics2D g2 = labelImage.createGraphics();
172 //rowButton[i-1].paint(g2);
173 rowButton.setFont(font);
174 rowButton.setText("Distributions");
175 rowButton.addMouseListener(rowButton);
176 rowButton.addMouseMotionListener(rowButton);
177 c.column = j;
178 c.row = i;
179 matrixLayout.setConstraints(rowButton, c);
180 add(rowButton);
181 }
182 else {
183 c.weightx = 1.0;
184 c.weighty = 1.0;
185 c.gridwidth = 4;
186 c.gridheight = 4;
187 if (j == plotNumber)
188 c.gridwidth = GridBagConstraints.REMAINDER; //end row
189 int indexCurrent = (j - 1);
190 int dataIndex;
191 dataIndex = plottedAttributes[j - 1];
192 //construct of each element
193 this.distributions[indexCurrent] = new MultiClassDistributions();
194 this.distributions[indexCurrent].setAxisOn(false);
195 this.distributions[indexCurrent].setDisplayVariableIndex(dataIndex);
196 this.distributions[indexCurrent].setBackground(background);
197 this.distributions[indexCurrent].setGaussOn(true);
198 this.distributions[indexCurrent].setDataVector(this.dataVector);
199 c.column = j;
200 c.row = i;
201 matrixLayout.setConstraints((Component)this.distributions[indexCurrent], c);
202 int w = matrixLayout.getConstraints((Component)this.distributions[indexCurrent]).gridwidth;
203 add((Component)this.distributions[indexCurrent]);
204
205 //this.distributions[indexCurrent].addActionListener(new ActionListener() {
206
207 /***
208 * Get the event from a unit plot and send it to all units.
209 * @param e
210 */
211 // public void actionPerformed (ActionEvent e) {
212 // This gets the source or originator of the event
213 // try {
214 // unit_actionPerformed(e);
215 // } catch (Exception exception) {}
216 // }
217 //});
218 }
219 }
220 }
221 } catch (Exception e) {
222 e.printStackTrace();
223 }
224 for (int j = 0; j < plotNumber; j++) {
225 if (columnButton[j].getPreferredSize().getWidth() > tagDm.getWidth())
226 tagDm = columnButton[j].getPreferredSize();
227 //System.out.println("tagDm: " + tagDm.getWidth() + tagDm.getHeight());
228 }
229 for (int j = 0; j < plotNumber; j++) {
230 columnButton[j].setPreferredSize(tagDm);
231 columnButton[j].setMinimumSize(tagDm);
232 columnButton[j].setMaximumSize(tagDm);
233 }
234 Dimension rowTag = new Dimension((int)tagDm.getHeight(), (int)tagDm.getHeight());
235 configButton.setPreferredSize(rowTag);
236 configButton.setMinimumSize(rowTag);
237 configButton.setMaximumSize(rowTag);
238 }
239
240
241 /***
242 * Shift columns or rows in the matrix.
243 * @param lastPos
244 * @param newPos
245 */
246 private void moveColumn (int lastPos, int newPos) {
247 int indicesRow;
248 int indicesCol;
249 int indicesLast1;
250 int indicesNew1;
251
252 String varTagMoved = new String(varTags[lastPos - 1]);
253 varTags[lastPos - 1] = varTags[newPos - 1];
254 this.columnButton[lastPos - 1].setText(varTags[lastPos - 1]);
255 varTags[newPos - 1] = varTagMoved;
256 this.columnButton[newPos - 1].setText(varTags[newPos - 1]);
257 //System.out.println("lastPos: " + lastPos + "newPos:" + newPos);
258 int graphTypeNumber = 1;
259 for (int i = 0; i < graphTypeNumber; i++) {
260 indicesLast1 = (distributions[i*plotNumber + lastPos - 1].getDisplayVariableIndex());
261 //indicesLast2 = (element[(lastPos - 1)*plotNumber + i].getElementPosition());
262 indicesRow = indicesLast1;
263 //indicesCol = indicesLast2[1];
264 indicesNew1 = distributions[i*plotNumber + newPos - 1].getDisplayVariableIndex();
265 //indicesNew2 = element[(newPos - 1)*plotNumber + i].getElementPosition();
266 indicesLast1 = indicesNew1;
267 //indicesLast2[1] = indicesNew2[1];
268 distributions[i*plotNumber + lastPos - 1].setDisplayVariableIndex(indicesLast1);
269 //element[(lastPos - 1)*plotNumber + i].setElementPosition(indicesLast2);
270 indicesNew1 = indicesRow;
271 //indicesNew2[1] = indicesCol;
272 distributions[i*plotNumber + newPos - 1].setDisplayVariableIndex(indicesNew1);
273 //element[(newPos - 1)*plotNumber + i].setElementPosition(indicesNew2);
274 }
275 }
276
277 /***
278 * Configure attributes for plotted in matrix.
279 * @param e
280 */
281 private void configButton_actionPerformed (ActionEvent e) {
282 attSelectDialog(400, 400);
283 }
284
285 JFrame dummyFrame = new JFrame();
286 JDialog dialog = null;
287 JScrollPane dialogPane = null;
288
289 /***
290 * put your documentation comment here
291 * @param x
292 * @param y
293 */
294 private void attSelectDialog (int x, int y) {
295 attList.setSelectedIndices(this.plottedAttributes);
296 if (this.dialog == null) {
297 this.dialog = new JDialog(dummyFrame, "Attributes for Plot", true);
298 JButton selectButton;
299 JButton closeButton;
300 dialog.setSize(150, 250);
301 dialog.getContentPane().setLayout(new BorderLayout());
302 attList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
303 //attList.addListSelectionListener(this);
304 //JScrollPane scrollPane = new JScrollPane(attList);
305 this.dialogPane = new JScrollPane(attList);
306
307 selectButton = new JButton("Apply");
308 selectButton.addActionListener(new java.awt.event.ActionListener() {
309
310 /***
311 * put your documentation comment here
312 * @param e
313 */
314 public void actionPerformed (ActionEvent e) {
315 selectButton_actionPerformed(e);
316 }
317 });
318 closeButton = new JButton("Close");
319 closeButton.addActionListener(new java.awt.event.ActionListener() {
320
321 /***
322 * put your documentation comment here
323 * @param e
324 */
325 public void actionPerformed (ActionEvent e) {
326 closeButton_actionPerformed(e);
327 }
328 });
329
330 JPanel buttonPanel = new JPanel(new BorderLayout());
331 buttonPanel.add(selectButton, BorderLayout.WEST);
332 buttonPanel.add(closeButton, BorderLayout.EAST);
333 JPanel namePanel = new JPanel(new GridLayout(1,1));
334 namePanel.add(new JLabel("Column Vars:"));
335 JPanel attPanel = new JPanel(new GridLayout(1,2));
336 attPanel.add(this.dialogPane);
337 dialog.getContentPane().add(namePanel, BorderLayout.NORTH);
338 dialog.getContentPane().add(attPanel, BorderLayout.CENTER);
339 dialog.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
340 }
341 else {
342 this.dialogPane.setViewportView(attList);
343 }
344 this.attList.addListSelectionListener(this);
345 this.plottedAttributes = attList.getSelectedIndices();
346 this.dialog.setLocation(x, y);
347 this.dialog.show();
348 }
349
350 /***
351 * put your documentation comment here
352 * @param e
353 */
354 private void selectButton_actionPerformed (ActionEvent e) {
355 plotNumber = plottedAttributes.length;
356 init();
357 }
358
359 /***
360 * put your documentation comment here
361 * @param e
362 */
363 private void closeButton_actionPerformed (ActionEvent e) {
364 dialog.hide();
365 }
366
367 protected class SPTagButton extends JButton
368 implements MouseListener, MouseMotionListener {
369
370 /***
371 * put your documentation comment here
372 * @param String label
373 */
374 SPTagButton (String label) {
375 super(label);
376 //buttonLabel = label;
377 }
378
379 /***
380 * put your documentation comment here
381 * @param ImageIcon icon
382 */
383 SPTagButton (ImageIcon icon) {
384 super(icon);
385 }
386
387 /***
388 * put your documentation comment here
389 * @param e
390 */
391 public void mousePressed (MouseEvent e) {
392 JButton button = (JButton)e.getSource();
393 SPGridBagConstraints gbconst = (SPGridBagConstraints)matrixLayout.getConstraints(button);
394 posLast = new Point(gbconst.column, gbconst.row);
395 posDrag = (Point)posLast.clone();
396 }
397
398 /***
399 * put your documentation comment here
400 * @param e
401 */
402 public void mouseReleased (MouseEvent e) {
403 posNew = SwingUtilities.convertPoint((SPTagButton)e.getSource(), e.getX(),
404 e.getY(), DistributionMatrix.this);
405 posNew = matrixLayout.location(posNew.x, posNew.y);
406 if (posNew.x > 4*(plotNumber - 1))
407 posNew.setLocation(posNew.x/4 + 1, posNew.y/4);
408 else
409 posNew.setLocation(posNew.x/4, posNew.y/4);
410 //System.out.println("PosNewX: " + posNew.x + "posNewY: " + posNew.y);
411 int lastPos = 0;
412 int newPos = 0;
413 if (!validCellPos(posDrag) || !validCellPos(posNew))
414 return;
415 if ((posDrag.x != posNew.x) && (posDrag.y == 0)) {
416 lastPos = posLast.x;
417 newPos = posNew.x;
418 moveColumn(lastPos, newPos);
419 posDrag = posNew;
420 repaint();
421 }
422 /*else if (posDrag.y != posNew.y) {
423 lastPos = posLast.y;
424 newPos = posNew.y;
425 }
426 if (lastPos != newPos) {
427 moveRowAndColumn(lastPos, newPos);
428 posDrag = posNew;
429 repaint();
430 }*/
431 }
432
433 /***
434 * put your documentation comment here
435 * @param e
436 */
437 public void mouseExited (MouseEvent e) {
438 ;
439 }
440
441 /***
442 * put your documentation comment here
443 * @param e
444 */
445 public void mouseDragged (MouseEvent e) {
446 posNew = SwingUtilities.convertPoint((SPTagButton)e.getSource(), e.getX(),
447 e.getY(), DistributionMatrix.this);
448 posNew = matrixLayout.location(posNew.x, posNew.y);
449 //System.out.println("PosNew0X: " + posNew.x + "posNew0Y: " + posNew.y);
450 if (posNew.x > 4*(plotNumber - 1))
451 posNew.setLocation(posNew.x/4 + 1, posNew.y/4);
452 else
453 posNew.setLocation(posNew.x/4, posNew.y/4);
454 //System.out.println("PosNewX: " + posNew.x + "posNewY: " + posNew.y);
455 int lastPos = 0;
456 int newPos = 0;
457 if (!validCellPos(posDrag) || !validCellPos(posNew))
458 return;
459 if (posDrag.x != posNew.x) {
460 lastPos = posDrag.x;
461 newPos = posNew.x;
462 moveColumn(lastPos, newPos);
463 posDrag = posNew;
464 repaint();
465 }
466 /*else if (posDrag.y != posNew.y) {
467 lastPos = posDrag.y;
468 newPos = posNew.y;
469 }
470 if (lastPos != newPos) {
471 moveRowAndColumn(lastPos, newPos);
472 posDrag = posNew;
473 repaint();
474 }*/
475 }
476
477 /***
478 * put your documentation comment here
479 * @param e
480 */
481 public void mouseMoved (MouseEvent e) {
482 ;
483 }
484
485 /***
486 * put your documentation comment here
487 * @param e
488 */
489 public void mouseEntered (MouseEvent e) {
490 ;
491 }
492
493 /***
494 * put your documentation comment here
495 * @param e
496 */
497 public void mouseClicked (MouseEvent e) {
498 ;
499 }
500 }
501
502 /***
503 * put your documentation comment here
504 * @param e
505 */
506 public void valueChanged (ListSelectionEvent e) {
507 if (e.getValueIsAdjusting())
508 return;
509 JList theList = (JList)e.getSource();
510 if (theList.isSelectionEmpty()) {
511 return;
512 }
513 else {
514 plottedAttributes = theList.getSelectedIndices();
515 }
516 }
517
518 private String stringToVertical (String s){
519 String vSt;
520 char[] sChar = s.toCharArray();
521 int len = sChar.length;
522 vSt = "<html> ";
523 for (int i = 0; i < len; i++){
524 vSt = vSt + sChar[i] + "<br>";
525 }
526 vSt = vSt + "</html> ";
527 return vSt;
528 }
529
530 }
This page was automatically generated by Maven