-
Notifications
You must be signed in to change notification settings - Fork 4
/
MultiLineCellEditor.java
159 lines (133 loc) · 3.37 KB
/
MultiLineCellEditor.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
package com.xored.quality.mockups.jface.celleditors;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class MultiLineCellEditor extends CellEditor {
Shell shell;
private FocusListener focus;
DisposeListener dispose;
private boolean isOpen;
Rectangle rectangle;
private String value;
public MultiLineCellEditor(Composite parent, String value) {
super(parent);
this.value = value;
focus = new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
deactivate();
}
@Override
public void focusGained(FocusEvent e) {
}
};
dispose = new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
shell.removeDisposeListener(dispose);
}
};
}
@Override
protected Control createControl(Composite parent) {
Composite cell = new Composite(parent, SWT.NONE);
cell.setLayout(new DialogCellLayout());
return cell;
}
@Override
protected Object doGetValue() {
return value;
}
@Override
protected void doSetFocus() {
}
@Override
protected void focusLost() {
super.focusLost();
}
@Override
public void activate() {
if (rectangle != null && !isOpen) {
isOpen = true;
shell = new Shell(getControl().getShell(), SWT.NONE);
shell.setBounds(rectangle);
shell.setLayout(new FillLayout());
Text text = new Text(shell, SWT.MULTI);
if (value != null) {
text.setText(value);
}
shell.update();
shell.layout();
shell.addDisposeListener(dispose);
text.addFocusListener(focus);
shell.open();
super.activate();
}
}
@Override
public void deactivate() {
if (isOpen) {
isOpen = false;
Display.getCurrent().syncExec(new Runnable() {
@Override
public void run() {
if (shell != null && !shell.isDisposed()) {
setValue();
fireApplyEditorValue();
if (!shell.isDisposed())
shell.close();
}
}
});
}
super.deactivate();
}
void setValue() {
if (shell != null && !shell.isDisposed()) {
Text text = (Text) shell.getChildren()[0];
String str = text.getText();
value = str.replaceAll("\\\\r\\\\n", "\n");
}
}
@Override
public void dispose() {
if (isOpen) {
deactivate();
}
if (shell != null && !shell.isDisposed()) {
shell.dispose();
}
super.dispose();
}
@Override
protected void doSetValue(Object value) {
}
class DialogCellLayout extends Layout {
@Override
public void layout(Composite editor, boolean force) {
Rectangle bounds = editor.getClientArea();
Point real = editor.toDisplay(bounds.x, bounds.y);
rectangle = new Rectangle(real.x, real.y, bounds.width, 55);
activate();
}
@Override
public Point computeSize(Composite editor, int wHint, int hHint, boolean force) {
if (wHint != SWT.DEFAULT && hHint != SWT.DEFAULT) {
return new Point(wHint, hHint);
}
return new Point(SWT.DEFAULT, SWT.DEFAULT);
}
}
}