1 package org
.argeo
.app
.ui
.forms
;
7 import org
.argeo
.app
.api
.Term
;
8 import org
.argeo
.app
.api
.TermsManager
;
9 import org
.argeo
.cms
.swt
.CmsSwtUtils
;
10 import org
.argeo
.cms
.swt
.SwtEditablePart
;
11 import org
.argeo
.cms
.swt
.MouseDoubleClick
;
12 import org
.argeo
.cms
.swt
.MouseDown
;
13 import org
.argeo
.cms
.swt
.Selected
;
14 import org
.argeo
.cms
.swt
.widgets
.ContextOverlay
;
15 import org
.argeo
.cms
.ui
.forms
.FormStyle
;
16 import org
.argeo
.jcr
.Jcr
;
17 import org
.eclipse
.swt
.SWT
;
18 import org
.eclipse
.swt
.events
.FocusEvent
;
19 import org
.eclipse
.swt
.events
.FocusListener
;
20 import org
.eclipse
.swt
.layout
.GridLayout
;
21 import org
.eclipse
.swt
.widgets
.Composite
;
22 import org
.eclipse
.swt
.widgets
.Control
;
23 import org
.eclipse
.swt
.widgets
.Label
;
24 import org
.eclipse
.swt
.widgets
.Text
;
25 import org
.eclipse
.swt
.widgets
.ToolBar
;
26 import org
.eclipse
.swt
.widgets
.ToolItem
;
28 /** {@link SwtEditablePart} for terms. */
29 public class SingleTermPart
extends AbstractTermsPart
{
30 private static final long serialVersionUID
= -4961135649177920808L;
32 public SingleTermPart(Composite parent
, int style
, Item item
, TermsManager termsManager
, String typology
) {
33 super(parent
, style
, item
, termsManager
, typology
);
37 protected Control
createControl(Composite box
, String style
) {
39 Composite block
= new Composite(box
, SWT
.NONE
);
40 block
.setLayout(CmsSwtUtils
.noSpaceGridLayout(3));
42 createHighlight(block
);
44 Text txt
= new Text(block
, SWT
.SINGLE
| SWT
.BORDER
);
45 CmsSwtUtils
.style(txt
, style
== null ? FormStyle
.propertyText
.style() : style
);
47 ToolBar toolBar
= new ToolBar(block
, SWT
.HORIZONTAL
);
48 ToolItem deleteItem
= new ToolItem(toolBar
, SWT
.PUSH
);
49 styleDelete(deleteItem
);
50 deleteItem
.addSelectionListener((Selected
) (e
) -> {
54 ToolItem cancelItem
= new ToolItem(toolBar
, SWT
.PUSH
);
55 styleCancel(cancelItem
);
56 cancelItem
.addSelectionListener((Selected
) (e
) -> {
60 ContextOverlay contextOverlay
= new ContextOverlay(txt
, SWT
.NONE
) {
61 private static final long serialVersionUID
= -7980078594405384874L;
64 protected void onHide() {
68 contextOverlay
.setLayout(new GridLayout());
70 txt
.addModifyListener((e
) -> {
71 String filter
= txt
.getText().toLowerCase();
72 if ("".equals(filter
.trim()))
74 refresh(contextOverlay
, filter
, txt
);
76 txt
.addFocusListener(new FocusListener() {
77 private static final long serialVersionUID
= -6024501573409619949L;
80 public void focusLost(FocusEvent event
) {
81 // if (!contextOverlay.isDisposed() && contextOverlay.isShellVisible())
82 // getDisplay().asyncExec(() -> stopEditing());
86 public void focusGained(FocusEvent event
) {
88 if (!contextOverlay
.isDisposed() && !contextOverlay
.isShellVisible())
89 refresh(contextOverlay
, null, txt
);
92 layout(new Control
[] { block
});
93 getDisplay().asyncExec(() -> txt
.setFocus());
96 Composite block
= new Composite(box
, SWT
.NONE
);
97 block
.setLayout(CmsSwtUtils
.noSpaceGridLayout(2));
98 Term currentValue
= getValue();
99 if (currentValue
!= null) {
100 Label lbl
= new Label(block
, SWT
.SINGLE
);
101 String display
= getTermLabel(currentValue
);
102 lbl
.setText(display
);
103 CmsSwtUtils
.style(lbl
, style
== null ? FormStyle
.propertyText
.style() : style
);
104 processTermListLabel(currentValue
, lbl
);
106 lbl
.addMouseListener((MouseDoubleClick
) (e
) -> {
112 ToolBar toolBar
= new ToolBar(block
, SWT
.HORIZONTAL
);
113 ToolItem addItem
= new ToolItem(toolBar
, SWT
.FLAT
);
115 addItem
.addSelectionListener((Selected
) (e
) -> {
125 protected void refresh(ContextOverlay contextArea
, String filter
, Text txt
) {
126 CmsSwtUtils
.clear(contextArea
);
127 List
<?
extends Term
> terms
= termsManager
.listAllTerms(typology
.getId());
128 terms
: for (Term term
: terms
) {
129 String display
= getTermLabel(term
);
130 if (filter
!= null && !display
.toLowerCase().contains(filter
))
132 Label termL
= new Label(contextArea
, SWT
.WRAP
);
133 termL
.setText(display
);
134 processTermListLabel(term
, termL
);
135 if (isTermSelectable(term
))
136 termL
.addMouseListener((MouseDown
) (e
) -> {
146 protected Term
getValue() {
147 String property
= typology
.getId();
148 String id
= Jcr
.get(getNode(), property
);
149 Term term
= termsManager
.getTerm(id
);
154 protected void setValue(Term value
) {
155 String property
= typology
.getId();
156 Jcr
.set(getNode(), property
, value
!= null ? value
.getId() : null);