KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > synchronize > SubscriberRefreshSchedule


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ui.synchronize;
12
13 import com.ibm.icu.text.DateFormat;
14 import java.util.Date JavaDoc;
15
16 import org.eclipse.core.runtime.jobs.Job;
17 import org.eclipse.osgi.util.NLS;
18 import org.eclipse.team.internal.ui.*;
19 import org.eclipse.team.ui.synchronize.ISynchronizeParticipant;
20 import org.eclipse.ui.IMemento;
21 import org.eclipse.ui.actions.ActionFactory;
22
23 /**
24  * Schedule to refresh a subscriber at a specified interval. The schedule can be disabled or enabled
25  * and will create the refresh job.
26  *
27  * @since 3.0
28  */

29 public class SubscriberRefreshSchedule {
30     private long refreshInterval = 3600; // 1 hour default
31

32     private boolean enabled = false;
33     
34     private RefreshParticipantJob job;
35     
36     private IRefreshable refreshable;
37     
38     private IRefreshEvent lastRefreshEvent;
39     
40     /**
41      * Key for settings in memento
42      */

43     private static final String JavaDoc CTX_REFRESHSCHEDULE_INTERVAL = TeamUIPlugin.ID + ".CTX_REFRESHSCHEDULE_INTERVAL"; //$NON-NLS-1$
44

45     /**
46      * Key for schedule in memento
47      */

48     private static final String JavaDoc CTX_REFRESHSCHEDULE_ENABLED = TeamUIPlugin.ID + ".CTX_REFRESHSCHEDULE_ENABLED"; //$NON-NLS-1$
49

50     private IRefreshSubscriberListener refreshSubscriberListener = new IRefreshSubscriberListener() {
51         public void refreshStarted(IRefreshEvent event) {
52         }
53         public ActionFactory.IWorkbenchAction refreshDone(final IRefreshEvent event) {
54             if (getRefreshable(event.getParticipant()) == refreshable) {
55                 lastRefreshEvent = event;
56                 if(enabled && event.getRefreshType() == IRefreshEvent.SCHEDULED_REFRESH) {
57                     RefreshUserNotificationPolicy policy = new RefreshUserNotificationPolicy(refreshable.getParticipant());
58                     policy.refreshDone(event);
59                 }
60             }
61             return null;
62         }
63         private IRefreshable getRefreshable(ISynchronizeParticipant participant) {
64             return (IRefreshable)Utils.getAdapter(participant, IRefreshable.class);
65         }
66     };
67     
68     
69     public SubscriberRefreshSchedule(IRefreshable refreshable) {
70         this.refreshable = refreshable;
71         RefreshParticipantJob.addRefreshListener(refreshSubscriberListener);
72     }
73
74     /**
75      * @return Returns the enabled.
76      */

77     public boolean isEnabled() {
78         return enabled;
79     }
80
81     /**
82      * @param enabled The enabled to set.
83      */

84     public void setEnabled(boolean enabled, boolean allowedToStart) {
85         boolean wasEnabled = isEnabled();
86         this.enabled = enabled;
87         if(enabled && ! wasEnabled) {
88             if(allowedToStart) {
89                 startJob();
90             }
91         } else {
92             stopJob();
93         }
94     }
95     
96     /**
97      * @return Returns the refreshInterval in seconds.
98      */

99     public long getRefreshInterval() {
100         return refreshInterval;
101     }
102
103     public ISynchronizeParticipant getParticipant() {
104         return refreshable.getParticipant();
105     }
106     
107     /**
108      * @param refreshInterval The refreshInterval to set.
109      */

110     public void setRefreshInterval(long refreshInterval) {
111         if(refreshInterval != getRefreshInterval()) {
112             stopJob();
113             this.refreshInterval = refreshInterval;
114             if(isEnabled()) {
115                 startJob();
116             }
117         }
118     }
119     
120     public void startJob() {
121         if(job == null) {
122             job = refreshable.createJob(getRefreshIntervalAsString());
123             job.setUser(false);
124         } else if(job.getState() != Job.NONE){
125             stopJob();
126         }
127         job.setRefreshInterval(getRefreshInterval());
128         job.setRestartOnCancel(true);
129         job.setReschedule(true);
130         // Schedule delay is in mills.
131
job.schedule(getRefreshInterval() * 1000);
132     }
133     
134     protected void stopJob() {
135         if(job != null) {
136             job.setRestartOnCancel(false /* don't restart the job */);
137             job.setReschedule(false);
138             job.cancel();
139             job = null;
140         }
141     }
142
143     public void dispose() {
144         stopJob();
145         RefreshParticipantJob.removeRefreshListener(refreshSubscriberListener);
146     }
147     
148     public void saveState(IMemento memento) {
149         memento.putString(CTX_REFRESHSCHEDULE_ENABLED, Boolean.toString(enabled));
150         memento.putInteger(CTX_REFRESHSCHEDULE_INTERVAL, (int)refreshInterval);
151     }
152
153     public static SubscriberRefreshSchedule init(IMemento memento, IRefreshable refreshable) {
154         SubscriberRefreshSchedule schedule = new SubscriberRefreshSchedule(refreshable);
155         if(memento != null) {
156             String JavaDoc enabled = memento.getString(CTX_REFRESHSCHEDULE_ENABLED);
157             int interval = memento.getInteger(CTX_REFRESHSCHEDULE_INTERVAL).intValue();
158             schedule.setRefreshInterval(interval);
159             schedule.setEnabled("true".equals(enabled) ? true : false, false /* don't start job */); //$NON-NLS-1$
160
}
161         // Use the defaults if a schedule hasn't been saved or can't be found.
162
return schedule;
163     }
164
165     public static String JavaDoc refreshEventAsString(IRefreshEvent event) {
166         if(event == null) {
167             return TeamUIMessages.SyncViewPreferencePage_lastRefreshRunNever;
168         }
169         long stopMills = event.getStopTime();
170         StringBuffer JavaDoc text = new StringBuffer JavaDoc();
171         if(stopMills <= 0) {
172             text.append(TeamUIMessages.SyncViewPreferencePage_lastRefreshRunNever);
173         } else {
174             Date JavaDoc lastTimeRun = new Date JavaDoc(stopMills);
175             text.append(DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(lastTimeRun));
176         }
177         int changeCount = event.getChangeDescription().getChangeCount();
178         if (changeCount == 0) {
179             text.append(TeamUIMessages.RefreshSchedule_7);
180         } else if (changeCount == 1) {
181             text.append(NLS.bind(TeamUIMessages.RefreshSchedule_changesSingular, new String JavaDoc[] { Integer.toString(changeCount) }));
182         } else {
183             text.append(NLS.bind(TeamUIMessages.RefreshSchedule_changesPlural, new String JavaDoc[] { Integer.toString(changeCount) }));
184         }
185         return text.toString();
186     }
187     
188     public IRefreshEvent getLastRefreshEvent() {
189         return lastRefreshEvent;
190     }
191     
192     private String JavaDoc getRefreshIntervalAsString() {
193         boolean hours = false;
194         long seconds = getRefreshInterval();
195         if(seconds <= 60) {
196             seconds = 60;
197         }
198         long minutes = seconds / 60;
199         if(minutes >= 60) {
200             minutes = minutes / 60;
201             hours = true;
202         }
203         String JavaDoc unit;
204         if(minutes >= 1) {
205             unit = (hours ? TeamUIMessages.RefreshSchedule_9 : TeamUIMessages.RefreshSchedule_10); //
206
} else {
207             unit = (hours ? TeamUIMessages.RefreshSchedule_11 : TeamUIMessages.RefreshSchedule_12); //
208
}
209         return NLS.bind(TeamUIMessages.RefreshSchedule_13, new String JavaDoc[] { Long.toString(minutes), unit });
210     }
211
212     public IRefreshable getRefreshable() {
213         return refreshable;
214     }
215 }
216
Popular Tags