KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > HistoryFilterDialog


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

12 package org.eclipse.team.internal.ccvs.ui;
13
14 import java.text.SimpleDateFormat JavaDoc;
15 import java.util.Calendar JavaDoc;
16 import java.util.Date JavaDoc;
17
18 import org.eclipse.jface.dialogs.Dialog;
19 import org.eclipse.jface.dialogs.IDialogConstants;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.*;
24 import org.eclipse.ui.PlatformUI;
25
26 public class HistoryFilterDialog extends Dialog {
27
28     private HistoryView historyView;
29     private HistoryFilter historyFilter;
30     
31     //widgets
32
private Button orRadio;
33     private Button andRadio;
34     private Combo fromDayCombo;
35     private Combo toDayCombo;
36     private Combo fromMonthCombo;
37     private Combo toMonthCombo;
38     private Combo fromYearCombo;
39     private Combo toYearCombo;
40     private Text author;
41     private Text comment;
42
43     public HistoryFilterDialog(HistoryView view) {
44         super(view.getViewSite().getShell());
45         this.historyView = view;
46     }
47
48     protected void configureShell(Shell newShell) {
49         super.configureShell(newShell);
50         newShell.setText(CVSUIMessages.HistoryFilterDialog_title); //$NON-NLS-1$
51
}
52
53     protected Control createDialogArea(Composite parent) {
54         Composite topLevel = new Composite(parent, SWT.NONE);
55         GridLayout layout = new GridLayout();
56         layout.numColumns = 2;
57         layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
58         layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
59         topLevel.setLayout(layout);
60         
61         //"and" and "or" search radio buttons
62
Label label = new Label(topLevel, SWT.NONE);
63         GridData data = new GridData(GridData.FILL_HORIZONTAL);
64         data.horizontalSpan = 2;
65         label.setLayoutData(data);
66         label.setText(CVSUIMessages.HistoryFilterDialog_showMatching); //$NON-NLS-1$
67

68         andRadio = new Button(topLevel, SWT.RADIO);
69         andRadio.setText(CVSUIMessages.HistoryFilterDialog_matchingAll); //$NON-NLS-1$
70
data = new GridData(GridData.FILL_HORIZONTAL);
71         data.horizontalSpan = 2;
72         andRadio.setLayoutData(data);
73         andRadio.setSelection(true);
74         
75         orRadio = new Button(topLevel, SWT.RADIO);
76         orRadio.setText(CVSUIMessages.HistoryFilterDialog_matchingAny); //$NON-NLS-1$
77
data = new GridData(GridData.FILL_HORIZONTAL);
78         data.horizontalSpan = 2;
79         orRadio.setLayoutData(data);
80         
81         //author
82
label = new Label(topLevel, SWT.NONE);
83         label.setText(CVSUIMessages.HistoryFilterDialog_author); //$NON-NLS-1$
84
author = new Text(topLevel, SWT.BORDER);
85         author.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
86
87         //comment
88
label = new Label(topLevel, SWT.NONE);
89         label.setText(CVSUIMessages.HistoryFilterDialog_comment); //$NON-NLS-1$
90
comment = new Text(topLevel, SWT.BORDER);
91         comment.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
92         
93         //"from" date
94
label = new Label(topLevel, SWT.NONE);
95         label.setText(CVSUIMessages.HistoryFilterDialog_fromDate); //$NON-NLS-1$
96
Composite fdComposite = new Composite(topLevel, SWT.NONE);
97         GridLayout fdLayout = new GridLayout();
98         fdLayout.numColumns = 3;
99         fdComposite.setLayout(fdLayout);
100         fromMonthCombo = new Combo(fdComposite, SWT.READ_ONLY);
101         fromDayCombo = new Combo(fdComposite, SWT.READ_ONLY);
102         fromYearCombo = new Combo(fdComposite, SWT.NONE);
103         fromYearCombo.setTextLimit(4);
104
105         //"to" date
106
label = new Label(topLevel, SWT.NONE);
107         label.setText(CVSUIMessages.HistoryFilterDialog_toDate); //$NON-NLS-1$
108
Composite tdComposite = new Composite(topLevel, SWT.NONE);
109         GridLayout tdLayout = new GridLayout();
110         tdLayout.numColumns = 3;
111         tdComposite.setLayout(tdLayout);
112         toMonthCombo = new Combo(tdComposite, SWT.READ_ONLY);
113         toDayCombo = new Combo(tdComposite, SWT.READ_ONLY);
114         toYearCombo = new Combo(tdComposite, SWT.NONE);
115         toYearCombo.setTextLimit(4);
116
117         //set day, month and year combos with numbers
118
//years allows a selection from the past 5 years
119
//or any year written in
120
String JavaDoc days[] = new String JavaDoc[32];
121         days[0] = "---"; //$NON-NLS-1$
122
for (int i = 1; i < 32; i++) {
123             days[i] = String.valueOf(i);
124         }
125
126         String JavaDoc months[] = new String JavaDoc[13];
127         months[0] = "---"; //$NON-NLS-1$
128
SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc("MMMM"); //$NON-NLS-1$
129
Calendar JavaDoc calendar = Calendar.getInstance();
130         for (int i = 1; i < 13; i++) {
131             calendar.set(Calendar.MONTH, i - 1);
132             months[i] = format.format(calendar.getTime());
133         }
134
135         String JavaDoc years[] = new String JavaDoc[5];
136         Calendar JavaDoc calender = Calendar.getInstance();
137         for (int i = 0; i < 5; i++) {
138             years[i] = String.valueOf(calender.get(1) - i);
139         }
140         fromDayCombo.setItems(days);
141         fromDayCombo.select(0);
142         toDayCombo.setItems(days);
143         toDayCombo.select(0);
144         fromMonthCombo.setItems(months);
145         fromMonthCombo.select(0);
146         toMonthCombo.setItems(months);
147         toMonthCombo.select(0);
148         fromYearCombo.setItems(years);
149         toYearCombo.setItems(years);
150         fromYearCombo.select(0);
151         toYearCombo.select(0);
152
153         initializeValues();
154         
155         // set F1 help
156
PlatformUI.getWorkbench().getHelpSystem().setHelp(topLevel, IHelpContextIds.HISTORY_FILTER_DIALOG);
157         Dialog.applyDialogFont(parent);
158         return topLevel;
159     }
160     void initializeValues() {
161         if (historyFilter == null) return;
162         if (historyFilter.author != null) {
163             author.setText(historyFilter.author);
164         }
165         if (historyFilter.comment != null) {
166             comment.setText(historyFilter.comment);
167         }
168         orRadio.setSelection(historyFilter.isOr);
169         andRadio.setSelection(!historyFilter.isOr);
170         Calendar JavaDoc calendar = Calendar.getInstance();
171         if (historyFilter.fromDate != null) {
172             calendar.setTime(historyFilter.fromDate);
173             fromDayCombo.select(calendar.get(Calendar.DATE));
174             fromMonthCombo.select(calendar.get(Calendar.MONTH) + 1);
175             String JavaDoc yearValue = String.valueOf(calendar.get(Calendar.YEAR));
176             int index = fromYearCombo.indexOf(yearValue);
177             if (index == -1) {
178                 fromYearCombo.add(yearValue);
179                 index = fromYearCombo.indexOf(yearValue);
180             }
181             fromYearCombo.select(index);
182         }
183         if (historyFilter.toDate != null) {
184             calendar.setTime(historyFilter.toDate);
185             toDayCombo.select(calendar.get(Calendar.DATE));
186             toMonthCombo.select(calendar.get(Calendar.MONTH) + 1);
187             String JavaDoc yearValue = String.valueOf(calendar.get(Calendar.YEAR));
188             int index = toYearCombo.indexOf(yearValue);
189             if (index == -1) {
190                 toYearCombo.add(yearValue);
191                 index = toYearCombo.indexOf(yearValue);
192             }
193             toYearCombo.select(index);
194         }
195     }
196     /**
197      * A button has been pressed. Process the dialog contents.
198      */

199     protected void buttonPressed(int buttonId) {
200         if (IDialogConstants.CANCEL_ID == buttonId) {
201             super.buttonPressed(buttonId);
202             return;
203         }
204         Date JavaDoc fromDate = null, toDate = null;
205
206         boolean fromSet=
207             (fromDayCombo.getSelectionIndex() > 0)
208                 && (fromMonthCombo.getSelectionIndex() > 0);
209         boolean toSet=
210             (toDayCombo.getSelectionIndex() > 0)
211                 && (toMonthCombo.getText().length() > 0);
212         
213         if (fromSet || toSet) {
214             Calendar JavaDoc calendar = Calendar.getInstance();
215             fromDate = getFromDate(calendar, fromSet);
216             toDate = getToDate(calendar, toSet);
217         }
218
219         //create the filter
220
historyFilter = new HistoryFilter(
221             historyView,
222             author.getText(),
223             comment.getText(),
224             fromDate,
225             toDate,
226             orRadio.getSelection());
227                 
228         super.buttonPressed(buttonId);
229     }
230
231     //either user input or the smallest date available
232
private Date JavaDoc getFromDate(Calendar JavaDoc calendar, boolean fromSet) {
233         if (fromSet) {
234             calendar.set(Calendar.YEAR, Integer.parseInt(String.valueOf(fromYearCombo.getText())));
235             calendar.set(Calendar.MONTH, fromMonthCombo.getSelectionIndex() - 1);
236             calendar.set(Calendar.DATE, Integer.parseInt(String.valueOf(fromDayCombo.getText())));
237         } else {
238             calendar.set(Calendar.YEAR, Integer.parseInt(String.valueOf(fromYearCombo.getItem(fromYearCombo.getItemCount() - 1))));
239             calendar.set(Calendar.MONTH, 0);
240             calendar.set(Calendar.DATE, 1);
241         }
242
243         //set the hours, minutes and seconds to 00
244
//so as to cover the whole day
245
calendar.set(Calendar.HOUR_OF_DAY, 0);
246         calendar.set(Calendar.MINUTE, 0);
247         calendar.set(Calendar.SECOND, 0);
248         return calendar.getTime();
249     }
250
251     //either user input or today
252
private Date JavaDoc getToDate(Calendar JavaDoc calendar, boolean toSet) {
253         if (toSet) {
254             calendar.set(Calendar.YEAR, Integer.parseInt(String.valueOf(toYearCombo.getText())));
255             calendar.set(Calendar.MONTH, toMonthCombo.getSelectionIndex() - 1);
256             calendar.set(Calendar.DATE, Integer.parseInt(String.valueOf(toDayCombo.getText())));
257         } else
258             calendar.setTimeInMillis(System.currentTimeMillis());
259
260         //set the hours, minutes and seconds to 23, 59, 59
261
//so as to cover the whole day
262
calendar.set(Calendar.HOUR_OF_DAY, 23);
263         calendar.set(Calendar.MINUTE, 59);
264         calendar.set(Calendar.SECOND, 59);
265         return calendar.getTime();
266     }
267
268     /**
269      * Returns the filter that was created from the provided
270      * user input.
271      */

272     public HistoryFilter getFilter() {
273         return historyFilter;
274     }
275     /**
276      * Set the intial value of the dialog to the given filter.
277      */

278     public void setFilter(HistoryFilter filter) {
279         this.historyFilter = filter;
280     }
281 }
282
Popular Tags