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
.cms
.CmsException
;
20 import org
.argeo
.cms
.CmsTypes
;
21 import org
.argeo
.cms
.text
.Img
;
22 import org
.argeo
.cms
.ui
.CmsUiProvider
;
23 import org
.argeo
.cms
.util
.CmsLink
;
24 import org
.argeo
.cms
.util
.CmsUtils
;
25 import org
.argeo
.cms
.widgets
.EditableImage
;
26 import org
.argeo
.jcr
.JcrUtils
;
27 import org
.eclipse
.jface
.viewers
.ColumnLabelProvider
;
28 import org
.eclipse
.jface
.viewers
.ILazyContentProvider
;
29 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
30 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
31 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
32 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
33 import org
.eclipse
.jface
.viewers
.TableViewer
;
34 import org
.eclipse
.jface
.viewers
.TableViewerColumn
;
35 import org
.eclipse
.jface
.viewers
.Viewer
;
36 import org
.eclipse
.rap
.rwt
.RWT
;
37 import org
.eclipse
.swt
.SWT
;
38 import org
.eclipse
.swt
.custom
.ScrolledComposite
;
39 import org
.eclipse
.swt
.events
.ControlAdapter
;
40 import org
.eclipse
.swt
.events
.ControlEvent
;
41 import org
.eclipse
.swt
.events
.KeyEvent
;
42 import org
.eclipse
.swt
.events
.KeyListener
;
43 import org
.eclipse
.swt
.events
.ModifyEvent
;
44 import org
.eclipse
.swt
.events
.ModifyListener
;
45 import org
.eclipse
.swt
.graphics
.Point
;
46 import org
.eclipse
.swt
.graphics
.Rectangle
;
47 import org
.eclipse
.swt
.layout
.GridData
;
48 import org
.eclipse
.swt
.layout
.GridLayout
;
49 import org
.eclipse
.swt
.widgets
.Composite
;
50 import org
.eclipse
.swt
.widgets
.Control
;
51 import org
.eclipse
.swt
.widgets
.Label
;
52 import org
.eclipse
.swt
.widgets
.Table
;
53 import org
.eclipse
.swt
.widgets
.TableColumn
;
54 import org
.eclipse
.swt
.widgets
.Text
;
56 public class Browse
implements CmsUiProvider
{
58 // Some local constants to experiment. should be cleaned
59 private final static String BROWSE_PREFIX
= "browse#";
60 private final static int THUMBNAIL_WIDTH
= 400;
61 private final static int COLUMN_WIDTH
= 160;
62 private DateFormat timeFormatter
= new SimpleDateFormat(
63 "dd-MM-yyyy', 'HH:mm");
65 // keep a cache of the opened nodes
67 private LinkedHashMap
<String
, FilterEntitiesVirtualTable
> browserCols
= new LinkedHashMap
<String
, Browse
.FilterEntitiesVirtualTable
>();
68 private Composite nodeDisplayParent
;
69 private Composite colViewer
;
70 private ScrolledComposite scrolledCmp
;
71 private Text parentPathTxt
;
72 private Text filterTxt
;
73 private Node currEdited
;
75 private String initialPath
;
78 public Control
createUi(Composite parent
, Node context
)
79 throws RepositoryException
{
82 throw new CmsException("Context cannot be null");
83 GridLayout layout
= CmsUtils
.noSpaceGridLayout();
84 layout
.numColumns
= 2;
85 parent
.setLayout(layout
);
88 Composite leftCmp
= new Composite(parent
, SWT
.NO_FOCUS
);
89 leftCmp
.setLayoutData(CmsUtils
.fillAll());
90 createBrowserPart(leftCmp
, context
);
93 nodeDisplayParent
= new Composite(parent
, SWT
.NO_FOCUS
| SWT
.BORDER
);
94 GridData gd
= new GridData(SWT
.RIGHT
, SWT
.FILL
, false, true);
95 gd
.widthHint
= THUMBNAIL_WIDTH
;
96 nodeDisplayParent
.setLayoutData(gd
);
97 createNodeView(nodeDisplayParent
, context
);
101 initialPath
= context
.getPath();
103 // Workaround we don't yet manage the delete to display parent of the
104 // initial context node
109 private void createBrowserPart(Composite parent
, Node context
)
110 throws RepositoryException
{
111 GridLayout layout
= CmsUtils
.noSpaceGridLayout();
112 parent
.setLayout(layout
);
113 Composite filterCmp
= new Composite(parent
, SWT
.NO_FOCUS
);
114 filterCmp
.setLayoutData(CmsUtils
.fillWidth());
117 addFilterPanel(filterCmp
);
119 // scrolled composite
120 scrolledCmp
= new ScrolledComposite(parent
, SWT
.H_SCROLL
| SWT
.BORDER
122 scrolledCmp
.setLayoutData(CmsUtils
.fillAll());
123 scrolledCmp
.setExpandVertical(true);
124 scrolledCmp
.setExpandHorizontal(true);
125 scrolledCmp
.setShowFocusedControl(true);
127 colViewer
= new Composite(scrolledCmp
, SWT
.NO_FOCUS
);
128 scrolledCmp
.setContent(colViewer
);
129 scrolledCmp
.addControlListener(new ControlAdapter() {
130 private static final long serialVersionUID
= 6589392045145698201L;
133 public void controlResized(ControlEvent e
) {
134 Rectangle r
= scrolledCmp
.getClientArea();
135 scrolledCmp
.setMinSize(colViewer
.computeSize(SWT
.DEFAULT
,
139 initExplorer(colViewer
, context
);
142 private Control
initExplorer(Composite parent
, Node context
)
143 throws RepositoryException
{
144 parent
.setLayout(CmsUtils
.noSpaceGridLayout());
145 createBrowserColumn(parent
, context
);
149 private Control
createBrowserColumn(Composite parent
, Node context
)
150 throws RepositoryException
{
151 // TODO style is not correctly managed.
152 FilterEntitiesVirtualTable table
= new FilterEntitiesVirtualTable(
153 parent
, SWT
.BORDER
| SWT
.NO_FOCUS
, context
);
154 // CmsUtils.style(table, ArgeoOrgStyle.browserColumn.style());
155 table
.filterList("*");
156 table
.setLayoutData(new GridData(SWT
.LEFT
, SWT
.FILL
, false, true));
157 browserCols
.put(context
.getPath(), table
);
161 public void addFilterPanel(Composite parent
) {
163 parent
.setLayout(CmsUtils
.noSpaceGridLayout(new GridLayout(2, false)));
165 // Text Area for the filter
166 parentPathTxt
= new Text(parent
, SWT
.NO_FOCUS
);
167 parentPathTxt
.setEditable(false);
168 filterTxt
= new Text(parent
, SWT
.SEARCH
| SWT
.ICON_CANCEL
);
169 filterTxt
.setMessage("Filter current list");
170 filterTxt
.setLayoutData(CmsUtils
.fillWidth());
171 filterTxt
.addModifyListener(new ModifyListener() {
172 private static final long serialVersionUID
= 7709303319740056286L;
174 public void modifyText(ModifyEvent event
) {
179 filterTxt
.addKeyListener(new KeyListener() {
180 private static final long serialVersionUID
= -4523394262771183968L;
183 public void keyReleased(KeyEvent e
) {
187 public void keyPressed(KeyEvent e
) {
188 boolean shiftPressed
= (e
.stateMask
& SWT
.SHIFT
) != 0;
189 // boolean altPressed = (e.stateMask & SWT.ALT) != 0;
190 FilterEntitiesVirtualTable currTable
= null;
191 if (currEdited
!= null) {
192 FilterEntitiesVirtualTable table
= browserCols
193 .get(getPath(currEdited
));
194 if (table
!= null && !table
.isDisposed())
199 if (e
.keyCode
== SWT
.ARROW_DOWN
)
200 currTable
.setFocus();
201 else if (e
.keyCode
== SWT
.BS
) {
202 if (filterTxt
.getText().equals("")
203 && !(getPath(currEdited
).equals("/") || getPath(
204 currEdited
).equals(initialPath
))) {
205 setEdited(currEdited
.getParent());
207 filterTxt
.setFocus();
209 } else if (e
.keyCode
== SWT
.TAB
&& !shiftPressed
) {
210 if (currEdited
.getNodes(filterTxt
.getText() + "*")
212 setEdited(currEdited
.getNodes(
213 filterTxt
.getText() + "*").nextNode());
215 filterTxt
.setFocus();
218 } catch (RepositoryException e1
) {
219 throw new CmsException(
220 "Unexpected error in key management for "
221 + currEdited
+ "with filter "
222 + filterTxt
.getText(), e1
);
229 private void setEdited(Node node
) {
232 CmsUtils
.clear(nodeDisplayParent
);
233 createNodeView(nodeDisplayParent
, currEdited
);
234 nodeDisplayParent
.layout();
235 refreshFilters(node
);
236 refreshBrowser(node
);
237 } catch (RepositoryException re
) {
238 throw new CmsException("Unable to update browser for " + node
, re
);
242 private void refreshFilters(Node node
) throws RepositoryException
{
243 String currNodePath
= node
.getPath();
244 parentPathTxt
.setText(currNodePath
);
245 filterTxt
.setText("");
246 filterTxt
.getParent().layout();
249 private void refreshBrowser(Node node
) throws RepositoryException
{
252 String currNodePath
= node
.getPath();
253 String currParPath
= "";
254 if (!"/".equals(currNodePath
))
255 currParPath
= JcrUtils
.parentPath(currNodePath
);
256 if ("".equals(currParPath
))
262 Object
[][] colMatrix
= new Object
[browserCols
.size()][2];
264 int i
= 0, j
= -1, k
= -1;
265 for (String path
: browserCols
.keySet()) {
266 colMatrix
[i
][0] = path
;
267 colMatrix
[i
][1] = browserCols
.get(path
);
268 if (j
>= 0 && k
< 0 && !currNodePath
.equals("/")) {
269 boolean leaveOpened
= path
.startsWith(currNodePath
);
271 // workaround for same name siblings
272 // fix me weird side effect when we go left or click on anb
273 // already selected, unfocused node
275 && (path
.lastIndexOf("/") == 0
276 && currNodePath
.lastIndexOf("/") == 0 || JcrUtils
277 .parentPath(path
).equals(
278 JcrUtils
.parentPath(currNodePath
))))
279 leaveOpened
= JcrUtils
.lastPathElement(path
).equals(
280 JcrUtils
.lastPathElement(currNodePath
));
285 if (currParPath
.equals(path
))
290 if (j
>= 0 && k
>= 0)
291 // remove useless cols
292 for (int l
= i
- 1; l
>= k
; l
--) {
293 browserCols
.remove(colMatrix
[l
][0]);
294 ((FilterEntitiesVirtualTable
) colMatrix
[l
][1]).dispose();
297 // Remove disposed columns
298 // TODO investigate and fix the mechanism that leave them there after
300 if (browserCols
.containsKey(currNodePath
)) {
301 FilterEntitiesVirtualTable currCol
= browserCols
.get(currNodePath
);
302 if (currCol
.isDisposed())
303 browserCols
.remove(currNodePath
);
306 if (!browserCols
.containsKey(currNodePath
))
307 createBrowserColumn(colViewer
, node
);
309 colViewer
.setLayout(CmsUtils
.noSpaceGridLayout(new GridLayout(
310 browserCols
.size(), false)));
313 // also resize the scrolled composite
314 scrolledCmp
.layout();
315 scrolledCmp
.getShowFocusedControl();
316 // colViewer.getParent().layout();
317 // if (JcrUtils.parentPath(currNodePath).equals(currBrowserKey)) {
322 private void modifyFilter(boolean fromOutside
) {
324 if (currEdited
!= null) {
325 String filter
= filterTxt
.getText() + "*";
326 FilterEntitiesVirtualTable table
= browserCols
327 .get(getPath(currEdited
));
328 if (table
!= null && !table
.isDisposed())
329 table
.filterList(filter
);
334 private String
getPath(Node node
) {
336 return node
.getPath();
337 } catch (RepositoryException e
) {
338 throw new CmsException("Unable to get path for node " + node
, e
);
342 private Point imageWidth
= new Point(250, 0);
345 * Recreates the content of the box that displays information about the
346 * current selected node.
348 private Control
createNodeView(Composite parent
, Node context
)
349 throws RepositoryException
{
351 parent
.setLayout(new GridLayout(2, false));
353 if (isImg(context
)) {
354 EditableImage image
= new Img(parent
, RIGHT
, context
, imageWidth
);
355 image
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.CENTER
, true,
359 // Name and primary type
360 Label contextL
= new Label(parent
, SWT
.NONE
);
361 contextL
.setData(RWT
.MARKUP_ENABLED
, true);
362 contextL
.setText("<b>" + context
.getName() + "</b>");
363 new Label(parent
, SWT
.NONE
).setText(context
.getPrimaryNodeType()
367 for (NodeIterator nIt
= context
.getNodes(); nIt
.hasNext();) {
368 Node child
= nIt
.nextNode();
369 new CmsLink(child
.getName(), BROWSE_PREFIX
+ child
.getPath())
370 .createUi(parent
, context
);
371 new Label(parent
, SWT
.NONE
).setText(child
.getPrimaryNodeType()
376 for (PropertyIterator pIt
= context
.getProperties(); pIt
.hasNext();) {
377 Property property
= pIt
.nextProperty();
378 Label label
= new Label(parent
, SWT
.NONE
);
379 label
.setText(property
.getName());
380 label
.setToolTipText(JcrUtils
381 .getPropertyDefinitionAsString(property
));
382 new Label(parent
, SWT
.NONE
).setText(getPropAsString(property
));
388 private boolean isImg(Node node
) throws RepositoryException
{
389 return node
.hasNode(JCR_CONTENT
) && node
.isNodeType(CmsTypes
.CMS_IMAGE
);
392 private String
getPropAsString(Property property
)
393 throws RepositoryException
{
395 if (property
.isMultiple()) {
396 result
= getMultiAsString(property
, ", ");
398 Value value
= property
.getValue();
399 if (value
.getType() == PropertyType
.BINARY
)
401 else if (value
.getType() == PropertyType
.DATE
)
402 result
= timeFormatter
.format(value
.getDate().getTime());
404 result
= value
.getString();
409 private String
getMultiAsString(Property property
, String separator
)
410 throws RepositoryException
{
411 if (separator
== null)
413 Value
[] values
= property
.getValues();
414 StringBuilder builder
= new StringBuilder();
415 for (Value val
: values
) {
416 String currStr
= val
.getString();
417 if (!"".equals(currStr
.trim()))
418 builder
.append(currStr
).append(separator
);
420 if (builder
.lastIndexOf(separator
) >= 0)
421 return builder
.substring(0, builder
.length() - separator
.length());
423 return builder
.toString();
426 /** Almost canonical implementation of a table that display entities */
427 private class FilterEntitiesVirtualTable
extends Composite
{
428 private static final long serialVersionUID
= 8798147431706283824L;
431 private Node context
;
434 private TableViewer entityViewer
;
436 // enable management of multiple columns
442 public boolean setFocus() {
443 if (entityViewer
.getTable().isDisposed())
445 if (entityViewer
.getSelection().isEmpty()) {
446 Object first
= entityViewer
.getElementAt(0);
448 entityViewer
.setSelection(new StructuredSelection(first
),
452 return entityViewer
.getTable().setFocus();
455 void filterList(String filter
) {
457 NodeIterator nit
= context
.getNodes(filter
);
458 refreshFilteredList(nit
);
459 } catch (RepositoryException e
) {
460 throw new CmsException("Unable to filter " + getNode()
461 + " children with filter " + filter
, e
);
466 public FilterEntitiesVirtualTable(Composite parent
, int style
,
468 super(parent
, SWT
.NO_FOCUS
);
469 this.context
= context
;
473 protected void populate() {
474 Composite parent
= this;
475 GridLayout layout
= CmsUtils
.noSpaceGridLayout();
477 this.setLayout(layout
);
478 createTableViewer(parent
);
481 private void createTableViewer(final Composite parent
) {
483 // We must limit the size of the table otherwise the full list is
485 // before the layout happens
486 Composite listCmp
= new Composite(parent
, SWT
.NO_FOCUS
);
487 GridData gd
= new GridData(SWT
.LEFT
, SWT
.FILL
, false, true);
488 gd
.widthHint
= COLUMN_WIDTH
;
489 listCmp
.setLayoutData(gd
);
490 listCmp
.setLayout(CmsUtils
.noSpaceGridLayout());
492 entityViewer
= new TableViewer(listCmp
, SWT
.VIRTUAL
| SWT
.SINGLE
);
493 Table table
= entityViewer
.getTable();
495 table
.setLayoutData(CmsUtils
.fillAll());
496 table
.setLinesVisible(true);
497 table
.setHeaderVisible(false);
498 table
.setData(RWT
.MARKUP_ENABLED
, Boolean
.TRUE
);
500 CmsUtils
.style(table
, MaintenanceStyles
.BROWSER_COLUMN
);
503 TableViewerColumn column
= new TableViewerColumn(entityViewer
,
505 TableColumn tcol
= column
.getColumn();
506 tcol
.setWidth(COLUMN_WIDTH
);
507 tcol
.setResizable(true);
508 column
.setLabelProvider(new SimpleNameLP());
510 entityViewer
.setContentProvider(new MyLazyCP(entityViewer
));
512 .addSelectionChangedListener(new ISelectionChangedListener() {
515 public void selectionChanged(SelectionChangedEvent event
) {
516 IStructuredSelection selection
= (IStructuredSelection
) entityViewer
518 if (selection
.isEmpty())
521 setEdited((Node
) selection
.getFirstElement());
526 table
.addKeyListener(new KeyListener() {
527 private static final long serialVersionUID
= -330694313896036230L;
530 public void keyReleased(KeyEvent e
) {
534 public void keyPressed(KeyEvent e
) {
536 IStructuredSelection selection
= (IStructuredSelection
) entityViewer
538 Node selected
= null;
539 if (!selection
.isEmpty())
540 selected
= ((Node
) selection
.getFirstElement());
542 if (e
.keyCode
== SWT
.ARROW_RIGHT
) {
543 if (selected
!= null) {
545 browserCols
.get(selected
.getPath()).setFocus();
547 } else if (e
.keyCode
== SWT
.ARROW_LEFT
) {
549 selected
= getNode().getParent();
550 String newPath
= selected
.getPath(); // getNode().getParent()
552 if (browserCols
.containsKey(newPath
))
553 browserCols
.get(newPath
).setFocus();
554 } catch (ItemNotFoundException ie
) {
558 } catch (RepositoryException ie
) {
559 throw new CmsException("Error while managing arrow "
560 + "events in the browser for " + selected
, ie
);
566 private class MyLazyCP
implements ILazyContentProvider
{
567 private static final long serialVersionUID
= 1L;
568 private TableViewer viewer
;
569 private Object
[] elements
;
571 public MyLazyCP(TableViewer viewer
) {
572 this.viewer
= viewer
;
575 public void dispose() {
578 public void inputChanged(Viewer viewer
, Object oldInput
,
580 // IMPORTANT: don't forget this: an exception will be thrown if
581 // a selected object is not part of the results anymore.
582 viewer
.setSelection(null);
583 this.elements
= (Object
[]) newInput
;
586 public void updateElement(int index
) {
587 viewer
.replace(elements
[index
], index
);
591 protected void refreshFilteredList(NodeIterator children
) {
592 Object
[] rows
= JcrUtils
.nodeIteratorToList(children
).toArray();
593 entityViewer
.setInput(rows
);
594 entityViewer
.setItemCount(rows
.length
);
595 entityViewer
.refresh();
598 public class SimpleNameLP
extends ColumnLabelProvider
{
599 private static final long serialVersionUID
= 2465059387875338553L;
602 public String
getText(Object element
) {
603 if (element
instanceof Node
) {
604 Node curr
= ((Node
) element
);
606 return curr
.getName();
607 } catch (RepositoryException e
) {
608 throw new CmsException("Unable to get name for"
612 return super.getText(element
);