1 package org
.argeo
.cms
.ui
.forms
;
6 import javax
.jcr
.RepositoryException
;
8 import org
.argeo
.cms
.swt
.CmsSwtUtils
;
9 import org
.argeo
.cms
.swt
.EditablePart
;
10 import org
.argeo
.cms
.ui
.widgets
.StyledControl
;
11 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
12 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
13 import org
.eclipse
.swt
.SWT
;
14 import org
.eclipse
.swt
.events
.SelectionListener
;
15 import org
.eclipse
.swt
.events
.TraverseEvent
;
16 import org
.eclipse
.swt
.events
.TraverseListener
;
17 import org
.eclipse
.swt
.layout
.GridData
;
18 import org
.eclipse
.swt
.layout
.GridLayout
;
19 import org
.eclipse
.swt
.layout
.RowLayout
;
20 import org
.eclipse
.swt
.widgets
.Button
;
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
;
26 /** Display, add or remove values from a list in a CMS context */
27 public class EditableMultiStringProperty
extends StyledControl
implements EditablePart
{
28 private static final long serialVersionUID
= -7044614381252178595L;
30 private String propertyName
;
31 private String message
;
32 // TODO implement the ability to provide a list of possible values
33 // private String[] possibleValues;
34 private boolean canEdit
;
35 private SelectionListener removeValueSL
;
36 private List
<String
> values
;
38 // TODO manage within the CSS
39 private int rowSpacing
= 5;
40 private int rowMarging
= 0;
41 private int oneValueMargingRight
= 5;
42 private int btnWidth
= 16;
43 private int btnHeight
= 16;
44 private int btnHorizontalIndent
= 3;
46 public EditableMultiStringProperty(Composite parent
, int style
, Node node
, String propertyName
, List
<String
> values
,
47 String
[] possibleValues
, String addValueMsg
, SelectionListener removeValueSelectionListener
)
48 throws RepositoryException
{
49 super(parent
, style
, node
, true);
51 this.propertyName
= propertyName
;
53 // this.possibleValues = new String[] { "Un", "Deux", "Trois" };
54 this.message
= addValueMsg
;
55 this.canEdit
= removeValueSelectionListener
!= null;
56 this.removeValueSL
= removeValueSelectionListener
;
59 public List
<String
> getValues() {
63 public void setValues(List
<String
> values
) {
67 // Row layout items do not need explicit layout data
68 protected void setControlLayoutData(Control control
) {
71 /** To be overridden */
72 protected void setContainerLayoutData(Composite composite
) {
73 composite
.setLayoutData(CmsSwtUtils
.fillWidth());
77 public Control
getControl() {
78 return super.getControl();
82 protected Control
createControl(Composite box
, String style
) {
83 Composite row
= new Composite(box
, SWT
.NO_FOCUS
);
84 row
.setLayoutData(EclipseUiUtils
.fillAll());
86 RowLayout rl
= new RowLayout(SWT
.HORIZONTAL
);
88 rl
.spacing
= rowSpacing
;
89 rl
.marginRight
= rl
.marginLeft
= rl
.marginBottom
= rl
.marginTop
= rowMarging
;
93 for (final String value
: values
) {
95 createRemovableValue(row
, SWT
.SINGLE
, value
);
97 createValueLabel(row
, SWT
.SINGLE
, value
);
103 else if (isEditing())
104 return createText(row
, style
);
106 return createLabel(row
, style
);
110 * Override to provide specific layout for the existing values, typically adding
111 * a pound (#) char for tags or anchor info for browsable links. We assume the
112 * parent composite already has a layout and it is the caller responsibility to
113 * apply corresponding layout data
115 protected Label
createValueLabel(Composite parent
, int style
, String value
) {
116 Label label
= new Label(parent
, style
);
117 label
.setText("#" + value
);
118 CmsSwtUtils
.markup(label
);
119 CmsSwtUtils
.style(label
, FormStyle
.propertyText
.style());
123 private Composite
createRemovableValue(Composite parent
, int style
, String value
) {
124 Composite valCmp
= new Composite(parent
, SWT
.NO_FOCUS
);
125 GridLayout gl
= EclipseUiUtils
.noSpaceGridLayout(new GridLayout(2, false));
126 gl
.marginRight
= oneValueMargingRight
;
127 valCmp
.setLayout(gl
);
129 createValueLabel(valCmp
, SWT
.WRAP
, value
);
131 Button deleteBtn
= new Button(valCmp
, SWT
.FLAT
);
132 deleteBtn
.setData(FormConstants
.LINKED_VALUE
, value
);
133 deleteBtn
.addSelectionListener(removeValueSL
);
134 CmsSwtUtils
.style(deleteBtn
, FormStyle
.delete
.style() + FormStyle
.BUTTON_SUFFIX
);
135 GridData gd
= new GridData();
136 gd
.heightHint
= btnHeight
;
137 gd
.widthHint
= btnWidth
;
138 gd
.horizontalIndent
= btnHorizontalIndent
;
139 deleteBtn
.setLayoutData(gd
);
144 protected Text
createText(Composite box
, String style
) {
145 final Text text
= new Text(box
, getStyle());
146 // The "add new value" text is not meant to change, so we can set it on
148 text
.setMessage(message
);
149 CmsSwtUtils
.style(text
, style
);
152 text
.addTraverseListener(new TraverseListener() {
153 private static final long serialVersionUID
= 1L;
155 public void keyTraversed(TraverseEvent e
) {
156 if (e
.keyCode
== SWT
.CR
) {
163 // The OK button does not work with the focusOut listener
164 // because focus out is called before the OK button is pressed
166 // // we must call layout() now so that the row data can compute the
168 // // of the other controls.
169 // text.getParent().layout();
170 // int height = text.getSize().y;
172 // Button okBtn = new Button(box, SWT.BORDER | SWT.PUSH | SWT.BOTTOM);
173 // okBtn.setText("OK");
174 // RowData rd = new RowData(SWT.DEFAULT, height - 2);
175 // okBtn.setLayoutData(rd);
177 // okBtn.addSelectionListener(new SelectionAdapter() {
178 // private static final long serialVersionUID = 2780819012423622369L;
181 // public void widgetSelected(SelectionEvent e) {
189 /** Performs the real addition, overwrite to make further sanity checks */
190 protected void addValue(Text text
) {
191 String value
= text
.getText();
192 String errMsg
= null;
194 if (EclipseUiUtils
.isEmpty(value
))
197 if (values
.contains(value
))
198 errMsg
= "Dupplicated value: " + value
+ ", please correct and try again";
200 MessageDialog
.openError(this.getShell(), "Addition not allowed", errMsg
);
203 Composite newCmp
= createRemovableValue(text
.getParent(), SWT
.SINGLE
, value
);
204 newCmp
.moveAbove(text
);
206 newCmp
.getParent().layout();
210 protected Label
createLabel(Composite box
, String style
) {
212 Label lbl
= new Label(box
, getStyle());
213 lbl
.setText(message
);
214 CmsSwtUtils
.style(lbl
, style
);
215 CmsSwtUtils
.markup(lbl
);
216 if (mouseListener
!= null)
217 lbl
.addMouseListener(mouseListener
);
223 protected void clear(boolean deep
) {
224 Control child
= getControl();
228 child
.getParent().dispose();
232 public void setText(String text
) {
233 Control child
= getControl();
234 if (child
instanceof Label
) {
235 Label lbl
= (Label
) child
;
240 } else if (child
instanceof Text
) {
241 Text txt
= (Text
) child
;
246 public synchronized void startEditing() {
247 CmsSwtUtils
.style(getControl(), FormStyle
.propertyText
.style());
248 // getControl().setData(STYLE, FormStyle.propertyText.style());
249 super.startEditing();
252 public synchronized void stopEditing() {
253 CmsSwtUtils
.style(getControl(), FormStyle
.propertyMessage
.style());
254 // getControl().setData(STYLE, FormStyle.propertyMessage.style());
258 public String
getPropertyName() {