001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.lib.profiler.ui.threads;
042:
043: import java.awt.*;
044: import javax.swing.*;
045:
046: /**
047: * @author Jiri Sedlacek
048: */
049: public class CustomTimeLineViewport extends JViewport {
050: //~ Instance fields ----------------------------------------------------------------------------------------------------------
051:
052: private ThreadsPanel viewManager; // view manager for this cell
053: private int paintWidth;
054: private int paintX;
055: private long dataEnd;
056: private long dataStart;
057: private long viewEnd;
058: private long viewStart;
059:
060: //~ Constructors -------------------------------------------------------------------------------------------------------------
061:
062: public CustomTimeLineViewport(ThreadsPanel viewManager) {
063: this .viewManager = viewManager;
064: syncViewVariables();
065: }
066:
067: //~ Methods ------------------------------------------------------------------------------------------------------------------
068:
069: public void paint(Graphics g) {
070: super .paint(g);
071: g.setClip(paintX, getEmptySpaceY(), paintWidth, getHeight()
072: - getEmptySpaceY());
073: paintTimeMarks(g);
074: }
075:
076: private int getEmptySpaceY() {
077: if (getView() == null) {
078: return 0;
079: }
080:
081: return getView().getHeight();
082: }
083:
084: private void paintTimeMarks(Graphics g) {
085: syncViewVariables();
086:
087: if ((viewEnd - viewStart) > 0) {
088: int firstValue = (int) (viewStart - dataStart);
089: int lastValue = (int) (viewEnd - dataStart);
090: float factor = (float) paintWidth
091: / (float) (viewEnd - viewStart);
092: int optimalUnits = TimeLineUtils.getOptimalUnits(factor);
093:
094: int firstMark = Math
095: .max((int) (Math.ceil((double) firstValue
096: / optimalUnits) * optimalUnits), 0);
097:
098: int currentMark = firstMark - optimalUnits;
099:
100: while (currentMark <= (lastValue + optimalUnits)) {
101: if (currentMark >= 0) {
102: float currentMarkRel = currentMark - firstValue;
103: int markPosition = (int) (currentMarkRel * factor);
104: paintTimeTicks(
105: g,
106: (int) (currentMarkRel * factor),
107: (int) ((currentMarkRel + optimalUnits) * factor),
108: TimeLineUtils.getTicksCount(optimalUnits));
109: g.setColor(TimeLineUtils.MAIN_TIMELINE_COLOR);
110: g.drawLine(paintX + markPosition, getEmptySpaceY(),
111: paintX + markPosition, getHeight() - 1);
112: }
113:
114: currentMark += optimalUnits;
115: }
116: }
117: }
118:
119: private void paintTimeTicks(Graphics g, int startPos, int endPos,
120: int count) {
121: float factor = (float) (endPos - startPos) / (float) count;
122:
123: g.setColor(TimeLineUtils.TICK_TIMELINE_COLOR);
124:
125: for (int i = 1; i < count; i++) {
126: int x = startPos + (int) (i * factor);
127: g.drawLine(paintX + x, getEmptySpaceY(), paintX + x,
128: getHeight() - 1);
129: }
130: }
131:
132: private void syncViewVariables() {
133: viewStart = viewManager.getViewStart();
134: viewEnd = viewManager.getViewEnd();
135: dataStart = viewManager.getDataStart();
136: dataEnd = viewManager.getDataEnd();
137: paintWidth = viewManager.getDisplayColumnWidth();
138: paintX = getWidth() - paintWidth;
139: }
140: }
|