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.connect.ui.parts;
17  
18  import javax.jcr.Node;
19  import javax.jcr.Session;
20  
21  import org.argeo.connect.ConnectTypes;
22  import org.argeo.connect.ui.AppWorkbenchService;
23  import org.argeo.eclipse.ui.EclipseUiUtils;
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.GridLayout;
33  import org.eclipse.swt.widgets.Button;
34  import org.eclipse.swt.widgets.Composite;
35  import org.eclipse.swt.widgets.Control;
36  import org.eclipse.swt.widgets.Shell;
37  
38  /**
39   * Dialog with a filtered list to add reference to some related business
40   * entity(ies)
41   */
42  public class PickUpRelatedDialog extends TrayDialog {
43  	private static final long serialVersionUID = -2526572299370624808L;
44  
45  	// Context
46  	private final Session session;
47  	private final AppWorkbenchService appWorkbenchService;
48  	private Node selectedNode;
49  
50  	// this page widgets and UI objects
51  	private FilterEntitiesVirtualTable tableCmp;
52  	private final String title;
53  
54  	// draft workaround to prevent window close when the user presses return
55  	private Button dummyButton;
56  
57  	public PickUpRelatedDialog(Shell parentShell, String title, Session session,
58  			AppWorkbenchService appWorkbenchService, Node referencingNode) {
59  		super(parentShell);
60  		this.title = title;
61  		// this.referencingNode = referencingNode;
62  		this.session = session;
63  		this.appWorkbenchService = appWorkbenchService;
64  	}
65  
66  	protected Point getInitialSize() {
67  		return new Point(400, 600);
68  	}
69  
70  	protected Control createDialogArea(Composite parent) {
71  		Composite dialogArea = (Composite) super.createDialogArea(parent);
72  
73  		// final Button seeAllChk = new Button(dialogArea, SWT.CHECK);
74  		// seeAllChk.setText("See all organisation");
75  
76  		Composite bodyCmp = new Composite(dialogArea, SWT.NO_FOCUS);
77  		bodyCmp.setLayoutData(EclipseUiUtils.fillAll());
78  		bodyCmp.setLayout(new GridLayout());
79  
80  		int style = SWT.BORDER | SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL;
81  		tableCmp = new FilterEntitiesVirtualTable(bodyCmp, style, session, appWorkbenchService,
82  				ConnectTypes.CONNECT_ENTITY);
83  		tableCmp.setLayoutData(EclipseUiUtils.fillAll());
84  		tableCmp.getTableViewer().addDoubleClickListener(new MyDoubleClickListener());
85  		tableCmp.getTableViewer().addSelectionChangedListener(new MySelectionChangedListener());
86  
87  		// draft workaround to prevent window close when the user presses return
88  		dummyButton = new Button(dialogArea, SWT.PUSH);
89  		dummyButton.setVisible(false);
90  
91  		parent.pack();
92  		return dialogArea;
93  	}
94  
95  	public Node getSelected() {
96  		return selectedNode;
97  	}
98  
99  	protected void configureShell(Shell shell) {
100 		super.configureShell(shell);
101 		shell.setText(title);
102 	}
103 
104 	@Override
105 	public void create() {
106 		super.create();
107 		getShell().setDefaultButton(dummyButton);
108 	}
109 
110 	class MySelectionChangedListener implements ISelectionChangedListener {
111 		@Override
112 		public void selectionChanged(SelectionChangedEvent event) {
113 			if (event.getSelection().isEmpty())
114 				return;
115 
116 			Object obj = ((IStructuredSelection) event.getSelection()).getFirstElement();
117 			if (obj instanceof Node) {
118 				selectedNode = (Node) obj;
119 			}
120 		}
121 	}
122 
123 	class MyDoubleClickListener implements IDoubleClickListener {
124 		public void doubleClick(DoubleClickEvent evt) {
125 			if (evt.getSelection().isEmpty())
126 				return;
127 
128 			Object obj = ((IStructuredSelection) evt.getSelection()).getFirstElement();
129 			if (obj instanceof Node) {
130 				selectedNode = (Node) obj;
131 				okPressed();
132 			}
133 		}
134 	}
135 }