forked from sbastn/com.happyprog.pairhero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainView.java
91 lines (74 loc) · 2.14 KB
/
MainView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.happyprog.pairhero.views;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
public class MainView extends ViewPart {
public static final String ID = "com.happyprog.pairhero.views.MainView";
private Label timerLabel;
@Override
public void createPartControl(Composite parent) {
createStartButton();
createTimerLabel(parent);
parent.layout();
}
private void createTimerLabel(Composite parent) {
Group group = new Group(parent, SWT.NONE);
group.setLayout(createLayout());
timerLabel = new Label(group, SWT.NONE);
timerLabel.setText("timer goes here");
}
private RowLayout createLayout() {
RowLayout layout = new RowLayout();
layout.wrap = true;
layout.pack = true;
layout.justify = false;
layout.type = SWT.VERTICAL;
return layout;
}
private void createStartButton() {
IToolBarManager toolbarManager = getViewSite().getActionBars().getToolBarManager();
toolbarManager.add(new Action() {
@Override
public void run() {
onStart();
}
});
}
private void onStart() {
Game game = new Game(this, new Timer());
game.start();
}
@Override
public void setFocus() {
// TODO Auto-generated method stub
}
public void gameFinished() {
EndDialog dialog = new EndDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
dialog.open();
}
public void onTimeChange(int timeInSeconds) {
int minutes = timeInSeconds / 60;
int seconds = timeInSeconds % 60;
updateInfo(timerLabel, String.format("%s:%s", withZeroes(minutes), withZeroes(seconds)));
}
private String withZeroes(int time) {
if (time < 10) {
return String.format("0%d", time);
}
return String.format("%d", time);
}
private void updateInfo(final Label label, final String text) {
PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
@Override
public void run() {
label.setText(text);
}
});
}
}