/
TimelineTopComponent.java
1077 lines (927 loc) · 33.5 KB
/
TimelineTopComponent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package jp.atr.dni.bmi.desktop.timeline;
import com.jogamp.opengl.util.Animator;
import com.jogamp.opengl.util.awt.TextRenderer;
import com.jogamp.opengl.util.gl2.GLUT;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.nio.DoubleBuffer;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.TreeSet;
import java.util.logging.Logger;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.GLU;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import jp.atr.dni.bmi.desktop.model.Channel;
import jp.atr.dni.bmi.desktop.model.Workspace;
import jp.atr.dni.bmi.desktop.neuroshareutils.AnalogInfo;
import jp.atr.dni.bmi.desktop.neuroshareutils.ElemType;
import jp.atr.dni.bmi.desktop.neuroshareutils.Entity;
import jp.atr.dni.bmi.desktop.timeline.model.ViewerChannel;
import jp.atr.dni.bmi.desktop.workingfileutils.CSVReader;
import jp.atr.dni.bmi.desktop.workingfileutils.TSData;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
import org.openide.util.ImageUtilities;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.util.Lookup;
/**
* Top component which displays something.
*/
@ConvertAsProperties(dtd = "-//jp.atr.dni.bmi.desktop.timeline//timeline//EN",
autostore = false)
public final class TimelineTopComponent extends TopComponent implements PropertyChangeListener, GLEventListener/*implements LookupListener */ {
private boolean SNAP_TO_GRID = false;
private boolean SHOW_GRID;
/** the x translation of the map */
private double translationX = 0;
/** the y translation of the map */
private double translationY = 0;
/** the scale of the map */
private double scale = 1.0;
/** the transform for virtual to screen coordinates */
private AffineTransform transform = new AffineTransform();
/** the transform for screen to virtual coordinates */
private AffineTransform inverseTransform = new AffineTransform();
private GLCanvas glCanvas;
private GLUT glut;
private GLU glu;
private TextRenderer renderer;
private double compression = 1;
private static TimelineTopComponent instance;
/** path to the icon used by the component and its open action */
static final String ICON_PATH = "jp/atr/dni/bmi/desktop/timeline/graphPrev.png";
private static final String PREFERRED_ID = "TimelineTopComponent";
private DoubleBuffer[] colors;
private Lookup.Result result = null;
private ModeHandler handler;
private ArrayList<ViewerChannel> viewerChannels;
private ArrayList<Date> endTimes;
private ArrayList<Channel> channels;
public static final int SCROLLBAR_HEIGHT = 25;
public static final double INCREMENT = .25;
private double dataUpperX;
private double dataUpperY;
private double dataLowerX;
private double dataLowerY;
private Point2D dataLower;
private Point2D dataUpper;
private Date startTime;
private Date endTime;
private long timespan;
private int numEntities;
private double lastY;
public TimelineTopComponent() {
initGL();
SHOW_GRID = true;
setName(NbBundle.getMessage(TimelineTopComponent.class, "CTL_TimelineTopComponent"));
setToolTipText(NbBundle.getMessage(TimelineTopComponent.class, "HINT_TimelineTopComponent"));
setIcon(ImageUtilities.loadImage(ICON_PATH, true));
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
setPreferredSize(new java.awt.Dimension(100, 100));
jPanel1.setBackground(new java.awt.Color(255, 0, 51));
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(445, 445, 445)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(121, 121, 121))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(82, 82, 82)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(58, 58, 58))
);
}// </editor-fold>//GEN-END:initComponents
/**
* code to initialize the openGL timeline
*/
private void initGL() {
setPreferredSize(new java.awt.Dimension(100, 100));
GLProfile.initSingleton(false);
GLProfile glp = GLProfile.getDefault();
GLCapabilities caps = new GLCapabilities(glp);
scale = .05;
renderer = new TextRenderer(new Font("SansSerif", Font.BOLD, 12));
setGlCanvas(new GLCanvas(caps));
viewerChannels = new ArrayList<ViewerChannel>();
getGlCanvas().addGLEventListener(this);
handler = new ModeHandler(this);
getGlCanvas().addMouseListener(handler);
getGlCanvas().addMouseMotionListener(handler);
getGlCanvas().addMouseWheelListener(handler);
getGlCanvas().addKeyListener(handler);
// glCanvas.addMouseListener(new MouseListener() {
//
// @Override
// public void mouseClicked(MouseEvent me) {
// }
//
// @Override
// public void mousePressed(MouseEvent me) {
// // update the virtual points
// screenCurrentPoint = me.getPoint();
// screenPickedPoint = screenCurrentPoint;
// currentPoint = getVirtualCoordinates(me.getX(), me.getY());
// pickedPoint = currentPoint;
// if (previousPoint == null) {
// screenPreviousPoint = screenCurrentPoint;
// previousPoint = currentPoint;
// }
// }
//
// @Override
// public void mouseReleased(MouseEvent me) {
// }
//
// @Override
// public void mouseEntered(MouseEvent me) {
// }
//
// @Override
// public void mouseExited(MouseEvent me) {
// }
// });
// glCanvas.addMouseMotionListener(new MouseMotionListener() {
//
// @Override
// public void mouseDragged(MouseEvent me) {
// Point2D currentPoint = me.getPoint();// getVirtualCoordinates(arg0.getX(),
// // arg0.getY());
// // getVirtualCoordinates(arg0.getX(), arg0.getY());
//
// // left button performs an action or drags the canvas
// if ((me.getModifiers() & MouseEvent.BUTTON1_MASK) > 0) {
// double dx = currentPoint.getX() - previousPoint.getX();
// double dy = previousPoint.getY() - currentPoint.getY();
// // System.out.println("dx: " + dx + "\tdy: " + dy);
// translationX += dx;
// translationY += dy;
//
// previousPoint = me.getPoint();
//
// buildTransforms();
// }
//
// else if ((me.getModifiers() & MouseEvent.BUTTON3_MASK) > 0) {
//
// }
// }
//
// @Override
// public void mouseMoved(MouseEvent me) {
// previousPoint = me.getPoint();// getVirtualCoordinates(arg0.getX(), arg0.getY());
// }
// });
// glCanvas.addMouseWheelListener(new MouseWheelListener() {
//
// @Override
// public void mouseWheelMoved(MouseWheelEvent mwe) {
// System.out.println("wheel moved");
// if (mwe.getWheelRotation() < 0) {
// setScale(getScale()*SCALE_AMOUNT);
// }
// else if (mwe.getWheelRotation() > 0) {
// setScale(getScale()/SCALE_AMOUNT);
// }
// }
// });
Animator animator = new Animator(getGlCanvas());
animator.add(getGlCanvas());
animator.start();
// javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
// BorderLayout layout = new BorderLayout();
// layout.add(getGlCanvas(), BorderLayout.CENTER);
JPanel p = new JPanel();
p.add(getGlCanvas());
this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
this.add(getGlCanvas());
// this.add(getGlCanvas(), BorderLayout.CENTER);
// this.setVisible(true);
// this.setLayout(layout);
// layout.setHorizontalGroup(
// layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addComponent(getGlCanvas(), javax.swing.GroupLayout.DEFAULT_SIZE, 0, Short.MAX_VALUE)));
// layout.setVerticalGroup(
// layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(getGlCanvas(), javax.swing.GroupLayout.DEFAULT_SIZE, 0, Short.MAX_VALUE));
//add a resize listener
// this.addComponentListener(new ComponentAdapter() {
// public void componentResized(ComponentEvent arg0) {
//
// // resize about the center of the scene
// if (size != null) {
// Dimension newSize = Canvas.this.getSize();
// translationX += (newSize.width - size.width)/2.0;
// translationY += (newSize.height - size.height)/2.0;
// }
//
// // update the view transforms when the canvas is resized
// buildTransforms();
// size = Canvas.this.getSize();
// }
// });
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel jPanel1;
// End of variables declaration//GEN-END:variables
/**
* Gets default instance. Do not use directly: reserved for *.settings files only,
* i.e. deserialization routines; otherwise you could get a non-deserialized instance.
* To obtain the singleton instance, use {@link #findInstance}.
*/
public static synchronized TimelineTopComponent getDefault() {
if (instance == null) {
instance = new TimelineTopComponent();
}
return instance;
}
/**
* Obtain the TimelineTopComponent instance. Never call {@link #getDefault} directly!
*/
public static synchronized TimelineTopComponent findInstance() {
TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
if (win == null) {
Logger.getLogger(TimelineTopComponent.class.getName()).warning(
"Cannot find " + PREFERRED_ID + " component. It will not be located properly in the window system.");
return getDefault();
}
if (win instanceof TimelineTopComponent) {
return (TimelineTopComponent) win;
}
Logger.getLogger(TimelineTopComponent.class.getName()).warning(
"There seem to be multiple components with the '" + PREFERRED_ID
+ "' ID. That is a potential source of errors and unexpected behavior.");
return getDefault();
}
@Override
public int getPersistenceType() {
return TopComponent.PERSISTENCE_ALWAYS;
}
private Lookup.Result fileInfos = null;
@Override
public void componentOpened() {
/*fileInfos = Utilities.actionsGlobalContext().lookupResult(GeneralFileInfo.class);
// fileInfos.allItems(); // This means something. THIS IS IMPORTANT.
fileInfos.addLookupListener(this);*/
Workspace.addPropertyChangeListener(this);
}
@Override
public void componentClosed() {
Workspace.removePropertyChangeListener(this);
// fileInfos.removeLookupListener(this);
// fileInfos = null;
//TODO: stop animator
// Add remove listener
Workspace.removePropertyChangeListener(this);
}
void writeProperties(java.util.Properties p) {
// better to version settings since initial version as advocated at
// http://wiki.apidesign.org/wiki/PropertyFiles
p.setProperty("version", "1.0");
// TODO store your settings
}
Object readProperties(java.util.Properties p) {
if (instance == null) {
instance = this;
}
instance.readPropertiesImpl(p);
return instance;
}
private void readPropertiesImpl(java.util.Properties p) {
String version = p.getProperty("version");
// TODO read your settings according to their version
}
@Override
protected String preferredID() {
return PREFERRED_ID;
}
@Override
public void display(GLAutoDrawable drawable) {
// update();
render(drawable);
}
@Override
public void dispose(GLAutoDrawable arg0) {
// TODO Auto-generated method stub
}
@Override
public void init(GLAutoDrawable drawable) {
GL2 gl = (GL2) drawable.getGL();
glu = new GLU();
glut = new GLUT();
// set the drawing parameters
gl.glClearColor(0f, 0f, 0f, 1.0f);
gl.glPointSize(3.0f);
gl.glEnable(GL2.GL_LINE_SMOOTH);
gl.glEnable(GL2.GL_BLEND);
gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL2.GL_ONE_MINUS_SRC_ALPHA);
gl.glHint(GL2.GL_LINE_SMOOTH_HINT, GL2.GL_DONT_CARE);
gl.glLineWidth(1.5f);
drawable.getGL().setSwapInterval(1);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
buildTransforms();
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL2 gl = (GL2) drawable.getGL();
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluOrtho2D(0.0, width, height, 0);
}
/**
* Converts screen coordinates to virtual coordinates.
*
* @param x - the x component of the screen coordinate
* @param y - the y component of the screen coordinate
* @return - the point in virtual coordinates.
*/
public Point2D getVirtualCoordinates(double x, double y) {
return inverseTransform.transform(new Point2D.Double(x, y), null);
}
/**
* Converts virtual coordinates to screen coordinates.
*
* @param x - the x component of the virtual coordinate
* @param y - the y component of the virtual coordinate
* @return - the point in screen coordinates.
*/
public Point2D getScreenCoordinates(double x, double y) {
return transform.transform(new Point2D.Double(x, -y), null);
}
/**
* Rebuilds the view transform and the inverse view transforms.
*/
private void buildTransforms() {
double width = getWidth();
double height = getHeight();
transform = new AffineTransform(1, 0, 0, 1, 0, 0);
transform.translate(0.5 * width, 0.5 * height);
transform.scale(scale, scale);
transform.translate(translationX - width / 2.0, translationY - height / 2.0);
try {
inverseTransform = transform.createInverse();
} catch (Exception e) {
}
}
/**
* Order to draw:
* 1) grid
* 2) data
* 3) labels
* 4) timeline
*
* @param drawable
*/
private void render(GLAutoDrawable drawable) {
int width = getWidth();
int height = getHeight();
GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
gl.glClearColor(1, 1, 1, 1);
glCanvas.setSize(width, height);
if (channels == null || endTime == null) {
return;
}
gl.glMatrixMode(GL2.GL_PROJECTION);
// gl.glLoadIdentity();
// gl.glOrtho(0, 640, 480, 0, -1, 1);
// gl.glMatrixMode(GL2.GL_MODELVIEW);
// if (SHOW_GRID){
// gl.glLoadIdentity();
// }
//
// gl.glLoadIdentity();
// gl.glTranslated(translationX / (width*.5), translationY / (height*.5), 0);
// gl.glScaled(scale, scale, 0);
// gl.glLoadIdentity();
//glu.gluOrtho2D (0,
// getWidth(),
// 0,
// getHeight());
// gl.glViewport(0, 0, width, height);//TODO: look into this some more
// System.out.println("width: " + width + "\theight: " + height);
int timeMult = 500;
gl.glColor3i(0, 0, 0);
numEntities = channels.size();
//Draw data
gl.glLineWidth(1);
gl.glBegin(GL.GL_LINES);
double yOffset = 0;
double maxX = 0;
Point2D minPoint = getVirtualCoordinates(0, 0);
Point2D maxPoint = getVirtualCoordinates(getWidth(), getHeight());
//draw data
for (Channel c : channels) {
Entity e = c.getEntity();
if (e.getTag().getElemType() == ElemType.ENTITY_ANALOG) {
AnalogInfo ai = (AnalogInfo) e;
if (ai == null) {
continue;
}
double timeIncrement = (1.0 / (ai.getSampleRate()) * timeMult);
double normalizer = Math.max(Math.abs(ai.getMaxVal()), Math.abs(ai.getMinVal()));
double subtractor = 0;
if (ai.getMinVal() > 0) {
subtractor = ai.getMinVal();
normalizer -= subtractor;
} else if (ai.getMaxVal() < 0) {
subtractor = -ai.getMaxVal();
normalizer -= subtractor;
}
double xVal = 0;
CSVReader cr = new CSVReader();
if (c.getChannelType().equals("TS")) {
// Get TSData from the WorkingFile to display.
TSData tSData = cr.getTSData(c.getWorkingFilePath());
ArrayList<Double> vals = tSData.getAllValues().get(0);
double entityTime = vals.size() / timeIncrement;
double lastX = 0;
lastY = ((vals.get(0) - subtractor) / normalizer) - yOffset * 5;
for (int i = 0; i < vals.size(); i++) {
if (i % 2 == 0) {
Point2D p = getScreenCoordinates(lastX, lastY);
gl.glVertex2d(p.getX(), p.getY());
} else {
lastY = ((vals.get(i) - subtractor) / normalizer) - yOffset * 5;
Point2D p = getScreenCoordinates(xVal, lastY);
gl.glVertex2d(p.getX(), p.getY());
}
lastX = xVal;
xVal += timeIncrement;
}
if (xVal > maxX) {
maxX = xVal;
}
yOffset++;
}
}
}
gl.glEnd();
lastY = (yOffset - 1) * 5 - 1;
// endTime.setTime(endTime.getTime() + (long) maxX);
// timespan = endTime.getTime() - startTime.getTime();
boolean showMin = timespan > 60000, showHour = timespan > 360000;
width -= SCROLLBAR_HEIGHT;
height -= SCROLLBAR_HEIGHT;
//Draw Y-axis labels
for (int i = 0; i < numEntities; i++) {
//Draw label
Point2D p = getScreenCoordinates(0, -5 * i);
float tSize = (float) (getScale() + (ERROR) * 0.0125f);
if (tSize > .15) {
tSize = .15f;
}
Entity e = channels.get(i).getEntity();
String probeInfo = "";
if (e instanceof AnalogInfo) {
AnalogInfo aE = (AnalogInfo) e;
probeInfo = aE.getProbeInfo();
}
drawTextUnscaled(gl, e.getEntityInfo().getEntityLabel() + "-" + probeInfo, SCROLLBAR_HEIGHT, p.getY(), tSize, 2.0f);
}
//Calculate screen area for data
dataLower = getScreenCoordinates(0, 0);
dataUpper = getScreenCoordinates(timespan, -lastY);
double diffX = dataUpper.getX() - dataLower.getX();
double diffY = dataUpper.getY() - dataLower.getY();
double lowerDiffX = Math.abs(SCROLLBAR_HEIGHT - dataLower.getX());
double upperDiffX = Math.abs(dataUpper.getX() - width);
dataLowerX = dataLower.getX() < 0 ? lowerDiffX * (width / diffX) + 0 : 0;
dataUpperX = dataUpper.getX() > width ? width - (upperDiffX * (width / diffX)) : width;
double lowerDiffY = Math.abs(0 - dataLower.getY());
double upperDiffY = Math.abs(dataUpper.getY() - height);
dataLowerY = dataLower.getY() < 0 ? lowerDiffY * (height / diffY) : 0;
dataUpperY = dataUpper.getY() > height ? height - dataLowerY - upperDiffY * (height / diffY) : height - dataLowerY;
double incr = Math.abs(getScreenCoordinates(200, 0).getX() - dataLower.getX());
incr = (200 / incr) * 200;
if (incr < 1) {
incr = 1;
}
//Draw X-axis labels
for (int i = 0; i < timespan; i += incr) {
Point2D p = getScreenCoordinates(i, 0);
Date currDate = new Date(startTime.getTime() + i);
String ms = currDate.getTime() + "";
ms = ms.length() > 3 ? ms.substring(ms.length() - 3) : ms;
drawTextUnscaled(gl, (showHour ? currDate.getHours() + ":" : "") + (showMin ? currDate.getMinutes() + ":" : "") + currDate.getSeconds() + ":" + ms, p.getX(), height, 0.15f, 2f);
drawVerticalLine(gl, p.getX());
if (i + incr >= timespan) {
i += incr;
p = getScreenCoordinates(i, 0);
currDate = new Date(startTime.getTime() + i);
ms = currDate.getTime() + "";
ms = ms.length() > 3 ? ms.substring(ms.length() - 3) : ms;
drawTextUnscaled(gl, (showHour ? currDate.getHours() + ":" : "") + (showMin ? currDate.getMinutes() + ":" : "") + currDate.getSeconds() + ":" + ms, p.getX(), height, 0.15f, 2f);
drawVerticalLine(gl, p.getX());
break;
}
}
//Draw bottom time scroller
drawHorizontalTimelineScroller(gl, dataLowerX + SCROLLBAR_HEIGHT, height, dataUpperX - dataLowerX);
drawVerticalTimelineScroller(gl, SCROLLBAR_HEIGHT, dataLowerY, dataUpperY);
}
public void drawVerticalLine(GL2 gl, double x) {
gl.glPushMatrix();
gl.glLineWidth(1);
gl.glTranslated(x, 0, 0);
gl.glColor4f(1, 0, 0, .6f);
gl.glBegin(GL2.GL_LINES);
gl.glVertex2d(0, 0);
gl.glVertex2d(0, getHeight());
gl.glEnd();
gl.glPopMatrix();
}
public void drawTextUnscaled(GL2 gl, String text, double x, double y, float size, float width) {
gl.glColor3i(0, 0, 0);
gl.glPushMatrix();
gl.glTranslated(x, y, 0);
gl.glScalef(size, -size, 0.0f);
gl.glLineWidth((float) (width));
glut.glutStrokeString(GLUT.STROKE_ROMAN, text);
gl.glPopMatrix();
}
/**
* Utility function for drawing text
*
* @param gl - the JOGL context
* @param text 0 the text to draw
* @param x - the x position
* @param y - the y position
* @param size - the size of the text
* @param width - the width of the letters
*/
public void drawText(GL2 gl, String text, double x, double y, float size, float width) {
// gl.glPushMatrix();
//gl.glTranslated(x, y, 0);
// gl.glScalef(size, size, 0.0f);
// renderer.beginRendering(glCanvas.getWidth(), glCanvas.getHeight());
// // optionally set the color
// renderer.setColor(1.0f, 0.2f, 0.2f, 0.8f);
// renderer.draw("Text to draw", x, y);
// // ... more draw commands, color changes, etc.
// renderer.endRendering();
// gl.glPopMatrix();
gl.glColor3i(0, 0, 0);
gl.glPushMatrix();
gl.glTranslated(x, y, 0);
size = (float) (2f * (scale * size));
// gl.glRotatef(angle, 0, 0, 1);
gl.glScalef(size, -size, 0.0f);
gl.glLineWidth(width);
glut.glutStrokeString(GLUT.STROKE_ROMAN, text);
gl.glPopMatrix();
}
public void drawHorizontalTimelineScroller(GL2 gl, double x, double y, double x2) {
gl.glPushMatrix();
gl.glTranslated(x, y, 0);
gl.glColor4f(1, 0, 0, .7f);
gl.glBegin(GL2.GL_QUADS);
gl.glVertex3d(0, SCROLLBAR_HEIGHT, 0);
gl.glVertex3d(0, -1, 0);
gl.glVertex3d(x2, -1, 0);
gl.glVertex3d(x2, SCROLLBAR_HEIGHT, 0);
gl.glEnd();
gl.glPopMatrix();
}
public void drawVerticalTimelineScroller(GL2 gl, double x, double y, double y2) {
gl.glPushMatrix();
gl.glTranslated(x, y, 0);
gl.glColor4f(1, 0, 0, .7f);
gl.glBegin(GL2.GL_QUADS);
gl.glVertex3d(-SCROLLBAR_HEIGHT, 0, 0);
gl.glVertex3d(-1, 0, 0);
gl.glVertex3d(-1, y2, 0);
gl.glVertex3d(-SCROLLBAR_HEIGHT, y2, 0);
gl.glEnd();
gl.glPopMatrix();
}
/**
* Zooms to the boundries of the current map.
*/
public void zoomAll() {
// reset the view transform
translationX = 0;
translationY = 0;
scale = 0.5;
// zoom to the current map
boolean zoom = false;
double minX = Double.MAX_VALUE;
double maxX = -Double.MAX_VALUE;
double minY = Double.MAX_VALUE;
double maxY = -Double.MAX_VALUE;
for (ViewerChannel object : viewerChannels) {
// if (object.getGraphics() != null) {
// float[] coords = object.getGraphics().getCoordinates();
// for (int i=0; i<coords.length; i+=2) {
// if (coords[i] < minX) minX = coords[i];
// if (coords[i] > maxX) maxX = coords[i];
// if (coords[i + 1] < minY) minY = coords[i + 1];
// if (coords[i + 1] > maxY) maxY = coords[i + 1];
// }
//
// zoom = true;
// }
}
if (zoom) {
translationX = -(maxX + minX) / 2 + getWidth() / 2;
translationY = -(maxY + minY) / 2 + getHeight() / 2;
double dx = maxX - minX;
dx = Math.max(dx, maxY - minY);
scale = getHeight() / dx;
}
buildTransforms();
}
/**
* Returns the object at the specified virtual coordiantes or null if no
* object is located at the specified coordinates. If two objects are
* located at the specified point, then the first object is always
* returned.
*
* The method first checks for objects using the shape interface. However,
* the interface does not allow for the selection of lines. If no object
* is found, then the closest object within a small radius is returned.
*
* @param x - the x value in virutal coordinates
* @param y - the y value in virtual coordinates
* @return - the object or null if no object is picked
*/
public ViewerChannel getPickedObject(double x, double y, boolean toggleSelected) {
// sort the visible objects from highest depth to lowest depth
TreeSet<ViewerChannel> objects = new TreeSet<ViewerChannel>(new Comparator<ViewerChannel>() {
public int compare(ViewerChannel arg0, ViewerChannel arg1) {
int depth1 = arg0.getDepth();
int depth2 = arg1.getDepth();
if (depth2 != depth1) {
return depth2 - depth1;
} else {
return arg1.hashCode() - arg0.hashCode();
}
}
});
for (ViewerChannel object : viewerChannels) {
objects.add(object);
}
// check for selection using shapes
ViewerChannel selected = null;
for (ViewerChannel object : objects) {
// if (object.getGraphics().getBounds2D().contains(x, y)) {
// if (object.getGraphics().getFillColor() != null && object.getGraphics().contains(x, y)) {
// if (object.equals(modeHandler.getSelectedObject())) {
// selected = object;
// if (toggleSelected == false) {
// return selected;
// }
// }
// else {
// return object;
// }
// }
// }
}
if (selected != null) {
return selected;
}
// find the closest line
double pickTolerance = 10.0 / scale;
double closest = pickTolerance;
selected = null;
// for (ViewerChannel object : viewerChannels) {
// Rectangle2D bounds = object.getGraphics().getBounds2D();
// if (x > (bounds.getMinX() - pickTolerance)
// && x < (bounds.getMaxX() + pickTolerance)
// && y > (bounds.getMinY() - pickTolerance)
// && y < (bounds.getMaxY() + pickTolerance)) {
//
// float[] coords = object.getGraphics().getCoordinates();
// float px = coords[0];
// float py = coords[1];
// for (int index=2; index<coords.length; index += 2) {
// float nx = coords[index%coords.length];
// float ny = coords[(index + 1)%coords.length];
//
// Line2D line = new Line2D.Float(px, py, nx, ny);
// double dist = line.ptSegDist(new Point2D.Double(x, y));
// if (dist < closest) {
// closest = dist;
// selected = object;
// }
//
// px = nx;
// py = ny;
// }
// }
// }
return selected;
}
/**
* Gets the tranlation in the x direction.
*/
public double getTranslationX() {
return translationX;
}
/**
* Sets the translation in the x direction and rebuilds the transforms.
*/
public void setTranslationX(double translationX) {
this.translationX = translationX;
buildTransforms();
}
/**
* Gets the tranlation in the y direction.
*/
public double getTranslationY() {
return translationY;
}
/**
* Sets the translation in the y direction and rebuilds the transforms.
*/
public void setTranslationY(double translationY) {
this.translationY = translationY;
buildTransforms();
}
/**
* Gets the scale scale.
*/
public double getScale() {
return scale;
}
/**
* Sets the scale and rebuilds the affine transforms.
*/
public void setScale(double scale) {
// if (scale < 0.008 || scale > .2) {
// return;
// }
this.scale = scale;
buildTransforms();
}
/* @Override
public void resultChanged(LookupEvent le) {
// System.out.println("change");
GeneralFileInfo obj = Utilities.actionsGlobalContext().lookup(GeneralFileInfo.class);
if (obj != null && obj.getFileExtention().equals("nsn")) {
// System.out.println("adding data");
fileInfo = obj;
NSReader reader = new NSReader();
NeuroshareFile nsn = reader.readNSFileAllData(fileInfo.getFilePath());
fileInfo.setNsObj(nsn);
}
}*/
/**
* @return the glCanvas
*/
public GLCanvas getGlCanvas() {
return glCanvas;
}
/**
* @param glCanvas the glCanvas to set
*/
public void setGlCanvas(GLCanvas glCanvas) {
this.glCanvas = glCanvas;
}
/**
* @return the dataUpperX
*/
public double getDataUpperX() {
return dataUpperX;
}
/**
* @param dataUpperX the dataUpperX to set
*/
public void setDataUpperX(double dataUpperX) {
this.dataUpperX = dataUpperX;
}
/**
* @return the dataUpperY
*/
public double getDataUpperY() {
return dataUpperY;
}
/**
* @param dataUpperY the dataUpperY to set
*/
public void setDataUpperY(double dataUpperY) {
this.dataUpperY = dataUpperY;
}
/**
* @return the dataLowerX
*/
public double getDataLowerX() {
return dataLowerX;
}
/**
* @param dataLowerX the dataLowerX to set
*/
public void setDataLowerX(double dataLowerX) {
this.dataLowerX = dataLowerX;
}
/**
* @return the dataLowerY
*/
public double getDataLowerY() {
return dataLowerY;
}