View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.argeo.people.ui.dialogs;
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.RepositoryException;
24  import javax.jcr.Session;
25  import javax.jcr.query.Query;
26  import javax.jcr.query.QueryManager;
27  import javax.jcr.query.QueryResult;
28  import javax.jcr.query.Row;
29  
30  import org.argeo.connect.ConnectConstants;
31  import org.argeo.connect.ui.ConnectColumnDefinition;
32  import org.argeo.connect.ui.SystemWorkbenchService;
33  import org.argeo.connect.ui.util.TitleIconRowLP;
34  import org.argeo.connect.ui.util.VirtualJcrTableViewer;
35  import org.argeo.connect.util.ConnectJcrUtils;
36  import org.argeo.connect.util.XPathUtils;
37  import org.argeo.eclipse.ui.EclipseUiUtils;
38  import org.argeo.people.PeopleException;
39  import org.eclipse.jface.dialogs.TrayDialog;
40  import org.eclipse.jface.viewers.DoubleClickEvent;
41  import org.eclipse.jface.viewers.IDoubleClickListener;
42  import org.eclipse.jface.viewers.ISelectionChangedListener;
43  import org.eclipse.jface.viewers.IStructuredSelection;
44  import org.eclipse.jface.viewers.SelectionChangedEvent;
45  import org.eclipse.jface.viewers.TableViewer;
46  import org.eclipse.swt.SWT;
47  import org.eclipse.swt.events.ModifyEvent;
48  import org.eclipse.swt.events.ModifyListener;
49  import org.eclipse.swt.graphics.Point;
50  import org.eclipse.swt.layout.GridData;
51  import org.eclipse.swt.widgets.Composite;
52  import org.eclipse.swt.widgets.Control;
53  import org.eclipse.swt.widgets.Shell;
54  import org.eclipse.swt.widgets.Text;
55  
56  /**
57   * Dialog with a filtered list to add some members in a mailing list
58   */
59  public class PickUpContactableDialog extends TrayDialog {
60  	private static final long serialVersionUID = -2526572299370624808L;
61  
62  	// Business objects
63  	private final Session session;
64  	private Node selectedNode;
65  	private String nodeType;
66  
67  	// this page widgets and UI objects
68  	// private EntityTableComposite tableCmp;
69  	private final String title;
70  
71  	private List<ConnectColumnDefinition> colDefs;
72  	private Text filterTxt;
73  	private TableViewer tableViewer;
74  
75  	public PickUpContactableDialog(Shell parentShell, String title, Session session,
76  			SystemWorkbenchService systemWorkbenchService, String nodeType) {
77  		super(parentShell);
78  		this.title = title;
79  		this.session = session;
80  		this.nodeType = nodeType;
81  
82  		colDefs = new ArrayList<ConnectColumnDefinition>();
83  		colDefs.add(new ConnectColumnDefinition("Display Name",
84  				new TitleIconRowLP(systemWorkbenchService, null, Property.JCR_TITLE), 300));
85  	}
86  
87  	protected Point getInitialSize() {
88  		return new Point(400, 650);
89  	}
90  
91  	protected Control createDialogArea(Composite parent) {
92  		Composite dialogArea = (Composite) super.createDialogArea(parent);
93  
94  		createFilterPart(dialogArea);
95  		VirtualJcrTableViewer tableCmp = new VirtualJcrTableViewer(dialogArea, SWT.SINGLE | SWT.BORDER, colDefs);
96  		tableViewer = tableCmp.getTableViewer();
97  		tableCmp.setLayoutData(EclipseUiUtils.fillAll());
98  		tableViewer.addDoubleClickListener(new MyDoubleClickListener());
99  		tableViewer.addSelectionChangedListener(new MySelectionChangedListener());
100 		parent.pack();
101 		refreshFilteredList();
102 		filterTxt.setFocus();
103 		return dialogArea;
104 	}
105 
106 	public Node getSelected() {
107 		return selectedNode;
108 	}
109 
110 	/** Use this method to update the result table */
111 	protected void setViewerInput(Row[] rows) {
112 		tableViewer.setInput(rows);
113 		// we must explicitly set the items count
114 		tableViewer.setItemCount(rows.length);
115 		tableViewer.refresh();
116 	}
117 
118 	protected void configureShell(Shell shell) {
119 		super.configureShell(shell);
120 		shell.setText(title);
121 	}
122 
123 	class MySelectionChangedListener implements ISelectionChangedListener {
124 		@Override
125 		public void selectionChanged(SelectionChangedEvent event) {
126 			if (event.getSelection().isEmpty()) {
127 				// selectedNode = null;
128 				return;
129 			}
130 
131 			Object obj = ((IStructuredSelection) event.getSelection()).getFirstElement();
132 			if (obj instanceof Row) {
133 				selectedNode = ConnectJcrUtils.getNode((Row) obj, null);
134 			}
135 		}
136 	}
137 
138 	class MyDoubleClickListener implements IDoubleClickListener {
139 		public void doubleClick(DoubleClickEvent evt) {
140 			if (evt.getSelection().isEmpty())
141 				return;
142 
143 			Object obj = ((IStructuredSelection) evt.getSelection()).getFirstElement();
144 			if (obj instanceof Row) {
145 				ConnectJcrUtils.getNode((Row) obj, null);
146 				okPressed();
147 			}
148 		}
149 	}
150 
151 	private void createFilterPart(Composite parent) {
152 		// Text Area for the filter
153 		filterTxt = new Text(parent, SWT.BORDER | SWT.SEARCH | SWT.ICON_SEARCH | SWT.ICON_CANCEL);
154 		filterTxt.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
155 		filterTxt.addModifyListener(new ModifyListener() {
156 			private static final long serialVersionUID = 1L;
157 
158 			public void modifyText(ModifyEvent event) {
159 				refreshFilteredList();
160 			}
161 		});
162 	}
163 
164 	/** Refresh the table viewer based on the free text search field */
165 	protected void refreshFilteredList() {
166 		try {
167 			QueryManager queryManager = session.getWorkspace().getQueryManager();
168 			String xpathQueryStr = "//element(*, " + nodeType + ")";
169 			String attrQuery = XPathUtils.getFreeTextConstraint(filterTxt.getText());
170 			if (EclipseUiUtils.notEmpty(attrQuery))
171 				xpathQueryStr += "[" + attrQuery + "]";
172 			Query xpathQuery = queryManager.createQuery(xpathQueryStr, ConnectConstants.QUERY_XPATH);
173 			QueryResult result = xpathQuery.execute();
174 
175 			Row[] rows = ConnectJcrUtils.rowIteratorToArray(result.getRows());
176 			setViewerInput(rows);
177 		} catch (RepositoryException e) {
178 			throw new PeopleException("Unable to list " + nodeType + " entities", e);
179 		}
180 	}
181 }