Skip to content

Commit

Permalink
Add a stop watch
Browse files Browse the repository at this point in the history
  • Loading branch information
trixon committed Sep 14, 2024
1 parent 5105b99 commit ddb3fde
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
2 changes: 2 additions & 0 deletions main/src/main/java/se/trixon/nbrsync/NbRsync.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
*/
public class NbRsync {

public static final String GSC_TIMER_STOP = "key.timer.stop";
public static final String GSC_TIMER_START = "key.timer.start";
public static final String GSC_EDITOR = "key.editor";
public static final String GSC_LAST_JOB_ID = "key.last_job_id";
private static final File sRunningJobsDirectory = new File(Places.getUserDirectory(), "runningJobs");
Expand Down
3 changes: 3 additions & 0 deletions main/src/main/java/se/trixon/nbrsync/core/JobExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ public void run() {
return true;
};

NbRsync.getInstance().getGlobalState().put(NbRsync.GSC_TIMER_START, System.currentTimeMillis());

mInterrupted = false;
mStartTime = System.currentTimeMillis();
mProgressHandle = ProgressHandle.createHandle(mJob.getName(), allowToCancel);
Expand Down Expand Up @@ -224,6 +226,7 @@ private void jobEnded(OutputLineMode outputLineMode, String action, int exitCode

mStatusDisplayer.setStatusText(action);
mOutputHelper.printSummary(outputLineMode, action, Dict.JOB.toString());
NbRsync.getInstance().getGlobalState().put(NbRsync.GSC_TIMER_STOP, System.currentTimeMillis());
}

private boolean run(String command, boolean stopOnError, String description) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2024 Patrik Karlström.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package se.trixon.nbrsync.gui;

import java.awt.Component;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
import org.openide.awt.StatusLineElementProvider;
import org.openide.util.lookup.ServiceProvider;
import se.trixon.almond.util.SystemHelper;
import se.trixon.almond.util.TimeHelper;
import se.trixon.almond.util.swing.SwingHelper;
import se.trixon.nbrsync.NbRsync;

/**
*
* @author Patrik Karlström
*/
@ServiceProvider(service = StatusLineElementProvider.class, position = 10000)
public class TimerStatusLineElement implements StatusLineElementProvider {

private JLabel mLabel;
private long mStartTime;
private Timer mTimer;

public TimerStatusLineElement() {
}

@Override
public Component getStatusLineElement() {
if (mLabel == null) {
init();
initListeners();
}

return mLabel;
}

private void init() {
mLabel = new JLabel();
mLabel.setFont(new Font("monospaced", Font.PLAIN, mLabel.getFont().getSize()));
mLabel.setBorder(new EmptyBorder(0, 0, 0, SwingHelper.getUIScaled(16)));

mTimer = new Timer(250, actionEvent -> {
var minSec = TimeHelper.millisToMinSec(SystemHelper.age(mStartTime));
mLabel.setText("%5d:%02d".formatted(minSec[0], minSec[1]));
});

}

private void initListeners() {
NbRsync.getInstance().getGlobalState().addListener(gsce -> {
mStartTime = gsce.getValue();
mTimer.start();
}, NbRsync.GSC_TIMER_START);

NbRsync.getInstance().getGlobalState().addListener(gsce -> {
mTimer.stop();
}, NbRsync.GSC_TIMER_STOP);
}
}

0 comments on commit ddb3fde

Please sign in to comment.