This is an automated email from the git hooks/post-receive script. New commit to branch feature/sync in repository jtimer. See https://gitlab.nuiton.org/chorem/jtimer.git commit 8edf5d39f9b221bed0d7af78d88c62bdf79c5acf Author: servantie <servantie.c@gmail.com> Date: Fri Apr 29 11:19:40 2016 +0200 Display of a task after edition of url in prettified json format methods to jsonify and then prettify a task (small typo edit) --- src/main/java/org/chorem/jtimer/JTimer.java | 8 +- .../chorem/jtimer/entities/TimerTaskHelper.java | 134 +++++++++++++++++++-- .../chorem/jtimer/io/GTimerIncrementalSaver.java | 2 +- .../org/chorem/jtimer/ui/TaskJsonDisplayer.java | 61 ++++++++++ .../java/org/chorem/jtimer/ui/TimerTaskEditor.java | 1 + 5 files changed, 190 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/chorem/jtimer/JTimer.java b/src/main/java/org/chorem/jtimer/JTimer.java index 6d89e2f..4c488b3 100644 --- a/src/main/java/org/chorem/jtimer/JTimer.java +++ b/src/main/java/org/chorem/jtimer/JTimer.java @@ -66,10 +66,7 @@ import org.chorem.jtimer.data.DataViolationException; import org.chorem.jtimer.data.TimerCore; import org.chorem.jtimer.entities.TimerProject; import org.chorem.jtimer.entities.TimerTask; -import org.chorem.jtimer.ui.HelpFrame; -import org.chorem.jtimer.ui.NewTaskView; -import org.chorem.jtimer.ui.StatusBar; -import org.chorem.jtimer.ui.TimerTaskEditor; +import org.chorem.jtimer.ui.*; import org.chorem.jtimer.ui.alert.AlertEditor; import org.chorem.jtimer.ui.report.ReportView; import org.chorem.jtimer.ui.systray.SystrayManager; @@ -670,6 +667,9 @@ public class JTimer extends SingleFrameApplication implements TimerTaskEditor editor = new TimerTaskEditor(this, task, core); show(editor); + + TaskJsonDisplayer displayJson = new TaskJsonDisplayer(this, task); + show(displayJson); } /** diff --git a/src/main/java/org/chorem/jtimer/entities/TimerTaskHelper.java b/src/main/java/org/chorem/jtimer/entities/TimerTaskHelper.java index ded3a96..2edf112 100644 --- a/src/main/java/org/chorem/jtimer/entities/TimerTaskHelper.java +++ b/src/main/java/org/chorem/jtimer/entities/TimerTaskHelper.java @@ -22,17 +22,8 @@ package org.chorem.jtimer.entities; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.Date; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.SortedMap; -import java.util.TreeMap; +import java.time.LocalDateTime; +import java.util.*; /** * Helper to remove process code from entity @@ -331,4 +322,125 @@ public class TimerTaskHelper { return components; } + + /** + * Returns a String in JSON format (cf schema) + * with all the times and annotations of one task (and + * its subtasks) + * @param task the task to make a JSON from + * @return subtaskString the String representing the task + */ + public static String tasktoJSONFormat(TimerTask task) { + String taskNumber = Integer.toString(task.getNumber()); + //task time ? or all times (including subtasks) + String taskTotalTime = Long.toString(getAllTotalTime(task)); + + String subtaskString = ""; + if (task.getSubTasks().size() != 0) { + for (TimerTask subtask : task.getSubTasks()) { + subtaskString += tasktoJSONFormat(subtask); + } + } + else { + subtaskString = "[]"; + } + + String res = "{\"path\":\"" + pathForJSON(task) + "\",\"name\":\"" + taskNumber + + "\",\"annotations\":{" + annotationsForJSON(task) + "},\"times\":" + + timesForJSON(task) + + ",\"taskTime\":" + taskTotalTime + ",\"subTasks\":" +subtaskString + "}"; + + return res; + } + + /** + * Returns a String with the task's path + * @param task : the task + * @return path : the task's path + */ + public static String pathForJSON(TimerTask task) { + String path = ""; + TimerTask clonetask = task; + while (clonetask != null) { + path = clonetask.getName()+ "/" + path; + clonetask = clonetask.getParent(); + } + return path; + } + + /** + * Returns the annotations of the task as + * "Date" : "annotation" + * @param task : the task + * @return annotations : the task's annotations + */ + public static String annotationsForJSON(TimerTask task) { + String annotations=""; + if (task.getAllDaysAnnotations().size() != 0) { + SortedMap<Date, String> annotationsMap = task.getAllDaysAnnotations(); + //toString pas au bon format fixme (do it manually) + for (SortedMap.Entry<Date, String> entry : annotationsMap.entrySet()) { + annotations = annotations + "\"" + entry.getKey().toString() + "\":\"" + entry.getValue() + "\","; + } + //fix for last , in the String + annotations = annotations.substring(0,annotations.length()-1); + } + return annotations; + } + + /** + * Returns the times of the task as + * "Date" : time + * @param task the task + * @return times : the task's times + */ + public static String timesForJSON(TimerTask task) { + String times = "{"; + if (task.getAllDaysAndTimes().size() !=0) { + SortedMap<Date, Long> timesMap = task.allDaysTimes; + for (SortedMap.Entry<Date, Long> entry : timesMap.entrySet()) { + times = times + "\"" + entry.getKey().toString() + "\":\"" + entry.getValue() + "\","; + } + times = times.substring(0, times.length()-1); + } + times += "}"; + return times; + } + + /** + * Prettify the JSON String to make it + * human readable + * @param jsonString : the JSON string to prettify + * @return an easier to read String + */ + public static String prettifyJSON(String jsonString) { + int indent = 0; + String prettyJson; + String prettiest = ""; + //return line after each { + prettyJson = jsonString.replace("{", "{\n\t"); + //fix { \n } (useless space} + prettyJson = prettyJson.replace("}", "\n}"); + prettyJson = prettyJson.replace("{\n\t\n}", "{}"); + // return after each , + prettyJson = prettyJson.replace(",", ",\n\t"); + //change ":" into " : " + prettyJson = prettyJson.replace("\":\"", "\" : \""); + prettyJson = prettyJson.replace("\":", "\" : "); + for (int i = 0 ; i < prettyJson.length(); ++i) { + prettiest = prettiest + prettyJson.charAt(i); + if (prettyJson.charAt(i) == '{') { + ++indent; + } + else if (prettyJson.charAt(i) == '}') { + --indent; + } + if ((indent != 0) && (prettyJson.charAt(i) == '\n')) { + for (int j = 0 ; j < indent ; ++j) { + prettiest = prettiest + "\t"; + } + } + } + return prettiest; + } } diff --git a/src/main/java/org/chorem/jtimer/io/GTimerIncrementalSaver.java b/src/main/java/org/chorem/jtimer/io/GTimerIncrementalSaver.java index 8800b3f..4408bd0 100644 --- a/src/main/java/org/chorem/jtimer/io/GTimerIncrementalSaver.java +++ b/src/main/java/org/chorem/jtimer/io/GTimerIncrementalSaver.java @@ -71,6 +71,7 @@ import org.chorem.jtimer.entities.TimerAlert; import org.chorem.jtimer.entities.TimerAlert.Type; import org.chorem.jtimer.entities.TimerProject; import org.chorem.jtimer.entities.TimerTask; +import org.chorem.jtimer.entities.TimerTaskHelper; /** * Charge et sauve les fichiers au format gTimer. @@ -1042,7 +1043,6 @@ public class GTimerIncrementalSaver extends AbstractSaver implements Saver, File backupfile = null; File taskfile = new File(filename); - // encode it to iso-8859-1, because props.load() use this encoding try (Writer out = new OutputStreamWriter(new FileOutputStream(taskfile), "ISO-8859-1")) { diff --git a/src/main/java/org/chorem/jtimer/ui/TaskJsonDisplayer.java b/src/main/java/org/chorem/jtimer/ui/TaskJsonDisplayer.java new file mode 100644 index 0000000..c97de1c --- /dev/null +++ b/src/main/java/org/chorem/jtimer/ui/TaskJsonDisplayer.java @@ -0,0 +1,61 @@ +package org.chorem.jtimer.ui; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.chorem.jtimer.JTimer; +import org.chorem.jtimer.entities.TimerTask; +import org.chorem.jtimer.entities.TimerTaskHelper; +import org.chorem.jtimer.ui.widget.DialogView; + +import javax.swing.*; +import java.awt.*; + +/** + * Dummy class to display the json made from a task + * Created by servantie on 29/04/16. + */ +public class TaskJsonDisplayer extends DialogView { + /** log */ + private static Log log = LogFactory.getLog(TaskJsonDisplayer.class); + + protected JTimer parent; + + //task to display + protected TimerTask task; + + + /** + * Constructor, building the frame with one task displayed in json (prettified) + * @param parent + * @param task + */ + public TaskJsonDisplayer(JTimer parent, TimerTask task) { + super(parent.getMainFrame(), parent); + this.parent = parent; + this.task = task; + + buildUi(); + + } + + /** + * building the frame + */ + public void buildUi() { + JPanel panel = new JPanel(); + panel.add(createDisplayPanel()); + setComponent(panel); + } + + /** + * Displaying the text + */ + public JPanel createDisplayPanel() { + JPanel panel = new JPanel(); + JTextArea displayArea = new JTextArea(TimerTaskHelper.prettifyJSON(TimerTaskHelper.tasktoJSONFormat(this.task))); + panel.add(displayArea); + + return panel; + + } +} diff --git a/src/main/java/org/chorem/jtimer/ui/TimerTaskEditor.java b/src/main/java/org/chorem/jtimer/ui/TimerTaskEditor.java index 1fc47eb..8552e63 100644 --- a/src/main/java/org/chorem/jtimer/ui/TimerTaskEditor.java +++ b/src/main/java/org/chorem/jtimer/ui/TimerTaskEditor.java @@ -65,6 +65,7 @@ import org.chorem.jtimer.entities.TimerTask; import org.chorem.jtimer.entities.TimerTaskHelper; import org.chorem.jtimer.ui.widget.DialogView; import org.jdesktop.application.Action; +import org.jdesktop.application.Task; import org.jdesktop.swingx.JXMonthView; import org.jdesktop.swingx.calendar.DateSelectionModel; -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.