-
Notifications
You must be signed in to change notification settings - Fork 96
/
PTChooserEditor.java
185 lines (161 loc) · 5.86 KB
/
PTChooserEditor.java
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
/*******************************************************************************
* Copyright (c) 2012 Laurent CARON
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors: Laurent CARON (laurent.caron at gmail dot com) - initial API
* and implementation
*******************************************************************************/
package org.eclipse.nebula.widgets.opal.propertytable.editor;
import org.eclipse.nebula.widgets.opal.commons.ResourceManager;
import org.eclipse.nebula.widgets.opal.commons.SWTGraphicUtil;
import org.eclipse.nebula.widgets.opal.propertytable.PTProperty;
import org.eclipse.nebula.widgets.opal.propertytable.PTWidget;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ControlEditor;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.custom.TreeEditor;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
/**
* This abstract class represents a chooser. A chooser is composed of :
* <ul>
* <li>a displayed value (text)
* <li>a "X" button to erase the value (set to null)
* <li>a "..." button to open an extra window to set up the value
* </ul>
*
*/
public abstract class PTChooserEditor extends PTEditor {
private PTWidget widget;
private Item item;
private PTProperty property;
/**
* @see org.eclipse.nebula.widgets.opal.propertytable.editor.PTEditor#render(org.eclipse.nebula.widgets.opal.propertytable.PTWidget,
* org.eclipse.swt.widgets.Item, org.eclipse.nebula.widgets.opal.propertytable.PTProperty)
*/
@Override
public ControlEditor render(final PTWidget widget, final Item item, final PTProperty property) {
this.widget = widget;
this.item = item;
this.property = property;
if (item instanceof TableItem) {
((TableItem) item).setText(1, getTextFor(property));
} else {
((TreeItem) item).setText(1, getTextFor(property));
}
final Color bgColor = getBackgroundColor(property);
if (bgColor != null) {
if (item instanceof TableItem) {
((TableItem) item).setBackground(1, bgColor);
}
if (item instanceof TreeItem) {
((TreeItem) item).setBackground(1, bgColor);
}
SWTGraphicUtil.addDisposer(item, bgColor);
}
ControlEditor editor;
if (widget.getWidget() instanceof Table) {
editor = new TableEditor((Table) widget.getWidget());
} else {
editor = new TreeEditor((Tree) widget.getWidget());
}
final Composite buttonHolder = new Composite(widget.getWidget(), SWT.NONE);
final FillLayout buttonHolderLayout = new FillLayout(SWT.HORIZONTAL);
buttonHolderLayout.marginWidth = buttonHolderLayout.marginHeight = 0;
buttonHolder.setLayout(buttonHolderLayout);
createEraseButton(buttonHolder);
createPlusButton(buttonHolder);
buttonHolder.pack();
editor.minimumWidth = buttonHolder.getSize().x;
editor.horizontalAlignment = SWT.RIGHT;
if (widget.getWidget() instanceof Table) {
((TableEditor) editor).setEditor(buttonHolder, (TableItem) item, 1);
} else {
((TreeEditor) editor).setEditor(buttonHolder, (TreeItem) item, 1);
}
return editor;
}
/**
* Creates the "erase" button
*
* @param buttonHolder parent composite
*/
private void createEraseButton(final Composite buttonHolder) {
final Button eraseButton = new Button(buttonHolder, SWT.PUSH);
eraseButton.setText(" X ");
eraseButton.setToolTipText(ResourceManager.getLabel(ResourceManager.ERASE_PROPERTY));
eraseButton.setEnabled(property.isEnabled());
eraseButton.pack();
eraseButton.addListener(SWT.Selection, event -> {
property.setValue(null);
if (item instanceof TableItem) {
((TableItem) item).setBackground(1, Display.getDefault().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
((TableItem) item).setText(1, getTextFor(property));
}
if (item instanceof TreeItem) {
((TreeItem) item).setBackground(1, Display.getDefault().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
((TreeItem) item).setText(1, getTextFor(property));
}
});
eraseButton.addListener(SWT.FocusIn, new Listener() {
@Override
public void handleEvent(final Event event) {
widget.updateDescriptionPanel(property);
}
});
}
/**
* Creates the "plus" button
*
* @param buttonHolder aprent composite
*/
private void createPlusButton(final Composite buttonHolder) {
final Button plusButton = new Button(buttonHolder, SWT.PUSH);
plusButton.setText("...");
plusButton.setToolTipText(ResourceManager.getLabel(ResourceManager.EDIT_PROPERTY));
plusButton.setEnabled(property.isEnabled());
plusButton.addListener(SWT.Selection, event -> {
openWindow(widget, item, property);
});
plusButton.addListener(SWT.FocusIn, event -> {
widget.updateDescriptionPanel(property);
});
plusButton.pack();
}
/**
* Open the window to edit the property
*
* @param widget parent widget
* @param item item
* @param property edited property
*/
protected abstract void openWindow(PTWidget widget, Item item, PTProperty property);
/**
* @param property property
* @return the string representation of the value stored in the property
*/
protected abstract String getTextFor(PTProperty property);
/**
* Get the background color of an item
*
* @param property property
* @return a background color (for the PTColorEditor) or null (for other
* editors).
*/
protected abstract Color getBackgroundColor(PTProperty property);
}