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 java.util.List;
19  
20  import javax.jcr.Node;
21  import javax.jcr.NodeIterator;
22  import javax.jcr.RepositoryException;
23  import javax.jcr.Session;
24  
25  import org.argeo.connect.ui.ConnectColumnDefinition;
26  import org.argeo.connect.ui.SystemWorkbenchService;
27  import org.argeo.eclipse.ui.EclipseUiUtils;
28  import org.eclipse.jface.dialogs.TrayDialog;
29  import org.eclipse.jface.viewers.DoubleClickEvent;
30  import org.eclipse.jface.viewers.IDoubleClickListener;
31  import org.eclipse.jface.viewers.ISelectionChangedListener;
32  import org.eclipse.jface.viewers.IStructuredSelection;
33  import org.eclipse.jface.viewers.SelectionChangedEvent;
34  import org.eclipse.swt.SWT;
35  import org.eclipse.swt.events.SelectionAdapter;
36  import org.eclipse.swt.events.SelectionEvent;
37  import org.eclipse.swt.graphics.Point;
38  import org.eclipse.swt.layout.GridData;
39  import org.eclipse.swt.widgets.Button;
40  import org.eclipse.swt.widgets.Composite;
41  import org.eclipse.swt.widgets.Control;
42  import org.eclipse.swt.widgets.Shell;
43  
44  /**
45   * Dialog with a filtered list to add reference to some "business" jcr Node.
46   * Choosable nodes are all nodes of the NodeType that is given upon creation.
47   * This nodeType must inherit from people:base nodeType, so that the created
48   * table finds information it expects to display in the table.
49   */
50  public class PickUpByNodeTypeDialog extends TrayDialog {
51  	private static final long serialVersionUID = -2526572299370624808L;
52  
53  	// Business objects
54  	private final Session session;
55  	private final SystemWorkbenchService systemWorkbenchService;
56  	private final String nodeType;
57  	private Node selectedNode;
58  	private List<ConnectColumnDefinition> colDefs;
59  
60  	// this page widgets and UI objects
61  	private FilterEntitiesVirtualTable tableCmp;
62  	private final String title;
63  
64  	// draft workaround to prevent window close when the user presses return
65  	private Button dummyButton;
66  
67  	public PickUpByNodeTypeDialog(Shell parentShell, String title, Session session,
68  			SystemWorkbenchService systemWorkbenchService, String nodeType) {
69  		super(parentShell);
70  		this.title = title;
71  		this.session = session;
72  		this.systemWorkbenchService = systemWorkbenchService;
73  		this.nodeType = nodeType;
74  	}
75  
76  	public PickUpByNodeTypeDialog(Shell parentShell, String title, Session session,
77  			SystemWorkbenchService systemWorkbenchService, String nodeType, List<ConnectColumnDefinition> colDefs) {
78  		super(parentShell);
79  		this.title = title;
80  		this.session = session;
81  		this.systemWorkbenchService = systemWorkbenchService;
82  		this.nodeType = nodeType;
83  		this.colDefs = colDefs;
84  	}
85  
86  	protected Point getInitialSize() {
87  		return new Point(400, 615);
88  	}
89  
90  	protected Control createDialogArea(Composite parent) {
91  		Composite dialogArea = (Composite) super.createDialogArea(parent);
92  
93  		Composite main = new Composite(dialogArea, SWT.NO_FOCUS);
94  		main.setLayout(EclipseUiUtils.noSpaceGridLayout());
95  		main.setLayoutData(EclipseUiUtils.fillAll());
96  
97  		int style = SWT.V_SCROLL | SWT.SINGLE | SWT.BORDER;
98  		tableCmp = new MyFilterEntitiesVirtualTable(main, style, session, systemWorkbenchService, nodeType, colDefs);
99  		GridData gd = EclipseUiUtils.fillAll();
100 		tableCmp.setLayoutData(gd);
101 
102 		// Add listeners
103 		tableCmp.getTableViewer().addDoubleClickListener(new MyDoubleClickListener());
104 		tableCmp.getTableViewer().addSelectionChangedListener(new MySelectionChangedListener());
105 
106 		tableCmp.getTableViewer().getTable().pack();
107 		tableCmp.getTableViewer().getTable().layout();
108 		tableCmp.layout();
109 
110 		// draft workaround to prevent window close when the user presses return
111 		dummyButton = new Button(main, SWT.PUSH);
112 		dummyButton.setLayoutData(new GridData(1, 1));
113 
114 		dialogArea.layout();
115 		return dialogArea;
116 	}
117 
118 	public Node getSelected() {
119 		return selectedNode;
120 	}
121 
122 	protected void configureShell(Shell shell) {
123 		super.configureShell(shell);
124 		shell.setText(title);
125 	}
126 
127 	@Override
128 	public void create() {
129 		super.create();
130 		dummyButton.addSelectionListener(new SelectionAdapter() {
131 			private static final long serialVersionUID = -7900611671119542857L;
132 
133 			@Override
134 			public void widgetSelected(SelectionEvent e) {
135 				// Do nothing, rather than call ok pressed
136 			}
137 		});
138 		Shell shell = getShell();
139 		shell.setDefaultButton(dummyButton);
140 	}
141 
142 	class MySelectionChangedListener implements ISelectionChangedListener {
143 		@Override
144 		public void selectionChanged(SelectionChangedEvent event) {
145 			if (event.getSelection().isEmpty())
146 				return;
147 
148 			Object obj = ((IStructuredSelection) event.getSelection()).getFirstElement();
149 			if (obj instanceof Node) {
150 				selectedNode = (Node) obj;
151 			}
152 		}
153 	}
154 
155 	class MyDoubleClickListener implements IDoubleClickListener {
156 		public void doubleClick(DoubleClickEvent evt) {
157 			if (evt.getSelection().isEmpty())
158 				return;
159 			Object obj = ((IStructuredSelection) evt.getSelection()).getFirstElement();
160 			if (obj instanceof Node) {
161 				selectedNode = (Node) obj;
162 				okPressed();
163 			}
164 		}
165 	}
166 
167 	/** Overwrite and return true to provide specific filtering */
168 	protected boolean defineSpecificQuery() {
169 		return false;
170 	}
171 
172 	/** Overwrite to provide specific filtering */
173 	protected NodeIterator query(Session session, String filter) throws RepositoryException {
174 		return null;
175 	}
176 
177 	// Add the ability to provide a business specific label provider for the
178 	// given entity type
179 	private class MyFilterEntitiesVirtualTable extends FilterEntitiesVirtualTable {
180 		private static final long serialVersionUID = 3122449385321832511L;
181 		private List<ConnectColumnDefinition> colDefs;
182 
183 		public MyFilterEntitiesVirtualTable(Composite parent, int style, Session session,
184 				SystemWorkbenchService systemWorkbenchService, String nodeType) {
185 			super(parent, style, session, systemWorkbenchService, nodeType);
186 		}
187 
188 		public MyFilterEntitiesVirtualTable(Composite parent, int style, Session session,
189 				SystemWorkbenchService systemWorkbenchService, String nodeType, List<ConnectColumnDefinition> colDefs) {
190 			super(parent, style, session, systemWorkbenchService, nodeType, true);
191 			this.colDefs = colDefs;
192 			populate();
193 		}
194 
195 		protected NodeIterator listFilteredElements(Session session, String filter) throws RepositoryException {
196 			if (defineSpecificQuery())
197 				return query(session, filter);
198 			else
199 				return super.listFilteredElements(session, filter);
200 		}
201 
202 		protected List<ConnectColumnDefinition> getColumnsDef() {
203 			if (colDefs == null)
204 				return super.getColumnsDef();
205 			else
206 				return colDefs;
207 		}
208 	}
209 }