TimeTracking: Add support for intuitive time notations like "1h 30m 10s"
This commit is contained in:
@@ -32,6 +32,8 @@ import java.awt.event.FocusEvent;
|
||||
import java.awt.event.FocusListener;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.time.Duration;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import javax.swing.BorderFactory;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.SwingConstants;
|
||||
@@ -52,6 +54,9 @@ abstract class ClockPanel extends JPanel
|
||||
private static final Color ACTIVE_CLOCK_COLOR = ColorScheme.LIGHT_GRAY_COLOR.brighter();
|
||||
private static final Color INACTIVE_CLOCK_COLOR = ColorScheme.LIGHT_GRAY_COLOR.darker();
|
||||
|
||||
private static final String INPUT_HMS_REGEX = ".*[hms].*";
|
||||
private static final String WHITESPACE_REGEX = "\\s+";
|
||||
|
||||
// additional content or buttons should be added to these panels in the subclasses
|
||||
final JPanel contentContainer;
|
||||
final JPanel leftActions;
|
||||
@@ -133,21 +138,38 @@ abstract class ClockPanel extends JPanel
|
||||
@Override
|
||||
public void focusLost(FocusEvent e)
|
||||
{
|
||||
String[] parts = displayInput.getText().split(":");
|
||||
long duration = 0;
|
||||
String text = displayInput.getText();
|
||||
|
||||
// parse from back to front, so as to accept hour:min:sec, min:sec, and sec formats
|
||||
for (int i = parts.length - 1, multiplier = 1; i >= 0 && multiplier <= 3600; i--, multiplier *= 60)
|
||||
if (text.matches(INPUT_HMS_REGEX))
|
||||
{
|
||||
String textWithoutWhitespaces = text.replaceAll(WHITESPACE_REGEX, "");
|
||||
try
|
||||
{
|
||||
duration += Integer.parseInt(parts[i].trim()) * multiplier;
|
||||
//parse input using ISO-8601 Duration format (e.g. 'PT1h30m10s')
|
||||
duration = Duration.parse("PT" + textWithoutWhitespaces).toMillis() / 1000;
|
||||
}
|
||||
catch (NumberFormatException nfe)
|
||||
catch (DateTimeParseException exception)
|
||||
{
|
||||
// ignored
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
String[] parts = text.split(":");
|
||||
// parse from back to front, so as to accept hour:min:sec, min:sec, and sec formats
|
||||
for (int i = parts.length - 1, multiplier = 1; i >= 0 && multiplier <= 3600; i--, multiplier *= 60)
|
||||
{
|
||||
try
|
||||
{
|
||||
duration += Integer.parseInt(parts[i].trim()) * multiplier;
|
||||
}
|
||||
catch (NumberFormatException nfe)
|
||||
{
|
||||
// ignored
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
clock.setDuration(Math.max(0, duration));
|
||||
clock.reset();
|
||||
|
||||
Reference in New Issue
Block a user