Autopsy  4.21.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
DurationCellRenderer.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2015-2021 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.guiutils;
20 
21 import java.awt.Component;
22 import java.time.Duration;
23 import javax.swing.JTable;
24 
30 public final class DurationCellRenderer extends GrayableCellRenderer {
31 
32  private static final long serialVersionUID = 1L;
33  private static final char UNIT_SEPARATOR_CHAR = ':';
34 
36  setHorizontalAlignment(LEFT);
37  }
38 
39  @Override
40  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
41  if (value instanceof Long) {
42  setText(DurationCellRenderer.longToDurationString((long) value));
43  }
44  grayCellIfTableNotEnabled(table, isSelected);
45  return this;
46  }
47 
48  public static char getUnitSeperator() {
49  return UNIT_SEPARATOR_CHAR;
50  }
51 
60  public static String longToDurationString(long duration) {
61  Duration d = Duration.ofMillis(duration);
62  if (d.isNegative()) {
63  d = Duration.ofMillis(0); //it being 0 for a few seconds seems preferable to it counting down to 0 then back up from 0
64  }
65  long days = d.toDays();
66  long hours = d.minusDays(days).toHours();
67  long minutes = d.minusDays(days).minusHours(hours).toMinutes();
68  long seconds = d.minusDays(days).minusHours(hours).minusMinutes(minutes).getSeconds();
69  if (days < 0) {
70  days = 0;
71  }
72  if (hours < 0) {
73  hours = 0;
74  }
75  if (minutes < 0) {
76  minutes = 0;
77  }
78  if (seconds < 0) {
79  seconds = 0;
80  }
81  StringBuilder results = new StringBuilder(12);
82  if (days < 99) {
83  results.append(String.format("%02d", days));
84  } else {
85  results.append(days); //in the off chance something has been running for over 99 days lets allow it to stand out a bit by having as many characters as it needs
86  }
87  results.append(UNIT_SEPARATOR_CHAR);
88  results.append(String.format("%02d", hours));
89  results.append(UNIT_SEPARATOR_CHAR);
90  results.append(String.format("%02d", minutes));
91  results.append(UNIT_SEPARATOR_CHAR);
92  results.append(String.format("%02d", seconds));
93  return results.toString();
94  }
95 
96 }
Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
void grayCellIfTableNotEnabled(JTable table, boolean isSelected)

Copyright © 2012-2022 Basis Technology. Generated on: Tue Feb 6 2024
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.