Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c19743555270c12e62d7d3abb7f5eded93fb4a23 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*******************************************************************************
 * Copyright (c) 2016, 2021 Dirk Fauth and others.
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Dirk Fauth <dirk.fauth@googlemail.com> - initial API and implementation
 *******************************************************************************/
package org.eclipse.nebula.widgets.nattable.extension.nebula.cdatetime;

import java.util.Calendar;
import java.util.Date;

import org.eclipse.nebula.widgets.cdatetime.CDT;
import org.eclipse.nebula.widgets.cdatetime.CDateTime;
import org.eclipse.nebula.widgets.nattable.edit.editor.AbstractCellEditor;
import org.eclipse.nebula.widgets.nattable.selection.SelectionLayer.MoveDirectionEnum;
import org.eclipse.nebula.widgets.nattable.style.CellStyleAttributes;
import org.eclipse.nebula.widgets.nattable.widget.EditModeEnum;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

/**
 * ICellEditor implementation that uses the Nebula {@link CDateTime} control for
 * editing. It supports objects of type Date and Calendar aswell.
 * <p>
 * Introduces the contract that the editor control value is of type Calendar.
 * Therefore the methods to deal with the canonical values need to be overriden
 * too, to avoid conversion of the canonical value to display value by using the
 * IDisplayConverter that is registered together with this editor.
 *
 * @since 1.1
 */
public class CDateTimeCellEditor extends AbstractCellEditor {

    /**
     * The DateTime control which is the editor wrapped by this DateCellEditor.
     */
    private CDateTime dateTime;

    /**
     * Flag to configure whether the selection should move after a value was
     * committed after pressing enter.
     */
    private final boolean moveSelectionOnEnter;

    /**
     * The style bits that should be used to create the {@link CDateTime}
     * control of the editor.
     */
    private int style;

    /**
     * Flag to configure whether the editor should provide a {@link Calendar}
     * object on editing or a {@link Date}. By default a {@link Date} is
     * returned as {@link CDateTime} returns {@link Date} objects.
     */
    private boolean provideCalendar = false;

    /**
     * Creates the default DateCellEditor that does not move the selection on
     * committing the value by pressing enter.
     */
    public CDateTimeCellEditor() {
        this(false);
    }

    /**
     * Creates a DateCellEditor.
     *
     * @param moveSelectionOnEnter
     *            Flag to configure whether the selection should move after a
     *            value was committed after pressing enter.
     */
    public CDateTimeCellEditor(boolean moveSelectionOnEnter) {
        this(moveSelectionOnEnter, CDT.DROP_DOWN | CDT.DATE_SHORT | CDT.TIME_SHORT);
    }

    /**
     * Creates a DateCellEditor.
     *
     * @param moveSelectionOnEnter
     *            Flag to configure whether the selection should move after a
     *            value was committed after pressing enter.
     * @param style
     *            The style bits that should be used to create the
     *            {@link CDateTime} control of the editor.
     */
    public CDateTimeCellEditor(boolean moveSelectionOnEnter, int style) {
        this.moveSelectionOnEnter = moveSelectionOnEnter;
        this.style = style;
    }

    @Override
    public Object getEditorValue() {
        if (this.dateTime.getSelection() != null) {
            return this.dateTime.getSelection();
        }
        return null;
    }

    @Override
    public void setEditorValue(Object value) {
        if (value instanceof Calendar) {
            Calendar cal = (Calendar) value;
            this.dateTime.setSelection(cal.getTime());
        } else if (value instanceof Date) {
            this.dateTime.setSelection((Date) value);
        }
    }

    @Override
    public Object getCanonicalValue() {
        // there is no need for conversion because the CDateTime control
        // already returns a Date
        if (this.provideCalendar) {
            Calendar cal = Calendar.getInstance();
            cal.setTime((Date) getEditorValue());
            return cal;
        }
        return getEditorValue();
    }

    @Override
    public void setCanonicalValue(Object canonicalValue) {
        Date editorValue = null;
        if (canonicalValue instanceof Calendar) {
            editorValue = ((Calendar) canonicalValue).getTime();
        } else if (canonicalValue instanceof Date) {
            editorValue = (Date) canonicalValue;
        }

        if (editorValue != null) {
            setEditorValue(editorValue);
        }
    }

    @Override
    public CDateTime getEditorControl() {
        return this.dateTime;
    }

    @Override
    public CDateTime createEditorControl(final Composite parent) {
        final CDateTime dateControl = new CDateTime(parent, this.style) {
            @Override
            protected Shell getContentShell() {
                Shell shell = super.getContentShell();
                shell.addShellListener(new ShellAdapter() {

                    @Override
                    public void shellActivated(ShellEvent e) {
                        if (CDateTimeCellEditor.this.focusListener instanceof InlineFocusListener) {
                            ((InlineFocusListener) CDateTimeCellEditor.this.focusListener).handleFocusChanges = false;
                        }
                    }

                    @Override
                    public void shellClosed(ShellEvent e) {
                        if (CDateTimeCellEditor.this.focusListener instanceof InlineFocusListener) {
                            ((InlineFocusListener) CDateTimeCellEditor.this.focusListener).handleFocusChanges = true;
                        }
                    }
                });
                return shell;
            }

            @Override
            protected void postClose(Shell popup) {
                if (CDateTimeCellEditor.this.focusListener instanceof InlineFocusListener) {
                    ((InlineFocusListener) CDateTimeCellEditor.this.focusListener).handleFocusChanges = true;
                }
                super.postClose(popup);
            }

            @Override
            public void setOpen(boolean open) {
                if (CDateTimeCellEditor.this.focusListener instanceof InlineFocusListener) {
                    ((InlineFocusListener) CDateTimeCellEditor.this.focusListener).handleFocusChanges = false;
                }
                super.setOpen(open);
            }

            @Override
            public void setOpen(boolean open, Runnable callback) {
                if (CDateTimeCellEditor.this.focusListener instanceof InlineFocusListener) {
                    ((InlineFocusListener) CDateTimeCellEditor.this.focusListener).handleFocusChanges = false;
                }
                super.setOpen(open, callback);
            }

            @Override
            protected void addTextListener() {
                super.addTextListener();

                this.text.getControl().addTraverseListener(event -> {
                    boolean committed = false;
                    if (event.keyCode == SWT.TAB && event.stateMask == SWT.MOD2) {
                        committed = commit(MoveDirectionEnum.LEFT);
                    } else if (event.keyCode == SWT.TAB && event.stateMask == 0) {
                        committed = commit(MoveDirectionEnum.RIGHT);
                    } else if (event.detail == SWT.TRAVERSE_ESCAPE) {
                        close();
                    }
                    if (!committed) {
                        event.doit = false;
                    }
                });
            }
        };

        // set style information configured in the associated cell style
        dateControl.setBackground(this.cellStyle.getAttributeValue(CellStyleAttributes.BACKGROUND_COLOR));
        dateControl.setForeground(this.cellStyle.getAttributeValue(CellStyleAttributes.FOREGROUND_COLOR));
        // when trying to set a font in simple mode (embedded GUI) we will get a
        // StackOverflowError
        if ((this.style & CDT.SIMPLE) == 0) {
            dateControl.setFont(this.cellStyle.getAttributeValue(CellStyleAttributes.FONT));
        }

        dateControl.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetDefaultSelected(SelectionEvent event) {
                boolean commit = (event.stateMask != SWT.MOD3);
                MoveDirectionEnum move = MoveDirectionEnum.NONE;
                if (CDateTimeCellEditor.this.moveSelectionOnEnter
                        && CDateTimeCellEditor.this.editMode == EditModeEnum.INLINE) {
                    if (event.stateMask == 0) {
                        move = MoveDirectionEnum.DOWN;
                    } else if (event.stateMask == SWT.MOD2) {
                        move = MoveDirectionEnum.UP;
                    }
                }

                if (commit) {
                    commit(move);
                }

                if (CDateTimeCellEditor.this.editMode == EditModeEnum.DIALOG) {
                    parent.forceFocus();
                }
            }
        });

        return dateControl;
    }

    @Override
    protected Control activateCell(Composite parent, Object originalCanonicalValue) {
        this.dateTime = createEditorControl(parent);
        setCanonicalValue(originalCanonicalValue);

        // this is necessary so the control gets the focus
        // but this also causing some issues as focusing the DateTime control
        // programmatically does some strange things with showing the editable
        // data also it seems to be not possible to open the dropdown
        // programmatically
        this.dateTime.forceFocus();

        return this.dateTime;
    }

    /**
     *
     * @param provideCalendar
     *            <code>true</code> if this editor should provide a
     *            {@link Calendar} object on editing, <code>false</code> if it
     *            should provide a {@link Date} object.
     */
    public void setProvideCalendar(boolean provideCalendar) {
        this.provideCalendar = provideCalendar;
    }
}

Back to the top