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 java.util.HashMap;
045: import java.util.ResourceBundle;
046:
047: /**
048: *
049: * @author Jiri Sedlacek
050: */
051: public class TimeLineUtils {
052: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
053:
054: // -----
055: // I18N String constants
056: private static final ResourceBundle messages = ResourceBundle
057: .getBundle("org.netbeans.lib.profiler.ui.threads.Bundle"); // NOI18N
058: private static final String MINUTES_ABBR = messages
059: .getString("TimeLineUtils_MinutesAbbr"); // NOI18N
060: private static final String HOURS_ABBR = messages
061: .getString("TimeLineUtils_HoursAbbr"); // NOI18N
062: private static final String HOURS_LEGEND_ABBR = messages
063: .getString("TimeLineUtils_HoursLegendAbbr"); // NOI18N
064: private static final String MINUTES_LEGEND_ABBR = messages
065: .getString("TimeLineUtils_MinutesLegendAbbr"); // NOI18N
066: private static final String SECONDS_LEGEND_ABBR = messages
067: .getString("TimeLineUtils_SecondsLegendAbbr"); // NOI18N
068: private static final String MILLISECONDS_LEGEND_ABBR = messages
069: .getString("TimeLineUtils_MillisecondsLegendAbbr"); // NOI18N
070: // -----
071: public static final int MIN_TIMEMARK_STEP = 150; // The minimal distance between two time marks
072: public static final Color BASE_TIMELINE_COLOR = new Color(0, 0, 0);
073: public static final Color MAIN_TIMELINE_COLOR = new Color(150, 150,
074: 150);
075: public static final Color TICK_TIMELINE_COLOR = new Color(230, 230,
076: 230);
077: private static final int TIME_FORMAT_UNKNOWN = -1;
078: private static final int TIME_FORMAT_MILLIS = 10;
079: private static final int TIME_FORMAT_SECONDS = 20;
080: private static final int TIME_FORMAT_MINUTES = 30;
081: private static final int TIME_FORMAT_HOURS = 40;
082: private static final int[] timeUnitsGrid = new int[] { 10, 50, 100,
083: 250, 500, // milliseconds
084: 1000, 5000, 10000, 30000, // seconds
085: 60000, 180000, 300000, 600000, // minutes
086: 3600000, 18000000, 36000000 }; // hours
087: private static final int[] ticksCountGrid = new int[] { 10, 5, 10,
088: 5, 5, 10, 5, 10, 6, 6, 6, 5, 10, 6, 5, 10 };
089: private static final int[] timeUnitsFormat = new int[] {
090: TIME_FORMAT_MILLIS, TIME_FORMAT_MILLIS, TIME_FORMAT_MILLIS,
091: TIME_FORMAT_MILLIS, TIME_FORMAT_MILLIS,
092: TIME_FORMAT_SECONDS, TIME_FORMAT_SECONDS,
093: TIME_FORMAT_SECONDS, TIME_FORMAT_SECONDS,
094: TIME_FORMAT_MINUTES, TIME_FORMAT_MINUTES,
095: TIME_FORMAT_MINUTES, TIME_FORMAT_MINUTES,
096: TIME_FORMAT_HOURS, TIME_FORMAT_HOURS, TIME_FORMAT_HOURS };
097: private static final HashMap timeUnitsToIndex = new HashMap();
098:
099: static {
100: for (int i = 0; i < timeUnitsGrid.length; i++) {
101: timeUnitsToIndex.put(new Integer(timeUnitsGrid[i]),
102: new Integer(i));
103: }
104: }
105:
106: //~ Methods ------------------------------------------------------------------------------------------------------------------
107:
108: public static String getHoursValue(long mark) {
109: // Hours
110: long hours = mark / 3600000;
111:
112: return "" + hours + " " + HOURS_ABBR; // NOI18N
113: }
114:
115: public static String getMillisValue(long mark) {
116: // Hours
117: long hours = mark / 3600000;
118: String sHours = ((hours == 0) ? "" : ("" + hours + ":")); // NOI18N
119: mark = mark % 3600000;
120:
121: // Minutes
122: long minutes = mark / 60000;
123: String sMinutes = (((hours > 0) && (minutes < 10)) ? ("0" + minutes)
124: : ("" + minutes))
125: + ":"; // NOI18N
126: mark = mark % 60000;
127:
128: // Seconds
129: long seconds = mark / 1000;
130: String sSeconds = ((seconds < 10) ? ("0" + seconds)
131: : ("" + seconds))
132: + "."; // NOI18N
133: mark = mark % 1000;
134:
135: // Milliseconds
136: long millis = mark;
137: String sMillis = "" + millis; // NOI18N
138:
139: if (millis < 10) {
140: sMillis = "0" + sMillis; // NOI18N
141: }
142:
143: if (millis < 100) {
144: sMillis = "0" + sMillis; // NOI18N
145: }
146:
147: return sHours + sMinutes + sSeconds + sMillis;
148: }
149:
150: public static String getMinutesValue(long mark) {
151: // Hours
152: long hours = mark / 3600000;
153: String sHours = ((hours == 0) ? "" : ("" + hours + ":")); // NOI18N
154: mark = mark % 3600000;
155:
156: // Minutes
157: long minutes = mark / 60000;
158: String sMinutes = (((hours > 0) && (minutes < 10)) ? ("0" + minutes)
159: : ("" + minutes + " " + MINUTES_ABBR)); // NOI18N
160:
161: return sHours + sMinutes;
162: }
163:
164: public static int getOptimalUnits(float factor) {
165: for (int i = 0; i < (timeUnitsGrid.length - 1); i++) {
166: if ((timeUnitsGrid[i] * factor) >= MIN_TIMEMARK_STEP) {
167: return timeUnitsGrid[i];
168: }
169: }
170:
171: return timeUnitsGrid[timeUnitsGrid.length - 1];
172: }
173:
174: public static String getSecondsValue(long mark) {
175: // Hours
176: long hours = mark / 3600000;
177: String sHours = ((hours == 0) ? "" : ("" + hours + ":")); // NOI18N
178: mark = mark % 3600000;
179:
180: // Minutes
181: long minutes = mark / 60000;
182: String sMinutes = (((hours > 0) && (minutes < 10)) ? ("0" + minutes)
183: : ("" + minutes))
184: + ":"; // NOI18N
185: mark = mark % 60000;
186:
187: // Seconds
188: long seconds = mark / 1000;
189: String sSeconds = ((seconds < 10) ? ("0" + seconds)
190: : ("" + seconds)); // NOI18N
191:
192: return sHours + sMinutes + sSeconds;
193: }
194:
195: public static int getTicksCount(int optimalUnits) {
196: int ticksGridIndex = getUnitsIndex(optimalUnits);
197:
198: if (ticksGridIndex == -1) {
199: return 0;
200: }
201:
202: return ticksCountGrid[ticksGridIndex];
203: }
204:
205: public static String getTimeMarkMillisString(int mark,
206: int optimalUnits) {
207: int format = getTimeUnitsFormat(optimalUnits);
208:
209: if (format != TIME_FORMAT_MILLIS) {
210: return ""; // NOI18N
211: }
212:
213: // Hours
214: mark = mark % 3600000;
215: // Minutes
216: mark = mark % 60000;
217: // Seconds
218: mark = mark % 1000;
219:
220: // Milliseconds
221: int millis = mark;
222: String sMillis = "" + millis; // NOI18N
223:
224: if (millis < 10) {
225: sMillis = "0" + sMillis; // NOI18N
226: }
227:
228: if (millis < 100) {
229: sMillis = "0" + sMillis; // NOI18N
230: }
231:
232: return sMillis;
233: }
234:
235: public static String getTimeMarkNoMillisString(int mark,
236: int optimalUnits) {
237: int format = getTimeUnitsFormat(optimalUnits);
238:
239: if (format == TIME_FORMAT_UNKNOWN) {
240: return ""; // NOI18N
241: }
242:
243: if (format == TIME_FORMAT_MILLIS) {
244: format = TIME_FORMAT_SECONDS;
245: }
246:
247: return getTimeMarkStringFromFormat(mark, format);
248: }
249:
250: public static String getTimeMarkString(int mark, int optimalUnits) {
251: int format = getTimeUnitsFormat(optimalUnits);
252:
253: if (format == TIME_FORMAT_UNKNOWN) {
254: return ""; // NOI18N
255: }
256:
257: return getTimeMarkStringFromFormat(mark, format);
258: }
259:
260: public static String getUnitsLegend(int lastMark, int optimalUnits) {
261: String timeMarkNoMillis = getTimeMarkNoMillisString(lastMark,
262: optimalUnits);
263:
264: if (timeMarkNoMillis.endsWith(MINUTES_ABBR)) {
265: return "[" + MINUTES_LEGEND_ABBR + "]"; // NOI18N
266: }
267:
268: if (timeMarkNoMillis.endsWith(HOURS_ABBR)) {
269: return "[" + HOURS_LEGEND_ABBR + "]"; // NOI18N
270: }
271:
272: String sMillis = ""; // NOI18N
273:
274: if (!getTimeMarkMillisString(lastMark, optimalUnits).equals("")) {
275: sMillis = "." + MILLISECONDS_LEGEND_ABBR; // NOI18N
276: }
277:
278: int hours = lastMark / 3600000;
279:
280: if (hours != 0) {
281: return "[" + HOURS_LEGEND_ABBR + ":" + MINUTES_LEGEND_ABBR
282: + ":" + SECONDS_LEGEND_ABBR + sMillis + "]"; // NOI18N
283: }
284:
285: return "[" + MINUTES_LEGEND_ABBR + ":" + SECONDS_LEGEND_ABBR
286: + sMillis + "]"; // NOI18N
287: }
288:
289: private static String getTimeMarkStringFromFormat(int mark,
290: int format) {
291: switch (format) {
292: case TIME_FORMAT_MILLIS:
293: return getMillisValue(mark);
294: case TIME_FORMAT_SECONDS:
295: return getSecondsValue(mark);
296: case TIME_FORMAT_MINUTES:
297: return getMinutesValue(mark);
298: case TIME_FORMAT_HOURS:
299: return getHoursValue(mark);
300: default:
301: return ""; // NOI18N
302: }
303: }
304:
305: private static int getTimeUnitsFormat(int optimalUnits) {
306: int timeUnitsFormatIndex = getUnitsIndex(optimalUnits);
307:
308: if (timeUnitsFormatIndex == -1) {
309: return TIME_FORMAT_UNKNOWN;
310: }
311:
312: return timeUnitsFormat[timeUnitsFormatIndex];
313: }
314:
315: private static int getUnitsIndex(int optimalUnits) {
316: Object oTimeUnitsFormatIndex = timeUnitsToIndex
317: .get(new Integer(optimalUnits));
318:
319: if (oTimeUnitsFormatIndex == null) {
320: return -1;
321: }
322:
323: return ((Integer) oTimeUnitsFormatIndex).intValue();
324: }
325: }
|