From 13d5b13faaffc5e97e6d0237ff3ae3b2051bb734 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 17 Jul 2015 11:24:44 +0200 Subject: [client] Get rid of copypaste-mania in ImageList and LectureList --- dozentenmodul/pom.xml | 7 +- .../org/openslx/dozmod/gui/helper/TableHelper.java | 7 +- .../openslx/dozmod/gui/window/ImageListWindow.java | 13 +--- .../dozmod/gui/window/LectureListWindow.java | 82 +++++----------------- .../java/org/openslx/dozmod/util/FormatHelper.java | 62 +++++++++++----- 5 files changed, 74 insertions(+), 97 deletions(-) (limited to 'dozentenmodul') diff --git a/dozentenmodul/pom.xml b/dozentenmodul/pom.xml index ef130c4b..fa88699e 100644 --- a/dozentenmodul/pom.xml +++ b/dozentenmodul/pom.xml @@ -265,7 +265,7 @@ org.apache.thrift libthrift - 0.9.1 + 0.9.2 compile @@ -315,6 +315,11 @@ 2.2.4 compile + + joda-time + joda-time + 2.8 + org.eclipse.jface org.eclipse.jface diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/TableHelper.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/TableHelper.java index 1b1be2c0..933831af 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/TableHelper.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/TableHelper.java @@ -1,8 +1,5 @@ package org.openslx.dozmod.gui.helper; -import java.text.SimpleDateFormat; -import java.util.Date; - import org.eclipse.jface.viewers.ColumnLabelProvider; import org.eclipse.jface.viewers.TableViewer; import org.eclipse.jface.viewers.TableViewerColumn; @@ -98,10 +95,8 @@ public final class TableHelper { @Override public String getText(Object element) { ImageSummaryRead image = (ImageSummaryRead) element; - Date date = new Date(image.getUpdateTime() * 1000L); - SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); - return String.valueOf(sdf.format(date)); + return FormatHelper.shortDate(image.getUpdateTime()); } }); diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageListWindow.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageListWindow.java index a022c92f..be7bfad0 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageListWindow.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageListWindow.java @@ -1,7 +1,5 @@ package org.openslx.dozmod.gui.window; -import java.text.SimpleDateFormat; -import java.util.Date; import java.util.List; import org.apache.log4j.Logger; @@ -29,6 +27,7 @@ import org.openslx.dozmod.gui.wizard.ImageWizard; import org.openslx.dozmod.thrift.ImageCache; import org.openslx.dozmod.thrift.Session; import org.openslx.dozmod.thrift.UserCache; +import org.openslx.dozmod.util.FormatHelper; import org.openslx.thrifthelper.ThriftManager; import org.openslx.util.QuickTimer; import org.openslx.util.QuickTimer.Task; @@ -91,15 +90,7 @@ public class ImageListWindow extends ImageListWindowLayout { } // set the time, the image has last been updated - long unixTimestamp = selectedElement.getUpdateTime(); - if (unixTimestamp == 0) { - lastUpdateInfo.setText("Unknown"); - } else { - Date date = new Date(unixTimestamp * 1000L); - SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); - String formattedDate = sdf.format(date); - lastUpdateInfo.setText(formattedDate); - } + lastUpdateInfo.setText(FormatHelper.shortDate(selectedElement.getUpdateTime())); // info about the image permissions ImagePermissions p = selectedElement.getUserPermissions(); diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/LectureListWindow.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/LectureListWindow.java index 2f3aa76b..026057fd 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/LectureListWindow.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/LectureListWindow.java @@ -1,7 +1,5 @@ package org.openslx.dozmod.gui.window; -import java.text.SimpleDateFormat; -import java.util.Date; import java.util.List; import org.apache.log4j.Logger; @@ -13,8 +11,8 @@ import org.eclipse.swt.events.KeyEvent; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.Text; import org.openslx.bwlp.thrift.iface.LectureSummary; -import org.openslx.bwlp.thrift.iface.UserInfo; import org.openslx.dozmod.gui.MainWindow; import org.openslx.dozmod.gui.helper.LectureListComparator; import org.openslx.dozmod.gui.helper.LectureListFilter; @@ -22,6 +20,7 @@ import org.openslx.dozmod.gui.helper.TableHelper; import org.openslx.dozmod.gui.window.layout.LectureListWindowLayout; import org.openslx.dozmod.thrift.LectureCache; import org.openslx.dozmod.thrift.UserCache; +import org.openslx.dozmod.util.FormatHelper; public class LectureListWindow extends LectureListWindowLayout { @@ -56,73 +55,31 @@ public class LectureListWindow extends LectureListWindowLayout { public void selectionChanged(SelectionChangedEvent event) { IStructuredSelection selection = (IStructuredSelection) tableViewer.getSelection(); LectureSummary lecture = (LectureSummary) selection.getFirstElement(); - if (lecture == null) return; - String leName = lecture.getLectureName(); - if (leName == null) { - lectureName.setText("Unknown"); - } else { - lectureName.setText(leName); - } - - + if (lecture == null) + return; + // Fill detail information fields + // Lecture name + setFieldText(lectureName, lecture.getLectureName()); // id of the lecture - String lectureBaseId = lecture.getLectureId(); - if (lectureBaseId == null) { - idInfo.setText("Unknown"); - } else { - idInfo.setText(lectureBaseId); - } - + setFieldText(idInfo, lecture.getLectureId()); // id of the image linked to the lecture - String baseId = lecture.getImageBaseId(); - if (baseId == null) { - imageBaseId.setText("Unknown"); - } else { - imageBaseId.setText(baseId); - } - + setFieldText(imageBaseId, lecture.getImageBaseId()); + // the owner of the selected lecture + setFieldText(ownerInfo, FormatHelper.userName(UserCache.find(lecture.getOwnerId()))); // set the time, the lecture has last been used - long lastUsedTimestamp = lecture.getLastUsed(); - if (lastUsedTimestamp == 0) { - lastusedInfo.setText("Unknown"); - } else { - Date date = new Date(lastUsedTimestamp * 1000L); - SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); - String formattedDate = sdf.format(date); - lastusedInfo.setText(formattedDate); - } - + lastusedInfo.setText(FormatHelper.longDate(lecture.getLastUsed())); // set the start time of the lecture - long startTimestamp = lecture.getStartTime(); - if (startTimestamp == 0) { - startTime.setText("Unknown"); - } else { - Date date = new Date(startTimestamp * 1000L); - SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); - String formattedDate = sdf.format(date); - startTime.setText(formattedDate); - } - + startTime.setText(FormatHelper.longDate(lecture.getStartTime())); // set the end time of the lecture - long endTimestamp = lecture.getEndTime(); - if (endTimestamp == 0) { - endTime.setText("Unknown"); - } else { - Date date = new Date(endTimestamp * 1000L); - SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); - String formattedDate = sdf.format(date); - endTime.setText(formattedDate); - } + endTime.setText(FormatHelper.longDate(lecture.getEndTime())); + } - // the owner id of the selected lecture - String ownerId = lecture.getOwnerId(); - UserInfo user = UserCache.find(ownerId); - if (user != null) { - ownerInfo.setText(user.getLastName() + ", " + user.getFirstName()); + private void setFieldText(Text control, String content) { + if (content == null) { + control.setText(""); } else { - ownerInfo.setText("Unknown"); + control.setText(content); } - } }); @@ -154,7 +111,6 @@ public class LectureListWindow extends LectureListWindowLayout { private boolean refreshList() { List lectureList = LectureCache.get(false); - tableViewer.setInput(lectureList); tableViewer.refresh(); return true; diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/util/FormatHelper.java b/dozentenmodul/src/main/java/org/openslx/dozmod/util/FormatHelper.java index 5b36a18c..2fae918e 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/util/FormatHelper.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/util/FormatHelper.java @@ -1,35 +1,65 @@ package org.openslx.dozmod.util; -import java.text.ParseException; -import java.text.SimpleDateFormat; +import org.joda.time.format.DateTimeFormat; +import org.joda.time.format.DateTimeFormatter; +import org.openslx.bwlp.thrift.iface.UserInfo; public class FormatHelper { - private static final SimpleDateFormat in = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - private static final SimpleDateFormat out = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); + private static final DateTimeFormatter shortDateFormatter = DateTimeFormat.forPattern("dd.MM.yy HH:mm"); + private static final DateTimeFormatter longDateFormatter = DateTimeFormat.forPattern("dd.MM.yyyy HH:mm:ss"); /** - * Convert mysql date/time format to human readable (German) format. - * If the given date is not parsable, "" will be returned. + * Convert unix timestamp to short date. * - * @param dateTime yyyy-MM-dd HH:mm:ss + * @param timestamp unix time * @return dd.MM.yy HH:mm */ - public static String mysqlDateToGerman(String dateTime) { - try { - return out.format(in.parse(dateTime)); - } catch (ParseException e) { - return ""; - } + public static String shortDate(long timestamp) { + if (timestamp == 0) + return "???"; + return shortDateFormatter.print(timestamp * 1000l); + } + + /** + * Convert unix timestamp to long date. + * + * @param timestamp unix time + * @return dd.MM.yyyy HH:mm:ss + */ + public static String longDate(long timestamp) { + if (timestamp == 0) + return "???"; + return longDateFormatter.print(timestamp * 1000l); } + /** + * Format the given user's name. + * + * @param user a {@link UserInfo} instance + * @return "LastName, FirstName" + */ + public static String userName(UserInfo user) { + if (user == null) + return ""; + return user.getLastName() + ", " + user.getFirstName(); + } + + /** + * Format bytes using suitable unit prefix. + * + * @param bytes unformatted byte count + * @param si true to use SI mode (1000B = 1KB), false for normal mode + * (1024B = 1KiB) + * @return Formatted string, i.e. 54.6 GiB + */ public static String byteToGigabyte(long bytes, boolean si) { int unit = si ? 1000 : 1024; if (bytes < unit) - return bytes + " B"; + return bytes + "\u202FB"; int exp = (int) (Math.log(bytes) / Math.log(unit)); - String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i"); - return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre); + String pre = (si ? "kMGTPE" : "KMGTPEZY").charAt(exp - 1) + (si ? "" : "i"); + return String.format("%.1f\u202F%sB", bytes / Math.pow(unit, exp), pre); } /** -- cgit v1.2.3-55-g7522