1 package org
.argeo
.slc
.akb
.ui
.composites
;
6 import javax
.jcr
.Property
;
7 import javax
.jcr
.RepositoryException
;
8 import javax
.jcr
.Session
;
10 import org
.argeo
.eclipse
.ui
.utils
.CommandUtils
;
11 import org
.argeo
.jcr
.JcrUtils
;
12 import org
.argeo
.slc
.akb
.AkbException
;
13 import org
.argeo
.slc
.akb
.AkbNames
;
14 import org
.argeo
.slc
.akb
.AkbService
;
15 import org
.argeo
.slc
.akb
.ui
.AkbUiUtils
;
16 import org
.argeo
.slc
.akb
.ui
.commands
.OpenAkbNodeEditor
;
17 import org
.argeo
.slc
.akb
.utils
.AkbJcrUtils
;
18 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
19 import org
.eclipse
.swt
.SWT
;
20 import org
.eclipse
.swt
.events
.ModifyEvent
;
21 import org
.eclipse
.swt
.events
.ModifyListener
;
22 import org
.eclipse
.swt
.events
.SelectionAdapter
;
23 import org
.eclipse
.swt
.events
.SelectionEvent
;
24 import org
.eclipse
.swt
.layout
.GridData
;
25 import org
.eclipse
.swt
.layout
.GridLayout
;
26 import org
.eclipse
.swt
.widgets
.Combo
;
27 import org
.eclipse
.swt
.widgets
.Composite
;
28 import org
.eclipse
.swt
.widgets
.Label
;
29 import org
.eclipse
.swt
.widgets
.Link
;
30 import org
.eclipse
.swt
.widgets
.Text
;
31 import org
.eclipse
.ui
.forms
.AbstractFormPart
;
32 import org
.eclipse
.ui
.forms
.IManagedForm
;
33 import org
.eclipse
.ui
.forms
.widgets
.FormToolkit
;
35 public class ItemTemplateTitleComposite
extends Composite
{
36 // private final static Log log =
37 // LogFactory.getLog(MixTitleComposite.class);
39 private final AkbService akbService
;
40 private final Node akbNode
;
41 private final FormToolkit toolkit
;
42 private final IManagedForm form
;
43 // Don't forget to unregister on dispose
44 private AbstractFormPart formPart
;
46 // To enable set focus
47 private Text titleTxt
;
48 private Combo typeCmb
;
50 private List
<Node
> definedAliases
;
52 public ItemTemplateTitleComposite(Composite parent
, int style
,
53 FormToolkit toolkit
, IManagedForm form
, Node akbNode
,
54 AkbService akbService
) {
56 this.akbNode
= akbNode
;
57 this.toolkit
= toolkit
;
59 this.akbService
= akbService
;
64 private void populate() {
66 Composite parent
= this;
68 parent
.setLayout(new GridLayout(5, false));
70 // first line: Item name
71 toolkit
.createLabel(parent
, "Name");
72 titleTxt
= toolkit
.createText(parent
, "", SWT
.BORDER
);
73 GridData gd
= new GridData(SWT
.FILL
, SWT
.TOP
, true, false, 4, 1);
74 titleTxt
.setLayoutData(gd
);
76 // Second line: alias management
77 toolkit
.createLabel(parent
, "Alias");
78 typeCmb
= new Combo(parent
, SWT
.READ_ONLY
);
79 toolkit
.adapt(typeCmb
, false, false);
80 gd
= new GridData(SWT
.FILL
, SWT
.TOP
, true, false, 3, 1);
81 typeCmb
.setLayoutData(gd
);
83 final Link openAliasLk
= new Link(parent
, SWT
.NONE
);
84 toolkit
.adapt(openAliasLk
, false, false);
85 openAliasLk
.setText("<a> Edit Alias </a>");
86 openAliasLk
.addSelectionListener(new SelectionAdapter() {
88 public void widgetSelected(final SelectionEvent event
) {
89 int index
= typeCmb
.getSelectionIndex();
91 Node currAlias
= definedAliases
.get(index
);
92 String id
= AkbJcrUtils
.getIdentifierQuietly(currAlias
);
93 CommandUtils
.callCommand(OpenAkbNodeEditor
.ID
,
94 OpenAkbNodeEditor
.PARAM_NODE_JCR_ID
, id
);
96 MessageDialog
.openError(getShell(), "Error",
101 // 3rd line: description
102 Label lbl
= toolkit
.createLabel(parent
, "Description");
103 lbl
.setLayoutData(new GridData(SWT
.LEFT
, SWT
.TOP
, false, false));
104 final Text descTxt
= toolkit
.createText(parent
, "", SWT
.BORDER
105 | SWT
.MULTI
| SWT
.WRAP
);
106 gd
= new GridData(SWT
.FILL
, SWT
.FILL
, true, true, 4, 1);
107 descTxt
.setLayoutData(gd
);
110 final AbstractFormPart part
= new AbstractFormPart() {
111 public void refresh() {
113 // update display value
114 AkbUiUtils
.refreshFormTextWidget(titleTxt
, akbNode
,
116 AkbUiUtils
.refreshFormTextWidget(descTxt
, akbNode
,
117 Property
.JCR_DESCRIPTION
);
119 refreshTypeCmbValues();
120 typeCmb
.select(getCurrAliasIndex());
121 typeCmb
.setEnabled(AkbJcrUtils
.isNodeCheckedOutByMe(akbNode
));
126 AkbUiUtils
.addTextModifyListener(titleTxt
, akbNode
, Property
.JCR_TITLE
,
128 AkbUiUtils
.addTextModifyListener(descTxt
, akbNode
,
129 Property
.JCR_DESCRIPTION
, part
);
131 typeCmb
.addModifyListener(new ModifyListener() {
133 public void modifyText(ModifyEvent event
) {
135 int oldIndex
= getCurrAliasIndex();
136 int selIndex
= typeCmb
.getSelectionIndex();
138 // insure something has really been modified
139 if (selIndex
< 0 || oldIndex
== selIndex
)
143 Node newAlias
= definedAliases
.get(selIndex
);
145 // Only relies on the alias
146 akbNode
.setProperty(AkbNames
.AKB_USED_CONNECTOR
, newAlias
147 .getProperty(Property
.JCR_TITLE
).getString());
149 } catch (RepositoryException e
) {
150 throw new AkbException(
151 "Error while updating connector alias", e
);
159 private void refreshTypeCmbValues() {
160 List
<Node
> newAliases
;
162 newAliases
= JcrUtils
.nodeIteratorToList(akbService
163 .getDefinedAliases(AkbJcrUtils
.getCurrentTemplate(akbNode
),
164 AkbJcrUtils
.getAliasTypeForNode(akbNode
)));
165 } catch (RepositoryException e
) {
166 throw new AkbException("Unable to get defined aliases for node "
169 boolean hasChanged
= false;
170 // manually ckeck if something has changed
171 if (definedAliases
== null
172 || newAliases
.size() != definedAliases
.size())
176 for (int i
= 0; i
< newAliases
.size(); i
++) {
177 if (!newAliases
.get(i
).equals(definedAliases
.get(i
))) {
185 definedAliases
= newAliases
;
186 final String
[] names
= new String
[definedAliases
.size()];
188 for (Node node
: definedAliases
)
189 names
[i
++] = AkbJcrUtils
.get(node
, Property
.JCR_TITLE
);
190 typeCmb
.setItems(names
);
195 * Returns the index in definedAliases list of the CURRENT defined alias as
196 * set in the item node <CODE>AkbNames.AKB_USED_CONNECTOR</CODE> if defined,
199 private int getCurrAliasIndex() {
202 int currAliasIndex
= -1;
203 if (akbNode
.hasProperty(AkbNames
.AKB_USED_CONNECTOR
))
204 path
= akbNode
.getProperty(AkbNames
.AKB_USED_CONNECTOR
)
207 Session session
= akbNode
.getSession();
208 if (path
!= null && session
.nodeExists(path
)) {
209 for (int i
= 0; i
< definedAliases
.size(); i
++) {
210 if (path
.equals(definedAliases
.get(i
).getPath())) {
216 // Node chosenAlias = session.getNode(path);
217 // currAliasIndex = definedAliases.indexOf(chosenAlias);
219 return currAliasIndex
;
220 } catch (RepositoryException re
) {
221 throw new AkbException("Unable to retrieve current Alias", re
);
226 public boolean setFocus() {
227 return titleTxt
.setFocus();
230 protected void disposePart(AbstractFormPart part
) {
232 form
.removePart(part
);
238 public void dispose() {
239 disposePart(formPart
);