1 package org
.argeo
.entity
.ui
.forms
;
3 import java
.util
.ArrayList
;
8 import org
.apache
.commons
.logging
.Log
;
9 import org
.apache
.commons
.logging
.LogFactory
;
10 import org
.argeo
.cms
.ui
.forms
.FormStyle
;
11 import org
.argeo
.cms
.ui
.util
.CmsUiUtils
;
12 import org
.argeo
.cms
.ui
.viewers
.EditablePart
;
13 import org
.argeo
.cms
.ui
.widgets
.ContextOverlay
;
14 import org
.argeo
.eclipse
.ui
.MouseDoubleClick
;
15 import org
.argeo
.eclipse
.ui
.MouseDown
;
16 import org
.argeo
.eclipse
.ui
.Selected
;
17 import org
.argeo
.entity
.Term
;
18 import org
.argeo
.entity
.TermsManager
;
19 import org
.argeo
.jcr
.Jcr
;
20 import org
.eclipse
.swt
.SWT
;
21 import org
.eclipse
.swt
.events
.FocusEvent
;
22 import org
.eclipse
.swt
.events
.FocusListener
;
23 import org
.eclipse
.swt
.layout
.GridLayout
;
24 import org
.eclipse
.swt
.layout
.RowLayout
;
25 import org
.eclipse
.swt
.widgets
.Composite
;
26 import org
.eclipse
.swt
.widgets
.Control
;
27 import org
.eclipse
.swt
.widgets
.Label
;
28 import org
.eclipse
.swt
.widgets
.Text
;
29 import org
.eclipse
.swt
.widgets
.ToolBar
;
30 import org
.eclipse
.swt
.widgets
.ToolItem
;
32 /** {@link EditablePart} for multiple terms. */
33 public class MultiTermsPart
extends AbstractTermsPart
{
34 private static final long serialVersionUID
= -4961135649177920808L;
35 private final static Log log
= LogFactory
.getLog(MultiTermsPart
.class);
37 public MultiTermsPart(Composite parent
, int style
, Item item
, TermsManager termsManager
, String typology
) {
38 super(parent
, style
, item
, termsManager
, typology
);
42 protected Control
createControl(Composite box
, String style
) {
43 Composite placeholder
= new Composite(box
, SWT
.NONE
);
45 boolean vertical
= SWT
.VERTICAL
== (getStyle() & SWT
.VERTICAL
);
46 RowLayout rl
= new RowLayout(vertical ? SWT
.VERTICAL
: SWT
.HORIZONTAL
);
48 placeholder
.setLayout(rl
);
49 List
<Term
> currentValue
= getValue();
50 if (currentValue
!= null && !currentValue
.isEmpty())
51 for (Term value
: currentValue
) {
52 Composite block
= new Composite(placeholder
, SWT
.NONE
);
53 block
.setLayout(CmsUiUtils
.noSpaceGridLayout(3));
54 Label lbl
= new Label(block
, SWT
.SINGLE
);
55 String display
= getTermLabel(value
);
57 CmsUiUtils
.style(lbl
, style
== null ? FormStyle
.propertyText
.style() : style
);
59 lbl
.addMouseListener((MouseDoubleClick
) (e
) -> {
63 ToolBar toolBar
= new ToolBar(block
, SWT
.HORIZONTAL
);
64 ToolItem deleteItem
= new ToolItem(toolBar
, SWT
.FLAT
);
65 styleDelete(deleteItem
);
66 deleteItem
.addSelectionListener((Selected
) (e
) -> {
67 // we retrieve them again here because they may have changed
68 List
<Term
> curr
= getValue();
69 List
<Term
> newValue
= new ArrayList
<>();
82 if (editable
&& !isEditing()) {
83 ToolBar toolBar
= new ToolBar(placeholder
, SWT
.HORIZONTAL
);
84 ToolItem addItem
= new ToolItem(toolBar
, SWT
.FLAT
);
86 addItem
.addSelectionListener((Selected
) (e
) -> {
93 Composite block
= new Composite(placeholder
, SWT
.NONE
);
94 block
.setLayout(CmsUiUtils
.noSpaceGridLayout(3));
96 createHighlight(block
);
98 Text txt
= new Text(block
, SWT
.SINGLE
| SWT
.BORDER
);
99 txt
.setLayoutData(CmsUiUtils
.fillWidth());
100 // txt.setMessage("[new]");
102 CmsUiUtils
.style(txt
, style
== null ? FormStyle
.propertyText
.style() : style
);
104 ToolBar toolBar
= new ToolBar(block
, SWT
.HORIZONTAL
);
105 ToolItem cancelItem
= new ToolItem(toolBar
, SWT
.FLAT
);
106 styleCancel(cancelItem
);
107 cancelItem
.addSelectionListener((Selected
) (e
) -> {
111 ContextOverlay contextOverlay
= new ContextOverlay(txt
, SWT
.NONE
) {
112 private static final long serialVersionUID
= -7980078594405384874L;
115 protected void onHide() {
119 contextOverlay
.setLayout(new GridLayout());
121 txt
.addModifyListener((e
) -> {
122 String filter
= txt
.getText().toLowerCase();
123 if ("".equals(filter
.trim()))
125 refresh(contextOverlay
, filter
, txt
);
127 txt
.addFocusListener(new FocusListener() {
128 private static final long serialVersionUID
= -6024501573409619949L;
131 public void focusLost(FocusEvent event
) {
132 // if (!contextOverlay.isDisposed() && contextOverlay.isShellVisible())
133 // getDisplay().asyncExec(() -> stopEditing());
137 public void focusGained(FocusEvent event
) {
139 if (!contextOverlay
.isDisposed() && !contextOverlay
.isShellVisible())
140 refresh(contextOverlay
, null, txt
);
143 layout(new Control
[] { txt
});
144 // getDisplay().asyncExec(() -> txt.setFocus());
150 protected void refresh(ContextOverlay contextArea
, String filter
, Text txt
) {
151 CmsUiUtils
.clear(contextArea
);
152 List
<?
extends Term
> terms
= termsManager
.listAllTerms(typology
.getId());
153 List
<Term
> currentValue
= getValue();
154 terms
: for (Term term
: terms
) {
155 if (currentValue
!= null && currentValue
.contains(term
))
157 String display
= getTermLabel(term
);
158 if (filter
!= null && !display
.toLowerCase().contains(filter
))
160 Label termL
= new Label(contextArea
, SWT
.WRAP
);
161 termL
.setText(display
);
162 processTermListLabel(term
, termL
);
163 if (isTermSelectable(term
))
164 termL
.addMouseListener((MouseDown
) (e
) -> {
165 List
<Term
> newValue
= new ArrayList
<>();
166 List
<Term
> curr
= getValue();
167 if (currentValue
!= null)
168 newValue
.addAll(curr
);
178 protected List
<Term
> getValue() {
179 String property
= typology
.getId();
180 List
<String
> curr
= Jcr
.getMultiple(getNode(), property
);
181 List
<Term
> res
= new ArrayList
<>();
183 terms
: for (String str
: curr
) {
184 Term term
= termsManager
.getTerm(str
);
186 log
.warn("Ignoring term " + str
+ " for " + getNode() + ", as it was not found.");
194 protected void setValue(List
<Term
> value
) {
195 String property
= typology
.getId();
196 List
<String
> ids
= new ArrayList
<>();
197 for (Term term
: value
) {
198 ids
.add(term
.getId());
200 Jcr
.set(getNode(), property
, ids
);