KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 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 java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.jface.dialogs.MessageDialog;
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.team.core.synchronize.SyncInfo;
22 import org.eclipse.team.internal.ui.*;
23 import org.eclipse.team.internal.ui.synchronize.actions.OpenInCompareAction;
24 import org.eclipse.team.ui.TeamUI;
25 import org.eclipse.team.ui.synchronize.*;
26 import org.eclipse.ui.actions.ActionFactory;
27
28 /**
29  * This class manages the notification and setup that occurs after a refresh is completed.
30  */

31 public class RefreshUserNotificationPolicy implements IRefreshSubscriberListener {
32
33     private ISynchronizeParticipant participant;
34
35     public RefreshUserNotificationPolicy(ISynchronizeParticipant participant) {
36         this.participant = participant;
37     }
38
39     /*
40      * (non-Javadoc)
41      * @see org.eclipse.team.internal.ui.jobs.IRefreshSubscriberListener#refreshStarted(org.eclipse.team.internal.ui.jobs.IRefreshEvent)
42      */

43     public void refreshStarted(final IRefreshEvent event) {
44         TeamUIPlugin.getStandardDisplay().asyncExec(new Runnable JavaDoc() {
45             public void run() {
46                 if (event.getRefreshType() == IRefreshEvent.USER_REFRESH && event.getParticipant() == participant) {
47                     ISynchronizeView view = TeamUI.getSynchronizeManager().showSynchronizeViewInActivePage();
48                     if (view != null) {
49                         view.display(participant);
50                     }
51                 }
52             }
53         });
54     }
55
56     /*
57      * (non-Javadoc)
58      * @see org.eclipse.team.internal.ui.jobs.IRefreshSubscriberListener#refreshDone(org.eclipse.team.internal.ui.jobs.IRefreshEvent)
59      */

60     public ActionFactory.IWorkbenchAction refreshDone(final IRefreshEvent event) {
61         // Ensure that this event was generated for this participant
62
if (event.getParticipant() != participant) return null;
63         // If the event is for a canceled operation, there's nothing to do
64
int severity = event.getStatus().getSeverity();
65         if(severity == IStatus.CANCEL || severity == IStatus.ERROR) return null;
66         // Decide on what action to take after the refresh is completed
67
return new WorkbenchAction() {
68             public void run() {
69                 boolean prompt = (event.getStatus().getCode() == IRefreshEvent.STATUS_NO_CHANGES);
70                 
71                 prompt = handleRefreshDone(event, prompt);
72                 
73                 // Prompt user if preferences are set for this type of refresh.
74
if (prompt) {
75                     notifyIfNeededModal(event);
76                 }
77                 setToolTipText(getToolTipText());
78                 if (event.isLink()) {
79                     // Go to the sync view
80
ISynchronizeView view = TeamUI.getSynchronizeManager().showSynchronizeViewInActivePage();
81                     if (view != null) {
82                         view.display(participant);
83                     }
84                 }
85             }
86             
87             public String JavaDoc getToolTipText() {
88                 boolean prompt = (event.getStatus().getCode() == IRefreshEvent.STATUS_NO_CHANGES);
89                 if(prompt) {
90                     return TeamUIMessages.RefreshSubscriberJob_2a;
91                 } else {
92                     return NLS.bind(TeamUIMessages.RefreshSubscriberJob_2b, new String JavaDoc[] { Utils.shortenText(SynchronizeView.MAX_NAME_LENGTH, participant.getName()) });
93                 }
94             }
95         };
96     }
97     
98     private void notifyIfNeededModal(final IRefreshEvent event) {
99         TeamUIPlugin.getStandardDisplay().asyncExec(new Runnable JavaDoc() {
100             public void run() {
101                 String JavaDoc title = (event.getRefreshType() == IRefreshEvent.SCHEDULED_REFRESH ?
102                         NLS.bind(TeamUIMessages.RefreshCompleteDialog_4a, new String JavaDoc[] { Utils.getTypeName(participant) }) :
103                             NLS.bind(TeamUIMessages.RefreshCompleteDialog_4, new String JavaDoc[] { Utils.getTypeName(participant) })
104                             );
105                 MessageDialog.openInformation(Utils.getShell(null), title, event.getStatus().getMessage());
106             }
107         });
108     }
109     
110     protected boolean handleRefreshDone(final IRefreshEvent event, boolean prompt) {
111         if (participant instanceof SubscriberParticipant) {
112             SubscriberParticipant sp = (SubscriberParticipant) participant;
113             SyncInfo[] infos = ((RefreshChangeListener)event.getChangeDescription()).getChanges();
114             List JavaDoc selectedResources = new ArrayList JavaDoc();
115             selectedResources.addAll(Arrays.asList(((RefreshChangeListener)event.getChangeDescription()).getResources()));
116             for (int i = 0; i < infos.length; i++) {
117                 selectedResources.add(infos[i].getLocal());
118             }
119             IResource[] resources = (IResource[]) selectedResources.toArray(new IResource[selectedResources.size()]);
120             
121             // If it's a file, simply show the compare editor
122
if (resources.length == 1 && resources[0].getType() == IResource.FILE) {
123                 IResource file = resources[0];
124                 SyncInfo info = sp.getSubscriberSyncInfoCollector().getSyncInfoSet().getSyncInfo(file);
125                 if(info != null) {
126                     OpenInCompareAction.openCompareEditor(participant, info, null);
127                     prompt = false;
128                 }
129             }
130         }
131         // TODO: Implement one change case for model participant
132
return prompt;
133     }
134 }
135
Popular Tags