Mercurial > hg > truffle
comparison visualizer/Util/src/com/sun/hotspot/igv/util/RangeSlider.java @ 4512:015fb895586b
Moved visualizer to new directory.
author | Thomas Wuerthinger <thomas.wuerthinger@oracle.com> |
---|---|
date | Tue, 07 Feb 2012 22:41:09 +0100 |
parents | |
children | ab4a47b6a187 |
comparison
equal
deleted
inserted
replaced
4511:6cb549627941 | 4512:015fb895586b |
---|---|
1 /* | |
2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. Oracle designates this | |
8 * particular file as subject to the "Classpath" exception as provided | |
9 * by Oracle in the LICENSE file that accompanied this code. | |
10 * | |
11 * This code is distributed in the hope that it will be useful, but WITHOUT | |
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 * version 2 for more details (a copy is included in the LICENSE file that | |
15 * accompanied this code). | |
16 * | |
17 * You should have received a copy of the GNU General Public License version | |
18 * 2 along with this work; if not, write to the Free Software Foundation, | |
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
20 * | |
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
22 * or visit www.oracle.com if you need additional information or have any | |
23 * questions. | |
24 */ | |
25 package com.sun.hotspot.igv.util; | |
26 | |
27 import com.sun.hotspot.igv.data.ChangedListener; | |
28 import java.awt.*; | |
29 import java.awt.event.MouseEvent; | |
30 import java.awt.event.MouseListener; | |
31 import java.awt.event.MouseMotionListener; | |
32 import java.util.List; | |
33 import javax.swing.JComponent; | |
34 | |
35 public final class RangeSlider extends JComponent { | |
36 | |
37 public static final int BAR_THICKNESS = 2; | |
38 public static final int BAR_CIRCLE_SIZE = 9; | |
39 public static final int MOUSE_ENDING_OFFSET = 3; | |
40 public static final Color BACKGROUND_COLOR = Color.white; | |
41 public static final Color BAR_COLOR = Color.black; | |
42 public static final Color BAR_SELECTION_COLOR = new Color(255, 0, 0, 120); | |
43 public static final Color TEXT_SELECTION_COLOR = new Color(200, 200, 200, 255); | |
44 public static final int ITEM_HEIGHT = 30; | |
45 public static final int ITEM_WIDTH = 30; | |
46 private RangeSliderModel model; | |
47 private Point startPoint; | |
48 private RangeSliderModel tempModel; | |
49 private Point lastMouseMove; | |
50 | |
51 public RangeSlider(RangeSliderModel newModel) { | |
52 this.addMouseMotionListener(mouseMotionListener); | |
53 this.addMouseListener(mouseListener); | |
54 setModel(newModel); | |
55 } | |
56 | |
57 private RangeSliderModel getPaintingModel() { | |
58 if (tempModel != null) { | |
59 return tempModel; | |
60 } | |
61 return model; | |
62 } | |
63 | |
64 @Override | |
65 public Dimension getPreferredSize() { | |
66 if (getPaintingModel() != null) { | |
67 Graphics g = this.getGraphics(); | |
68 int maxWidth = 0; | |
69 List<String> list = getPaintingModel().getPositions(); | |
70 for (int i = 0; i < list.size(); i++) { | |
71 String curS = list.get(i); | |
72 if (curS != null && curS.length() > 0) { | |
73 FontMetrics metrics = g.getFontMetrics(); | |
74 Rectangle bounds = metrics.getStringBounds(curS, g).getBounds(); | |
75 maxWidth = Math.max(maxWidth, (int) bounds.getWidth()); | |
76 } | |
77 } | |
78 return new Dimension(maxWidth + ITEM_WIDTH, ITEM_HEIGHT * list.size()); | |
79 } | |
80 return super.getPreferredSize(); | |
81 } | |
82 private ChangedListener<RangeSliderModel> modelChangedListener = new ChangedListener<RangeSliderModel>() { | |
83 | |
84 @Override | |
85 public void changed(RangeSliderModel source) { | |
86 update(); | |
87 } | |
88 }; | |
89 | |
90 private void update() { | |
91 this.repaint(); | |
92 } | |
93 | |
94 private Rectangle getItemBounds(int index) { | |
95 Rectangle r = new Rectangle(); | |
96 r.width = ITEM_WIDTH; | |
97 r.height = ITEM_HEIGHT; | |
98 r.x = 0; | |
99 r.y = ITEM_HEIGHT * index; | |
100 return r; | |
101 } | |
102 | |
103 @Override | |
104 public void paint(Graphics g) { | |
105 super.paint(g); | |
106 Graphics2D g2 = (Graphics2D) g; | |
107 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, | |
108 RenderingHints.VALUE_ANTIALIAS_ON); | |
109 int width = getWidth(); | |
110 int height = getHeight(); | |
111 | |
112 g2.setColor(BACKGROUND_COLOR); | |
113 g2.fillRect(0, 0, width, height); | |
114 | |
115 // Nothing to paint? | |
116 if (getPaintingModel() == null || getPaintingModel().getPositions().isEmpty()) { | |
117 return; | |
118 } | |
119 | |
120 paintSelected(g2); | |
121 paintBar(g2); | |
122 | |
123 } | |
124 | |
125 private void fillRect(Graphics2D g, int startX, int startY, int endY, int thickness) { | |
126 g.fillRect(startX - thickness / 2, startY, thickness, endY - startY); | |
127 } | |
128 | |
129 private void paintBar(Graphics2D g) { | |
130 List<String> list = getPaintingModel().getPositions(); | |
131 | |
132 g.setColor(BAR_COLOR); | |
133 Rectangle firstItemBounds = getItemBounds(0); | |
134 Rectangle lastItemBounds = getItemBounds(list.size() - 1); | |
135 fillRect(g, (int) firstItemBounds.getCenterX(), (int) firstItemBounds.getCenterY(), (int) lastItemBounds.getCenterY(), BAR_THICKNESS); | |
136 | |
137 for (int i = 0; i < list.size(); i++) { | |
138 Rectangle curItemBounds = getItemBounds(i); | |
139 g.setColor(getPaintingModel().getColors().get(i)); | |
140 g.fillOval((int) curItemBounds.getCenterX() - BAR_CIRCLE_SIZE / 2, (int) curItemBounds.getCenterY() - BAR_CIRCLE_SIZE / 2, BAR_CIRCLE_SIZE, BAR_CIRCLE_SIZE); | |
141 g.setColor(Color.black); | |
142 g.drawOval((int) curItemBounds.getCenterX() - BAR_CIRCLE_SIZE / 2, (int) curItemBounds.getCenterY() - BAR_CIRCLE_SIZE / 2, BAR_CIRCLE_SIZE, BAR_CIRCLE_SIZE); | |
143 | |
144 String curS = list.get(i); | |
145 if (curS != null && curS.length() > 0) { | |
146 FontMetrics metrics = g.getFontMetrics(); | |
147 Rectangle bounds = metrics.getStringBounds(curS, g).getBounds(); | |
148 g.setColor(Color.black); | |
149 g.drawString(curS, curItemBounds.x + curItemBounds.width, (int) curItemBounds.getCenterY() + bounds.height / 2 - 2); | |
150 } | |
151 } | |
152 | |
153 } | |
154 | |
155 private void paintSelected(Graphics2D g) { | |
156 List<String> list = getPaintingModel().getPositions(); | |
157 for (int i = 0; i < list.size(); i++) { | |
158 Rectangle curItemBounds = getItemBounds(i); | |
159 if (lastMouseMove != null && curItemBounds.y <= lastMouseMove.y && curItemBounds.y + curItemBounds.height > lastMouseMove.y) { | |
160 g.setColor(TEXT_SELECTION_COLOR); | |
161 g.fillRect(0, curItemBounds.y, getWidth(), curItemBounds.height); | |
162 } | |
163 } | |
164 final Rectangle barBounds = getBarBounds(); | |
165 | |
166 g.setColor(BAR_SELECTION_COLOR); | |
167 g.fill(barBounds); | |
168 } | |
169 | |
170 private Rectangle getBarBounds() { | |
171 final Rectangle startItemBounds = getItemBounds(getPaintingModel().getFirstPosition()); | |
172 final Rectangle endItemBounds = getItemBounds(getPaintingModel().getSecondPosition()); | |
173 int startY = startItemBounds.y; | |
174 int endY = endItemBounds.y + endItemBounds.height; | |
175 return new Rectangle(0, startY, getWidth(), endY - startY); | |
176 } | |
177 | |
178 private int getIndexFromPosition(int y) { | |
179 for (int i = 0; i < getPaintingModel().getPositions().size() - 1; i++) { | |
180 Rectangle bounds = getItemBounds(i); | |
181 if (bounds.y <= y && bounds.y + bounds.height >= y) { | |
182 return i; | |
183 } | |
184 } | |
185 return getPaintingModel().getPositions().size() - 1; | |
186 } | |
187 private final MouseMotionListener mouseMotionListener = new MouseMotionListener() { | |
188 | |
189 @Override | |
190 public void mouseDragged(MouseEvent e) { | |
191 if (startPoint != null) { | |
192 int startIndex = getIndexFromPosition(startPoint.y); | |
193 int curIndex = getIndexFromPosition(e.getPoint().y); | |
194 tempModel.setPositions(startIndex, curIndex); | |
195 } | |
196 } | |
197 | |
198 @Override | |
199 public void mouseMoved(MouseEvent e) { | |
200 lastMouseMove = e.getPoint(); | |
201 update(); | |
202 } | |
203 }; | |
204 private final MouseListener mouseListener = new MouseListener() { | |
205 | |
206 @Override | |
207 public void mouseClicked(MouseEvent e) { | |
208 if (model != null) { | |
209 int index = getIndexFromPosition(e.getPoint().y); | |
210 model.setPositions(index, index); | |
211 } | |
212 } | |
213 | |
214 @Override | |
215 public void mousePressed(MouseEvent e) { | |
216 if (model != null) { | |
217 int index = getIndexFromPosition(e.getPoint().y); | |
218 startPoint = e.getPoint(); | |
219 tempModel = model.copy(); | |
220 tempModel.getChangedEvent().addListener(modelChangedListener); | |
221 tempModel.setPositions(index, index); | |
222 } | |
223 } | |
224 | |
225 @Override | |
226 public void mouseReleased(MouseEvent e) { | |
227 if (tempModel != null) { | |
228 model.setPositions(tempModel.getFirstPosition(), tempModel.getSecondPosition()); | |
229 tempModel = null; | |
230 startPoint = null; | |
231 } | |
232 } | |
233 | |
234 @Override | |
235 public void mouseEntered(MouseEvent e) { | |
236 } | |
237 | |
238 @Override | |
239 public void mouseExited(MouseEvent e) { | |
240 lastMouseMove = null; | |
241 repaint(); | |
242 } | |
243 }; | |
244 | |
245 public RangeSliderModel getModel() { | |
246 return model; | |
247 } | |
248 | |
249 public void setModel(RangeSliderModel newModel) { | |
250 if (newModel != this.model) { | |
251 if (this.model != null) { | |
252 this.model.getChangedEvent().removeListener(modelChangedListener); | |
253 } | |
254 this.model = newModel; | |
255 if (newModel != null) { | |
256 newModel.getChangedEvent().addListener(modelChangedListener); | |
257 } | |
258 this.tempModel = null; | |
259 update(); | |
260 } | |
261 } | |
262 } |