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.charts;
042:
043: import java.awt.*;
044: import java.text.SimpleDateFormat;
045: import java.util.Date;
046: import java.util.HashMap;
047: import java.util.ResourceBundle;
048:
049: /**
050: *
051: * @author Jiri Sedlacek
052: */
053: public class DateTimeAxisUtils {
054: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
055:
056: // -----
057: // I18N String constants
058: private static final ResourceBundle messages = ResourceBundle
059: .getBundle("org.netbeans.lib.profiler.ui.charts.Bundle"); // NOI18N
060: private static final String DAYS_FORMAT = messages
061: .getString("DateTimeAxisUtils_DaysFormat"); // NOI18N
062: private static final String HOURS_FORMAT = messages
063: .getString("DateTimeAxisUtils_HoursFormat"); // NOI18N
064: private static final String HOURS_EXT_FORMAT = messages
065: .getString("DateTimeAxisUtils_HoursExtFormat"); // NOI18N
066: private static final String MINUTES_FORMAT = messages
067: .getString("DateTimeAxisUtils_MinutesFormat"); // NOI18N
068: private static final String MINUTES_EXT_FORMAT = messages
069: .getString("DateTimeAxisUtils_MinutesExtFormat"); // NOI18N
070: private static final String SECONDS_FORMAT = messages
071: .getString("DateTimeAxisUtils_SecondsFormat"); // NOI18N
072: private static final String SECONDS_EXT_FORMAT = messages
073: .getString("DateTimeAxisUtils_SecondsExtFormat"); // NOI18N
074: private static final String MILLIS_FORMAT = messages
075: .getString("DateTimeAxisUtils_MillisFormat"); // NOI18N
076: private static final String MILLIS_EXT_FORMAT = messages
077: .getString("DateTimeAxisUtils_MillisExtFormat"); // NOI18N
078: private static final String MILLIS_FULL_FORMAT = messages
079: .getString("DateTimeAxisUtils_MillisFullFormat"); // NOI18N
080: private static final String MILLIS_ONLY_FORMAT = messages
081: .getString("DateTimeAxisUtils_MillisOnlyFormat"); // NOI18N
082: // -----
083: public static final int MIN_TIMEMARK_STEP = 100; // The minimal distance between two time marks
084: public static final Color BASE_TIMELINE_COLOR = new Color(0, 0, 0);
085: public static final Color MAIN_TIMELINE_COLOR = new Color(150, 150,
086: 150);
087: public static final Color TICK_TIMELINE_COLOR = new Color(230, 230,
088: 230);
089: private static final int TIME_FORMAT_UNKNOWN = -1;
090: private static final int TIME_FORMAT_MILLIS = 10;
091: private static final int TIME_FORMAT_SECONDS = 20;
092: private static final int TIME_FORMAT_MINUTES = 30;
093: private static final int TIME_FORMAT_HOURS = 40;
094: private static final int TIME_FORMAT_DAYS = 50;
095: private static final SimpleDateFormat daysDateFormat = new SimpleDateFormat(
096: DAYS_FORMAT);
097: private static final SimpleDateFormat hoursDateFormat = new SimpleDateFormat(
098: HOURS_FORMAT);
099: private static final SimpleDateFormat hoursDateFormatD = new SimpleDateFormat(
100: HOURS_EXT_FORMAT);
101: private static final SimpleDateFormat minutesDateFormat = new SimpleDateFormat(
102: MINUTES_FORMAT);
103: private static final SimpleDateFormat minutesDateFormatD = new SimpleDateFormat(
104: MINUTES_EXT_FORMAT);
105: private static final SimpleDateFormat secondsDateFormat = new SimpleDateFormat(
106: SECONDS_FORMAT);
107: private static final SimpleDateFormat secondsDateFormatD = new SimpleDateFormat(
108: SECONDS_EXT_FORMAT);
109: private static final SimpleDateFormat millisDateFormat = new SimpleDateFormat(
110: MILLIS_FORMAT);
111: private static final SimpleDateFormat millisDateFormatD = new SimpleDateFormat(
112: MILLIS_EXT_FORMAT);
113: private static final SimpleDateFormat millisDateFormatF = new SimpleDateFormat(
114: MILLIS_FULL_FORMAT);
115: private static final SimpleDateFormat onlyMillisDateFormat = new SimpleDateFormat(
116: MILLIS_ONLY_FORMAT);
117: private static final long[] timeUnitsGrid = new long[] { 10, 20,
118: 50, 100, 250, 500, // milliseconds
119: 1000, 2000, 5000, 10000, 15000, 30000, // seconds
120: 60000, 120000, 300000, 600000, 900000, 1800000, // minutes
121: 3600000, 7200000, 10800000, 21600000, 43200000, // hours
122: 86400000, 172800000, 259200000, 432000000 }; // days
123: private static final int[] timeUnitsFormat = new int[] {
124: TIME_FORMAT_MILLIS, TIME_FORMAT_MILLIS, TIME_FORMAT_MILLIS,
125: TIME_FORMAT_MILLIS, TIME_FORMAT_MILLIS, TIME_FORMAT_MILLIS,
126: TIME_FORMAT_SECONDS, TIME_FORMAT_SECONDS,
127: TIME_FORMAT_SECONDS, TIME_FORMAT_SECONDS,
128: TIME_FORMAT_SECONDS, TIME_FORMAT_SECONDS,
129: TIME_FORMAT_MINUTES, TIME_FORMAT_MINUTES,
130: TIME_FORMAT_MINUTES, TIME_FORMAT_MINUTES,
131: TIME_FORMAT_MINUTES, TIME_FORMAT_MINUTES,
132: TIME_FORMAT_HOURS, TIME_FORMAT_HOURS, TIME_FORMAT_HOURS,
133: TIME_FORMAT_HOURS, TIME_FORMAT_HOURS, TIME_FORMAT_DAYS,
134: TIME_FORMAT_DAYS, TIME_FORMAT_DAYS, TIME_FORMAT_DAYS };
135: private static final HashMap timeUnitsToIndex = new HashMap();
136:
137: static {
138: for (int i = 0; i < timeUnitsGrid.length; i++) {
139: timeUnitsToIndex.put(new Long(timeUnitsGrid[i]),
140: new Integer(i));
141: }
142: }
143:
144: //~ Methods ------------------------------------------------------------------------------------------------------------------
145:
146: public static String getDaysValue(long mark, boolean useDayMark) {
147: return daysDateFormat.format(new Date(mark));
148: }
149:
150: public static String getHoursValue(long mark, boolean useDayMark) {
151: return (useDayMark ? hoursDateFormatD.format(new Date(mark))
152: : hoursDateFormat.format(new Date(mark)));
153: }
154:
155: public static double getMaximumScale(long optimalUnits) {
156: return (double) MIN_TIMEMARK_STEP / (double) optimalUnits;
157: }
158:
159: public static String getMillisValue(long mark, boolean useDayMark) {
160: return (useDayMark ? millisDateFormatD.format(new Date(mark))
161: : millisDateFormat.format(new Date(mark)));
162: }
163:
164: public static String getMillisValueFull(long mark) {
165: return millisDateFormatF.format(new Date(mark));
166: }
167:
168: public static String getMinutesValue(long mark, boolean useDayMark) {
169: return (useDayMark ? minutesDateFormatD.format(new Date(mark))
170: : minutesDateFormat.format(new Date(mark)));
171: }
172:
173: public static long getOptimalUnits(double factor) {
174: for (int i = 0; i < timeUnitsGrid.length; i++) {
175: if ((timeUnitsGrid[i] * factor) >= MIN_TIMEMARK_STEP) {
176: return timeUnitsGrid[i];
177: }
178: }
179:
180: return timeUnitsGrid[timeUnitsGrid.length - 1];
181: }
182:
183: public static String getSecondsValue(long mark, boolean useDayMark) {
184: return (useDayMark ? secondsDateFormatD.format(new Date(mark))
185: : secondsDateFormat.format(new Date(mark)));
186: }
187:
188: public static String getTimeMarkMillisString(long mark,
189: long optimalUnits) {
190: int format = getTimeUnitsFormat(optimalUnits);
191:
192: if (format != TIME_FORMAT_MILLIS) {
193: return ""; // NOI18N
194: }
195:
196: return onlyMillisDateFormat.format(new Date(mark));
197: }
198:
199: public static String getTimeMarkNoMillisString(long mark,
200: long optimalUnits, boolean useDayMark) {
201: int format = getTimeUnitsFormat(optimalUnits);
202:
203: if (format == TIME_FORMAT_UNKNOWN) {
204: return ""; // NOI18N
205: }
206:
207: if (format == TIME_FORMAT_MILLIS) {
208: format = TIME_FORMAT_SECONDS;
209: }
210:
211: return getTimeMarkStringFromFormat(mark, format, useDayMark);
212: }
213:
214: public static String getTimeMarkString(long mark,
215: long optimalUnits, boolean useDayMark) {
216: int format = getTimeUnitsFormat(optimalUnits);
217:
218: if (format == TIME_FORMAT_UNKNOWN) {
219: return ""; // NOI18N
220: }
221:
222: return getTimeMarkStringFromFormat(mark, format, useDayMark);
223: }
224:
225: private static String getTimeMarkStringFromFormat(long mark,
226: int format, boolean useDayMark) {
227: switch (format) {
228: case TIME_FORMAT_MILLIS:
229: return getMillisValue(mark, useDayMark);
230: case TIME_FORMAT_SECONDS:
231:
232: //return getSecondsValue(mark, useDayMark);
233: case TIME_FORMAT_MINUTES:
234:
235: //return getMinutesValue(mark, useDayMark);
236: case TIME_FORMAT_HOURS:
237:
238: //return getHoursValue(mark, useDayMark);
239: return getSecondsValue(mark, useDayMark);
240: case TIME_FORMAT_DAYS:
241: return getDaysValue(mark, useDayMark);
242: default:
243: return ""; // NOI18N
244: }
245: }
246:
247: private static int getTimeUnitsFormat(long optimalUnits) {
248: int timeUnitsFormatIndex = getUnitsIndex(optimalUnits);
249:
250: if (timeUnitsFormatIndex == -1) {
251: return TIME_FORMAT_UNKNOWN;
252: }
253:
254: return timeUnitsFormat[timeUnitsFormatIndex];
255: }
256:
257: private static int getUnitsIndex(long optimalUnits) {
258: Object oTimeUnitsFormatIndex = timeUnitsToIndex.get(new Long(
259: optimalUnits));
260:
261: if (oTimeUnitsFormatIndex == null) {
262: return -1;
263: }
264:
265: return ((Integer) oTimeUnitsFormatIndex).intValue();
266: }
267: }
|