KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Sebastian Davids <sdavids@gmx.de> - bug 54630
11  *******************************************************************************/

12 package org.eclipse.team.internal.ui.synchronize;
13
14 import org.eclipse.jface.dialogs.Dialog;
15 import org.eclipse.jface.dialogs.IDialogConstants;
16 import org.eclipse.jface.dialogs.MessageDialog;
17 import org.eclipse.jface.resource.JFaceResources;
18 import org.eclipse.osgi.util.NLS;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.events.*;
21 import org.eclipse.swt.graphics.FontMetrics;
22 import org.eclipse.swt.graphics.GC;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.widgets.Combo;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Label;
29 import org.eclipse.swt.widgets.Text;
30 import org.eclipse.team.internal.ui.TeamUIMessages;
31 import org.eclipse.team.internal.ui.Utils;
32 import org.eclipse.team.ui.synchronize.ISynchronizeParticipant;
33
34 /**
35  * A composite that allows editing a subscriber refresh schedule. A validator can be used to allow
36  * containers to show page completion.
37  *
38  * @since 3.0
39  */

40 public class ConfigureSynchronizeScheduleComposite extends Composite {
41
42     private SubscriberRefreshSchedule schedule;
43     private Button userRefreshOnly;
44     private Button enableBackgroundRefresh;
45     private Text time;
46     private Combo hoursOrSeconds;
47     private IPageValidator validator;
48     
49     public ConfigureSynchronizeScheduleComposite(Composite parent, SubscriberRefreshSchedule schedule, IPageValidator validator) {
50         super(parent, SWT.NONE);
51         this.schedule = schedule;
52         this.validator = validator;
53         createMainDialogArea(parent);
54     }
55
56     private void initializeValues() {
57         boolean enableBackground = schedule.isEnabled();
58         boolean hours = false;
59         
60         userRefreshOnly.setSelection(! enableBackground);
61         enableBackgroundRefresh.setSelection(enableBackground);
62         
63         long seconds = schedule.getRefreshInterval();
64         if(seconds <= 60) {
65             seconds = 60;
66         }
67
68         long minutes = seconds / 60;
69         
70         if(minutes >= 60) {
71             minutes = minutes / 60;
72             hours = true;
73         }
74         hoursOrSeconds.select(hours ? 0 : 1);
75         time.setText(Long.toString(minutes));
76     }
77
78     /* (non-Javadoc)
79      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
80      */

81     protected void createMainDialogArea(Composite parent) {
82         GC gc = new GC(parent);
83         gc.setFont(JFaceResources.getDialogFont());
84         FontMetrics fontMetrics = gc.getFontMetrics();
85         gc.dispose();
86         final GridLayout gridLayout = new GridLayout();
87         gridLayout.numColumns = 2;
88         gridLayout.marginHeight = 0;
89         gridLayout.marginWidth = 0;
90         gridLayout.horizontalSpacing = Dialog.convertHorizontalDLUsToPixels(fontMetrics, IDialogConstants.HORIZONTAL_SPACING);
91         gridLayout.verticalSpacing = Dialog.convertVerticalDLUsToPixels(fontMetrics, IDialogConstants.VERTICAL_SPACING);
92         setLayout(gridLayout);
93         setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
94         Composite area = this;
95
96         createWrappingLabel(area, NLS.bind(TeamUIMessages.ConfigureRefreshScheduleDialog_1, new String JavaDoc[] { Utils.shortenText(SynchronizeView.MAX_NAME_LENGTH, schedule.getParticipant().getName()) }), 0, 2);
97         {
98             final Label label = new Label(area, SWT.WRAP);
99             final GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
100             gridData.horizontalSpan = 2;
101             label.setLayoutData(gridData);
102             label.setText(NLS.bind(TeamUIMessages.ConfigureRefreshScheduleDialog_1a, new String JavaDoc[] { SubscriberRefreshSchedule.refreshEventAsString(schedule.getLastRefreshEvent()) }));
103         }
104         {
105             userRefreshOnly = new Button(area, SWT.RADIO);
106             final GridData gridData = new GridData();
107             gridData.horizontalSpan = 2;
108             userRefreshOnly.setLayoutData(gridData);
109             userRefreshOnly.setText(TeamUIMessages.ConfigureRefreshScheduleDialog_2);
110             userRefreshOnly.addSelectionListener(new SelectionListener() {
111                 public void widgetSelected(SelectionEvent e) {
112                     updateEnablements();
113                 }
114                 public void widgetDefaultSelected(SelectionEvent e) {
115                 }
116             });
117         }
118         {
119             enableBackgroundRefresh = new Button(area, SWT.RADIO);
120             final GridData gridData = new GridData();
121             gridData.horizontalSpan = 2;
122             enableBackgroundRefresh.setLayoutData(gridData);
123             enableBackgroundRefresh.setText(TeamUIMessages.ConfigureRefreshScheduleDialog_3);
124             enableBackgroundRefresh.addSelectionListener(new SelectionListener() {
125                 public void widgetSelected(SelectionEvent e) {
126                     updateEnablements();
127                 }
128                 public void widgetDefaultSelected(SelectionEvent e) {
129                 }
130             });
131         }
132         {
133             final Composite composite = new Composite(area, SWT.NONE);
134             final GridData gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.VERTICAL_ALIGN_BEGINNING);
135             gridData.horizontalSpan = 2;
136             composite.setLayoutData(gridData);
137             final GridLayout gridLayout_1 = new GridLayout();
138             gridLayout_1.numColumns = 3;
139             gridLayout_1.marginWidth = 0;
140             gridLayout_1.marginHeight = 0;
141             gridLayout_1.horizontalSpacing = Dialog.convertHorizontalDLUsToPixels(fontMetrics, IDialogConstants.HORIZONTAL_SPACING);
142             gridLayout_1.verticalSpacing = Dialog.convertVerticalDLUsToPixels(fontMetrics, IDialogConstants.VERTICAL_SPACING);
143             composite.setLayout(gridLayout_1);
144             {
145                 final Label label = new Label(composite, SWT.NONE);
146                 label.setText(TeamUIMessages.ConfigureRefreshScheduleDialog_4);
147             }
148             {
149                 time = new Text(composite, SWT.BORDER | SWT.RIGHT);
150                 final GridData gridData_1 = new GridData();
151                 gridData_1.widthHint = 35;
152                 time.setLayoutData(gridData_1);
153                 time.addModifyListener(new ModifyListener() {
154                     public void modifyText(ModifyEvent e) {
155                         updateEnablements();
156                     }
157                 });
158                 time.addVerifyListener(new VerifyListener() {
159                     public void verifyText(VerifyEvent e) {
160                         String JavaDoc string = e.text;
161                         char [] chars = new char [string.length ()];
162                         string.getChars (0, chars.length, chars, 0);
163                         for (int i=0; i<chars.length; i++) {
164                             if (!('0' <= chars [i] && chars [i] <= '9')) {
165                                 e.doit = false;
166                                 return;
167                             }
168                         }
169                     }
170                 });
171             }
172             {
173                 hoursOrSeconds = new Combo(composite, SWT.READ_ONLY);
174                 hoursOrSeconds.setItems(new String JavaDoc[] { TeamUIMessages.ConfigureRefreshScheduleDialog_5, TeamUIMessages.ConfigureRefreshScheduleDialog_6 }); //
175
hoursOrSeconds.setLayoutData(new GridData());
176             }
177         }
178         initializeValues();
179     }
180     
181     /* (non-Javadoc)
182      * @see org.eclipse.jface.dialogs.Dialog#okPressed()
183      */

184     public void saveValues() {
185         int hours = hoursOrSeconds.getSelectionIndex();
186         try {
187             long seconds = Long.parseLong(time.getText());
188             if(hours == 0) {
189                 seconds = seconds * 3600;
190             } else {
191                 seconds = seconds * 60;
192             }
193             schedule.setRefreshInterval(seconds);
194         } catch (NumberFormatException JavaDoc e) {
195             // keep old value
196
}
197         if(schedule.isEnabled() != enableBackgroundRefresh.getSelection()) {
198             schedule.setEnabled(enableBackgroundRefresh.getSelection(), true /* allow to start */);
199         }
200         
201         // update schedule
202
ISynchronizeParticipant participant = schedule.getParticipant();
203         if (!participant.isPinned() && schedule.isEnabled()) {
204             participant.setPinned(MessageDialog.openQuestion(getShell(),
205                     NLS.bind(TeamUIMessages.ConfigureSynchronizeScheduleComposite_0, new String JavaDoc[] { Utils.getTypeName(participant) }),
206                     NLS.bind(TeamUIMessages.ConfigureSynchronizeScheduleComposite_1, new String JavaDoc[] { Utils.getTypeName(participant) })));
207         }
208         schedule.getRefreshable().setRefreshSchedule(schedule);
209     }
210
211     /* (non-Javadoc)
212      * @see org.eclipse.team.internal.ui.dialogs.DetailsDialog#updateEnablements()
213      */

214     public void updateEnablements() {
215         if (userRefreshOnly.getSelection()) {
216             validator.setComplete(null);
217         } else {
218             try {
219                 long number = Long.parseLong(time.getText());
220                 if(number <= 0) {
221                     validator.setComplete(TeamUIMessages.ConfigureRefreshScheduleDialog_7);
222                 } else {
223                     validator.setComplete(null);
224                 }
225             } catch (NumberFormatException JavaDoc e) {
226                 validator.setComplete(TeamUIMessages.ConfigureRefreshScheduleDialog_8);
227             }
228         }
229         time.setEnabled(enableBackgroundRefresh.getSelection());
230         hoursOrSeconds.setEnabled(enableBackgroundRefresh.getSelection());
231     }
232     
233     private Label createWrappingLabel(Composite parent, String JavaDoc text, int indent, int horizontalSpan) {
234         Label label = new Label(parent, SWT.LEFT | SWT.WRAP);
235         label.setText(text);
236         GridData data = new GridData();
237         data.horizontalSpan = horizontalSpan;
238         data.horizontalAlignment = GridData.FILL;
239         data.horizontalIndent = indent;
240         data.grabExcessHorizontalSpace = true;
241         data.widthHint = 400;
242         label.setLayoutData(data);
243         return label;
244     }
245 }
246
Popular Tags