KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.resources.IContainer;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.jface.viewers.*;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.swt.layout.GridData;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.team.internal.ui.TeamUIMessages;
25 import org.eclipse.team.ui.synchronize.*;
26 import org.eclipse.ui.IWorkingSet;
27 import org.eclipse.ui.PlatformUI;
28 import org.eclipse.ui.dialogs.ContainerCheckedTreeViewer;
29 import org.eclipse.ui.ide.IDE;
30 import org.eclipse.ui.model.BaseWorkbenchContentProvider;
31 import org.eclipse.ui.model.WorkbenchLabelProvider;
32 import org.eclipse.ui.views.navigator.ResourceSorter;
33
34 /**
35  * Page that allows the user to select a set of resources that are managed by a subscriber
36  * participant. Callers can provide a scope hint to determine the initial selection for the
37  * resource list. By default, the resources in the current selection are checked, otherwise
38  * all resources are checked.
39  *
40  * @since 3.0
41  */

42 public class GlobalRefreshResourceSelectionPage extends GlobalRefreshElementSelectionPage {
43     
44     private List JavaDoc resources;
45     
46     /**
47      * Content provider that accepts a <code>SubscriberParticipant</code> as input and
48      * returns the participants root resources.
49      */

50     class MyContentProvider extends BaseWorkbenchContentProvider {
51         public Object JavaDoc[] getChildren(Object JavaDoc element) {
52             if(element instanceof List JavaDoc) {
53                 return (IResource[]) ((List JavaDoc)element).toArray(new IResource[((List JavaDoc)element).size()]);
54             }
55             return super.getChildren(element);
56         }
57     }
58     
59     /**
60      * Label decorator that will display the full path for participant roots that are folders. This
61      * is useful for participants that have non-project roots.
62      */

63     class MyLabelProvider extends LabelProvider {
64         private LabelProvider workbenchProvider = new WorkbenchLabelProvider();
65         public String JavaDoc getText(Object JavaDoc element) {
66             if(element instanceof IContainer) {
67                 IContainer c = (IContainer)element;
68                 if(c.getType() != IResource.PROJECT && resources.contains(c)) {
69                     return c.getFullPath().toString();
70                 }
71             }
72             return workbenchProvider.getText(element);
73         }
74         public Image getImage(Object JavaDoc element) {
75             return workbenchProvider.getImage(element);
76         }
77     }
78         
79     /**
80      * Create a new page for the given participant. The scope hint will determine the initial selection.
81      *
82      * @param resources the resources to synchronize
83      */

84     public GlobalRefreshResourceSelectionPage(IResource[] resources) {
85         super(TeamUIMessages.GlobalRefreshResourceSelectionPage_1);
86         // Caching the roots so that the decorator doesn't have to recompute all the time.
87
this.resources = Arrays.asList(resources);
88         setDescription(TeamUIMessages.GlobalRefreshResourceSelectionPage_2);
89         setTitle(TeamUIMessages.GlobalRefreshResourceSelectionPage_3);
90     }
91     
92     protected ContainerCheckedTreeViewer createViewer(Composite top) {
93         GridData data;
94         ContainerCheckedTreeViewer fViewer = new ContainerCheckedTreeViewer(top, SWT.BORDER);
95         data = new GridData(GridData.FILL_BOTH);
96         //data.widthHint = 200;
97
data.heightHint = 100;
98         fViewer.getControl().setLayoutData(data);
99         fViewer.setContentProvider(new MyContentProvider());
100         fViewer.setLabelProvider(new DecoratingLabelProvider(new MyLabelProvider(), PlatformUI.getWorkbench().getDecoratorManager().getLabelDecorator()));
101         fViewer.addCheckStateListener(new ICheckStateListener() {
102
103             public void checkStateChanged(CheckStateChangedEvent event) {
104                 updateOKStatus();
105             }
106         });
107         fViewer.setSorter(new ResourceSorter(ResourceSorter.NAME));
108         fViewer.setInput(resources);
109         return fViewer;
110     }
111     
112     protected void checkAll() {
113         getViewer().setCheckedElements(resources.toArray());
114     }
115     
116     protected boolean checkWorkingSetElements() {
117         List JavaDoc allWorkingSetResources = new ArrayList JavaDoc();
118         IWorkingSet[] workingSets = getWorkingSets();
119         for (int i = 0; i < workingSets.length; i++) {
120             IWorkingSet set = workingSets[i];
121             allWorkingSetResources.addAll(IDE.computeSelectedResources(new StructuredSelection(set.getElements())));
122         }
123         getViewer().setCheckedElements(allWorkingSetResources.toArray(new IResource[allWorkingSetResources.size()]));
124         return !allWorkingSetResources.isEmpty();
125     }
126
127     public IResource[] getRootResources() {
128         Object JavaDoc[] objects = getRootElement();
129         IResource[] resources = new IResource[objects.length];
130         for (int i = 0; i < resources.length; i++) {
131             resources[i] = (IResource)objects[i];
132             
133         }
134         return resources;
135     }
136     
137     public ISynchronizeScope getSynchronizeScope() {
138         if (isWorkingSetSelected()) {
139             return new WorkingSetScope(getWorkingSets());
140         }
141         if (isWorkspaceSelected()) {
142             return new WorkspaceScope();
143         }
144         return new ResourceScope(getRootResources());
145     }
146
147 }
148
Popular Tags