ref #8450 Create and cache label for quantitative data in row wrapper
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / descriptiveDataSet / matrix / quantitative / QuantitativeDataDialogEditor.java
1 /**
2 * Copyright (C) 2018 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix.quantitative;
10
11 import java.util.Map;
12
13 import org.eclipse.jface.window.Window;
14 import org.eclipse.nebula.widgets.nattable.config.IConfigRegistry;
15 import org.eclipse.nebula.widgets.nattable.edit.ICellEditHandler;
16 import org.eclipse.nebula.widgets.nattable.edit.gui.AbstractDialogCellEditor;
17 import org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell;
18 import org.eclipse.nebula.widgets.nattable.widget.EditModeEnum;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
21
22 import eu.etaxonomy.cdm.api.service.dto.RowWrapperDTO;
23 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
24 import eu.etaxonomy.cdm.model.description.Feature;
25 import eu.etaxonomy.cdm.model.description.QuantitativeData;
26 import eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix.CharacterMatrix;
27
28 /**
29 * @author pplitzner
30 * @since Jul 19, 2018
31 *
32 */
33 public class QuantitativeDataDialogEditor extends AbstractDialogCellEditor {
34
35 private QuantitativeData editorValue;
36 private RowWrapperDTO<?> rowWrapperDTO;
37 private boolean closed;
38 private CharacterMatrix matrix;
39 private Character initialInput;
40
41 public QuantitativeDataDialogEditor(CharacterMatrix matrix) {
42 super();
43 this.matrix = matrix;
44 }
45
46 @Override
47 public int open() {
48 Object rowObject = matrix.getBodyDataProvider().getRowObject(getRowIndex());
49 if(rowObject instanceof RowWrapperDTO){
50 this.closed = false;
51 return getDialogInstance().open();
52 }
53 return Window.CANCEL;
54 }
55
56 @Override
57 public QuantitativeDataDialog createDialogInstance() {
58 return new QuantitativeDataDialog(this.initialInput, matrix, this.parent.getShell(), getCanonicalValue(), this.layerCell, this, this.configRegistry);
59 }
60
61 @Override
62 public QuantitativeDataDialog getDialogInstance() {
63 return (QuantitativeDataDialog) this.dialog;
64 }
65
66 @Override
67 public Object getEditorValue() {
68 return this.editorValue;
69 }
70
71 @Override
72 public void setEditorValue(Object value) {
73 ((QuantitativeDataDialog)this.dialog).setEditorValue((QuantitativeData) value);
74 ((QuantitativeDataDialog)this.dialog).setRowWrapper(rowWrapperDTO);
75 this.editorValue = (QuantitativeData) value;
76 }
77
78 @Override
79 public void close() {
80 this.closed = true;
81 this.dialog = null;
82 this.editorValue = null;
83 }
84
85 @Override
86 public boolean isClosed() {
87 return closed;
88 }
89
90 @Override
91 public Control activateCell(Composite parent, Object originalCanonicalValue, EditModeEnum editMode,
92 ICellEditHandler editHandler, ILayerCell cell, IConfigRegistry configRegistry) {
93 this.initialInput = null;
94 Object rowObject = matrix.getBodyDataProvider().getRowObject(cell.getRowIndex());
95 Feature feature = matrix.getIndexToFeatureMap().get(cell.getColumnIndex());
96 QuantitativeData quantitativeData = null;
97 if(rowObject instanceof RowWrapperDTO){
98 rowWrapperDTO = (RowWrapperDTO<?>) rowObject;
99 DescriptionElementBase dataValueForFeature = rowWrapperDTO.getDataValueForFeature(feature);
100 if(dataValueForFeature instanceof QuantitativeData){
101 quantitativeData = (QuantitativeData) dataValueForFeature;
102 }
103 //create new Quantitative Data
104 if (quantitativeData == null) {
105 quantitativeData = ((RowWrapperDTO) rowObject).addQuantitativeData(feature);
106 }
107 /**
108 * when opening the the dialog by a key stroke, store the key pressed
109 * and pass it to the dialog. This is common behavior but by overriding
110 * this method we always pass the QuantitativeData object to the dialog
111 * so we need to buffer the key stroke
112 */
113 if(originalCanonicalValue instanceof Character){
114 this.initialInput = (Character) originalCanonicalValue;
115 }
116 }
117 return super.activateCell(parent, quantitativeData, editMode, editHandler, cell, configRegistry);
118 }
119
120 @Override
121 public void setDialogSettings(Map<String, Object> editDialogSettings) {
122 //pass through dialog setting to wrapped dialog
123 ((QuantitativeDataDialog)this.dialog).setDialogSettings(editDialogSettings);
124 }
125
126 }