]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.threadlog/src/org/simantics/threadlog/ui/TimeLineViewer.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.threadlog / src / org / simantics / threadlog / ui / TimeLineViewer.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.threadlog.ui;
13
14 import java.awt.Color;
15 import java.awt.Dimension;
16 import java.awt.Graphics;
17 import java.awt.Graphics2D;
18 import java.awt.Shape;
19 import java.awt.event.MouseAdapter;
20 import java.awt.event.MouseEvent;
21 import java.awt.event.MouseMotionAdapter;
22 import java.awt.event.MouseWheelEvent;
23 import java.awt.event.MouseWheelListener;
24 import java.awt.geom.Rectangle2D;
25 import java.awt.geom.RoundRectangle2D;
26 import java.text.DecimalFormat;
27 import java.text.Format;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Timer;
31 import java.util.TimerTask;
32
33 import javax.swing.JPanel;
34
35 public class TimeLineViewer extends JPanel {
36
37     private static final long serialVersionUID = -7410066541298449720L;
38     
39     public static long TOOL_TIP_DELAY = 500;
40         public static double MIN_GRID_LINE_SEPARATION = 15;
41         
42         // Time line data
43         List<Lane> lanes = new ArrayList<Lane>();
44         
45         // For panning and zooming
46         double xScale = 100.0;
47         double xOffset = 0.0;
48         double yOffset = 0.0;
49         int oldX;
50         int oldY;
51         
52         // Tool tips
53         Timer toolTipTimer = new Timer();
54         class ToolTipTask extends TimerTask {
55
56                 @Override
57                 public void run() {
58                         System.out.println("show tooltip");                     
59                 }
60                 
61         }
62         
63         public TimeLineViewer() {
64                 addMouseListener(new MouseAdapter() {
65                 @Override
66                 public void mousePressed(MouseEvent e) {
67                         oldX = e.getX();
68                         oldY = e.getY();
69                 }
70                 });
71         addMouseMotionListener(new MouseMotionAdapter() {               
72                 @Override
73                 public void mouseDragged(MouseEvent e) {
74                         int curX = e.getX();
75                         int curY = e.getY();
76                         int diffX = curX - oldX;
77                         int diffY = curY - oldY;
78                         oldX = curX;
79                         oldY = curY;
80                         
81                         xOffset -= diffX/xScale;
82                         yOffset -= diffY;
83                         repaint();
84                 }
85                 TimerTask toolTipTask;
86                         @Override
87                 public void mouseMoved(MouseEvent e) {
88                                 if(toolTipTask != null)
89                                         toolTipTask.cancel();
90                                 toolTipTask = new ToolTipTask();
91                         toolTipTimer.schedule(toolTipTask, TOOL_TIP_DELAY);
92                 }
93         });
94         addMouseWheelListener(new MouseWheelListener() {                        
95                         @Override
96                         public void mouseWheelMoved(MouseWheelEvent e) {
97                                 xOffset += e.getX() / xScale;
98                                 xScale *= Math.pow(1.25, -e.getWheelRotation());
99                                 xOffset -= e.getX() / xScale;
100                                 repaint();
101                         }
102                 });
103         }
104         
105         public void addLane(Lane lane) {
106                 lanes.add(lane);
107         }
108         
109         public void clear() {
110                 lanes.clear();
111         }
112         
113         public void paintIntervals(Graphics2D g) {              
114                 for(int laneId=0;laneId < lanes.size();++laneId) {
115                         Lane lane = lanes.get(laneId);
116                         
117                         double y = 35.0*laneId - yOffset;
118                         double height = 30.0;
119                         
120                         for(Interval interval : lane.intervals) {
121                                 double x = (interval.begin - xOffset) * xScale; 
122                                 double width = (interval.end - interval.begin) * xScale;
123                                 
124                                 Shape shape = new RoundRectangle2D.Double(x, y, width, height, 10.0, 10.0);
125                                 g.setColor(Color.WHITE);
126                                 g.fill(shape);
127                                 g.setColor(Color.BLACK);
128                                 g.draw(shape);
129                                 
130                                 Rectangle2D bounds = g.getFontMetrics().getStringBounds(interval.text, g);
131                                 if(bounds.getWidth() < width)
132                                         g.drawString(interval.text, (float)(x+0.5*(width-bounds.getWidth())), (float)(y+20.0));
133                         }
134                 }
135         }
136         
137         public void paintGrid(Graphics2D g) {
138                 Dimension dim = getSize();
139                 
140                 g.setBackground(Color.WHITE);
141                 g.clearRect(0, 0, dim.width, dim.height);
142                 
143                 double stepsize = 0.001;
144                 double majorStepsize;
145                 while(true) {
146                         majorStepsize = stepsize * 5.0;
147                         if(stepsize * xScale >= MIN_GRID_LINE_SEPARATION)
148                                 break;
149                         stepsize = majorStepsize;
150                         majorStepsize = stepsize * 2.0;
151                         if(stepsize * xScale >= MIN_GRID_LINE_SEPARATION)
152                                 break;
153                         stepsize = majorStepsize;
154                 }
155                 
156                 double firstP = Math.ceil(xOffset / stepsize) * stepsize;
157                 double lastP = Math.floor((xOffset + dim.width / xScale) / stepsize) * stepsize;
158                 if(firstP < 0.0)
159                         firstP = 0.0;
160
161                 Format format = new DecimalFormat();
162                 for(double p = firstP;p <= lastP; p+=stepsize) {
163                         int x = (int)((p - xOffset) * xScale);
164                         if(Math.abs(p/majorStepsize - Math.round(p/majorStepsize)) < 1e-3) {
165                                 g.setColor(Color.BLACK);
166                                 String text = format.format(p);
167                                 Rectangle2D bounds = g.getFontMetrics().getStringBounds(text, g);
168                                 g.drawString(text, (float)(x-bounds.getWidth()*0.5), 12.0f);                            
169                         }
170                         else
171                                 g.setColor(Color.LIGHT_GRAY);
172                         g.drawLine(x, 14, x, (int)dim.getHeight());
173                 }
174         }
175         
176         @Override
177         public void paint(Graphics _g) {
178                 Graphics2D g = (Graphics2D)_g;  
179                                 
180                 paintGrid(g);
181                 paintIntervals(g);
182         }
183         
184 }