1 package org
.argeo
.cms
.ui
.forms
;
3 import java
.text
.DateFormat
;
4 import java
.util
.Calendar
;
5 import java
.util
.GregorianCalendar
;
8 import javax
.jcr
.RepositoryException
;
10 import org
.argeo
.cms
.swt
.CmsSwtUtils
;
11 import org
.argeo
.cms
.ui
.viewers
.EditablePart
;
12 import org
.argeo
.cms
.ui
.widgets
.StyledControl
;
13 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
14 import org
.eclipse
.swt
.SWT
;
15 import org
.eclipse
.swt
.events
.MouseEvent
;
16 import org
.eclipse
.swt
.events
.MouseListener
;
17 import org
.eclipse
.swt
.events
.SelectionAdapter
;
18 import org
.eclipse
.swt
.events
.SelectionEvent
;
19 import org
.eclipse
.swt
.events
.ShellAdapter
;
20 import org
.eclipse
.swt
.events
.ShellEvent
;
21 import org
.eclipse
.swt
.layout
.GridData
;
22 import org
.eclipse
.swt
.layout
.GridLayout
;
23 import org
.eclipse
.swt
.widgets
.Button
;
24 import org
.eclipse
.swt
.widgets
.Composite
;
25 import org
.eclipse
.swt
.widgets
.Control
;
26 import org
.eclipse
.swt
.widgets
.DateTime
;
27 import org
.eclipse
.swt
.widgets
.Label
;
28 import org
.eclipse
.swt
.widgets
.Shell
;
29 import org
.eclipse
.swt
.widgets
.Text
;
31 /** CMS form part to display and edit a date */
32 public class EditablePropertyDate
extends StyledControl
implements EditablePart
{
33 private static final long serialVersionUID
= 2500215515778162468L;
36 private String propertyName
;
37 private String message
;
38 private DateFormat dateFormat
;
42 private Button openCalBtn
;
44 // TODO manage within the CSS
45 private int fieldBtnSpacing
= 5;
54 * @param dateFormat provide a {@link DateFormat} as contract to be able to
55 * read/write dates as strings
56 * @throws RepositoryException
58 public EditablePropertyDate(Composite parent
, int style
, Node node
, String propertyName
, String message
,
59 DateFormat dateFormat
) throws RepositoryException
{
60 super(parent
, style
, node
, false);
62 this.propertyName
= propertyName
;
63 this.message
= message
;
64 this.dateFormat
= dateFormat
;
66 if (node
.hasProperty(propertyName
)) {
67 this.setStyle(FormStyle
.propertyText
.style());
68 this.setText(dateFormat
.format(node
.getProperty(propertyName
).getDate().getTime()));
70 this.setStyle(FormStyle
.propertyMessage
.style());
71 this.setText(message
);
75 public void setText(String text
) {
76 Control child
= getControl();
77 if (child
instanceof Label
) {
78 Label lbl
= (Label
) child
;
79 if (EclipseUiUtils
.isEmpty(text
))
83 } else if (child
instanceof Text
) {
84 Text txt
= (Text
) child
;
85 if (EclipseUiUtils
.isEmpty(text
)) {
92 public synchronized void startEditing() {
93 // if (dateTxt != null && !dateTxt.isDisposed())
94 CmsSwtUtils
.style(getControl(), FormStyle
.propertyText
);
95 // getControl().setData(STYLE, FormStyle.propertyText.style());
99 public synchronized void stopEditing() {
100 if (EclipseUiUtils
.isEmpty(dateTxt
.getText()))
101 CmsSwtUtils
.style(getControl(), FormStyle
.propertyMessage
);
102 // getControl().setData(STYLE, FormStyle.propertyMessage.style());
104 CmsSwtUtils
.style(getControl(), FormStyle
.propertyText
);
105 // getControl().setData(STYLE, FormStyle.propertyText.style());
109 public String
getPropertyName() {
114 protected Control
createControl(Composite box
, String style
) {
116 return createCustomEditableControl(box
, style
);
118 return createLabel(box
, style
);
121 protected Label
createLabel(Composite box
, String style
) {
122 Label lbl
= new Label(box
, getStyle() | SWT
.WRAP
);
123 lbl
.setLayoutData(CmsSwtUtils
.fillWidth());
124 CmsSwtUtils
.style(lbl
, style
);
125 CmsSwtUtils
.markup(lbl
);
126 if (mouseListener
!= null)
127 lbl
.addMouseListener(mouseListener
);
131 private Control
createCustomEditableControl(Composite box
, String style
) {
132 box
.setLayoutData(CmsSwtUtils
.fillWidth());
133 Composite dateComposite
= new Composite(box
, SWT
.NONE
);
134 GridLayout gl
= EclipseUiUtils
.noSpaceGridLayout(new GridLayout(2, false));
135 gl
.horizontalSpacing
= fieldBtnSpacing
;
136 dateComposite
.setLayout(gl
);
137 dateTxt
= new Text(dateComposite
, SWT
.BORDER
);
138 CmsSwtUtils
.style(dateTxt
, style
);
139 dateTxt
.setLayoutData(new GridData(120, SWT
.DEFAULT
));
140 dateTxt
.setToolTipText(
141 "Enter a date with form \"" + FormUtils
.DEFAULT_SHORT_DATE_FORMAT
+ "\" or use the calendar");
142 openCalBtn
= new Button(dateComposite
, SWT
.FLAT
);
143 CmsSwtUtils
.style(openCalBtn
, FormStyle
.calendar
.style() + FormStyle
.BUTTON_SUFFIX
);
144 GridData gd
= new GridData(SWT
.CENTER
, SWT
.CENTER
, false, false);
146 openCalBtn
.setLayoutData(gd
);
147 // openCalBtn.setImage(PeopleRapImages.CALENDAR_BTN);
149 openCalBtn
.addSelectionListener(new SelectionAdapter() {
150 private static final long serialVersionUID
= 1L;
152 public void widgetSelected(SelectionEvent event
) {
153 CalendarPopup popup
= new CalendarPopup(dateTxt
);
158 // dateTxt.addFocusListener(new FocusListener() {
159 // private static final long serialVersionUID = 1L;
162 // public void focusLost(FocusEvent event) {
163 // String newVal = dateTxt.getText();
164 // // Enable reset of the field
165 // if (FormUtils.notNull(newVal))
169 // Calendar newCal = parseDate(newVal);
170 // // DateText.this.setText(newCal);
171 // calendar = newCal;
172 // } catch (ParseException pe) {
173 // // Silent. Manage error popup?
174 // if (calendar != null)
175 // EditablePropertyDate.this.setText(calendar);
181 // public void focusGained(FocusEvent event) {
187 protected void clear(boolean deep
) {
188 Control child
= getControl();
189 if (deep
|| child
instanceof Label
)
192 child
.getParent().dispose();
196 /** Enable setting a custom tooltip on the underlying text */
198 public void setToolTipText(String toolTipText
) {
199 dateTxt
.setToolTipText(toolTipText
);
203 /** Enable setting a custom message on the underlying text */
204 public void setMessage(String message
) {
205 dateTxt
.setMessage(message
);
209 public void setText(Calendar cal
) {
210 String newValueStr
= "";
212 newValueStr
= dateFormat
.format(cal
.getTime());
213 if (!newValueStr
.equals(dateTxt
.getText()))
214 dateTxt
.setText(newValueStr
);
217 // UTILITIES TO MANAGE THE CALENDAR POPUP
218 // TODO manage the popup shell in a cleaner way
219 private class CalendarPopup
extends Shell
{
220 private static final long serialVersionUID
= 1L;
221 private DateTime dateTimeCtl
;
223 public CalendarPopup(Control source
) {
224 super(source
.getDisplay(), SWT
.NO_TRIM
| SWT
.BORDER
| SWT
.ON_TOP
);
226 // Add border and shadow style
227 CmsSwtUtils
.markup(CalendarPopup
.this);
228 CmsSwtUtils
.style(CalendarPopup
.this, FormStyle
.popupCalendar
.style());
231 setLocation(source
.toDisplay((source
.getLocation().x
- 2), (source
.getSize().y
) + 3));
233 addShellListener(new ShellAdapter() {
234 private static final long serialVersionUID
= 5178980294808435833L;
237 public void shellDeactivated(ShellEvent e
) {
245 private void setProperty() {
246 // Direct set does not seems to work. investigate
247 // cal.set(dateTimeCtl.getYear(), dateTimeCtl.getMonth(),
248 // dateTimeCtl.getDay(), 12, 0);
249 Calendar cal
= new GregorianCalendar();
250 cal
.set(Calendar
.YEAR
, dateTimeCtl
.getYear());
251 cal
.set(Calendar
.MONTH
, dateTimeCtl
.getMonth());
252 cal
.set(Calendar
.DAY_OF_MONTH
, dateTimeCtl
.getDay());
253 String dateStr
= dateFormat
.format(cal
.getTime());
254 dateTxt
.setText(dateStr
);
257 protected void populate() {
258 setLayout(EclipseUiUtils
.noSpaceGridLayout());
260 dateTimeCtl
= new DateTime(this, SWT
.CALENDAR
);
261 dateTimeCtl
.setLayoutData(EclipseUiUtils
.fillAll());
263 Calendar calendar
= FormUtils
.parseDate(dateFormat
, dateTxt
.getText());
265 if (calendar
!= null)
266 dateTimeCtl
.setDate(calendar
.get(Calendar
.YEAR
), calendar
.get(Calendar
.MONTH
),
267 calendar
.get(Calendar
.DAY_OF_MONTH
));
269 dateTimeCtl
.addSelectionListener(new SelectionAdapter() {
270 private static final long serialVersionUID
= -8414377364434281112L;
273 public void widgetSelected(SelectionEvent e
) {
278 dateTimeCtl
.addMouseListener(new MouseListener() {
279 private static final long serialVersionUID
= 1L;
282 public void mouseUp(MouseEvent e
) {
286 public void mouseDown(MouseEvent e
) {
290 public void mouseDoubleClick(MouseEvent e
) {