KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > util > DurationFormat


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.usertasks.util;
21
22 import java.text.MessageFormat JavaDoc;
23 import java.text.ParseException JavaDoc;
24 import java.util.regex.Matcher JavaDoc;
25 import java.util.regex.Pattern JavaDoc;
26 import org.netbeans.modules.tasklist.usertasks.model.Duration;
27 import org.openide.util.NbBundle;
28
29 /**
30  * Formats durations.
31  *
32  * @author tl
33  */

34 public class DurationFormat {
35     /**
36      * format type
37      * long: 1 week 2 days 5 hours 5 minutes
38      * short: 1w 2d 05:05
39      */

40     public enum Type {SHORT, LONG};
41
42     private MessageFormat JavaDoc format;
43     private Pattern JavaDoc parsePattern;
44     private Type type;
45
46     /**
47      * Creates a new instance of EnDurationFormatter.
48      *
49      * @param type format type
50      */

51     public DurationFormat(Type type) {
52         this.type = type;
53         String JavaDoc s;
54         if (type == Type.LONG) {
55             s = NbBundle.getMessage(DurationFormat.class,
56                 "DurationFormat"); // NOI18N
57
parsePattern = Pattern.compile(
58                     NbBundle.getMessage(DurationFormat.class,
59                     "DurationParseFormat")); // NOI18N;
60
} else {
61             s = NbBundle.getMessage(DurationFormat.class,
62                 "DurationShortFormat"); // NOI18N
63
String JavaDoc pp = NbBundle.getMessage(DurationFormat.class,
64                     "DurationShortParseFormat"); // NOI18N
65
parsePattern = Pattern.compile(pp); // NOI18N;
66
}
67
68         if (s.trim().length() != 0)
69             format = new MessageFormat JavaDoc(s);
70     }
71     
72     /**
73      * Parses duration.
74      * The method may not use the entire text of the given string.
75      *
76      * @param source A <code>String</code> whose beginning should be parsed.
77      * @return parsed duration
78      * @exception ParseException if the beginning of the specified string
79      * cannot be parsed.
80      */

81     public Duration parse(String JavaDoc source) throws ParseException JavaDoc {
82         Matcher JavaDoc matcher = parsePattern.matcher(source);
83         if (!matcher.matches())
84             throw new ParseException JavaDoc(source, 0);
85         if (matcher.groupCount() != 4) {
86             // System.out.println("" + matcher.groupCount());
87
throw new ParseException JavaDoc(source, 0);
88         }
89         try {
90             //System.out.println(matcher.group(1) + " " +
91
// matcher.group(2) + " " +
92
// matcher.group(3) + " " +
93
// matcher.group(4));
94
String JavaDoc ws = matcher.group(1);
95             String JavaDoc ds = matcher.group(2);
96             String JavaDoc hs = matcher.group(3);
97             String JavaDoc ms = matcher.group(4);
98             int w = ws == null ? 0 : Integer.parseInt(ws);
99             int d = ds == null ? 0 : Integer.parseInt(ds);
100             int h = hs == null ? 0 : Integer.parseInt(hs);
101             int m = ms == null ? 0 : Integer.parseInt(ms);
102             // System.out.println(w + " " + d + " " + h + " " + m);
103
return new Duration(w, d, h, m);
104         } catch (NumberFormatException JavaDoc e) {
105             throw new ParseException JavaDoc(source, 0); // NOI18N
106
}
107     }
108     
109     /**
110      * Formats a duration.
111      *
112      * @param d the duration value
113      * @return string representation
114      */

115     public String JavaDoc format(Duration d) {
116         if (format != null)
117             return format.format(new Object JavaDoc[] {
118                     new Integer JavaDoc(d.weeks),
119                     new Integer JavaDoc(d.days),
120                     new Integer JavaDoc(d.hours), new Integer JavaDoc(d.minutes)
121                     }).trim();
122         
123         if (type == Type.LONG) {
124             StringBuilder JavaDoc sb = new StringBuilder JavaDoc(25);
125             switch (d.weeks) {
126                 case 0:
127                     break;
128                 case 1:
129                     sb.append("1 week");
130                     break;
131                 default:
132                     sb.append(d.weeks).append(" weeks");
133                     break;
134             }
135             switch (d.days) {
136                 case 0:
137                     break;
138                 case 1:
139                     sb.append(" 1 day");
140                     break;
141                 default:
142                     sb.append(' ').append(d.days).append(" days");
143                     break;
144             }
145             switch (d.hours) {
146                 case 0:
147                     break;
148                 case 1:
149                     sb.append(" 1 hour");
150                     break;
151                 default:
152                     sb.append(' ').append(d.hours).append(" hours");
153                     break;
154             }
155             switch (d.minutes) {
156                 case 0:
157                     break;
158                 case 1:
159                     sb.append(" 1 minute");
160                     break;
161                 default:
162                     sb.append(' ').append(d.minutes).append(" minutes");
163                     break;
164             }
165             if (sb.length() > 0 && sb.charAt(0) == ' ')
166                 sb.delete(0, 1);
167             return sb.toString();
168         } else {
169             StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
170             switch (d.weeks) {
171                 case 0:
172                     break;
173                 default:
174                     sb.append(d.weeks).append("w");
175                     break;
176             }
177             switch (d.days) {
178                 case 0:
179                     break;
180                 default:
181                     sb.append(' ').append(d.days).append("d");
182                     break;
183             }
184             
185             if (d.hours != 0 || d.minutes != 0) {
186                 sb.append(' ');
187                 if (d.hours < 10)
188                     sb.append('0');
189                 sb.append(d.hours);
190                 sb.append(':');
191                 if (d.minutes < 10)
192                     sb.append('0');
193                 sb.append(d.minutes);
194             }
195             
196             if (sb.length() > 0 && sb.charAt(0) == ' ')
197                 sb.delete(0, 1);
198             return sb.toString();
199         }
200     }
201 }
202
Popular Tags