Subversion Repositories svnkaklik

Rev

Details | Last modification | View Log

Rev Author Line No. Line
157 kaklik 1
package avr.swing;
2
 
3
import com.sun.java.swing.SwingUtilities2;
4
import java.awt.*;
5
import java.awt.event.*;
6
import java.util.*;
7
import javax.swing.*;
8
import javax.swing.border.*;
9
import javax.swing.event.*;
10
import javax.swing.plaf.metal.*;
11
 
12
import avr.lang.*;
13
import avr.swing.*;
14
import avr.connection.event.ConnectionListener;
15
import avr.connection.event.ConnectionEvent;
16
import avr.device.event.DataAdapter;
17
import avr.device.event.DataListener;
18
import java.io.IOException;
19
 
20
public class JNewColorMapPanel extends JColorMapInterface {
21
 
22
   public static void main(String[] args) {
23
 
24
      JFrame frame = new JFrame("Testing");
25
 
26
//      frame.getContentPane().setLayout(new FlowLayout());
27
      frame.getContentPane().add(new JNewColorMapPanel(null), BorderLayout.CENTER);
28
      frame.pack();
29
 
30
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
31
 
32
      frame.show();
33
   }
34
 
35
   private JSelectionPanel redP;
36
   private JSelectionPanel greenP;
37
   private JSelectionPanel blueP;
38
   private JIndexRadioButton indicies[];
39
 
40
   private static final int RED_INDEX = 0;
41
   private static final int GREEN_INDEX = 1;
42
   private static final int BLUE_INDEX = 2;
43
 
44
   private static final int RED_MASK = 0xFF0000;
45
   private static final int GREEN_MASK = 0x00FF00;
46
   private static final int BLUE_MASK = 0x0000FF;
47
 
48
   private static final int MIN_INDEX = 0;
49
   private static final int MAX_INDEX = 1;
50
 
51
   private int[][][] cachedMap;
52
 
53
   private JMessagePanel messageP;
54
 
55
   private Action checkAction;
56
   private Action clearColumnAction;
57
   private Action clearAction;
58
   private Action resetAction;
59
   private Action sendAction;
60
 
61
   public JNewColorMapPanel(JMessagePanel messageP) {
62
      super(null);
63
 
64
      setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
65
 
66
      this.messageP = messageP;
67
 
68
      cachedMap = new int[8][3][2];
69
 
70
      redP = new JSelectionPanel(JSelectionPanel.RED_INDEX);
71
      greenP = new JSelectionPanel(JSelectionPanel.GREEN_INDEX);
72
      blueP = new JSelectionPanel(JSelectionPanel.BLUE_INDEX);
73
 
74
      JPanel topP = new JPanel();
75
      topP.add(new JLabel("Color Index:"));
76
 
77
      ButtonGroup bg = new ButtonGroup();
78
 
79
      indicies = new JIndexRadioButton[8];
80
 
81
      ProxyAction indexAction = new ProxyAction(this, "updateSliders", (String)null);
82
      ItemListener itemHandler = new ItemHandler();
83
 
84
      for(int i = 0; i < 8; i++) {
85
         indicies[i] = new JIndexRadioButton("" + (i + 1), i == 0);
86
         indicies[i].addActionListener(indexAction);
87
         indicies[i].addItemListener(itemHandler);
88
         indicies[i].setOpaque(false);
89
         bg.add(indicies[i]);
90
         topP.add(indicies[i]);
91
      }
92
 
93
      checkAction = new ProxyAction(this, "check", "Auto Check", 'a');
94
      clearColumnAction = new ProxyAction(this, "clearColumn", "Clear Indicies", 'l');
95
      clearAction = new ProxyAction(this, "clear", "Clear All", 'c');
96
      resetAction = new ProxyAction(this, "reset", "Reset", 'r');
97
      sendAction = new ProxyAction(this, "send", "Send", 's');
98
 
99
      sendAction.setEnabled(false);
100
 
101
      Box southBox = new Box(BoxLayout.X_AXIS);
102
      southBox.setBorder(new CompoundBorder(new EtchedBorder(), new EmptyBorder(5, 5, 5, 5)));
103
 
104
      southBox.add(new JButton(checkAction));
105
      southBox.add(Box.createHorizontalGlue());
106
      southBox.add(Box.createHorizontalStrut(5));
107
      southBox.add(new JButton(clearColumnAction));
108
      southBox.add(Box.createHorizontalStrut(5));
109
      southBox.add(new JButton(clearAction));
110
      southBox.add(Box.createHorizontalStrut(5));
111
      southBox.add(new JButton(resetAction));
112
      southBox.add(Box.createHorizontalStrut(5));
113
      southBox.add(new JButton(sendAction));
114
 
115
      add(topP);
116
      add(Box.createVerticalStrut(5));
117
      add(redP);
118
      add(Box.createVerticalStrut(5));
119
      add(greenP);
120
      add(Box.createVerticalStrut(5));
121
      add(blueP);
122
      add(Box.createVerticalStrut(5));
123
      add(southBox);
124
 
125
      reset();
126
 
127
      AVRSystem.DEVICE.addConnectionListener(new ConnectionHandler());
128
   }
129
 
130
   private static String formatColorMapException(InvalidColorMapException icme) {
131
 
132
      StringBuffer builder = new StringBuffer("Invalid Color Map: ");
133
 
134
      int[] indicies = icme.getIndicies();
135
 
136
      builder.append("Indicies ");
137
 
138
      for(int i = 0; i < indicies.length; i++) {
139
 
140
         builder.append(indicies[i]);
141
 
142
         if((i + 1) < indicies.length) {
143
            builder.append(" and ");
144
         }
145
 
146
      }
147
 
148
      builder.append(" intersect at values Red: ")
149
             .append(icme.getRed())
150
             .append(" Green: ")
151
             .append(icme.getGreen())
152
             .append(" Blue: ")
153
             .append(icme.getBlue());
154
 
155
      return builder.toString();
156
 
157
   }
158
 
159
   public void check() {
160
 
161
      try {
162
         checkMap();
163
 
164
         JOptionPane.showMessageDialog(getRootPane(),
165
                                       "Color Map is valid.",
166
                                       "Color Map Validated",
167
                                       JOptionPane.INFORMATION_MESSAGE);
168
      } catch(InvalidColorMapException icme) {
169
         JOptionPane.showMessageDialog(getRootPane(), formatColorMapException(icme));
170
      }
171
 
172
   }
173
 
174
   /* *************************************
175
    * Copied from java.lang.Integer class
176
    * from the JDK 1.5 version.
177
    */
178
   private int bitCount(int i) {
179
      i = i - ((i >>> 1) & 0x55555555);
180
      i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
181
      i = (i + (i >>> 4)) & 0x0f0f0f0f;
182
      i = i + (i >>> 8);
183
      i = i + (i >>> 16);
184
      return i & 0x3f;
185
   }
186
 
187
   private void checkMap() throws InvalidColorMapException {
188
 
189
      updateCacheMap();
190
 
191
      int[][] colorMap = convert();
192
 
193
      for(int redI = 0; redI < AVRSystem.NUM_INTENSITIES; redI++) {
194
         int red = colorMap[0][redI];
195
         for(int greenI = 0; greenI < AVRSystem.NUM_INTENSITIES; greenI++) {
196
            int green = colorMap[1][greenI];
197
            for(int blueI = 0; blueI < AVRSystem.NUM_INTENSITIES; blueI++) {
198
               int blue = colorMap[2][blueI];
199
 
200
               int value = red & green & blue;
201
 
202
               // In JDk 1.5 the Integer class has the bitCount
203
               // method.  To be backward compatible, use the bitCount
204
               // method above.
205
//               if(value != 0 && (Integer.bitCount(value) > 1)) {
206
               if(value != 0 && (bitCount(value) > 1)) {
207
 
208
                  int[] indicies = new int[bitCount(value)];
209
                  int count = 0;
210
                  for(int i = 0; i < 8; i++) {
211
                     if((value & (0x80 >>> i)) != 0) {
212
                        indicies[count++] = (i + 1);
213
                     }
214
                  }
215
 
216
                  throw new InvalidColorMapException("Color Map is invalid.", indicies, redI * 16, greenI * 16, blueI * 16);
217
               }
218
 
219
            }
220
 
221
         }
222
      }
223
 
224
   }
225
 
226
   private int[][] convert() {
227
 
228
      int[][] colorMap = new int[3][AVRSystem.NUM_INTENSITIES];
229
 
230
      for(int color = 0; color < 3; color++) {
231
         for(int col = 0; col < 8; col++) {
232
            for(int i = 1; i < AVRSystem.NUM_INTENSITIES; i++) {
233
 
234
               int min = cachedMap[col][color][MIN_INDEX];
235
               int max = cachedMap[col][color][MAX_INDEX];
236
 
237
               if((min <= (i * 16)) && ((i * 16) <= max)) {
238
                  colorMap[color][i] |= (0x01 << (7 - col));
239
               }
240
 
241
            }
242
 
243
         }
244
      }
245
 
246
      return colorMap;
247
 
248
   }
249
 
250
   public boolean isColumnClear(int column) {
251
 
252
      return cachedMap[column][RED_INDEX][MIN_INDEX] == 0 &&
253
             cachedMap[column][RED_INDEX][MAX_INDEX] == 0 &&
254
             cachedMap[column][GREEN_INDEX][MIN_INDEX] == 0 &&
255
             cachedMap[column][GREEN_INDEX][MAX_INDEX] == 0 &&
256
             cachedMap[column][BLUE_INDEX][MIN_INDEX] == 0 &&
257
             cachedMap[column][BLUE_INDEX][MAX_INDEX] == 0;
258
 
259
   }
260
 
261
   public void clear() {
262
      cachedMap = new int[8][3][2];
263
 
264
      updateSliders();
265
      updateBackgrounds();
266
   }
267
 
268
   public void reset() {
269
 
270
      int[][] colorMap = AVRSystem.DEVICE.getColorMap();
271
 
272
      for(int c = 0; c < 8; c++) {
273
         int minRed = 0;
274
         int maxRed = 0;
275
         int minGreen = 0;
276
         int maxGreen = 0;
277
         int minBlue = 0;
278
         int maxBlue = 0;
279
 
280
         for(int i = 0; i < AVRSystem.NUM_INTENSITIES; i++) {
281
 
282
            int value = i * 16;
283
 
284
            if((colorMap[0][i] & (0x01 << (7 - c))) != 0) {
285
               if(minRed == 0 || value < minRed) {
286
                  minRed = value;
287
               }
288
               if(maxRed < (value)) {
289
                  maxRed = value;
290
               }
291
            }
292
 
293
            if((colorMap[1][i] & (0x01 << (7 - c))) != 0) {
294
               if(minGreen == 0 || value < minGreen) {
295
                  minGreen = value;
296
               }
297
               if(maxGreen < (value)) {
298
                  maxGreen = value;
299
               }
300
            }
301
 
302
            if((colorMap[2][i] & (0x01 << (7 - c))) != 0) {
303
               if(minBlue == 0 || value < minBlue) {
304
                  minBlue = value;
305
               }
306
               if(maxBlue < (value)) {
307
                  maxBlue = value;
308
               }
309
            }
310
 
311
         }
312
 
313
         int col = c;
314
 
315
         cachedMap[col][RED_INDEX][MIN_INDEX] = minRed;
316
         cachedMap[col][RED_INDEX][MAX_INDEX] = maxRed;
317
 
318
         cachedMap[col][GREEN_INDEX][MIN_INDEX] = minGreen;
319
         cachedMap[col][GREEN_INDEX][MAX_INDEX] = maxGreen;
320
 
321
         cachedMap[col][BLUE_INDEX][MIN_INDEX] = minBlue;
322
         cachedMap[col][BLUE_INDEX][MAX_INDEX] = maxBlue;
323
 
324
      }
325
 
326
      updateSliders();
327
      updateBackgrounds();
328
 
329
   }
330
 
331
   public void clearColumn() {
332
 
333
      JPanel displayP = new JPanel(new BorderLayout());
334
 
335
      JPanel selectColP = new JPanel();
336
 
337
      JCheckBox[] colCB = new JCheckBox[8];
338
      for(int i = 0; i < colCB.length; i++) {
339
         colCB[i] = new JCheckBox((i + 1) + "");
340
         selectColP.add(colCB[i]);
341
      }
342
 
343
      displayP.add(new JLabel("Select Color Map Index:"), BorderLayout.NORTH);
344
      displayP.add(selectColP, BorderLayout.SOUTH);
345
 
346
      int option = JOptionPane.showConfirmDialog(getRootPane(),
347
                                                 displayP,
348
                                                 "Select Indicies to clear:",
349
                                                 JOptionPane.OK_CANCEL_OPTION,
350
                                                 JOptionPane.QUESTION_MESSAGE);
351
 
352
      if(option == JOptionPane.OK_OPTION) {
353
 
354
         /* *********************************************
355
          * NOTE: This one loop is only checking the
356
          * length of the red color panels but is also
357
          * looping over the green and blue ones!!!!
358
          **/
359
         for(int col = 0; col < 8; col++) {
360
            if(colCB[col].isSelected()) {
361
               cachedMap[col][RED_INDEX][MIN_INDEX] = 0;
362
               cachedMap[col][RED_INDEX][MAX_INDEX] = 0;
363
               cachedMap[col][GREEN_INDEX][MIN_INDEX] = 0;
364
               cachedMap[col][GREEN_INDEX][MAX_INDEX] = 0;
365
               cachedMap[col][BLUE_INDEX][MIN_INDEX] = 0;
366
               cachedMap[col][BLUE_INDEX][MAX_INDEX] = 0;
367
            }
368
         }
369
 
370
         updateSliders();
371
         updateBackgrounds();
372
      }
373
   }
374
 
375
   public void send() {
376
 
377
      try {
378
 
379
         checkMap();
380
 
381
         int[][] newColorMap = convert();
382
 
383
         DataListener handler = new DataHandler(newColorMap);
384
         try {
385
 
386
            AVRSystem.DEVICE.addDataListener(handler);
387
            getRootPane().getGlassPane().setVisible(true);
388
            SwingUtilities.getRootPane(messageP).getGlassPane().setVisible(true);
389
            AVRSystem.DEVICE.sendSetColorMap(newColorMap[0], newColorMap[1], newColorMap[2]);
390
            messageP.append("Sent Color Map");
391
         } catch(IOException ioe) {
392
            AVRSystem.DEVICE.removeDataListener(handler);
393
            getRootPane().getGlassPane().setVisible(false);
394
            SwingUtilities.getRootPane(messageP).getGlassPane().setVisible(false);
395
            ioe.printStackTrace();
396
            AVRSystem.LOG.severe(ioe.getMessage());
397
         }
398
      } catch(InvalidColorMapException icme) {
399
         JOptionPane.showMessageDialog(getRootPane(),
400
                                       formatColorMapException(icme),
401
                                       "Check Failed",
402
                                       JOptionPane.ERROR_MESSAGE);
403
      }
404
 
405
   }
406
 
407
   public void setColor(int index, int color) {
408
 
409
      int minRed = (color & RED_MASK) >>> 16;
410
      int minGreen = (color & GREEN_MASK) >>> 8;
411
      int minBlue = (color & BLUE_MASK) >>> 0;
412
 
413
      int maxRed = minRed;
414
      int maxGreen = minGreen;
415
      int maxBlue = minBlue;
416
 
417
      if(minRed == 16) {
418
         maxRed *= 3;
419
      } else if(minRed == 240) {
420
         minRed -= (16 * 2);
421
      } else {
422
         minRed -= 16;
423
         maxRed += 16;
424
      }
425
 
426
      if(minGreen == 16) {
427
         maxGreen *= 3;
428
      } else if(minGreen == 240) {
429
         minGreen -= (16 * 2);
430
      } else {
431
         minGreen -= 16;
432
         maxGreen += 16;
433
      }
434
 
435
      if(minBlue == 16) {
436
         maxBlue *= 3;
437
      } else if(minBlue == 240) {
438
         minBlue -= (16 * 2);
439
      } else {
440
         minBlue -= 16;
441
         maxBlue += 16;
442
      }
443
 
444
      cachedMap[index][RED_INDEX][MIN_INDEX] = minRed;
445
      cachedMap[index][RED_INDEX][MAX_INDEX] = maxRed;
446
 
447
      cachedMap[index][GREEN_INDEX][MIN_INDEX] = minGreen;
448
      cachedMap[index][GREEN_INDEX][MAX_INDEX] = maxGreen;
449
 
450
      cachedMap[index][BLUE_INDEX][MIN_INDEX] = minBlue;
451
      cachedMap[index][BLUE_INDEX][MAX_INDEX] = maxBlue;
452
 
453
      indicies[index].setSelected(true);
454
 
455
      updateSliders();
456
//      updateBackgrounds();
457
 
458
   }
459
 
460
   private void updateCacheMap() {
461
 
462
      for(int i = 0; i < 8; i++) {
463
         if(indicies[i].isSelected()) {
464
            cachedMap[i][RED_INDEX][MIN_INDEX] = redP.getMin();
465
            cachedMap[i][RED_INDEX][MAX_INDEX] = redP.getMax();
466
            cachedMap[i][GREEN_INDEX][MIN_INDEX] = greenP.getMin();
467
            cachedMap[i][GREEN_INDEX][MAX_INDEX] = greenP.getMax();
468
            cachedMap[i][BLUE_INDEX][MIN_INDEX] = blueP.getMin();
469
            cachedMap[i][BLUE_INDEX][MAX_INDEX] = blueP.getMax();
470
         }
471
      }
472
 
473
   }
474
 
475
   public void update() {
476
      updateCacheMap();
477
      updateSliders();
478
   }
479
 
480
   public void updateBackgrounds() {
481
 
482
      for(int i = 0; i < indicies.length; i++) {
483
 
484
         int avgRed = (cachedMap[i][RED_INDEX][MIN_INDEX] +
485
                       cachedMap[i][RED_INDEX][MAX_INDEX]) / 2;
486
         int avgGreen = (cachedMap[i][GREEN_INDEX][MIN_INDEX] +
487
                         cachedMap[i][GREEN_INDEX][MAX_INDEX]) / 2;
488
         int avgBlue = (cachedMap[i][BLUE_INDEX][MIN_INDEX] +
489
                        cachedMap[i][BLUE_INDEX][MAX_INDEX]) / 2;
490
 
491
         indicies[i].setToolTipText("Average Color: Red (" + avgRed + ") Green (" + avgGreen + ") Blue (" + avgBlue + ")");
492
 
493
         indicies[i].setMinColor(
494
            new Color(cachedMap[i][RED_INDEX][MIN_INDEX],
495
                      cachedMap[i][GREEN_INDEX][MIN_INDEX],
496
                      cachedMap[i][BLUE_INDEX][MIN_INDEX]));
497
         indicies[i].setMaxColor(
498
            new Color(cachedMap[i][RED_INDEX][MAX_INDEX],
499
                      cachedMap[i][GREEN_INDEX][MAX_INDEX],
500
                      cachedMap[i][BLUE_INDEX][MAX_INDEX]));
501
         indicies[i].repaint();
502
 
503
      }
504
 
505
   }
506
 
507
   public void updateSliders() {
508
 
509
      for(int i = 0; i < indicies.length; i++) {
510
 
511
         if(indicies[i].isSelected()) {
512
 
513
            redP.setMin(cachedMap[i][RED_INDEX][MIN_INDEX]);
514
            redP.setMax(cachedMap[i][RED_INDEX][MAX_INDEX]);
515
            greenP.setMin(cachedMap[i][GREEN_INDEX][MIN_INDEX]);
516
            greenP.setMax(cachedMap[i][GREEN_INDEX][MAX_INDEX]);
517
            blueP.setMin(cachedMap[i][BLUE_INDEX][MIN_INDEX]);
518
            blueP.setMax(cachedMap[i][BLUE_INDEX][MAX_INDEX]);
519
 
520
         }
521
 
522
      }
523
 
524
 
525
   }
526
 
527
   private static final class JIndexRadioButton extends JRadioButton {
528
 
529
      private Color min;
530
      private Color max;
531
 
532
      public JIndexRadioButton(String text, boolean selected) {
533
         super(text, selected);
534
         min = Color.BLACK;
535
         max = Color.BLACK;
536
      }
537
 
538
      public Dimension getPreferredSize() {
539
         Dimension size = super.getPreferredSize();
540
         size.width += 20;
541
         return size;
542
      }
543
 
544
      public void setMinColor(Color min) {
545
         this.min = min;
546
      }
547
 
548
      public void setMaxColor(Color max) {
549
         this.max = max;
550
      }
551
 
552
      public void paintComponent(Graphics g) {
553
 
554
         AbstractButton b = (AbstractButton) this;
555
         JComponent c = this;
556
 
557
         Dimension size = c.getSize();
558
 
559
         Font f = c.getFont();
560
         FontMetrics fm = SwingUtilities2.getFontMetrics(c, g, f);
561
 
562
         Rectangle viewRect = new Rectangle(size);
563
         Rectangle iconRect = new Rectangle();
564
         Rectangle textRect = new Rectangle();
565
 
566
         Insets i = c.getInsets();
567
         viewRect.x += i.left;
568
         viewRect.y += i.top;
569
         viewRect.width -= (i.right + viewRect.x);
570
         viewRect.height -= (i.bottom + viewRect.y);
571
 
572
         Icon altIcon = b.getIcon();
573
 
574
         String text = SwingUtilities.layoutCompoundLabel(
575
             c, fm, b.getText(), altIcon != null ? altIcon : UIManager.getIcon("RadioButton.icon"),
576
             b.getVerticalAlignment(), b.getHorizontalAlignment(),
577
             b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
578
             viewRect, iconRect, textRect, b.getIconTextGap());
579
 
580
         Graphics2D g2d = (Graphics2D)g;
581
 
582
         Paint paint = g2d.getPaint();
583
         Stroke stroke = g2d.getStroke();
584
 
585
         g2d.setPaint(new GradientPaint(0, 0, min, size.width, 0, max));
586
         g2d.setStroke(new BasicStroke(size.height));
587
         g2d.fillRect(0, 0, size.width, size.height);
588
 
589
         g2d.setPaint(paint);
590
         g2d.setStroke(stroke);
591
 
592
         super.paintComponent(g);
593
 
594
         g.setColor(Color.WHITE);
595
         g.fillRect(textRect.x - 1, textRect.y + 2, textRect.width + 2, textRect.height - 4);
596
         paintText(g, c, textRect, text);
597
 
598
 
599
      }
600
 
601
      protected void paintText(Graphics g, JComponent c, Rectangle textRect,
602
                               String text) {
603
         AbstractButton b = (AbstractButton)c;
604
         ButtonModel model = b.getModel();
605
         FontMetrics fm = SwingUtilities2.getFontMetrics(c, g);
606
         int mnemonicIndex = b.getDisplayedMnemonicIndex();
607
 
608
         /* Draw the Text */
609
         if(model.isEnabled()) {
610
            /*** paint the text normally */
611
            g.setColor(b.getForeground());
612
            SwingUtilities2.drawStringUnderlineCharAt(c, g, text, mnemonicIndex,
613
               textRect.x + UIManager.getInt("Button.textShiftOffset"),
614
               textRect.y + fm.getAscent() + UIManager.getInt("Button.textShiftOffset"));
615
         } else {
616
            /*** paint the text disabled ***/
617
            g.setColor(b.getBackground().brighter());
618
            SwingUtilities2.drawStringUnderlineCharAt(c, g, text, mnemonicIndex,
619
               textRect.x, textRect.y + fm.getAscent());
620
            g.setColor(b.getBackground().darker());
621
            SwingUtilities2.drawStringUnderlineCharAt(c, g, text, mnemonicIndex,
622
               textRect.x - 1, textRect.y + fm.getAscent() - 1);
623
         }
624
      }
625
 
626
   }
627
 
628
   private final class ItemHandler implements ItemListener {
629
 
630
      public void itemStateChanged(ItemEvent e) {
631
         if(e.getStateChange() == ItemEvent.DESELECTED) {
632
            for(int i = 0; i < 8; i++) {
633
               if(indicies[i] == e.getSource()) {
634
                  cachedMap[i][RED_INDEX][MIN_INDEX] = redP.getMin();
635
                  cachedMap[i][RED_INDEX][MAX_INDEX] = redP.getMax();
636
                  cachedMap[i][GREEN_INDEX][MIN_INDEX] = greenP.getMin();
637
                  cachedMap[i][GREEN_INDEX][MAX_INDEX] = greenP.getMax();
638
                  cachedMap[i][BLUE_INDEX][MIN_INDEX] = blueP.getMin();
639
                  cachedMap[i][BLUE_INDEX][MAX_INDEX] = blueP.getMax();
640
               }
641
            }
642
 
643
         }
644
      }
645
 
646
   }
647
 
648
   private static final class JSelectionPanel extends Box {
649
 
650
      public static final int RED_INDEX = 0;
651
      public static final int GREEN_INDEX = 1;
652
      public static final int BLUE_INDEX = 2;
653
 
654
      private JSlider minS;
655
      private JSlider maxS;
656
      private JColorLabel colorL;
657
 
658
      private int index;
659
 
660
      public JSelectionPanel(int index) {
661
         super(BoxLayout.Y_AXIS);
662
 
663
         this.index = index;
664
 
665
         minS = new JSlider(0, 16 * (AVRSystem.NUM_INTENSITIES - 1), 16);
666
         maxS = new JSlider(0, 16 * (AVRSystem.NUM_INTENSITIES - 1), 16);
667
 
668
         minS.setUI(new AVRSliderUI(AVRSliderUI.RIGHT));
669
         maxS.setUI(new AVRSliderUI(AVRSliderUI.LEFT));
670
 
671
         colorL = new JColorLabel(index);
672
 
673
         minS.setMajorTickSpacing(16);
674
         minS.setSnapToTicks(true);
675
         minS.setOpaque(false);
676
 
677
         maxS.setPaintTicks(true);
678
         maxS.setMajorTickSpacing(16);
679
         maxS.setSnapToTicks(true);
680
         maxS.setOpaque(false);
681
         maxS.setPaintLabels(true);
682
 
683
         minS.addChangeListener(new MinChangeHandler());
684
         maxS.addChangeListener(new MaxChangeHandler());
685
 
686
         minS.setForeground(Color.WHITE);
687
         maxS.setForeground(Color.WHITE);
688
 
689
         int sliderWidth = Math.max(minS.getPreferredSize().width,
690
                                    maxS.getPreferredSize().width);
691
 
692
         sliderWidth = (int)(sliderWidth * 1.75);
693
 
694
         minS.setBorder(new CompoundBorder(minS.getBorder(), new EmptyBorder(0, 3, 0, 3)));
695
 
696
         Dimension sliderDim = minS.getPreferredSize();
697
         sliderDim.width = sliderWidth;
698
         minS.setPreferredSize(sliderDim);
699
 
700
         sliderDim = maxS.getPreferredSize();
701
         sliderDim.width = sliderWidth;
702
         maxS.setPreferredSize(sliderDim);
703
 
704
         Enumeration labels = maxS.getLabelTable().elements();
705
 
706
         while(labels.hasMoreElements()) {
707
            ((JLabel)labels.nextElement()).setForeground(Color.WHITE);
708
         }
709
 
710
         add(minS);
711
         add(maxS);
712
 
713
      }
714
 
715
      public int getMin() {
716
         return minS.getValue();
717
      }
718
 
719
      public void setMin(int min) {
720
         minS.setValue(min);
721
      }
722
 
723
      public int getMax() {
724
         return maxS.getValue();
725
      }
726
 
727
      public void setMax(int max) {
728
         maxS.setValue(max);
729
      }
730
 
731
      public void paintComponent(Graphics g) {
732
 
733
         Insets insets = getInsets();
734
         Dimension size = getSize();
735
 
736
         Graphics2D g2d = (Graphics2D)g;
737
 
738
         int minRed = 0;
739
         int minGreen = 0;
740
         int minBlue = 0;
741
 
742
         int maxRed = 0;
743
         int maxGreen = 0;
744
         int maxBlue = 0;
745
 
746
         switch(index) {
747
            case RED_INDEX:
748
               maxRed = maxS.getValue();
749
               minRed = minS.getValue();
750
               break;
751
            case GREEN_INDEX:
752
               maxGreen = maxS.getValue();
753
               minGreen = minS.getValue();
754
               break;
755
            case BLUE_INDEX:
756
               maxBlue = maxS.getValue();
757
               minBlue = minS.getValue();
758
               break;
759
         }
760
 
761
         Color minColor = new Color(minRed, minGreen, minBlue);
762
         Color maxColor = new Color(maxRed, maxGreen, maxBlue);
763
 
764
         Paint paint = new GradientPaint(insets.left, insets.top, minColor,
765
                                         size.width - insets.right, insets.top, maxColor);
766
         g2d.setPaint(paint);
767
         g2d.fillRect(insets.left, insets.top,
768
                      size.width - insets.left - insets.right,
769
                      size.height - insets.top - insets.bottom);
770
 
771
      }
772
 
773
      private final class MinChangeHandler implements ChangeListener {
774
 
775
         public void stateChanged(ChangeEvent e) {
776
            if(maxS.getValue() < minS.getValue()) {
777
               maxS.setValue(minS.getValue());
778
            }
779
//            colorL.setMin(minS.getValue());
780
            repaint();
781
         }
782
 
783
      }
784
 
785
      private final class MaxChangeHandler implements ChangeListener {
786
 
787
         public void stateChanged(ChangeEvent e) {
788
            if(maxS.getValue() < minS.getValue()) {
789
               minS.setValue(maxS.getValue());
790
            }
791
//            colorL.setMax(maxS.getValue());
792
            repaint();
793
         }
794
 
795
      }
796
 
797
      private static final class JColorLabel extends JLabel {
798
 
799
         private int index;
800
 
801
         private int max;
802
         private int min;
803
 
804
         public JColorLabel(int index) {
805
            this.index = index;
806
            setBorder(new LineBorder(Color.BLACK));
807
            setOpaque(true);
808
            min = 0;
809
            max = 0;
810
         }
811
 
812
         public void setMin(int min) {
813
            this.min = min;
814
            repaint();
815
         }
816
 
817
         public void setMax(int max) {
818
            this.max = max;
819
            repaint();
820
         }
821
 
822
         public void paintComponent(Graphics g) {
823
 
824
            Insets insets = getInsets();
825
            Dimension size = getSize();
826
 
827
            Graphics2D g2d = (Graphics2D)g;
828
 
829
            int minRed = 0;
830
            int minGreen = 0;
831
            int minBlue = 0;
832
 
833
            int maxRed = 0;
834
            int maxGreen = 0;
835
            int maxBlue = 0;
836
 
837
            switch(index) {
838
               case RED_INDEX:
839
                  maxRed = max;
840
                  minRed = min;
841
                  break;
842
               case GREEN_INDEX:
843
                  maxGreen = max;
844
                  minGreen = min;
845
                  break;
846
               case BLUE_INDEX:
847
                  maxBlue = max;
848
                  minBlue = min;
849
                  break;
850
            }
851
 
852
            Color minColor = new Color(minRed, minGreen, minBlue);
853
            Color maxColor = new Color(maxRed, maxGreen, maxBlue);
854
 
855
            Paint paint = new GradientPaint(insets.left, insets.top, minColor,
856
                                            size.width - insets.right, insets.top, maxColor);
857
            g2d.setPaint(paint);
858
            g2d.fillRect(insets.left, insets.top,
859
                         size.width - insets.left - insets.right,
860
                         size.height - insets.top - insets.bottom);
861
 
862
         }
863
 
864
      }
865
 
866
      private static final class AVRSliderUI extends MetalSliderUI {
867
 
868
         public static final int LEFT = 0;
869
         public static final int RIGHT = 1;
870
 
871
         private int direction;
872
 
873
         public AVRSliderUI(int direction) {
874
            this.direction = direction;
875
            filledSlider = false;
876
         }
877
 
878
         // overridden to not fill in the track
879
         public void paintTrack(Graphics g) {
880
 
881
            boolean drawInverted = this.drawInverted();
882
 
883
            Rectangle paintRect = avrGetPaintTrackRect();
884
            g.translate(paintRect.x, paintRect.y);
885
 
886
            int w = paintRect.width;
887
            int h = paintRect.height;
888
 
889
            g.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
890
            g.drawRect(0, 0, w - 1, h - 1);
891
 
892
            g.translate(-paintRect.x, -paintRect.y);
893
 
894
         }
895
 
896
         // overridden to point the thumb to the left or the right
897
         public void paintThumb(Graphics g) {
898
            Rectangle knobBounds = thumbRect;
899
            if(direction == LEFT) {
900
                g.translate( knobBounds.x, knobBounds.y );
901
                ((Graphics2D)g).rotate(Math.PI, knobBounds.width / 2, knobBounds.height / 2);
902
 
903
                vertThumbIcon.paintIcon( slider, g, 0, 0 );
904
 
905
                ((Graphics2D)g).rotate(-Math.PI, knobBounds.width / 2, knobBounds.height / 2);
906
                g.translate( -knobBounds.x, -knobBounds.y );
907
            } else {
908
                g.translate( knobBounds.x, knobBounds.y );
909
 
910
                vertThumbIcon.paintIcon( slider, g, 0, 0 );
911
 
912
                g.translate( -knobBounds.x, -knobBounds.y );
913
            }
914
         }
915
 
916
         // this method is private in the MetalSliderUI class, so I have recreated
917
         // the method so I can use it
918
         private Rectangle avrGetPaintTrackRect() {
919
            int trackLeft = 0, trackRight = 0, trackTop = 0, trackBottom = 0;
920
            trackBottom = (trackRect.height - 1) - getThumbOverhang();
921
            trackTop = trackBottom - (getTrackWidth() - 1);
922
            trackRight = trackRect.width - 1;
923
            return new Rectangle(trackRect.x + trackLeft, trackRect.y + trackTop,
924
                                 trackRight - trackLeft, trackBottom - trackTop);
925
      }
926
   }
927
 
928
  }
929
 
930
  private final class ConnectionHandler implements ConnectionListener {
931
     public void connected(ConnectionEvent ce) {
932
        sendAction.setEnabled(true);
933
     }
934
 
935
     public void disconnected(ConnectionEvent ce) {
936
        sendAction.setEnabled(false);
937
     }
938
 
939
  }
940
 
941
  private final class DataHandler extends DataAdapter {
942
 
943
     private int[][] colorMap;
944
 
945
     public DataHandler(int[][] colorMap) {
946
        this.colorMap = colorMap;
947
     }
948
 
949
     public void ack() {
950
        AVRSystem.DEVICE.setColorMap(colorMap);
951
        reset();
952
        SwingUtilities.getRootPane(messageP).getGlassPane().setVisible(false);
953
        getRootPane().getGlassPane().setVisible(false);
954
        AVRSystem.DEVICE.removeDataListener(this);
955
     }
956
 
957
     public void nck() {
958
        getRootPane().getGlassPane().setVisible(false);
959
        SwingUtilities.getRootPane(messageP).getGlassPane().setVisible(false);
960
        AVRSystem.DEVICE.removeDataListener(this);
961
        JOptionPane.showMessageDialog(getRootPane(), "Set Color Map NCK Received", "NCK Received", JOptionPane.ERROR_MESSAGE);
962
     }
963
 
964
     public void responseTimerExpired() {
965
        getRootPane().getGlassPane().setVisible(false);
966
        SwingUtilities.getRootPane(messageP).getGlassPane().setVisible(false);
967
        AVRSystem.DEVICE.removeDataListener(this);
968
        JOptionPane.showMessageDialog(messageP.getRootPane(), "Response Timer Expired", "Timer Expired", JOptionPane.ERROR_MESSAGE);
969
     }
970
 
971
  }
972
 
973
 
974
 
975
}