1 19 20 package org.netbeans.modules.tasklist.usertasks.util; 21 22 import java.text.MessageFormat ; 23 import java.text.ParseException ; 24 import java.util.regex.Matcher ; 25 import java.util.regex.Pattern ; 26 import org.netbeans.modules.tasklist.usertasks.model.Duration; 27 import org.openide.util.NbBundle; 28 29 34 public class DurationFormat { 35 40 public enum Type {SHORT, LONG}; 41 42 private MessageFormat format; 43 private Pattern parsePattern; 44 private Type type; 45 46 51 public DurationFormat(Type type) { 52 this.type = type; 53 String s; 54 if (type == Type.LONG) { 55 s = NbBundle.getMessage(DurationFormat.class, 56 "DurationFormat"); parsePattern = Pattern.compile( 58 NbBundle.getMessage(DurationFormat.class, 59 "DurationParseFormat")); } else { 61 s = NbBundle.getMessage(DurationFormat.class, 62 "DurationShortFormat"); String pp = NbBundle.getMessage(DurationFormat.class, 64 "DurationShortParseFormat"); parsePattern = Pattern.compile(pp); } 67 68 if (s.trim().length() != 0) 69 format = new MessageFormat (s); 70 } 71 72 81 public Duration parse(String source) throws ParseException { 82 Matcher matcher = parsePattern.matcher(source); 83 if (!matcher.matches()) 84 throw new ParseException (source, 0); 85 if (matcher.groupCount() != 4) { 86 throw new ParseException (source, 0); 88 } 89 try { 90 String ws = matcher.group(1); 95 String ds = matcher.group(2); 96 String hs = matcher.group(3); 97 String 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 return new Duration(w, d, h, m); 104 } catch (NumberFormatException e) { 105 throw new ParseException (source, 0); } 107 } 108 109 115 public String format(Duration d) { 116 if (format != null) 117 return format.format(new Object [] { 118 new Integer (d.weeks), 119 new Integer (d.days), 120 new Integer (d.hours), new Integer (d.minutes) 121 }).trim(); 122 123 if (type == Type.LONG) { 124 StringBuilder sb = new StringBuilder (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 sb = new StringBuilder (); 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 |