1 11 package org.eclipse.team.internal.ui.synchronize; 12 13 import com.ibm.icu.text.DateFormat; 14 import java.util.Date ; 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 29 public class SubscriberRefreshSchedule { 30 private long refreshInterval = 3600; 32 private boolean enabled = false; 33 34 private RefreshParticipantJob job; 35 36 private IRefreshable refreshable; 37 38 private IRefreshEvent lastRefreshEvent; 39 40 43 private static final String CTX_REFRESHSCHEDULE_INTERVAL = TeamUIPlugin.ID + ".CTX_REFRESHSCHEDULE_INTERVAL"; 45 48 private static final String CTX_REFRESHSCHEDULE_ENABLED = TeamUIPlugin.ID + ".CTX_REFRESHSCHEDULE_ENABLED"; 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 77 public boolean isEnabled() { 78 return enabled; 79 } 80 81 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 99 public long getRefreshInterval() { 100 return refreshInterval; 101 } 102 103 public ISynchronizeParticipant getParticipant() { 104 return refreshable.getParticipant(); 105 } 106 107 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 job.schedule(getRefreshInterval() * 1000); 132 } 133 134 protected void stopJob() { 135 if(job != null) { 136 job.setRestartOnCancel(false ); 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 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 ); } 161 return schedule; 163 } 164 165 public static String refreshEventAsString(IRefreshEvent event) { 166 if(event == null) { 167 return TeamUIMessages.SyncViewPreferencePage_lastRefreshRunNever; 168 } 169 long stopMills = event.getStopTime(); 170 StringBuffer text = new StringBuffer (); 171 if(stopMills <= 0) { 172 text.append(TeamUIMessages.SyncViewPreferencePage_lastRefreshRunNever); 173 } else { 174 Date lastTimeRun = new Date (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 [] { Integer.toString(changeCount) })); 182 } else { 183 text.append(NLS.bind(TeamUIMessages.RefreshSchedule_changesPlural, new String [] { Integer.toString(changeCount) })); 184 } 185 return text.toString(); 186 } 187 188 public IRefreshEvent getLastRefreshEvent() { 189 return lastRefreshEvent; 190 } 191 192 private String 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 unit; 204 if(minutes >= 1) { 205 unit = (hours ? TeamUIMessages.RefreshSchedule_9 : TeamUIMessages.RefreshSchedule_10); } else { 207 unit = (hours ? TeamUIMessages.RefreshSchedule_11 : TeamUIMessages.RefreshSchedule_12); } 209 return NLS.bind(TeamUIMessages.RefreshSchedule_13, new String [] { Long.toString(minutes), unit }); 210 } 211 212 public IRefreshable getRefreshable() { 213 return refreshable; 214 } 215 } 216 | Popular Tags |