1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.threadlog.ui;
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;
33 import javax.swing.JPanel;
35 public class TimeLineViewer extends JPanel {
37 private static final long serialVersionUID = -7410066541298449720L;
39 public static long TOOL_TIP_DELAY = 500;
40 public static double MIN_GRID_LINE_SEPARATION = 15;
43 List<Lane> lanes = new ArrayList<Lane>();
45 // For panning and zooming
46 double xScale = 100.0;
53 Timer toolTipTimer = new Timer();
54 class ToolTipTask extends TimerTask {
58 System.out.println("show tooltip");
63 public TimeLineViewer() {
64 addMouseListener(new MouseAdapter() {
66 public void mousePressed(MouseEvent e) {
71 addMouseMotionListener(new MouseMotionAdapter() {
73 public void mouseDragged(MouseEvent e) {
76 int diffX = curX - oldX;
77 int diffY = curY - oldY;
81 xOffset -= diffX/xScale;
85 TimerTask toolTipTask;
87 public void mouseMoved(MouseEvent e) {
88 if(toolTipTask != null)
90 toolTipTask = new ToolTipTask();
91 toolTipTimer.schedule(toolTipTask, TOOL_TIP_DELAY);
94 addMouseWheelListener(new MouseWheelListener() {
96 public void mouseWheelMoved(MouseWheelEvent e) {
97 xOffset += e.getX() / xScale;
98 xScale *= Math.pow(1.25, -e.getWheelRotation());
99 xOffset -= e.getX() / xScale;
105 public void addLane(Lane lane) {
109 public void clear() {
113 public void paintIntervals(Graphics2D g) {
114 for(int laneId=0;laneId < lanes.size();++laneId) {
115 Lane lane = lanes.get(laneId);
117 double y = 35.0*laneId - yOffset;
118 double height = 30.0;
120 for(Interval interval : lane.intervals) {
121 double x = (interval.begin - xOffset) * xScale;
122 double width = (interval.end - interval.begin) * xScale;
124 Shape shape = new RoundRectangle2D.Double(x, y, width, height, 10.0, 10.0);
125 g.setColor(Color.WHITE);
127 g.setColor(Color.BLACK);
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));
137 public void paintGrid(Graphics2D g) {
138 Dimension dim = getSize();
140 g.setBackground(Color.WHITE);
141 g.clearRect(0, 0, dim.width, dim.height);
143 double stepsize = 0.001;
144 double majorStepsize;
146 majorStepsize = stepsize * 5.0;
147 if(stepsize * xScale >= MIN_GRID_LINE_SEPARATION)
149 stepsize = majorStepsize;
150 majorStepsize = stepsize * 2.0;
151 if(stepsize * xScale >= MIN_GRID_LINE_SEPARATION)
153 stepsize = majorStepsize;
156 double firstP = Math.ceil(xOffset / stepsize) * stepsize;
157 double lastP = Math.floor((xOffset + dim.width / xScale) / stepsize) * stepsize;
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);
171 g.setColor(Color.LIGHT_GRAY);
172 g.drawLine(x, 14, x, (int)dim.getHeight());
177 public void paint(Graphics _g) {
178 Graphics2D g = (Graphics2D)_g;