View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.argeo.connect.ui.parts;
17  
18  import java.util.Calendar;
19  import java.util.GregorianCalendar;
20  
21  import org.argeo.eclipse.ui.EclipseUiUtils;
22  import org.eclipse.jface.dialogs.TrayDialog;
23  import org.eclipse.swt.SWT;
24  import org.eclipse.swt.events.SelectionAdapter;
25  import org.eclipse.swt.events.SelectionEvent;
26  import org.eclipse.swt.graphics.Point;
27  import org.eclipse.swt.widgets.Composite;
28  import org.eclipse.swt.widgets.Control;
29  import org.eclipse.swt.widgets.DateTime;
30  import org.eclipse.swt.widgets.Shell;
31  
32  /** Dialog with a calendar to choose a date */
33  public class PickUpDateDialog extends TrayDialog {
34  	private static final long serialVersionUID = 3806120909726231133L;
35  
36  	// this page widgets and UI objects
37  	private DateTime dateTimeCtl;
38  	private String title;
39  
40  	private Calendar selectedDate = GregorianCalendar.getInstance();
41  
42  	public PickUpDateDialog(Shell parentShell, String title) {
43  		super(parentShell);
44  		this.title = title;
45  	}
46  
47  	protected Point getInitialSize() {
48  		return new Point(240, 240);
49  	}
50  
51  	protected Control createDialogArea(Composite parent) {
52  		Composite dialogArea = (Composite) super.createDialogArea(parent);
53  		dialogArea.setLayout(EclipseUiUtils.noSpaceGridLayout());
54  
55  		dateTimeCtl = new DateTime(dialogArea, SWT.CALENDAR);
56  		dateTimeCtl.setLayoutData(EclipseUiUtils.fillAll());
57  
58  		dateTimeCtl.addSelectionListener(new SelectionAdapter() {
59  			private static final long serialVersionUID = -8414377364434281112L;
60  
61  			@Override
62  			public void widgetSelected(SelectionEvent e) {
63  				selectedDate.set(dateTimeCtl.getYear(), dateTimeCtl.getMonth(),
64  						dateTimeCtl.getDay(), 12, 0);
65  			}
66  		});
67  
68  		parent.pack();
69  		return dialogArea;
70  	}
71  
72  	public Calendar getSelected() {
73  		return selectedDate;
74  	}
75  
76  	protected void configureShell(Shell shell) {
77  		super.configureShell(shell);
78  		shell.setText(title);
79  	}
80  }