KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > dialogs > MappingSelectionDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.dialogs;
12
13 import org.eclipse.core.resources.mapping.ResourceMapping;
14 import org.eclipse.jface.dialogs.IDialogConstants;
15 import org.eclipse.jface.util.IPropertyChangeListener;
16 import org.eclipse.jface.util.PropertyChangeEvent;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.widgets.*;
20
21 /**
22  * Dialog that will display any mappings that contain resources whose
23  * sync state match the provided filter.
24  */

25 public abstract class MappingSelectionDialog extends DetailsDialog implements IPropertyChangeListener {
26
27     private final ResourceMapping[] mappings;
28     private ResourceMapping[] checkedMappings;
29     private ResourceMappingSelectionArea mappingArea;
30     private ResourceMappingResourceDisplayArea resourceArea;
31     private final IResourceMappingResourceFilter filter;
32
33     protected MappingSelectionDialog(Shell parentShell, String JavaDoc dialogTitle, ResourceMapping[] mappings, IResourceMappingResourceFilter filter) {
34         super(parentShell, dialogTitle);
35         this.mappings = mappings;
36         this.filter = filter;
37     }
38
39     /* (non-Javadoc)
40      * @see org.eclipse.team.internal.ui.dialogs.DetailsDialog#createMainDialogArea(org.eclipse.swt.widgets.Composite)
41      */

42     protected void createMainDialogArea(Composite parent) {
43         if (mappings.length == 1) {
44             // There is only one mapping so just ask for a yes/no on including it
45
createWrappingLabel(parent, getSingleMappingMessage(mappings[0]));
46         } else {
47             // Allow the user to choose which mappings to include
48
createMappingSelectionArea(parent);
49         }
50     }
51
52     /*
53      * Create a list that allows the selection of mappings via checkbox
54      */

55     private void createMappingSelectionArea(Composite parent) {
56         Composite composite = createComposite(parent);
57         mappingArea = new ResourceMappingSelectionArea(mappings, true, true);
58         mappingArea.setDescription(getMultipleMappingsMessage());
59         mappingArea.addPropertyChangeListener(this);
60         mappingArea.createArea(composite);
61         // Create a separator between the two sets of buttons
62
Label seperator = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
63         seperator.setLayoutData(new GridData (GridData.FILL_HORIZONTAL));
64         
65         checkedMappings = mappingArea.getCheckedMappings();
66     }
67
68     /* (non-Javadoc)
69      * @see org.eclipse.team.internal.ui.dialogs.DetailsDialog#createDropDownDialogArea(org.eclipse.swt.widgets.Composite)
70      */

71     protected Composite createDropDownDialogArea(Composite parent) {
72         if (resourceArea == null) {
73             ResourceMapping selectedMapping = getSelectedMapping();
74             resourceArea = new ResourceMappingResourceDisplayArea(selectedMapping, getResourceListMessage(selectedMapping), filter);
75         }
76         Composite c = createComposite(parent);
77         resourceArea.createArea(c);
78         return c;
79     }
80
81     private ResourceMapping getSelectedMapping() {
82         if (mappingArea != null)
83             return mappingArea.getSelectedMapping();
84         if (mappings.length == 1)
85             return mappings[0];
86         return null;
87     }
88
89     /* (non-Javadoc)
90      * @see org.eclipse.team.internal.ui.dialogs.DetailsDialog#updateEnablements()
91      */

92     protected void updateEnablements() {
93         // Can always finish
94
setPageComplete(true);
95     }
96     
97     /* (non-Javadoc)
98      * @see org.eclipse.team.internal.ui.dialogs.DetailsDialog#includeErrorMessage()
99      */

100     protected boolean includeErrorMessage() {
101         return false;
102     }
103     
104     /* (non-Javadoc)
105      * @see org.eclipse.team.internal.ui.dialogs.DetailsDialog#includeOkButton()
106      */

107     protected boolean includeOkButton() {
108         return mappings.length != 1;
109     }
110
111     /* (non-Javadoc)
112      * @see org.eclipse.team.internal.ui.dialogs.DetailsDialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
113      */

114     protected void createButtonsForButtonBar(Composite parent) {
115         if (mappings.length == 1) {
116             createButton(parent, IDialogConstants.YES_ID, IDialogConstants.YES_LABEL, true);
117             createButton(parent, IDialogConstants.NO_ID, IDialogConstants.NO_LABEL, false);
118         }
119         super.createButtonsForButtonBar(parent);
120     }
121     
122     /* (non-Javadoc)
123      * @see org.eclipse.team.internal.ui.dialogs.DetailsDialog#buttonPressed(int)
124      */

125     protected void buttonPressed(int id) {
126         if (IDialogConstants.YES_ID == id) {
127             checkedMappings = mappings;
128             super.buttonPressed(IDialogConstants.OK_ID);
129         } else if (IDialogConstants.NO_ID == id) {
130             checkedMappings = new ResourceMapping[0];
131             super.buttonPressed(IDialogConstants.OK_ID);
132         } else {
133             super.buttonPressed(id);
134         }
135     }
136     
137     /* (non-Javadoc)
138      * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
139      */

140     public void propertyChange(PropertyChangeEvent event) {
141         if (event.getProperty().equals(ResourceMappingSelectionArea.SELECTED_MAPPING)) {
142             if (resourceArea != null) {
143                 ResourceMapping selectedMapping = getSelectedMapping();
144                 resourceArea.setMapping(selectedMapping, getResourceListMessage(selectedMapping));
145             }
146         } else if (event.getProperty().equals(ResourceMappingSelectionArea.CHECKED_MAPPINGS)) {
147             checkedMappings = mappingArea.getCheckedMappings();
148             updateEnablements();
149         }
150     }
151     
152     /**
153      * Provide the message that is displayed if there is only a single mapping to be selected.
154      * @param mapping the mapping
155      * @return the display string
156      */

157     protected abstract String JavaDoc getSingleMappingMessage(ResourceMapping mapping);
158     
159     /**
160      * Provide the message that is displayed if there are multiple nappings to choose from.
161      * @return the diusplay string
162      */

163     protected abstract String JavaDoc getMultipleMappingsMessage();
164     
165     /**
166      * Return the label to be used in the details area for the list that
167      * displays the resources contained in the mapping
168      * @param mapping the resource mapping
169      * @return the list label
170      */

171     protected abstract String JavaDoc getResourceListMessage(ResourceMapping mapping);
172
173     /**
174      * Return the <code>ResourceMappings</code> that are being displayed
175      * by the dialog.
176      * @return the <code>ResourceMappings</code> that are being displayed
177      * by the dialog
178      */

179     public final ResourceMapping[] getMappings() {
180         return mappings;
181     }
182     
183     /**
184      * Return the <code>ResourceMappings</code> that were checked
185      * by the user.
186      * @return the <code>ResourceMappings</code> that were checked
187      */

188     protected final ResourceMapping[] getCheckedMappings() {
189         return checkedMappings;
190     }
191 }
192
Popular Tags