/
Activator.java
283 lines (248 loc) · 8.11 KB
/
Activator.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package net.mornati.epomodoro;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.mornati.epomodoro.communication.Communication;
import net.mornati.epomodoro.communication.TimerMessage;
import net.mornati.epomodoro.preference.PomodoroPreferencePage;
import net.mornati.epomodoro.util.ConflictRule;
import net.mornati.epomodoro.util.PluginImages;
import net.mornati.epomodoro.util.PomodoroTimer;
import net.mornati.epomodoro.util.UIUtil;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
/**
* The activator class controls the plug-in life cycle
*/
public class Activator extends AbstractUIPlugin {
// The plug-in ID
public static final String PLUGIN_ID="net.mornati.epomodoro"; //$NON-NLS-1$
// The shared instance
private static Activator plugin;
private Communication communication;
private ISchedulingRule jobRule;
private PomodoroTimer timer;
private boolean showDialog=false;
private Timer scheduler=new Timer();
final java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("mm : ss");
private static final Logger LOG=Logger.getLogger(Activator.class.getName());
private List<Button> startButtons=new ArrayList<Button>();
private List<Label> counterLabels=new ArrayList<Label>();
/*
* (non-Javadoc)
*
* @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
*/
@Override
public void start(BundleContext context) throws Exception {
super.start(context);
plugin=this;
IPreferenceStore preferenceStore=Activator.getDefault().getPreferenceStore();
int preferenceTime=preferenceStore.getInt(PomodoroPreferencePage.POMODORO_TIME);
createTimer(preferenceTime * 60 * 1000, PomodoroTimer.TYPE_WORK);
initCommunication();
sendTimerMessage();
}
/*
* (non-Javadoc)
*
* @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
*/
@Override
public void stop(BundleContext context) throws Exception {
plugin=null;
super.stop(context);
scheduler.cancel();
scheduler=null;
timer.interrupt();
timer=null;
communication.close();
}
/**
* Returns the shared instance
*
* @return the shared instance
*/
public static Activator getDefault() {
return plugin;
}
/**
* Returns an image descriptor for the image file at the given plug-in relative path
*
* @param path
* the path
* @return the image descriptor
*/
public static ImageDescriptor getImageDescriptor(String path) {
return imageDescriptorFromPlugin(PLUGIN_ID, path);
}
public void initCommunication() {
Job job=new Job("ConnectToJGroups") {
@Override
protected IStatus run(IProgressMonitor monitor) {
IPreferenceStore preferenceStore=getPreferenceStore();
String groupName=preferenceStore.getString(PomodoroPreferencePage.GROUP_NAME);
boolean discardOwnMessage=preferenceStore.getBoolean(PomodoroPreferencePage.DISCARD_OWN_MESSAGE);
communication=Communication.getInstance();
try {
communication.connect(groupName, discardOwnMessage);
} catch (Exception e) {
e.printStackTrace();
}
return Status.OK_STATUS;
}
};
job.setUser(false);
job.setRule(getRule());
job.schedule();
}
private void sendTimerMessage() {
TimerTask task=new TimerTask() {
@Override
public void run() {
if (communication != null && communication.isConnected() && timer != null) {
TimerMessage message=(TimerMessage) Communication.createMessage(TimerMessage.class);
message.setTimer(timer.getFormatTime());
message.setStatus(timer.getStatus());
try {
communication.sendMessage(message);
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
scheduler.schedule(task, 1000, 1000);
}
public Communication getCommunication() {
return this.communication;
}
public ISchedulingRule getRule() {
if (jobRule == null) {
jobRule=new ConflictRule();
}
return jobRule;
}
public PomodoroTimer getTimer() {
return timer;
}
public PomodoroTimer createTimer(long totalTime, int type) {
if (timer == null || timer.getTime() == 0) {
timer=new PomodoroTimer(totalTime, type);
}
return timer;
}
public PomodoroTimer resetTimer(long totalTime, int type) {
if (timer != null) {
timer.interrupt();
timer=new PomodoroTimer(totalTime, type);
}
return timer;
}
public void setShowDialog(boolean showDialog) {
this.showDialog=showDialog;
}
public boolean isShowDialog() {
return showDialog;
}
public Timer getScheduler() {
return scheduler;
}
public void subscribeStartButton(Button button) {
startButtons.add(button);
}
public void subscribeCounterLabel(Label label) {
counterLabels.add(label);
}
public List<Button> getStartButtons() {
return startButtons;
}
public void scheduleTimer(final int changeInterval) {
final PomodoroTimer internalTimer;
if (timer == null) {
internalTimer=createTimer(timer.getConfigWorkTime(), PomodoroTimer.TYPE_WORK);
} else {
internalTimer=timer;
}
Display.getDefault().timerExec(changeInterval, new Runnable() {
public void run() {
if (internalTimer != null) {
for (Label countdown : counterLabels) {
if (!countdown.isDisposed()) {
if (timer.getType() == PomodoroTimer.TYPE_WORK) {
countdown.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_RED));
} else {
countdown.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_GREEN));
}
countdown.setText(internalTimer.getFormatTime());
}
}
if (Activator.getDefault().isShowDialog()) {
String message=(timer.getType() == PomodoroTimer.TYPE_WORK ? "Work " : "Pause ") + "Time finished";
MessageDialog.openInformation(Display.getDefault().getActiveShell(), "Pomodoro Timer Finished", message);
setShowDialog(false);
}
scheduleTimer(changeInterval);
} else {
for (Label countdown : counterLabels) {
if (!countdown.isDisposed()) {
countdown.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_RED));
countdown.setText(sdf.format(new Date(timer.getConfigWorkTime())));
}
}
scheduleTimer(changeInterval);
}
}
});
}
public void checkTimerStatus() {
TimerTask task=new TimerTask() {
@Override
public void run() {
PomodoroTimer newTimer;
if (timer == null || timer.isInterrupted()) {
return;
}
if (timer.getStatus().equals(PomodoroTimer.STATUS_FINISHED)) {
setShowDialog(true);
while (isShowDialog()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
LOG.log(Level.SEVERE, "Error sleeping Thread", e);
}
}
UIUtil.showReceivedMessages();
IPreferenceStore preferenceStore=getPreferenceStore();
if (timer != null && timer.getType() == PomodoroTimer.TYPE_WORK) {
int pauseTimer=preferenceStore.getInt(PomodoroPreferencePage.POMODORO_PAUSE) * 60 * 1000;
newTimer=createTimer(pauseTimer, PomodoroTimer.TYPE_PAUSE);
if (preferenceStore.getBoolean(PomodoroPreferencePage.WORK_PAUSE_AUTO_SWITCH)) {
newTimer.start();
}
} else {
newTimer=createTimer(timer.getConfigWorkTime(), PomodoroTimer.TYPE_WORK);
}
}
}
};
scheduler.schedule(task, 1000, 1000);
}
}