X-Git-Url: https://git.argeo.org/?p=gpl%2Fargeo-suite.git;a=blobdiff_plain;f=org.argeo.entity.ui%2Fsrc%2Forg%2Fargeo%2Fentity%2Fui%2Fforms%2FMultiTermsPart.java;h=00bcd7068af1ef185d427d478451418db67a19ea;hp=e26fabe98982e0d899e7189e7a070ad6da0157cd;hb=3cf66bc01bb8ad4c55139ae01be5a5bdb3759e2c;hpb=5401f7a23b5251bb37514f5495b75f79320bedc0 diff --git a/org.argeo.entity.ui/src/org/argeo/entity/ui/forms/MultiTermsPart.java b/org.argeo.entity.ui/src/org/argeo/entity/ui/forms/MultiTermsPart.java index e26fabe..00bcd70 100644 --- a/org.argeo.entity.ui/src/org/argeo/entity/ui/forms/MultiTermsPart.java +++ b/org.argeo.entity.ui/src/org/argeo/entity/ui/forms/MultiTermsPart.java @@ -5,13 +5,16 @@ import java.util.List; import javax.jcr.Item; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.argeo.cms.swt.CmsSwtUtils; +import org.argeo.cms.swt.MouseDoubleClick; +import org.argeo.cms.swt.MouseDown; +import org.argeo.cms.swt.Selected; import org.argeo.cms.ui.forms.FormStyle; -import org.argeo.cms.ui.util.CmsUiUtils; import org.argeo.cms.ui.viewers.EditablePart; import org.argeo.cms.ui.widgets.ContextOverlay; -import org.argeo.eclipse.ui.MouseDoubleClick; -import org.argeo.eclipse.ui.MouseDown; -import org.argeo.eclipse.ui.Selected; +import org.argeo.entity.Term; import org.argeo.entity.TermsManager; import org.argeo.jcr.Jcr; import org.eclipse.swt.SWT; @@ -29,6 +32,7 @@ import org.eclipse.swt.widgets.ToolItem; /** {@link EditablePart} for multiple terms. */ public class MultiTermsPart extends AbstractTermsPart { private static final long serialVersionUID = -4961135649177920808L; + private final static Log log = LogFactory.getLog(MultiTermsPart.class); public MultiTermsPart(Composite parent, int style, Item item, TermsManager termsManager, String typology) { super(parent, style, item, termsManager, typology); @@ -37,18 +41,24 @@ public class MultiTermsPart extends AbstractTermsPart { @Override protected Control createControl(Composite box, String style) { Composite placeholder = new Composite(box, SWT.NONE); - RowLayout rl = new RowLayout(SWT.HORIZONTAL | SWT.WRAP); + + boolean vertical = SWT.VERTICAL == (getStyle() & SWT.VERTICAL); + RowLayout rl = new RowLayout(vertical ? SWT.VERTICAL : SWT.HORIZONTAL); + rl = CmsSwtUtils.noMarginsRowLayout(rl); +// rl.wrap = true; +// rl.justify = true; placeholder.setLayout(rl); - List currentValue = Jcr.getMultiple(getNode(), typology); - if (currentValue != null && !currentValue.isEmpty()) - for (String value : currentValue) { + List currentValue = getValue(); + if (currentValue != null && !currentValue.isEmpty()) { + for (Term value : currentValue) { Composite block = new Composite(placeholder, SWT.NONE); - block.setLayout(CmsUiUtils.noSpaceGridLayout(3)); - Label lbl = new Label(block, SWT.SINGLE); + block.setLayout(CmsSwtUtils.noSpaceGridLayout(3)); + Label lbl = new Label(block, SWT.NONE); String display = getTermLabel(value); lbl.setText(display); - CmsUiUtils.style(lbl, style == null ? FormStyle.propertyText.style() : style); - if (editable) + CmsSwtUtils.style(lbl, style == null ? FormStyle.propertyText.style() : style); + processTermListLabel(value, lbl); + if (isEditable()) lbl.addMouseListener((MouseDoubleClick) (e) -> { startEditing(); }); @@ -57,21 +67,22 @@ public class MultiTermsPart extends AbstractTermsPart { ToolItem deleteItem = new ToolItem(toolBar, SWT.FLAT); styleDelete(deleteItem); deleteItem.addSelectionListener((Selected) (e) -> { - List newValue = new ArrayList<>(); - for (String v : currentValue) { + // we retrieve them again here because they may have changed + List curr = getValue(); + List newValue = new ArrayList<>(); + for (Term v : curr) { if (!v.equals(value)) newValue.add(v); } - Jcr.set(getNode(), typology, newValue); - Jcr.save(getNode()); + setValue(newValue); block.dispose(); layout(true, true); }); } } - else {// empty - if (editable && !isEditing()) { + } else {// empty + if (isEditable() && !isEditing()) { ToolBar toolBar = new ToolBar(placeholder, SWT.HORIZONTAL); ToolItem addItem = new ToolItem(toolBar, SWT.FLAT); styleAdd(addItem); @@ -83,15 +94,15 @@ public class MultiTermsPart extends AbstractTermsPart { if (isEditing()) { Composite block = new Composite(placeholder, SWT.NONE); - block.setLayout(CmsUiUtils.noSpaceGridLayout(3)); + block.setLayout(CmsSwtUtils.noSpaceGridLayout(3)); createHighlight(block); Text txt = new Text(block, SWT.SINGLE | SWT.BORDER); - txt.setLayoutData(CmsUiUtils.fillWidth()); + txt.setLayoutData(CmsSwtUtils.fillWidth()); // txt.setMessage("[new]"); - CmsUiUtils.style(txt, style == null ? FormStyle.propertyText.style() : style); + CmsSwtUtils.style(txt, style == null ? FormStyle.propertyText.style() : style); ToolBar toolBar = new ToolBar(block, SWT.HORIZONTAL); ToolItem cancelItem = new ToolItem(toolBar, SWT.FLAT); @@ -140,10 +151,10 @@ public class MultiTermsPart extends AbstractTermsPart { @Override protected void refresh(ContextOverlay contextArea, String filter, Text txt) { - CmsUiUtils.clear(contextArea); - List terms = termsManager.listAllTerms(typology); - List currentValue = Jcr.getMultiple(getNode(), typology); - terms: for (String term : terms) { + CmsSwtUtils.clear(contextArea); + List terms = termsManager.listAllTerms(typology.getId()); + List currentValue = getValue(); + terms: for (Term term : terms) { if (currentValue != null && currentValue.contains(term)) continue terms; String display = getTermLabel(term); @@ -154,12 +165,12 @@ public class MultiTermsPart extends AbstractTermsPart { processTermListLabel(term, termL); if (isTermSelectable(term)) termL.addMouseListener((MouseDown) (e) -> { - List newValue = new ArrayList<>(); + List newValue = new ArrayList<>(); + List curr = getValue(); if (currentValue != null) - newValue.addAll(currentValue); + newValue.addAll(curr); newValue.add(term); - Jcr.set(getNode(), typology, newValue); - Jcr.save(getNode()); + setValue(newValue); contextArea.hide(); stopEditing(); }); @@ -167,4 +178,30 @@ public class MultiTermsPart extends AbstractTermsPart { contextArea.show(); } + protected List getValue() { + String property = typology.getId(); + List curr = Jcr.getMultiple(getNode(), property); + List res = new ArrayList<>(); + if (curr != null) + terms: for (String str : curr) { + Term term = termsManager.getTerm(str); + if (term == null) { + log.warn("Ignoring term " + str + " for " + getNode() + ", as it was not found."); + continue terms; + } + res.add(term); + } + return res; + } + + protected void setValue(List value) { + String property = typology.getId(); + List ids = new ArrayList<>(); + for (Term term : value) { + ids.add(term.getId()); + } + Jcr.set(getNode(), property, ids); + Jcr.save(getNode()); + } + }