1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.argeo.connect.ui.parts;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import javax.jcr.Node;
22 import javax.jcr.Property;
23 import javax.jcr.PropertyType;
24 import javax.jcr.Session;
25
26 import org.argeo.connect.ConnectConstants;
27 import org.argeo.connect.resources.ResourcesNames;
28 import org.argeo.connect.resources.ResourcesService;
29 import org.argeo.connect.resources.ResourcesTypes;
30 import org.argeo.connect.ui.widgets.SimpleJcrTableComposite;
31 import org.argeo.connect.util.ConnectJcrUtils;
32 import org.argeo.eclipse.ui.EclipseUiUtils;
33 import org.argeo.eclipse.ui.jcr.lists.JcrColumnDefinition;
34 import org.eclipse.jface.dialogs.TrayDialog;
35 import org.eclipse.jface.viewers.DoubleClickEvent;
36 import org.eclipse.jface.viewers.IDoubleClickListener;
37 import org.eclipse.jface.viewers.ISelectionChangedListener;
38 import org.eclipse.jface.viewers.IStructuredSelection;
39 import org.eclipse.jface.viewers.SelectionChangedEvent;
40 import org.eclipse.swt.SWT;
41 import org.eclipse.swt.graphics.Point;
42 import org.eclipse.swt.widgets.Composite;
43 import org.eclipse.swt.widgets.Control;
44 import org.eclipse.swt.widgets.Shell;
45
46
47 public class PickUpLangDialog extends TrayDialog {
48 private static final long serialVersionUID = 3766899676609659573L;
49
50
51 private ResourcesService resourceService;
52 private final Session session;
53 private Node selectedNode;
54
55
56 private SimpleJcrTableComposite tableCmp;
57 private final String title;
58
59 private List<JcrColumnDefinition> colDefs = new ArrayList<JcrColumnDefinition>();
60 {
61 colDefs.add(new JcrColumnDefinition(null, ResourcesNames.RESOURCES_TAG_CODE, PropertyType.STRING, "Iso Code", 100));
62 colDefs.add(new JcrColumnDefinition(null, Property.JCR_TITLE, PropertyType.STRING, "Label", 300));
63 };
64
65 public PickUpLangDialog(Shell parentShell, ResourcesService resourceService, Session session, String title) {
66 super(parentShell);
67 this.title = title;
68 this.session = session;
69 this.resourceService = resourceService;
70 }
71
72 protected Point getInitialSize() {
73 return new Point(600, 400);
74 }
75
76 protected Control createDialogArea(Composite parent) {
77 Composite dialogArea = (Composite) super.createDialogArea(parent);
78
79 Node langTagParent = resourceService.getTagLikeResourceParent(session, ConnectConstants.RESOURCE_LANG);
80
81 int style = SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL;
82 tableCmp = new SimpleJcrTableComposite(dialogArea, style, session, ConnectJcrUtils.getPath(langTagParent),
83 ResourcesTypes.RESOURCES_ENCODED_TAG, colDefs, true, false);
84 tableCmp.setLayoutData(EclipseUiUtils.fillAll());
85
86
87 tableCmp.getTableViewer().addDoubleClickListener(new MyDoubleClickListener());
88 tableCmp.getTableViewer().addSelectionChangedListener(new MySelectionChangedListener());
89
90 parent.pack();
91 return dialogArea;
92 }
93
94 public String getSelected() {
95 if (selectedNode != null)
96 return ConnectJcrUtils.get(selectedNode, ResourcesNames.RESOURCES_TAG_CODE);
97 else
98 return null;
99 }
100
101 protected void configureShell(Shell shell) {
102 super.configureShell(shell);
103 shell.setText(title);
104 }
105
106 class MySelectionChangedListener implements ISelectionChangedListener {
107 @Override
108 public void selectionChanged(SelectionChangedEvent event) {
109 if (event.getSelection().isEmpty())
110 selectedNode = null;
111 else {
112 Object obj = ((IStructuredSelection) event.getSelection()).getFirstElement();
113 if (obj instanceof Node) {
114 selectedNode = (Node) obj;
115 }
116 }
117 }
118 }
119
120 class MyDoubleClickListener implements IDoubleClickListener {
121 public void doubleClick(DoubleClickEvent evt) {
122 if (evt.getSelection().isEmpty()) {
123 selectedNode = null;
124 return;
125 } else {
126 Object obj = ((IStructuredSelection) evt.getSelection()).getFirstElement();
127 if (obj instanceof Node) {
128 selectedNode = (Node) obj;
129 okPressed();
130 }
131 }
132 }
133 }
134 }