1 19 20 package org.netbeans.modules.tasklist.usertasks.translators; 21 22 import java.io.File ; 23 import java.net.MalformedURLException ; 24 import java.net.URL ; 25 import java.text.DateFormat ; 26 import java.text.MessageFormat ; 27 import java.util.Calendar ; 28 import java.util.Date ; 29 import java.util.List ; 30 import java.util.logging.Level ; 31 import javax.xml.parsers.DocumentBuilderFactory ; 32 import javax.xml.parsers.ParserConfigurationException ; 33 import javax.xml.transform.OutputKeys ; 34 import javax.xml.transform.Transformer ; 35 import javax.xml.transform.TransformerConfigurationException ; 36 import javax.xml.transform.TransformerException ; 37 import javax.xml.transform.TransformerFactory ; 38 import javax.xml.transform.TransformerFactoryConfigurationError ; 39 import javax.xml.transform.dom.DOMSource ; 40 import javax.xml.transform.stream.StreamResult ; 41 import org.netbeans.modules.tasklist.core.export.ExportImportFormat; 42 import org.netbeans.modules.tasklist.core.export.ExportImportProvider; 43 import org.netbeans.modules.tasklist.core.export.SaveFilePanel; 44 import org.netbeans.modules.tasklist.core.util.ExtensionFileFilter; 45 import org.netbeans.modules.tasklist.core.util.SimpleWizardPanel; 46 import org.netbeans.modules.tasklist.usertasks.options.Settings; 47 import org.netbeans.modules.tasklist.usertasks.UserTaskView; 48 import org.netbeans.modules.tasklist.usertasks.UserTaskViewRegistry; 49 import org.netbeans.modules.tasklist.usertasks.model.Duration; 50 import org.netbeans.modules.tasklist.usertasks.model.UserTask; 51 import org.netbeans.modules.tasklist.usertasks.model.UserTaskList; 52 import org.netbeans.modules.tasklist.usertasks.model.UserTaskObjectList; 53 import org.netbeans.modules.tasklist.usertasks.util.DurationFormat; 54 import org.netbeans.modules.tasklist.usertasks.util.TreeAbstraction; 55 import org.netbeans.modules.tasklist.usertasks.util.UTUtils; 56 import org.netbeans.modules.tasklist.usertasks.util.UnaryFunction; 57 import org.openide.DialogDisplayer; 58 import org.openide.NotifyDescriptor; 59 import org.openide.WizardDescriptor; 60 import org.openide.awt.HtmlBrowser; 61 import org.openide.util.NbBundle; 62 import org.w3c.dom.Document ; 63 import org.w3c.dom.Element ; 64 65 70 public class HistoryTextExportFormat implements ExportImportFormat { 71 private static DurationFormat DURATION_FORMAT = 72 new DurationFormat(DurationFormat.Type.SHORT); 73 74 80 private static String loc(String key) { 81 return NbBundle.getMessage(HistoryTextExportFormat.class, key); 82 } 83 84 public String getName() { 85 return loc("SpentTimes"); } 87 88 public WizardDescriptor getWizard() { 89 HistoryOptionsPanel hop = new HistoryOptionsPanel(); 90 SimpleWizardPanel hopwp = new SimpleWizardPanel(hop); 91 hop.setName(loc("ExportOptions")); hopwp.setContentHighlightedIndex(0); 93 94 SaveFilePanel chooseFilePanel = new SaveFilePanel(); 95 SimpleWizardPanel chooseFileWP = new SimpleWizardPanel(chooseFilePanel); 96 chooseFilePanel.setWizardPanel(chooseFileWP); 97 chooseFilePanel.getFileChooser().addChoosableFileFilter( 98 new ExtensionFileFilter(loc("HtmlFilter"), new String [] {".html"})); chooseFilePanel.setFile(new File ( 101 Settings.getDefault().getLastUsedExportFolder(), 102 "tasklist.html")); chooseFilePanel.setOpenFileCheckBoxVisible(true); 104 chooseFileWP.setContentHighlightedIndex(1); 105 106 WizardDescriptor.Iterator iterator = 108 new WizardDescriptor.ArrayIterator( 109 new WizardDescriptor.Panel[] {hopwp, chooseFileWP}); 110 WizardDescriptor wd = new WizardDescriptor(iterator); 111 wd.putProperty("WizardPanel_contentData", new String [] { 113 hop.getName(), 114 chooseFilePanel.getName() 115 } 116 ); 117 wd.putProperty("WizardPanel_autoWizardStyle", Boolean.TRUE); wd.putProperty("WizardPanel_contentDisplayed", Boolean.TRUE); wd.putProperty("WizardPanel_contentNumbered", Boolean.TRUE); wd.setTitle(loc("ExportText")); wd.putProperty("chooseFilePanel", chooseFilePanel); wd.putProperty("historyOptionsPanel", hop); wd.setTitleFormat(new java.text.MessageFormat ("{0}")); 125 return wd; 126 } 127 128 public void doExportImport(ExportImportProvider provider, 129 WizardDescriptor wd) { 130 HistoryOptionsPanel hop = (HistoryOptionsPanel) 131 wd.getProperty("historyOptionsPanel"); SaveFilePanel panel = (SaveFilePanel) 133 wd.getProperty("chooseFilePanel"); 135 Date from = hop.getFrom(); 136 Date to = hop.getTo(); 137 if (from.compareTo(to) > 0) { 138 Date tmp = from; 139 from = to; 140 to = tmp; 141 } 142 143 Calendar c = Calendar.getInstance(); 144 c.setTime(to); 145 c.add(Calendar.DAY_OF_YEAR, 1); 146 HistoryOptionsPanel.Group g = hop.getGroup(); 147 final int minDur = hop.getMinimumDuration(); 148 149 SpentTimesUserTaskProcessor p = 150 new SpentTimesUserTaskProcessor(from, 151 c.getTime(), g, minDur); 152 153 UserTaskInfo info = createInfos(); 154 TreeAbstraction<UserTaskInfo> tree = info.createTreeInterface(); 155 UTUtils.processDepthFirst(tree, p); 156 UTUtils.processDepthFirst(tree, new UnaryFunction() { 157 public Object compute(Object obj) { 158 UserTaskInfo info = (UserTaskInfo) obj; 159 160 int i = 0; 161 while (i < info.children.size()) { 162 UserTaskInfo ch = info.children.get(i); 163 if (UTUtils.sum(ch.spentTimes) < minDur * 60 * 1000) 164 info.children.remove(i); 165 else 166 i++; 167 } 168 169 return null; 170 } 171 }); 172 173 String fs; 176 switch (g) { 177 case DAILY: 178 fs = "{0,date,short}"; break; 180 case WEEKLY: 181 fs = "{0,date,short} - {1,date,short} ({0,date,w})"; break; 183 case MONTHLY: 184 fs = "{0,date,MMM yyyy}"; break; 186 case QUARTERLY: 187 fs = "{0,date,MMM} - {1,date,MMM yyyy}"; break; 189 case YEARLY: 190 fs = "{0,date,yyyy}"; break; 192 default: 193 fs = "{0} - {1}"; } 195 MessageFormat mf = new MessageFormat (fs); 196 197 Transformer tr; 198 try { 199 tr = TransformerFactory.newInstance().newTransformer(); 200 } catch (TransformerConfigurationException ex) { 201 UTUtils.LOGGER.log(Level.SEVERE, "", ex); return; 203 } catch (TransformerFactoryConfigurationError ex) { 204 UTUtils.LOGGER.log(Level.SEVERE, "", ex); return; 206 } 207 208 tr.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, 209 "-//W3C//DTD XHTML 1.0 Strict//EN"); tr.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, 211 "http://www.w3.org/TR/xhtml1" + "/DTD/xhtml1-strict.dtd"); tr.setOutputProperty(OutputKeys.METHOD, "xml"); tr.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); 216 Document html; 217 try { 218 html = export(info, p.getPeriods(), mf); 219 } catch (ParserConfigurationException ex) { 220 UTUtils.LOGGER.log(Level.SEVERE, "", ex); return; 222 } 223 224 DOMSource source = new DOMSource (html); 225 StreamResult result = new StreamResult (panel.getFile()); 226 try { 227 tr.transform(source, result); 228 Settings.getDefault().setLastUsedExportFolder( 229 panel.getFile().getParentFile()); 230 } catch (TransformerException ex) { 231 String msg = NbBundle.getMessage(HistoryTextExportFormat.class, 232 "CannotWriteFile", panel.getFile().getAbsolutePath(), ex.getMessage()); 234 NotifyDescriptor nd = new NotifyDescriptor.Message( 235 msg, NotifyDescriptor.ERROR_MESSAGE); 236 DialogDisplayer.getDefault().notify(nd); 237 } 238 if (panel.getOpenExportedFile()) 239 openFileInBrowser(panel.getFile()); 240 } 241 242 247 private UserTaskInfo createInfos() { 248 return createInfo(UserTaskViewRegistry.getInstance().getCurrent(). 249 getUserTaskList()); 250 } 251 252 258 private UserTaskInfo createInfo(UserTaskList utl) { 259 UserTaskInfo res = new UserTaskInfo(); 260 res.object = utl; 261 addChildren(res, utl.getSubtasks()); 262 return res; 263 } 264 265 271 private void addChildren(UserTaskInfo info, UserTaskObjectList list) { 272 for (int i = 0; i < list.size(); i++) { 273 UserTaskInfo c = new UserTaskInfo(); 274 c.object = list.get(i); 275 info.children.add(c); 276 addChildren(c, list.get(i).getSubtasks()); 277 } 278 } 279 280 285 private static void openFileInBrowser(File file) { 286 URL url; 287 try { 288 url = file.toURI().toURL(); 289 } catch (MalformedURLException e) { 290 UTUtils.LOGGER.log(Level.SEVERE, "", e); return; 293 } 294 295 HtmlBrowser.URLDisplayer.getDefault().showURL(url); 296 } 297 298 306 private Document export(UserTaskInfo info, List <Date > periods, 307 MessageFormat format) throws ParserConfigurationException { 308 Document doc = DocumentBuilderFactory.newInstance(). 309 newDocumentBuilder().newDocument(); 310 311 Element html = doc.createElement("html"); doc.appendChild(html); 313 Element head = UTUtils.appendElement(html, "head"); Element meta = UTUtils.appendElement(head, "meta"); 315 meta.setAttribute("http-equiv", "Content-Type"); meta.setAttribute("content", "text/html; charset=UTF-8"); UTUtils.appendElement(head, "title", loc("SpentTimes")); createStyle(head); 319 Element body = UTUtils.appendElement(html, "body"); 320 body.setAttribute("style", "font-family: sans-serif"); UTUtils.appendElement(body,"h1", loc("SpentTimes")); 323 DateFormat datef = DateFormat.getDateInstance(DateFormat.SHORT); 324 325 Element table = UTUtils.appendElement(body, "table"); table.setAttribute("border", "0"); table.setAttribute("cellspacing", "1"); 328 table.setAttribute("cellpadding", "0"); Element tbody = UTUtils.appendElement(table, "tbody"); 331 Element header = UTUtils.appendElement(tbody, "tr"); header.setAttribute("class", "header"); Element td = UTUtils.appendElement(header, "td", loc("Summary")); td.setAttribute("class", "summary"); Calendar c = Calendar.getInstance(); 337 for (int i = 0; i < periods.size() - 1; i++) { 338 c.setTime(periods.get(i + 1)); 339 c.add(Calendar.SECOND, -1); 340 Element td2 = UTUtils.appendElement(header, "td", format.format(new Object [] { periods.get(i), c.getTime()}, new StringBuffer (), 343 null).toString()); 344 td2.setAttribute("class", "data"); } 346 347 int[] row = new int[1]; 348 for (int i = 0; i < info.children.size(); i++) { 349 createTableRow(info.children.get(i), tbody, row, 0); 350 } 351 352 UTUtils.appendElement(body, "hr"); createFooter(body); 354 return doc; 355 } 356 357 365 private void createTableRow(UserTaskInfo info, Element tbody, int[] rowNumber, 366 int level) { 367 Element row = UTUtils.appendElement(tbody, "tr"); if (rowNumber[0] % 2 == 0) 369 row.setAttribute("class", "even"); Element td = UTUtils.appendElement(row, "td"); td.setAttribute("class", "summary"); if (info.object instanceof UserTask) { 373 fillDataCell(td, ((UserTask) info.object).getSummary(), level); 374 } else if (info.object instanceof UserTaskList) { 375 378 } else { 379 fillDataCell(td, loc("AllTaskLists"), level); } 381 for (int j = 0; j < info.spentTimes.length; j++) { 382 Duration d = new Duration((int) (info.spentTimes[j] / (1000 * 60)), 383 24, 5); 384 Element td2 = UTUtils.appendElement(row, "td", DURATION_FORMAT.format(d)); 386 td2.setAttribute("class", "data"); } 388 rowNumber[0]++; 389 390 for (int i = 0; i < info.children.size(); i++) { 391 createTableRow(info.children.get(i), tbody, rowNumber, level + 1); 392 } 393 } 394 395 401 private static Element createFooter(Element parent) { 402 Element p = UTUtils.appendElement(parent, "p"); Element a = UTUtils.appendElement(p, "a", loc("ValidXHTML")); 405 a.setAttribute("href", "http://validator.w3.org/check/referer"); UTUtils.appendText(p, loc("CreatedBy") + " "); UTUtils.appendElement(p, "a", "NetBeans"). setAttribute("href", "http://www.netbeans.org"); 411 UTUtils.appendText(p, "\u00a0"); 414 UTUtils.appendElement(p, "a", "User Tasks Module"). setAttribute("href", "http://tasklist.netbeans.org"); return p; 418 } 419 420 426 private static Element createStyle(Element parent) { 427 Element style = UTUtils.appendElement(parent, "style"); style.setAttribute("type", "text/css"); UTUtils.appendText(style, 430 "tr.header { background-color: #222288; " + "color: white; font-weight: bold }\n"); UTUtils.appendText(style, 433 "tr.even { background-color: #eeeeee }\n"); UTUtils.appendText(style, 435 "td.summary { text-align: left; width: 400px; }\n"); UTUtils.appendText(style, 437 "td.data { text-align: center }"); return style; 439 } 440 441 448 private static void fillDataCell(Element td, String v, int level) { 449 Element table = UTUtils.appendElement(td, "table"); 450 table.setAttribute("style", "width: 100%"); Element tbody = UTUtils.appendElement(table, "tbody"); 452 Element tr = UTUtils.appendElement(tbody, "tr"); Element td_ = UTUtils.appendElement(tr, "td"); 454 td_.setAttribute("style", "width: " + (level * 30 + 1) + "px"); Element td2 = UTUtils.appendElement(tr, "td"); UTUtils.appendText(td2, "\u2022"); Element span = UTUtils.appendElement(td2, "span", v); span.setAttribute("title", v); } 460 } 461 | Popular Tags |