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
);
58 processTermListLabel(value
, lbl
);
60 lbl
.addMouseListener((MouseDoubleClick
) (e
) -> {
64 ToolBar toolBar
= new ToolBar(block
, SWT
.HORIZONTAL
);
65 ToolItem deleteItem
= new ToolItem(toolBar
, SWT
.FLAT
);
66 styleDelete(deleteItem
);
67 deleteItem
.addSelectionListener((Selected
) (e
) -> {
68 // we retrieve them again here because they may have changed
69 List
<Term
> curr
= getValue();
70 List
<Term
> newValue
= new ArrayList
<>();
83 if (isEditable() && !isEditing()) {
84 ToolBar toolBar
= new ToolBar(placeholder
, SWT
.HORIZONTAL
);
85 ToolItem addItem
= new ToolItem(toolBar
, SWT
.FLAT
);
87 addItem
.addSelectionListener((Selected
) (e
) -> {
94 Composite block
= new Composite(placeholder
, SWT
.NONE
);
95 block
.setLayout(CmsUiUtils
.noSpaceGridLayout(3));
97 createHighlight(block
);
99 Text txt
= new Text(block
, SWT
.SINGLE
| SWT
.BORDER
);
100 txt
.setLayoutData(CmsUiUtils
.fillWidth());
101 // txt.setMessage("[new]");
103 CmsUiUtils
.style(txt
, style
== null ? FormStyle
.propertyText
.style() : style
);
105 ToolBar toolBar
= new ToolBar(block
, SWT
.HORIZONTAL
);
106 ToolItem cancelItem
= new ToolItem(toolBar
, SWT
.FLAT
);
107 styleCancel(cancelItem
);
108 cancelItem
.addSelectionListener((Selected
) (e
) -> {
112 ContextOverlay contextOverlay
= new ContextOverlay(txt
, SWT
.NONE
) {
113 private static final long serialVersionUID
= -7980078594405384874L;
116 protected void onHide() {
120 contextOverlay
.setLayout(new GridLayout());
122 txt
.addModifyListener((e
) -> {
123 String filter
= txt
.getText().toLowerCase();
124 if ("".equals(filter
.trim()))
126 refresh(contextOverlay
, filter
, txt
);
128 txt
.addFocusListener(new FocusListener() {
129 private static final long serialVersionUID
= -6024501573409619949L;
132 public void focusLost(FocusEvent event
) {
133 // if (!contextOverlay.isDisposed() && contextOverlay.isShellVisible())
134 // getDisplay().asyncExec(() -> stopEditing());
138 public void focusGained(FocusEvent event
) {
140 if (!contextOverlay
.isDisposed() && !contextOverlay
.isShellVisible())
141 refresh(contextOverlay
, null, txt
);
144 layout(new Control
[] { txt
});
145 // getDisplay().asyncExec(() -> txt.setFocus());
151 protected void refresh(ContextOverlay contextArea
, String filter
, Text txt
) {
152 CmsUiUtils
.clear(contextArea
);
153 List
<?
extends Term
> terms
= termsManager
.listAllTerms(typology
.getId());
154 List
<Term
> currentValue
= getValue();
155 terms
: for (Term term
: terms
) {
156 if (currentValue
!= null && currentValue
.contains(term
))
158 String display
= getTermLabel(term
);
159 if (filter
!= null && !display
.toLowerCase().contains(filter
))
161 Label termL
= new Label(contextArea
, SWT
.WRAP
);
162 termL
.setText(display
);
163 processTermListLabel(term
, termL
);
164 if (isTermSelectable(term
))
165 termL
.addMouseListener((MouseDown
) (e
) -> {
166 List
<Term
> newValue
= new ArrayList
<>();
167 List
<Term
> curr
= getValue();
168 if (currentValue
!= null)
169 newValue
.addAll(curr
);
179 protected List
<Term
> getValue() {
180 String property
= typology
.getId();
181 List
<String
> curr
= Jcr
.getMultiple(getNode(), property
);
182 List
<Term
> res
= new ArrayList
<>();
184 terms
: for (String str
: curr
) {
185 Term term
= termsManager
.getTerm(str
);
187 log
.warn("Ignoring term " + str
+ " for " + getNode() + ", as it was not found.");
195 protected void setValue(List
<Term
> value
) {
196 String property
= typology
.getId();
197 List
<String
> ids
= new ArrayList
<>();
198 for (Term term
: value
) {
199 ids
.add(term
.getId());
201 Jcr
.set(getNode(), property
, ids
);