View Javadoc
1   package org.argeo.people.web.parts;
2   
3   import javax.jcr.Node;
4   import javax.jcr.RepositoryException;
5   import javax.jcr.Value;
6   
7   import org.argeo.cms.ui.CmsUiProvider;
8   import org.argeo.cms.util.CmsUtils;
9   import org.argeo.connect.util.ConnectJcrUtils;
10  import org.eclipse.rap.rwt.RWT;
11  import org.eclipse.swt.SWT;
12  import org.eclipse.swt.events.SelectionAdapter;
13  import org.eclipse.swt.events.SelectionEvent;
14  import org.eclipse.swt.layout.RowLayout;
15  import org.eclipse.swt.widgets.Button;
16  import org.eclipse.swt.widgets.Composite;
17  import org.eclipse.swt.widgets.Control;
18  import org.eclipse.swt.widgets.Label;
19  
20  /**
21   * Display existing values of a multi-valued String property that has the
22   * injected name
23   **/
24  public class TagLikeValuesPart implements CmsUiProvider {
25  
26  	private String propertyName;
27  
28  	/** Don't forget to inject propertyName */
29  	public TagLikeValuesPart() {
30  	}
31  
32  	public TagLikeValuesPart(String propertyName) {
33  		this.propertyName = propertyName;
34  	}
35  
36  	@Override
37  	public Control createUi(Composite preParent, final Node context) throws RepositoryException {
38  		Composite parent = new Composite(preParent, SWT.NO_FOCUS);
39  
40  		RowLayout rl = new RowLayout(SWT.HORIZONTAL);
41  		rl.wrap = true;
42  		rl.spacing = 8;
43  		parent.setLayout(rl);
44  		if (context.hasProperty(propertyName)) {
45  
46  			Value[] values = context.getProperty(propertyName).getValues();
47  			for (Value value : values) {
48  				final String valueStr = value.getString();
49  				new Label(parent, SWT.NONE).setText(valueStr);
50  
51  				Button icon = new Button(parent, SWT.NONE);
52  				icon.setLayoutData(CmsUtils.rowData16px());
53  				icon.setData(RWT.CUSTOM_VARIANT, "cms_icon_delete");
54  				icon.addSelectionListener(new SelectionAdapter() {
55  					private static final long serialVersionUID = 1L;
56  
57  					@Override
58  					public void widgetSelected(SelectionEvent e) {
59  						ConnectJcrUtils.removeStringFromMultiValuedProp(context, propertyName, valueStr);
60  						// FIXME won't work: node is checked in
61  						// TODO refresh this part or the whole body
62  					}
63  				});
64  			}
65  		}
66  		return parent;
67  	}
68  
69  	public void setPropertyName(String propertyName) {
70  		this.propertyName = propertyName;
71  	}
72  }