提交 9e3a4155 编写于 作者: G Georgy Gvinepadze 提交者: Gvinepadze Georgy

#16078 recreate editors on mode switch instead of hiding

上级 11dbcc1f
......@@ -166,8 +166,12 @@ public abstract class BaseValueEditor<T extends Control> implements IValueEditor
}
}
final ControlModifyListener modifyListener = new ControlModifyListener();
inlineControl.addListener(SWT.Modify, modifyListener);
inlineControl.addListener(SWT.Selection, modifyListener);
addInlineListeners(inlineControl, modifyListener);
}
protected void addInlineListeners(@NotNull Control inlineControl, @NotNull Listener listener) {
inlineControl.addListener(SWT.Modify, listener);
inlineControl.addListener(SWT.Selection, listener);
}
private void addAutoSaveSupport(final Control inlineControl) {
......
......@@ -25,6 +25,7 @@ import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
......@@ -108,6 +109,9 @@ public class DateTimeInlineEditor extends BaseValueEditor<Control> {
@Override
public void run() {
super.run();
if (parent.isCalendarMode()) {
return;
}
if (parent.isDirty()) {
try {
Object value = parent.extractEditorValue();
......@@ -142,6 +146,24 @@ public class DateTimeInlineEditor extends BaseValueEditor<Control> {
super(controller);
}
@Override
protected void addInlineListeners(@NotNull Control inlineControl, @NotNull Listener listener) {
super.addInlineListeners(inlineControl, listener);
timeEditor.addSelectionAdapter(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
Event selectionEvent = new Event();
selectionEvent.widget = timeEditor.getControl();
timeEditor.getControl().notifyListeners(SWT.Selection, selectionEvent);
}
});
timeEditor.addModifyListener(e -> {
Event modificationEvent = new Event();
modificationEvent.widget = timeEditor.getControl();
timeEditor.getControl().notifyListeners(SWT.Modify, modificationEvent);
});
}
@Override
protected Control createControl(Composite editPlaceholder) {
Object value = valueController.getValue();
......@@ -157,21 +179,6 @@ public class DateTimeInlineEditor extends BaseValueEditor<Control> {
textMode.setChecked(true);
} else dateEditorMode.setChecked(true);
timeEditor.addSelectionAdapter(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
dirty = true;
Event selectionEvent = new Event();
selectionEvent.widget = timeEditor.getControl();
timeEditor.getControl().notifyListeners(SWT.Selection, selectionEvent);
}
});
timeEditor.addModifyListener(e -> {
dirty = true;
Event modificationEvent = new Event();
modificationEvent.widget = timeEditor.getControl();
timeEditor.getControl().notifyListeners(SWT.Modify, modificationEvent);
});
primeEditorValue(value);
timeEditor.createDateFormat(valueController.getValueType());
timeEditor.setEditable(!valueController.isReadOnly());
......
......@@ -17,14 +17,10 @@
package org.jkiss.dbeaver.ui.controls;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.*;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.model.impl.data.formatters.TimestampFormatSample;
......@@ -42,21 +38,26 @@ import java.util.Map;
*/
public class CustomTimeEditor {
private final static String FORMAT_PATTERN = "pattern";
private final int style;
private final boolean isPanel;
private DateTime dateEditor;
private DateTime timeEditor;
private final Composite basePart;
private Composite basePart;
private static final String TIMESTAMP_DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
private String format = "";
private Label timeLabel;
private Label dateLabel;
private int millis = -1;
private String dateAsText = "";
private final boolean isInline;
private InputMode inputMode = InputMode.None;
private final Calendar calendar = Calendar.getInstance();
private Text textEditor;
private Listener modifyListener;
private SelectionAdapter selectionListener;
private boolean editable;
private enum InputMode {
None,
......@@ -70,17 +71,11 @@ public class CustomTimeEditor {
switch (jdbcType) {
case DATE:
inputMode = InputMode.Date;
timeEditor.dispose();
if (timeLabel != null) {
timeLabel.dispose();
}
disposeDateEditor(timeEditor, timeLabel);
break;
case TIME:
inputMode = InputMode.Time;
dateEditor.dispose();
if (dateLabel != null) {
dateLabel.dispose();
}
disposeDateEditor(dateEditor, dateLabel);
break;
default:
inputMode = InputMode.DateTime;
......@@ -89,13 +84,14 @@ public class CustomTimeEditor {
}
public CustomTimeEditor(@NotNull Composite parent, int style, boolean isPanel, boolean isInline) {
basePart = getComposite(parent, style, isPanel, isInline);
this.isInline = isInline;
this.isPanel = isPanel;
this.style = style;
initEditor(parent, style);
}
@NotNull
private Composite getComposite(@NotNull Composite parent, int style, boolean isPanel, boolean isInline) {
final Composite basePart;
private Composite initEditor(@NotNull Composite parent, int style) {
basePart = new Composite(parent, style);
GridLayout layout = new GridLayout(2, false);
if (isInline) {
......@@ -103,97 +99,67 @@ public class CustomTimeEditor {
layout.marginHeight = 0;
}
basePart.setLayout(layout);
setToDateComposite();
final GridData layoutData = new GridData(SWT.FILL, SWT.RIGHT, true, false, 1, 1);
if (!isInline) dateLabel = UIUtils.createLabel(basePart, "Date");
this.dateEditor = new DateTime(basePart, SWT.DROP_DOWN);
dateEditor.setLayoutData(layoutData);
if (!isInline) timeLabel = UIUtils.createLabel(basePart, "Time");
this.timeEditor = new DateTime(basePart, SWT.TIME | SWT.MEDIUM);
this.timeEditor.setLayoutData(layoutData);
textEditor = new Text(basePart, isPanel && !isInline ? style : style | SWT.BORDER);
final GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
textEditor.setLayoutData(gridData);
textEditor.setVisible(false);
gridData.exclude = true;
//Magical part which fixes incorrect display of first value in inline mode
//fixes calendar issues on inline mode
basePart.pack();
basePart.layout();
this.format = getTimestampFormat();
return basePart;
}
/**
* Hides all DateTime editors and shows text editor instead
* Disposes all DateTime editors and their labels and creates text editor
*/
public void setToTextComposite() {
if (textEditor != null && !textEditor.isDisposed()) {
return;
}
disposeDateEditor(timeEditor, timeLabel);
timeEditor = null;
disposeDateEditor(dateEditor, dateLabel);
dateEditor = null;
textEditor = new Text(basePart, isPanel && !isInline ? style : style | SWT.BORDER);
final GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
textEditor.setLayoutData(gridData);
textEditor.setVisible(true);
GridLayout layout = new GridLayout(1, false);
{
layout.marginHeight = 0;
layout.marginWidth = 0;
}
basePart.setLayout(layout);
final GridData layoutData = new GridData(SWT.FILL, SWT.UP, true, false, 1, 1);
if (!timeEditor.isDisposed()) {
timeEditor.setLayoutData(layoutData);
timeEditor.setVisible(layoutData.exclude);
if (timeLabel != null) {
timeLabel.setLayoutData(layoutData);
timeLabel.setVisible(layoutData.exclude);
}
}
if (!dateEditor.isDisposed()) {
dateEditor.setLayoutData(layoutData);
dateEditor.setVisible(layoutData.exclude);
if (dateLabel != null) {
dateLabel.setLayoutData(layoutData);
dateLabel.setVisible(layoutData.exclude);
allowEdit();
textEditor.setText(dateAsText);
basePart.layout();
}
private void disposeDateEditor(DateTime dateTimeEditor, Label dateTimeLabel) {
if (dateTimeEditor != null) {
dateTimeEditor.dispose();
if (dateTimeLabel != null) {
dateTimeLabel.dispose();
}
}
layoutData.exclude = true;
basePart.layout();
}
/**
* Hides Text editor and shows DateTime ones
* Disposes text editor components and creates DateTime editors
*/
public void setToDateComposite() {
final GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
textEditor.setLayoutData(gridData);
textEditor.setVisible(false);
gridData.exclude = true;
GridLayout layout = new GridLayout(2, false);
if (isInline){
layout.marginWidth = 0;
layout.marginHeight = 0;
if (dateEditor != null || timeEditor != null) {
return;
}
if (textEditor != null && !textEditor.isDisposed()) {
textEditor.dispose();
textEditor = null;
}
basePart.setLayout(layout);
final GridData layoutData = new GridData(SWT.FILL, SWT.RIGHT, true, false, 1, 1);
final GridData layoutDataForLabels = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
if (!isInline) dateLabel = UIUtils.createLabel(basePart, "Date");
this.dateEditor = new DateTime(basePart, SWT.DROP_DOWN);
dateEditor.setLayoutData(layoutData);
if (!isInline) timeLabel = UIUtils.createLabel(basePart, "Time");
this.timeEditor = new DateTime(basePart, SWT.TIME | SWT.MEDIUM);
this.timeEditor.setLayoutData(layoutData);
allowEdit();
if (!dateEditor.isDisposed()) {
dateEditor.setLayoutData(layoutData);
dateEditor.setVisible(true);
dateLabel.setLayoutData(layoutDataForLabels);
dateLabel.setVisible(true);
}
if (!timeEditor.isDisposed()) {
timeEditor.setLayoutData(layoutData);
timeEditor.setVisible(true);
timeLabel.setLayoutData(layoutDataForLabels);
timeLabel.setVisible(true);
}
setDateFromCalendar();
updateListeners();
basePart.layout();
}
......@@ -201,23 +167,42 @@ public class CustomTimeEditor {
this.format = format;
}
public void updateListeners() {
if (selectionListener != null) {
if (dateEditor != null && !dateEditor.isDisposed()) {
dateEditor.addSelectionListener(selectionListener);
}
if (timeEditor != null && !timeEditor.isDisposed()) {
timeEditor.addSelectionListener(selectionListener);
}
}
if (modifyListener != null && textEditor != null && !textEditor.isDisposed()) {
textEditor.addListener(SWT.Modify, modifyListener);
}
}
/**
* Creates listeners for date editors.
*
* @param listener listener to add to all existing editors
*/
public void addSelectionAdapter(@NotNull SelectionAdapter listener) {
if (dateEditor != null && !dateEditor.isDisposed()) {
dateEditor.addSelectionListener(listener);
}
if (timeEditor != null && !timeEditor.isDisposed()) {
timeEditor.addSelectionListener(listener);
}
selectionListener = listener;
updateListeners();
}
public void addModifyListener(@NotNull ModifyListener listener) {
if (textEditor != null && !textEditor.isDisposed()) {
textEditor.addModifyListener(listener);
public void addModifyListener(@NotNull Listener listener) {
modifyListener = listener;
updateListeners();
}
private static void setWithoutListener(@NotNull Control control, int type, Listener listener, @NotNull Runnable blockToRun) {
if (listener != null) {
control.removeListener(type, listener);
blockToRun.run();
control.addListener(type, listener);
} else {
blockToRun.run();
}
}
......@@ -233,8 +218,11 @@ public class CustomTimeEditor {
}
public void setTextValue(@Nullable String value) {
dateAsText = value;
if (textEditor != null && !textEditor.isDisposed()) {
textEditor.setText(value != null ? value : "");
setWithoutListener(textEditor, SWT.Modify, modifyListener, () -> {
textEditor.setText(value);
});
}
}
......@@ -242,15 +230,17 @@ public class CustomTimeEditor {
if (value != null) {
calendar.setTime(value);
}
if (!dateEditor.isDisposed()) {
setDateFromCalendar();
}
private void setDateFromCalendar() {
if (dateEditor != null && !dateEditor.isDisposed()) {
dateEditor.setDate(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
}
if (!timeEditor.isDisposed()) {
if (timeEditor != null && !timeEditor.isDisposed()) {
timeEditor.addTraverseListener(e -> timeEditor.setFocus());
timeEditor.setTime(calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
try {
millis = calendar.get(Calendar.MILLISECOND);
} catch (ArrayIndexOutOfBoundsException e) {
......@@ -258,12 +248,11 @@ public class CustomTimeEditor {
millis = -1;
}
}
}
@Nullable
public String getValueAsString() {
if (textEditor != null && !textEditor.isDisposed() && textEditor.isVisible()) {
if (textEditor != null && !textEditor.isDisposed()) {
return textEditor.getText();
}
return null;
......@@ -294,6 +283,11 @@ public class CustomTimeEditor {
}
public void setEditable(boolean editable) {
this.editable = editable;
allowEdit();
}
public void allowEdit() {
if (this.dateEditor != null && !this.dateEditor.isDisposed()) {
this.dateEditor.setEnabled(editable);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册