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.FocusListener;
|
||||||
import java.awt.event.MouseAdapter;
|
import java.awt.event.MouseAdapter;
|
||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.time.format.DateTimeParseException;
|
||||||
import javax.swing.BorderFactory;
|
import javax.swing.BorderFactory;
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
import javax.swing.SwingConstants;
|
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 ACTIVE_CLOCK_COLOR = ColorScheme.LIGHT_GRAY_COLOR.brighter();
|
||||||
private static final Color INACTIVE_CLOCK_COLOR = ColorScheme.LIGHT_GRAY_COLOR.darker();
|
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
|
// additional content or buttons should be added to these panels in the subclasses
|
||||||
final JPanel contentContainer;
|
final JPanel contentContainer;
|
||||||
final JPanel leftActions;
|
final JPanel leftActions;
|
||||||
@@ -133,21 +138,38 @@ abstract class ClockPanel extends JPanel
|
|||||||
@Override
|
@Override
|
||||||
public void focusLost(FocusEvent e)
|
public void focusLost(FocusEvent e)
|
||||||
{
|
{
|
||||||
String[] parts = displayInput.getText().split(":");
|
|
||||||
long duration = 0;
|
long duration = 0;
|
||||||
|
String text = displayInput.getText();
|
||||||
|
|
||||||
// parse from back to front, so as to accept hour:min:sec, min:sec, and sec formats
|
if (text.matches(INPUT_HMS_REGEX))
|
||||||
for (int i = parts.length - 1, multiplier = 1; i >= 0 && multiplier <= 3600; i--, multiplier *= 60)
|
|
||||||
{
|
{
|
||||||
|
String textWithoutWhitespaces = text.replaceAll(WHITESPACE_REGEX, "");
|
||||||
try
|
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
|
// 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.setDuration(Math.max(0, duration));
|
||||||
clock.reset();
|
clock.reset();
|
||||||
|
|||||||
Reference in New Issue
Block a user