1 package org
.argeo
.cms
.maintenance
;
3 import static javax
.jcr
.Node
.JCR_CONTENT
;
4 import static org
.eclipse
.swt
.SWT
.RIGHT
;
6 import java
.text
.DateFormat
;
7 import java
.text
.SimpleDateFormat
;
8 import java
.util
.LinkedHashMap
;
10 import javax
.jcr
.ItemNotFoundException
;
11 import javax
.jcr
.Node
;
12 import javax
.jcr
.NodeIterator
;
13 import javax
.jcr
.Property
;
14 import javax
.jcr
.PropertyIterator
;
15 import javax
.jcr
.PropertyType
;
16 import javax
.jcr
.RepositoryException
;
17 import javax
.jcr
.Value
;
19 import org
.argeo
.ArgeoException
;
20 import org
.argeo
.cms
.CmsException
;
21 import org
.argeo
.cms
.CmsTypes
;
22 import org
.argeo
.cms
.CmsUiProvider
;
23 import org
.argeo
.cms
.text
.Img
;
24 import org
.argeo
.cms
.util
.CmsLink
;
25 import org
.argeo
.cms
.util
.CmsUtils
;
26 import org
.argeo
.cms
.widgets
.EditableImage
;
27 import org
.argeo
.jcr
.JcrUtils
;
28 import org
.eclipse
.jface
.viewers
.ColumnLabelProvider
;
29 import org
.eclipse
.jface
.viewers
.ILazyContentProvider
;
30 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
31 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
32 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
33 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
34 import org
.eclipse
.jface
.viewers
.TableViewer
;
35 import org
.eclipse
.jface
.viewers
.TableViewerColumn
;
36 import org
.eclipse
.jface
.viewers
.Viewer
;
37 import org
.eclipse
.rap
.rwt
.RWT
;
38 import org
.eclipse
.swt
.SWT
;
39 import org
.eclipse
.swt
.custom
.ScrolledComposite
;
40 import org
.eclipse
.swt
.events
.ControlAdapter
;
41 import org
.eclipse
.swt
.events
.ControlEvent
;
42 import org
.eclipse
.swt
.events
.KeyEvent
;
43 import org
.eclipse
.swt
.events
.KeyListener
;
44 import org
.eclipse
.swt
.events
.ModifyEvent
;
45 import org
.eclipse
.swt
.events
.ModifyListener
;
46 import org
.eclipse
.swt
.graphics
.Point
;
47 import org
.eclipse
.swt
.graphics
.Rectangle
;
48 import org
.eclipse
.swt
.layout
.GridData
;
49 import org
.eclipse
.swt
.layout
.GridLayout
;
50 import org
.eclipse
.swt
.widgets
.Composite
;
51 import org
.eclipse
.swt
.widgets
.Control
;
52 import org
.eclipse
.swt
.widgets
.Label
;
53 import org
.eclipse
.swt
.widgets
.Table
;
54 import org
.eclipse
.swt
.widgets
.TableColumn
;
55 import org
.eclipse
.swt
.widgets
.Text
;
57 public class Browse
implements CmsUiProvider
{
59 // Some local constants to experiment. should be cleaned
60 private final static String BROWSE_PREFIX
= "browse#";
61 private final static int THUMBNAIL_WIDTH
= 400;
62 private final static int COLUMN_WIDTH
= 160;
63 private DateFormat timeFormatter
= new SimpleDateFormat(
64 "dd-MM-yyyy', 'HH:mm");
66 // keep a cache of the opened nodes
68 private LinkedHashMap
<String
, FilterEntitiesVirtualTable
> browserCols
= new LinkedHashMap
<String
, Browse
.FilterEntitiesVirtualTable
>();
69 private Composite nodeDisplayParent
;
70 private Composite colViewer
;
71 private ScrolledComposite scrolledCmp
;
72 private Text parentPathTxt
;
73 private Text filterTxt
;
74 private Node currEdited
;
76 private String initialPath
;
79 public Control
createUi(Composite parent
, Node context
)
80 throws RepositoryException
{
83 throw new CmsException("Context cannot be null");
84 GridLayout layout
= CmsUtils
.noSpaceGridLayout();
85 layout
.numColumns
= 2;
86 parent
.setLayout(layout
);
89 Composite leftCmp
= new Composite(parent
, SWT
.NO_FOCUS
);
90 leftCmp
.setLayoutData(CmsUtils
.fillAll());
91 createBrowserPart(leftCmp
, context
);
94 nodeDisplayParent
= new Composite(parent
, SWT
.NO_FOCUS
| SWT
.BORDER
);
95 GridData gd
= new GridData(SWT
.RIGHT
, SWT
.FILL
, false, true);
96 gd
.widthHint
= THUMBNAIL_WIDTH
;
97 nodeDisplayParent
.setLayoutData(gd
);
98 createNodeView(nodeDisplayParent
, context
);
102 initialPath
= context
.getPath();
104 // Workaround we don't yet manage the delete to display parent of the
105 // initial context node
110 private void createBrowserPart(Composite parent
, Node context
)
111 throws RepositoryException
{
112 GridLayout layout
= CmsUtils
.noSpaceGridLayout();
113 parent
.setLayout(layout
);
114 Composite filterCmp
= new Composite(parent
, SWT
.NO_FOCUS
);
115 filterCmp
.setLayoutData(CmsUtils
.fillWidth());
118 addFilterPanel(filterCmp
);
120 // scrolled composite
121 scrolledCmp
= new ScrolledComposite(parent
, SWT
.H_SCROLL
| SWT
.BORDER
123 scrolledCmp
.setLayoutData(CmsUtils
.fillAll());
124 scrolledCmp
.setExpandVertical(true);
125 scrolledCmp
.setExpandHorizontal(true);
126 scrolledCmp
.setShowFocusedControl(true);
128 colViewer
= new Composite(scrolledCmp
, SWT
.NO_FOCUS
);
129 scrolledCmp
.setContent(colViewer
);
130 scrolledCmp
.addControlListener(new ControlAdapter() {
131 private static final long serialVersionUID
= 6589392045145698201L;
134 public void controlResized(ControlEvent e
) {
135 Rectangle r
= scrolledCmp
.getClientArea();
136 scrolledCmp
.setMinSize(colViewer
.computeSize(SWT
.DEFAULT
,
140 initExplorer(colViewer
, context
);
143 private Control
initExplorer(Composite parent
, Node context
)
144 throws RepositoryException
{
145 parent
.setLayout(CmsUtils
.noSpaceGridLayout());
146 createBrowserColumn(parent
, context
);
150 private Control
createBrowserColumn(Composite parent
, Node context
)
151 throws RepositoryException
{
152 // TODO style is not correctly managed.
153 FilterEntitiesVirtualTable table
= new FilterEntitiesVirtualTable(
154 parent
, SWT
.BORDER
| SWT
.NO_FOCUS
, context
);
155 // CmsUtils.style(table, ArgeoOrgStyle.browserColumn.style());
156 table
.filterList("*");
157 table
.setLayoutData(new GridData(SWT
.LEFT
, SWT
.FILL
, false, true));
158 browserCols
.put(context
.getPath(), table
);
162 public void addFilterPanel(Composite parent
) {
164 parent
.setLayout(CmsUtils
.noSpaceGridLayout(new GridLayout(2, false)));
166 // Text Area for the filter
167 parentPathTxt
= new Text(parent
, SWT
.NO_FOCUS
);
168 parentPathTxt
.setEditable(false);
169 filterTxt
= new Text(parent
, SWT
.SEARCH
| SWT
.ICON_CANCEL
);
170 filterTxt
.setMessage("Filter current list");
171 filterTxt
.setLayoutData(CmsUtils
.fillWidth());
172 filterTxt
.addModifyListener(new ModifyListener() {
173 private static final long serialVersionUID
= 7709303319740056286L;
175 public void modifyText(ModifyEvent event
) {
180 filterTxt
.addKeyListener(new KeyListener() {
181 private static final long serialVersionUID
= -4523394262771183968L;
184 public void keyReleased(KeyEvent e
) {
188 public void keyPressed(KeyEvent e
) {
189 boolean shiftPressed
= (e
.stateMask
& SWT
.SHIFT
) != 0;
190 // boolean altPressed = (e.stateMask & SWT.ALT) != 0;
191 FilterEntitiesVirtualTable currTable
= null;
192 if (currEdited
!= null) {
193 FilterEntitiesVirtualTable table
= browserCols
194 .get(getPath(currEdited
));
195 if (table
!= null && !table
.isDisposed())
200 if (e
.keyCode
== SWT
.ARROW_DOWN
)
201 currTable
.setFocus();
202 else if (e
.keyCode
== SWT
.BS
) {
203 if (filterTxt
.getText().equals("")
204 && !(getPath(currEdited
).equals("/") || getPath(
205 currEdited
).equals(initialPath
))) {
206 setEdited(currEdited
.getParent());
208 filterTxt
.setFocus();
210 } else if (e
.keyCode
== SWT
.TAB
&& !shiftPressed
) {
211 if (currEdited
.getNodes(filterTxt
.getText() + "*")
213 setEdited(currEdited
.getNodes(
214 filterTxt
.getText() + "*").nextNode());
216 filterTxt
.setFocus();
219 } catch (RepositoryException e1
) {
220 throw new ArgeoException(
221 "Unexpected error in key management for "
222 + currEdited
+ "with filter "
223 + filterTxt
.getText(), e1
);
230 private void setEdited(Node node
) {
233 CmsUtils
.clear(nodeDisplayParent
);
234 createNodeView(nodeDisplayParent
, currEdited
);
235 nodeDisplayParent
.layout();
236 refreshFilters(node
);
237 refreshBrowser(node
);
238 } catch (RepositoryException re
) {
239 throw new ArgeoException("Unable to update browser for " + node
, re
);
243 private void refreshFilters(Node node
) throws RepositoryException
{
244 String currNodePath
= node
.getPath();
245 parentPathTxt
.setText(currNodePath
);
246 filterTxt
.setText("");
247 filterTxt
.getParent().layout();
250 private void refreshBrowser(Node node
) throws RepositoryException
{
253 String currNodePath
= node
.getPath();
254 String currParPath
= "";
255 if (!"/".equals(currNodePath
))
256 currParPath
= JcrUtils
.parentPath(currNodePath
);
257 if ("".equals(currParPath
))
260 Object
[][] colMatrix
= new Object
[browserCols
.size()][2];
262 int i
= 0, j
= -1, k
= -1;
263 for (String path
: browserCols
.keySet()) {
264 colMatrix
[i
][0] = path
;
265 colMatrix
[i
][1] = browserCols
.get(path
);
266 if (j
>= 0 && k
< 0 && !currNodePath
.equals("/")) {
267 boolean leaveOpened
= path
.startsWith(currNodePath
);
269 // workaround for same name siblings
270 // fix me weird side effect when we go left or click on anb
271 // already selected, unfocused node
273 && (path
.lastIndexOf("/") == 0
274 && currNodePath
.lastIndexOf("/") == 0 || JcrUtils
275 .parentPath(path
).equals(
276 JcrUtils
.parentPath(currNodePath
))))
277 leaveOpened
= JcrUtils
.lastPathElement(path
).equals(
278 JcrUtils
.lastPathElement(currNodePath
));
283 if (currParPath
.equals(path
))
288 if (j
>= 0 && k
>= 0)
289 // remove useless cols
290 for (int l
= i
- 1; l
>= k
; l
--) {
291 browserCols
.remove(colMatrix
[l
][0]);
292 ((FilterEntitiesVirtualTable
) colMatrix
[l
][1]).dispose();
295 if (!browserCols
.containsKey(currNodePath
))
296 createBrowserColumn(colViewer
, node
);
298 colViewer
.setLayout(CmsUtils
.noSpaceGridLayout(new GridLayout(
299 browserCols
.size(), false)));
302 // also resize the scrolled composite
303 scrolledCmp
.layout();
304 scrolledCmp
.getShowFocusedControl();
305 // colViewer.getParent().layout();
306 // if (JcrUtils.parentPath(currNodePath).equals(currBrowserKey)) {
311 private void modifyFilter(boolean fromOutside
) {
313 if (currEdited
!= null) {
314 String filter
= filterTxt
.getText() + "*";
315 FilterEntitiesVirtualTable table
= browserCols
316 .get(getPath(currEdited
));
317 if (table
!= null && !table
.isDisposed())
318 table
.filterList(filter
);
323 private String
getPath(Node node
) {
325 return node
.getPath();
326 } catch (RepositoryException e
) {
327 throw new ArgeoException("Unable to get path for node " + node
, e
);
331 private Point imageWidth
= new Point(250, 0);
333 private Control
createNodeView(Composite parent
, Node context
)
334 throws RepositoryException
{
336 parent
.setLayout(new GridLayout(2, false));
338 if (isImg(context
)) {
339 EditableImage image
= new Img(parent
, RIGHT
, context
, imageWidth
);
340 image
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.CENTER
, true,
344 // Name and primary type
345 Label contextL
= new Label(parent
, SWT
.NONE
);
346 contextL
.setData(RWT
.MARKUP_ENABLED
, true);
347 contextL
.setText("<b>" + context
.getName() + "</b>");
348 new Label(parent
, SWT
.NONE
).setText(context
.getPrimaryNodeType()
352 for (NodeIterator nIt
= context
.getNodes(); nIt
.hasNext();) {
353 Node child
= nIt
.nextNode();
354 new CmsLink(child
.getName(), BROWSE_PREFIX
+ child
.getPath())
355 .createUi(parent
, context
);
356 new Label(parent
, SWT
.NONE
).setText(child
.getPrimaryNodeType()
361 for (PropertyIterator pIt
= context
.getProperties(); pIt
.hasNext();) {
362 Property property
= pIt
.nextProperty();
363 Label label
= new Label(parent
, SWT
.NONE
);
364 label
.setText(property
.getName());
365 label
.setToolTipText(JcrUtils
366 .getPropertyDefinitionAsString(property
));
367 new Label(parent
, SWT
.NONE
).setText(getPropAsString(property
));
373 private boolean isImg(Node node
) throws RepositoryException
{
374 return node
.hasNode(JCR_CONTENT
) && node
.isNodeType(CmsTypes
.CMS_IMAGE
);
377 private String
getPropAsString(Property property
)
378 throws RepositoryException
{
380 if (property
.isMultiple()) {
381 result
= getMultiAsString(property
, ", ");
383 Value value
= property
.getValue();
384 if (value
.getType() == PropertyType
.BINARY
)
386 else if (value
.getType() == PropertyType
.DATE
)
387 result
= timeFormatter
.format(value
.getDate().getTime());
389 result
= value
.getString();
394 private String
getMultiAsString(Property property
, String separator
)
395 throws RepositoryException
{
396 if (separator
== null)
398 Value
[] values
= property
.getValues();
399 StringBuilder builder
= new StringBuilder();
400 for (Value val
: values
) {
401 String currStr
= val
.getString();
402 if (!"".equals(currStr
.trim()))
403 builder
.append(currStr
).append(separator
);
405 if (builder
.lastIndexOf(separator
) >= 0)
406 return builder
.substring(0, builder
.length() - separator
.length());
408 return builder
.toString();
411 /** Almost canonical implementation of a table that display entities */
412 private class FilterEntitiesVirtualTable
extends Composite
{
413 private static final long serialVersionUID
= 8798147431706283824L;
416 private Node context
;
419 private TableViewer entityViewer
;
421 // enable management of multiple columns
427 public boolean setFocus() {
428 if (entityViewer
.getTable().isDisposed())
430 if (entityViewer
.getSelection().isEmpty()) {
431 Object first
= entityViewer
.getElementAt(0);
433 entityViewer
.setSelection(new StructuredSelection(first
),
437 return entityViewer
.getTable().setFocus();
440 void filterList(String filter
) {
442 NodeIterator nit
= context
.getNodes(filter
);
443 refreshFilteredList(nit
);
444 } catch (RepositoryException e
) {
445 throw new ArgeoException("Unable to filter " + getNode()
446 + " children with filter " + filter
, e
);
451 public FilterEntitiesVirtualTable(Composite parent
, int style
,
453 super(parent
, SWT
.NO_FOCUS
);
454 this.context
= context
;
458 protected void populate() {
459 Composite parent
= this;
460 GridLayout layout
= CmsUtils
.noSpaceGridLayout();
462 this.setLayout(layout
);
463 createTableViewer(parent
);
466 private void createTableViewer(final Composite parent
) {
468 // We must limit the size of the table otherwise the full list is
470 // before the layout happens
471 Composite listCmp
= new Composite(parent
, SWT
.NO_FOCUS
);
472 GridData gd
= new GridData(SWT
.LEFT
, SWT
.FILL
, false, true);
473 gd
.widthHint
= COLUMN_WIDTH
;
474 listCmp
.setLayoutData(gd
);
475 listCmp
.setLayout(CmsUtils
.noSpaceGridLayout());
477 entityViewer
= new TableViewer(listCmp
, SWT
.VIRTUAL
| SWT
.SINGLE
);
478 Table table
= entityViewer
.getTable();
480 table
.setLayoutData(CmsUtils
.fillAll());
481 table
.setLinesVisible(true);
482 table
.setHeaderVisible(false);
483 table
.setData(RWT
.MARKUP_ENABLED
, Boolean
.TRUE
);
485 CmsUtils
.style(table
, MaintenanceStyles
.BROWSER_COLUMN
);
488 TableViewerColumn column
= new TableViewerColumn(entityViewer
,
490 TableColumn tcol
= column
.getColumn();
491 tcol
.setWidth(COLUMN_WIDTH
);
492 tcol
.setResizable(true);
493 column
.setLabelProvider(new SimpleNameLP());
495 entityViewer
.setContentProvider(new MyLazyCP(entityViewer
));
497 .addSelectionChangedListener(new ISelectionChangedListener() {
500 public void selectionChanged(SelectionChangedEvent event
) {
501 IStructuredSelection selection
= (IStructuredSelection
) entityViewer
503 if (selection
.isEmpty())
506 setEdited((Node
) selection
.getFirstElement());
511 table
.addKeyListener(new KeyListener() {
512 private static final long serialVersionUID
= -330694313896036230L;
515 public void keyReleased(KeyEvent e
) {
519 public void keyPressed(KeyEvent e
) {
521 IStructuredSelection selection
= (IStructuredSelection
) entityViewer
523 Node selected
= null;
524 if (!selection
.isEmpty())
525 selected
= ((Node
) selection
.getFirstElement());
527 if (e
.keyCode
== SWT
.ARROW_RIGHT
) {
528 if (selected
!= null) {
530 browserCols
.get(selected
.getPath()).setFocus();
532 } else if (e
.keyCode
== SWT
.ARROW_LEFT
) {
534 selected
= getNode().getParent();
535 String newPath
= selected
.getPath(); // getNode().getParent()
537 if (browserCols
.containsKey(newPath
))
538 browserCols
.get(newPath
).setFocus();
539 } catch (ItemNotFoundException ie
) {
543 } catch (RepositoryException ie
) {
544 throw new ArgeoException("Error while managing arrow "
545 + "events in the browser for " + selected
, ie
);
551 private class MyLazyCP
implements ILazyContentProvider
{
552 private static final long serialVersionUID
= 1L;
553 private TableViewer viewer
;
554 private Object
[] elements
;
556 public MyLazyCP(TableViewer viewer
) {
557 this.viewer
= viewer
;
560 public void dispose() {
563 public void inputChanged(Viewer viewer
, Object oldInput
,
565 // IMPORTANT: don't forget this: an exception will be thrown if
566 // a selected object is not part of the results anymore.
567 viewer
.setSelection(null);
568 this.elements
= (Object
[]) newInput
;
571 public void updateElement(int index
) {
572 viewer
.replace(elements
[index
], index
);
576 protected void refreshFilteredList(NodeIterator children
) {
577 Object
[] rows
= JcrUtils
.nodeIteratorToList(children
).toArray();
578 entityViewer
.setInput(rows
);
579 entityViewer
.setItemCount(rows
.length
);
580 entityViewer
.refresh();
583 public class SimpleNameLP
extends ColumnLabelProvider
{
584 private static final long serialVersionUID
= 2465059387875338553L;
587 public String
getText(Object element
) {
588 if (element
instanceof Node
) {
589 Node curr
= ((Node
) element
);
591 return curr
.getName();
592 } catch (RepositoryException e
) {
593 throw new ArgeoException("Unable to get name for"
597 return super.getText(element
);