KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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.ccvs.ui;
12
13 import java.util.Date JavaDoc;
14
15 import org.eclipse.jface.dialogs.*;
16 import org.eclipse.jface.dialogs.Dialog;
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.ui.PlatformUI;
22
23 import com.ibm.icu.util.Calendar;
24
25 public class CVSHistoryFilterDialog extends TrayDialog {
26
27     private CVSHistoryFilter historyFilter;
28
29     //widgets
30
private Button orRadio;
31     private Button andRadio;
32     private Text author;
33     private Text comment;
34     private DateTime fromDate;
35     private DateTime toDate;
36
37     public CVSHistoryFilterDialog(Shell shell) {
38         super(shell);
39     }
40
41     protected void configureShell(Shell newShell) {
42         super.configureShell(newShell);
43         newShell.setText(CVSUIMessages.HistoryFilterDialog_title);
44     }
45
46     protected Control createDialogArea(Composite parent) {
47         Composite topLevel = new Composite(parent, SWT.NONE);
48         GridLayout layout = new GridLayout();
49         layout.numColumns = 2;
50         layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
51         layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
52         topLevel.setLayout(layout);
53
54         //"and" and "or" search radio buttons
55
Label label = new Label(topLevel, SWT.NONE);
56         GridData data = new GridData(GridData.FILL_HORIZONTAL);
57         data.horizontalSpan = 2;
58         label.setLayoutData(data);
59         label.setText(CVSUIMessages.HistoryFilterDialog_showMatching);
60
61         andRadio = new Button(topLevel, SWT.RADIO);
62         andRadio.setText(CVSUIMessages.HistoryFilterDialog_matchingAll);
63         data = new GridData(GridData.FILL_HORIZONTAL);
64         data.horizontalSpan = 2;
65         andRadio.setLayoutData(data);
66         andRadio.setSelection(true);
67
68         orRadio = new Button(topLevel, SWT.RADIO);
69         orRadio.setText(CVSUIMessages.HistoryFilterDialog_matchingAny);
70         data = new GridData(GridData.FILL_HORIZONTAL);
71         data.horizontalSpan = 2;
72         orRadio.setLayoutData(data);
73
74         //author
75
label = new Label(topLevel, SWT.NONE);
76         label.setText(CVSUIMessages.HistoryFilterDialog_author);
77         author = new Text(topLevel, SWT.BORDER);
78         author.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
79
80         //comment
81
label = new Label(topLevel, SWT.NONE);
82         label.setText(CVSUIMessages.HistoryFilterDialog_comment);
83         comment = new Text(topLevel, SWT.BORDER);
84         comment.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
85
86         //"from" date
87
label = new Label(topLevel, SWT.NONE);
88         label.setText(CVSUIMessages.HistoryFilterDialog_fromDate);
89         fromDate = new DateTime(topLevel, SWT.DATE);
90
91         //"to" date
92
label = new Label(topLevel, SWT.NONE);
93         label.setText(CVSUIMessages.HistoryFilterDialog_toDate);
94         toDate = new DateTime(topLevel, SWT.DATE);
95
96         initializeValues();
97
98         // set F1 help
99
PlatformUI.getWorkbench().getHelpSystem().setHelp(topLevel, IHelpContextIds.HISTORY_FILTER_DIALOG);
100         Dialog.applyDialogFont(parent);
101         return topLevel;
102     }
103
104     void initializeValues() {
105         if (historyFilter == null)
106             return;
107         if (historyFilter.author != null) {
108             author.setText(historyFilter.author);
109         }
110         if (historyFilter.comment != null) {
111             comment.setText(historyFilter.comment);
112         }
113         orRadio.setSelection(historyFilter.isOr);
114         andRadio.setSelection(!historyFilter.isOr);
115         Calendar calendar = Calendar.getInstance();
116         if (historyFilter.fromDate != null) {
117             calendar.setTime(historyFilter.fromDate);
118             fromDate.setDay(calendar.get(Calendar.DATE));
119             fromDate.setMonth(calendar.get(Calendar.MONTH));
120             fromDate.setYear(calendar.get(Calendar.YEAR));
121         }
122         if (historyFilter.toDate != null) {
123             calendar.setTime(historyFilter.toDate);
124             toDate.setDay(calendar.get(Calendar.DATE));
125             toDate.setMonth(calendar.get(Calendar.MONTH));
126             toDate.setYear(calendar.get(Calendar.YEAR));
127         }
128     }
129
130     /**
131      * A button has been pressed. Process the dialog contents.
132      */

133     protected void buttonPressed(int buttonId) {
134         if (IDialogConstants.CANCEL_ID == buttonId) {
135             super.buttonPressed(buttonId);
136             return;
137         }
138         Date JavaDoc fromDate = getFromDate();
139         Date JavaDoc toDate = getToDate();
140
141         //create the filter
142
historyFilter = new CVSHistoryFilter(author.getText(), comment.getText(), fromDate, toDate, orRadio.getSelection());
143
144         super.buttonPressed(buttonId);
145     }
146
147     /**
148      * Get the date from the given widget or <code>null</code>
149      * if the date is today's date.
150      * @param calendar a calendar to compute the date
151      * @param dateWidget the date widget holding the date
152      * @return the date from the given widget or <code>null</code>
153      */

154     private Calendar getCalendar(DateTime dateWidget) {
155         Calendar calendar = Calendar.getInstance();
156         if (isFutureDate(dateWidget, calendar)) {
157             return null;
158         }
159         calendar.set(Calendar.YEAR, dateWidget.getYear());
160         calendar.set(Calendar.MONTH, dateWidget.getMonth());
161         calendar.set(Calendar.DATE, dateWidget.getDay());
162
163         //set the hours, minutes and seconds to 00
164
//so as to cover the whole day
165
calendar.set(Calendar.HOUR_OF_DAY, 0);
166         calendar.set(Calendar.MINUTE, 0);
167         calendar.set(Calendar.SECOND, 0);
168         return calendar;
169     }
170
171     private boolean isFutureDate(DateTime dateWidget, Calendar calendar) {
172         if (calendar.get(Calendar.YEAR) < dateWidget.getYear())
173             return true;
174         if (calendar.get(Calendar.YEAR) == dateWidget.getYear()) {
175             if (calendar.get(Calendar.MONTH) < dateWidget.getMonth())
176                 return true;
177             if (calendar.get(Calendar.MONTH) == dateWidget.getMonth()
178                     && calendar.get(Calendar.DAY_OF_MONTH) <= dateWidget.getDay())
179                 return true;
180         }
181         return false;
182     }
183     
184     //either user input or the smallest date available
185
private Date JavaDoc getFromDate() {
186         Calendar calendar = getCalendar(fromDate);
187         if (calendar == null)
188             return null;
189
190         //set the hours, minutes and seconds to 00
191
//so as to cover the whole day
192
calendar.set(Calendar.HOUR_OF_DAY, 0);
193         calendar.set(Calendar.MINUTE, 0);
194         calendar.set(Calendar.SECOND, 0);
195         return calendar.getTime();
196     }
197
198     //either user input or today
199
private Date JavaDoc getToDate() {
200         Calendar calendar = getCalendar(toDate);
201         if (calendar == null)
202             return null;
203
204         //set the hours, minutes and seconds to 23, 59, 59
205
//so as to cover the whole day
206
calendar.set(Calendar.HOUR_OF_DAY, 23);
207         calendar.set(Calendar.MINUTE, 59);
208         calendar.set(Calendar.SECOND, 59);
209         return calendar.getTime();
210     }
211
212     /**
213      * Returns the filter that was created from the provided
214      * user input.
215      */

216     public CVSHistoryFilter getFilter() {
217         return historyFilter;
218     }
219
220     /**
221      * Set the intial value of the dialog to the given filter.
222      */

223     public void setFilter(CVSHistoryFilter filter) {
224         this.historyFilter = filter;
225     }
226
227 }
228
Popular Tags