1 /*******************************************************************************
2  * Copyright (c) 2006, 2011 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  * IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.team.internal.ccvs.ui;
15 
16 import java.util.Calendar;
17 import java.util.Date;
18 
19 import org.eclipse.jface.dialogs.Dialog;
20 import org.eclipse.jface.dialogs.IDialogConstants;
21 import org.eclipse.jface.dialogs.TrayDialog;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.*;
26 import org.eclipse.ui.PlatformUI;
27 
28 public class CVSHistoryFilterDialog extends TrayDialog {
29 
30 	private CVSHistoryFilter historyFilter;
31 
32 	//widgets
33 	private Button orRadio;
34 	private Button andRadio;
35 	private Text branchName;
36 	private Text author;
37 	private Text comment;
38 	private DateTime fromDate;
39 	private DateTime toDate;
40 
CVSHistoryFilterDialog(Shell shell)41 	public CVSHistoryFilterDialog(Shell shell) {
42 		super(shell);
43 		setHelpAvailable(false); // Disable help controls - F1 will still work
44 	}
45 
46 	@Override
configureShell(Shell newShell)47 	protected void configureShell(Shell newShell) {
48 		super.configureShell(newShell);
49 		newShell.setText(CVSUIMessages.HistoryFilterDialog_title);
50 		PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IHelpContextIds.HISTORY_FILTER_DIALOG);
51 	}
52 
53 	@Override
createDialogArea(Composite parent)54 	protected Control createDialogArea(Composite parent) {
55 		Composite topLevel = new Composite(parent, SWT.NONE);
56 		GridLayout layout = new GridLayout();
57 		layout.numColumns = 2;
58 		layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
59 		layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
60 		topLevel.setLayout(layout);
61 
62 		//"and" and "or" search radio buttons
63 		Label label = new Label(topLevel, SWT.NONE);
64 		GridData data = new GridData(GridData.FILL_HORIZONTAL);
65 		data.horizontalSpan = 2;
66 		label.setLayoutData(data);
67 		label.setText(CVSUIMessages.HistoryFilterDialog_showMatching);
68 
69 		andRadio = new Button(topLevel, SWT.RADIO);
70 		andRadio.setText(CVSUIMessages.HistoryFilterDialog_matchingAll);
71 		data = new GridData(GridData.FILL_HORIZONTAL);
72 		data.horizontalSpan = 2;
73 		andRadio.setLayoutData(data);
74 		andRadio.setSelection(true);
75 
76 		orRadio = new Button(topLevel, SWT.RADIO);
77 		orRadio.setText(CVSUIMessages.HistoryFilterDialog_matchingAny);
78 		data = new GridData(GridData.FILL_HORIZONTAL);
79 		data.horizontalSpan = 2;
80 		orRadio.setLayoutData(data);
81 
82 		//branch name
83 		label = new Label(topLevel, SWT.NONE);
84 		label.setText(CVSUIMessages.HistoryFilterDialog_branchName);
85 		branchName = new Text(topLevel, SWT.BORDER);
86 		branchName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
87 
88 		//author
89 		label = new Label(topLevel, SWT.NONE);
90 		label.setText(CVSUIMessages.HistoryFilterDialog_author);
91 		author = new Text(topLevel, SWT.BORDER);
92 		author.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
93 
94 		//comment
95 		label = new Label(topLevel, SWT.NONE);
96 		label.setText(CVSUIMessages.HistoryFilterDialog_comment);
97 		comment = new Text(topLevel, SWT.BORDER);
98 		comment.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
99 
100 		//"from" date
101 		label = new Label(topLevel, SWT.NONE);
102 		label.setText(CVSUIMessages.HistoryFilterDialog_fromDate);
103 		fromDate = new DateTime(topLevel, SWT.DATE | SWT.BORDER);
104 
105 		//"to" date
106 		label = new Label(topLevel, SWT.NONE);
107 		label.setText(CVSUIMessages.HistoryFilterDialog_toDate);
108 		toDate = new DateTime(topLevel, SWT.DATE | SWT.BORDER);
109 
110 		initializeValues();
111 
112 		Dialog.applyDialogFont(parent);
113 		return topLevel;
114 	}
115 
initializeValues()116 	void initializeValues() {
117 		if (historyFilter == null)
118 			return;
119 		if (historyFilter.branchName != null) {
120 			branchName.setText(historyFilter.branchName);
121 		}
122 		if (historyFilter.author != null) {
123 			author.setText(historyFilter.author);
124 		}
125 		if (historyFilter.comment != null) {
126 			comment.setText(historyFilter.comment);
127 		}
128 		orRadio.setSelection(historyFilter.isOr);
129 		andRadio.setSelection(!historyFilter.isOr);
130 		Calendar calendar = Calendar.getInstance();
131 		if (historyFilter.fromDate != null) {
132 			calendar.setTime(historyFilter.fromDate);
133 			fromDate.setDay(calendar.get(Calendar.DATE));
134 			fromDate.setMonth(calendar.get(Calendar.MONTH));
135 			fromDate.setYear(calendar.get(Calendar.YEAR));
136 		}
137 		if (historyFilter.toDate != null) {
138 			calendar.setTime(historyFilter.toDate);
139 			toDate.setDay(calendar.get(Calendar.DATE));
140 			toDate.setMonth(calendar.get(Calendar.MONTH));
141 			toDate.setYear(calendar.get(Calendar.YEAR));
142 		}
143 	}
144 
145 	/**
146 	 * A button has been pressed.  Process the dialog contents.
147 	 */
148 	@Override
buttonPressed(int buttonId)149 	protected void buttonPressed(int buttonId) {
150 		if (IDialogConstants.CANCEL_ID == buttonId) {
151 			super.buttonPressed(buttonId);
152 			return;
153 		}
154 		Date fromDate = getFromDate();
155 		Date toDate = getToDate();
156 
157 		//create the filter
158 		historyFilter = new CVSHistoryFilter(branchName.getText(), author.getText(), comment.getText(), fromDate, toDate, orRadio.getSelection());
159 
160 		super.buttonPressed(buttonId);
161 	}
162 
163 	/**
164 	 * Get the date from the given widget or <code>null</code>
165 	 * if the date is today's date.
166 	 * @param calendar a calendar to compute the date
167 	 * @param dateWidget the date widget holding the date
168 	 * @return the date from the given widget or <code>null</code>
169 	 */
getCalendar(DateTime dateWidget)170 	private Calendar getCalendar(DateTime dateWidget) {
171 		Calendar calendar = Calendar.getInstance();
172 		if (isFutureDate(dateWidget, calendar)) {
173 			return null;
174 		}
175 		calendar.set(Calendar.YEAR, dateWidget.getYear());
176 		calendar.set(Calendar.MONTH, dateWidget.getMonth());
177 		calendar.set(Calendar.DATE, dateWidget.getDay());
178 
179 		//set the hours, minutes and seconds to 00
180 		//so as to cover the whole day
181 		calendar.set(Calendar.HOUR_OF_DAY, 0);
182 		calendar.set(Calendar.MINUTE, 0);
183 		calendar.set(Calendar.SECOND, 0);
184 		return calendar;
185 	}
186 
isFutureDate(DateTime dateWidget, Calendar calendar)187 	private boolean isFutureDate(DateTime dateWidget, Calendar calendar) {
188 		if (calendar.get(Calendar.YEAR) < dateWidget.getYear())
189 			return true;
190 		if (calendar.get(Calendar.YEAR) == dateWidget.getYear()) {
191 			if (calendar.get(Calendar.MONTH) < dateWidget.getMonth())
192 				return true;
193 			if (calendar.get(Calendar.MONTH) == dateWidget.getMonth()
194 					&& calendar.get(Calendar.DAY_OF_MONTH) <= dateWidget.getDay())
195 				return true;
196 		}
197 		return false;
198 	}
199 
200 	//either user input or the smallest date available
getFromDate()201 	private Date getFromDate() {
202 		Calendar calendar = getCalendar(fromDate);
203 		if (calendar == null)
204 			return null;
205 
206 		//set the hours, minutes and seconds to 00
207 		//so as to cover the whole day
208 		calendar.set(Calendar.HOUR_OF_DAY, 0);
209 		calendar.set(Calendar.MINUTE, 0);
210 		calendar.set(Calendar.SECOND, 0);
211 		return calendar.getTime();
212 	}
213 
214 	//either user input or today
getToDate()215 	private Date getToDate() {
216 		Calendar calendar = getCalendar(toDate);
217 		if (calendar == null)
218 			return null;
219 
220 		//set the hours, minutes and seconds to 23, 59, 59
221 		//so as to cover the whole day
222 		calendar.set(Calendar.HOUR_OF_DAY, 23);
223 		calendar.set(Calendar.MINUTE, 59);
224 		calendar.set(Calendar.SECOND, 59);
225 		return calendar.getTime();
226 	}
227 
228 	/**
229 	 * Returns the filter that was created from the provided
230 	 * user input.
231 	 */
getFilter()232 	public CVSHistoryFilter getFilter() {
233 		return historyFilter;
234 	}
235 
236 	/**
237 	 * Set the intial value of the dialog to the given filter.
238 	 */
setFilter(CVSHistoryFilter filter)239 	public void setFilter(CVSHistoryFilter filter) {
240 		this.historyFilter = filter;
241 	}
242 
243 }
244