KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 org.eclipse.core.runtime.IProgressMonitor;
14 import org.eclipse.core.runtime.MultiStatus;
15 import org.eclipse.jface.dialogs.ErrorDialog;
16 import org.eclipse.osgi.util.NLS;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.*;
21 import org.eclipse.team.core.ITeamStatus;
22 import org.eclipse.team.core.synchronize.*;
23 import org.eclipse.team.internal.ui.*;
24 import org.eclipse.team.ui.synchronize.*;
25 import org.eclipse.ui.forms.events.HyperlinkAdapter;
26 import org.eclipse.ui.forms.events.HyperlinkEvent;
27 import org.eclipse.ui.forms.widgets.Hyperlink;
28
29 /**
30  * Section shown in a participant page to show the changes for this participant. This
31  * includes a diff viewer for browsing the changes.
32  *
33  * @since 3.0
34  */

35 public class SyncInfoSetChangesSection extends ForwardingChangesSection {
36     
37     /**
38      * Boolean that indicates whether the error page is being shown.
39      * This is used to avoid redrawing the error page when new events come in
40      */

41     private boolean showingError;
42
43     /**
44      * Register an action contribution in order to receive model
45      * change notification so that we can update message to user and totals.
46      */

47     private SynchronizePageActionGroup changedListener = new SynchronizePageActionGroup() {
48         public void modelChanged(ISynchronizeModelElement root) {
49             calculateDescription();
50         }
51     };
52     
53     /**
54      * Listener registered with the subscriber sync info set which contains
55      * all out-of-sync resources for the subscriber.
56      */

57     private ISyncInfoSetChangeListener subscriberListener = new ISyncInfoSetChangeListener() {
58         public void syncInfoSetReset(SyncInfoSet set, IProgressMonitor monitor) {
59             // Handled by output set listener
60
}
61         public void syncInfoChanged(ISyncInfoSetChangeEvent event, IProgressMonitor monitor) {
62             calculateDescription();
63         }
64         public void syncInfoSetErrors(SyncInfoSet set, ITeamStatus[] errors, IProgressMonitor monitor) {
65             // Handled by output set listener
66
}
67     };
68     
69     /**
70      * Listener registered with the output sync info set which contains
71      * only the visible sync info.
72      */

73     private ISyncInfoSetChangeListener outputSetListener = new ISyncInfoSetChangeListener() {
74         public void syncInfoSetReset(SyncInfoSet set, IProgressMonitor monitor) {
75             calculateDescription();
76         }
77         public void syncInfoChanged(ISyncInfoSetChangeEvent event, IProgressMonitor monitor) {
78             // Input changed listener will call calculateDescription()
79
// The input will then react to output set changes
80
}
81         public void syncInfoSetErrors(SyncInfoSet set, ITeamStatus[] errors, IProgressMonitor monitor) {
82             calculateDescription();
83         }
84     };
85     
86     /**
87      * Create a changes section on the following page.
88      *
89      * @param parent the parent control
90      * @param page the page showing this section
91      */

92     public SyncInfoSetChangesSection(Composite parent, AbstractSynchronizePage page, ISynchronizePageConfiguration configuration) {
93         super(parent, page, configuration);
94     }
95     
96     /* (non-Javadoc)
97      * @see org.eclipse.team.internal.ui.synchronize.ChangesSection#initializeChangesViewer()
98      */

99     protected void initializeChangesViewer() {
100         super.initializeChangesViewer();
101         getConfiguration().addActionContribution(changedListener);
102         getParticipantSyncInfoSet().addSyncSetChangedListener(subscriberListener);
103         getVisibleSyncInfoSet().addSyncSetChangedListener(outputSetListener);
104     }
105     
106     protected void calculateDescription() {
107         SyncInfoTree syncInfoTree = getVisibleSyncInfoSet();
108         if (syncInfoTree.getErrors().length > 0) {
109             if (!showingError) {
110                 TeamUIPlugin.getStandardDisplay().asyncExec(new Runnable JavaDoc() {
111                     public void run() {
112                         updatePage(getErrorComposite(getContainer()));
113                         showingError = true;
114                     }
115                 });
116             }
117             return;
118         }
119         
120         showingError = false;
121         super.calculateDescription();
122     }
123
124     protected long getChangesInMode(int candidateMode) {
125         SyncInfoSet participantSet = getParticipantSyncInfoSet();
126         long numChanges;
127         switch (candidateMode) {
128         case ISynchronizePageConfiguration.OUTGOING_MODE:
129             numChanges = participantSet.countFor(SyncInfo.OUTGOING, SyncInfo.DIRECTION_MASK);
130             break;
131         case ISynchronizePageConfiguration.INCOMING_MODE:
132             numChanges = participantSet.countFor(SyncInfo.INCOMING, SyncInfo.DIRECTION_MASK);
133             break;
134         case ISynchronizePageConfiguration.BOTH_MODE:
135             numChanges = participantSet.countFor(SyncInfo.INCOMING, SyncInfo.DIRECTION_MASK)
136                 + participantSet.countFor(SyncInfo.OUTGOING, SyncInfo.DIRECTION_MASK);
137             break;
138         default:
139             numChanges = 0;
140             break;
141         }
142         return numChanges;
143     }
144     
145     /*
146      * Return the candidate mode based on the presence of unfiltered changes
147      * and the modes supported by the page.
148      */

149     protected int getCandidateMode() {
150         SyncInfoSet participantSet = getParticipantSyncInfoSet();
151         SynchronizePageConfiguration configuration = (SynchronizePageConfiguration)getConfiguration();
152         long outgoingChanges = participantSet.countFor(SyncInfo.OUTGOING, SyncInfo.DIRECTION_MASK);
153         if (outgoingChanges > 0) {
154             if (configuration.isModeSupported(ISynchronizePageConfiguration.OUTGOING_MODE)) {
155                 return ISynchronizePageConfiguration.OUTGOING_MODE;
156             }
157             if (configuration.isModeSupported(ISynchronizePageConfiguration.BOTH_MODE)) {
158                 return ISynchronizePageConfiguration.BOTH_MODE;
159             }
160         }
161         long incomingChanges = participantSet.countFor(SyncInfo.INCOMING, SyncInfo.DIRECTION_MASK);
162         if (incomingChanges > 0) {
163             if (configuration.isModeSupported(ISynchronizePageConfiguration.INCOMING_MODE)) {
164                 return ISynchronizePageConfiguration.INCOMING_MODE;
165             }
166             if (configuration.isModeSupported(ISynchronizePageConfiguration.BOTH_MODE)) {
167                 return ISynchronizePageConfiguration.BOTH_MODE;
168             }
169         }
170         return configuration.getMode();
171     }
172     
173     public void dispose() {
174         super.dispose();
175         getConfiguration().removeActionContribution(changedListener);
176         getParticipantSyncInfoSet().removeSyncSetChangedListener(subscriberListener);
177         getVisibleSyncInfoSet().removeSyncSetChangedListener(outputSetListener);
178     }
179     
180     private Composite getErrorComposite(Composite parent) {
181         Composite composite = new Composite(parent, SWT.NONE);
182         composite.setBackground(getBackgroundColor());
183         GridLayout layout = new GridLayout();
184         layout.numColumns = 2;
185         composite.setLayout(layout);
186         GridData data = new GridData(GridData.FILL_BOTH);
187         data.grabExcessVerticalSpace = true;
188         composite.setLayoutData(data);
189
190         Hyperlink link = new Hyperlink(composite, SWT.WRAP);
191         link.setText(TeamUIMessages.ChangesSection_8);
192         link.addHyperlinkListener(new HyperlinkAdapter() {
193             public void linkActivated(HyperlinkEvent e) {
194                 showErrors();
195             }
196         });
197         link.setBackground(getBackgroundColor());
198         link.setUnderlined(true);
199         
200         link = new Hyperlink(composite, SWT.WRAP);
201         link.setText(TeamUIMessages.ChangesSection_9);
202         link.addHyperlinkListener(new HyperlinkAdapter() {
203             public void linkActivated(HyperlinkEvent e) {
204                 getPage().reset();
205             }
206         });
207         link.setBackground(getBackgroundColor());
208         link.setUnderlined(true);
209         
210         createDescriptionLabel(composite, NLS.bind(TeamUIMessages.ChangesSection_10, new String JavaDoc[] { Utils.shortenText(SynchronizeView.MAX_NAME_LENGTH, getConfiguration().getParticipant().getName()) }));
211
212         return composite;
213     }
214     
215     /* private */ void showErrors() {
216         ITeamStatus[] status = getVisibleSyncInfoSet().getErrors();
217         String JavaDoc title = TeamUIMessages.ChangesSection_11;
218         if (status.length == 1) {
219             ErrorDialog.openError(getShell(), title, status[0].getMessage(), status[0]);
220         } else {
221             MultiStatus multi = new MultiStatus(TeamUIPlugin.ID, 0, status, TeamUIMessages.ChangesSection_12, null);
222             ErrorDialog.openError(getShell(), title, null, multi);
223         }
224     }
225     
226     protected int getChangesCount() {
227         return getParticipantSyncInfoSet().size();
228     }
229     
230     protected long getVisibleChangesCount() {
231         return getVisibleSyncInfoSet().size();
232     }
233     
234     /*
235      * Return the sync info set that contains the visible resources
236      */

237     private SyncInfoTree getVisibleSyncInfoSet() {
238         return (SyncInfoTree)getConfiguration().getProperty(ISynchronizePageConfiguration.P_SYNC_INFO_SET);
239     }
240     
241     /*
242      * Return the sync info set for the participant that contains all the resources
243      * including those that may not be visible due to filters (e.g. mode)
244      */

245     private SyncInfoSet getParticipantSyncInfoSet() {
246         return (SyncInfoSet)getConfiguration().getProperty(SynchronizePageConfiguration.P_WORKING_SET_SYNC_INFO_SET);
247     }
248 }
249
Popular Tags