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 javax.jcr.Node;
19  import javax.jcr.Session;
20  
21  import org.argeo.connect.ui.SystemWorkbenchService;
22  import org.argeo.connect.ui.parts.FilterEntitiesVirtualTable;
23  import org.argeo.people.PeopleTypes;
24  import org.eclipse.jface.dialogs.TrayDialog;
25  import org.eclipse.jface.viewers.DoubleClickEvent;
26  import org.eclipse.jface.viewers.IDoubleClickListener;
27  import org.eclipse.jface.viewers.ISelectionChangedListener;
28  import org.eclipse.jface.viewers.IStructuredSelection;
29  import org.eclipse.jface.viewers.SelectionChangedEvent;
30  import org.eclipse.swt.SWT;
31  import org.eclipse.swt.graphics.Point;
32  import org.eclipse.swt.layout.GridData;
33  import org.eclipse.swt.widgets.Composite;
34  import org.eclipse.swt.widgets.Control;
35  import org.eclipse.swt.widgets.Shell;
36  
37  /** Dialog with a filtered list to choose an organisation */
38  public class PickUpOrgDialog extends TrayDialog {
39  	private static final long serialVersionUID = -2526572299370624808L;
40  
41  	// Business objects
42  	private final Session session;
43  	private SystemWorkbenchService systemWorkbenchService;
44  	private Node selectedNode;
45  
46  	// this page widgets and UI objects
47  	private FilterEntitiesVirtualTable tableCmp;
48  	private final String title;
49  
50  	public PickUpOrgDialog(Shell parentShell, String title, Session session,
51  			SystemWorkbenchService systemWorkbenchService, Node referencingNode) {
52  		super(parentShell);
53  		this.title = title;
54  		this.session = session;
55  		this.systemWorkbenchService = systemWorkbenchService;
56  	}
57  
58  	protected Point getInitialSize() {
59  		return new Point(400, 600);
60  	}
61  
62  	protected Control createDialogArea(Composite parent) {
63  		Composite dialogArea = (Composite) super.createDialogArea(parent);
64  
65  		// final Button seeAllChk = new Button(dialogArea, SWT.CHECK);
66  		// seeAllChk.setText("See all organisation");
67  
68  		int style = SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL;
69  		tableCmp = new FilterEntitiesVirtualTable(dialogArea, style, session, systemWorkbenchService,
70  				PeopleTypes.PEOPLE_ORG);
71  		tableCmp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
72  
73  		// Add listeners
74  		tableCmp.getTableViewer().addDoubleClickListener(new MyDoubleClickListener());
75  		tableCmp.getTableViewer().addSelectionChangedListener(new MySelectionChangedListener());
76  
77  		parent.pack();
78  		return dialogArea;
79  	}
80  
81  	public Node getSelected() {
82  		return selectedNode;
83  	}
84  
85  	protected void configureShell(Shell shell) {
86  		super.configureShell(shell);
87  		shell.setText(title);
88  	}
89  
90  	class MySelectionChangedListener implements ISelectionChangedListener {
91  		@Override
92  		public void selectionChanged(SelectionChangedEvent event) {
93  			if (event.getSelection().isEmpty())
94  				return;
95  
96  			Object obj = ((IStructuredSelection) event.getSelection()).getFirstElement();
97  			if (obj instanceof Node) {
98  				selectedNode = (Node) obj;
99  			}
100 		}
101 	}
102 
103 	class MyDoubleClickListener implements IDoubleClickListener {
104 		public void doubleClick(DoubleClickEvent evt) {
105 			if (evt.getSelection().isEmpty())
106 				return;
107 
108 			Object obj = ((IStructuredSelection) evt.getSelection()).getFirstElement();
109 			if (obj instanceof Node) {
110 				selectedNode = (Node) obj;
111 				okPressed();
112 			}
113 		}
114 	}
115 }