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.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   * Dialog with a filtered list to choose a country
48   */
49  public class PickUpCountryDialog extends TrayDialog {
50  	private static final long serialVersionUID = 3766899676609659573L;
51  
52  	// Context
53  	private ResourcesService resourceService;
54  	private final Session session;
55  	private Node selectedNode;
56  
57  	// this page widgets and UI objects
58  	private SimpleJcrTableComposite tableCmp;
59  	private final String title;
60  
61  	private List<JcrColumnDefinition> colDefs = new ArrayList<JcrColumnDefinition>();
62  	{ // By default, it displays only title
63  		colDefs.add(new JcrColumnDefinition(null, ResourcesNames.RESOURCES_TAG_CODE, PropertyType.STRING, "Iso Code", 100));
64  		colDefs.add(new JcrColumnDefinition(null, Property.JCR_TITLE, PropertyType.STRING, "Label", 240));
65  	};
66  
67  	public PickUpCountryDialog(Shell parentShell, ResourcesService resourceService, Session session, String title) {
68  		super(parentShell);
69  		this.resourceService = resourceService;
70  		this.session = session;
71  		this.title = title;
72  	}
73  
74  	protected Point getInitialSize() {
75  		return new Point(400, 400);
76  	}
77  
78  	protected Control createDialogArea(Composite parent) {
79  		Composite dialogArea = (Composite) super.createDialogArea(parent);
80  
81  		Node tagParent = resourceService.getTagLikeResourceParent(session, ConnectConstants.RESOURCE_COUNTRY);
82  
83  		int style = SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL;
84  		tableCmp = new SimpleJcrTableComposite(dialogArea, style, session, ConnectJcrUtils.getPath(tagParent),
85  				ResourcesTypes.RESOURCES_ENCODED_TAG, colDefs, true, false);
86  		tableCmp.setLayoutData(EclipseUiUtils.fillAll());
87  
88  		// Add listeners
89  		tableCmp.getTableViewer().addDoubleClickListener(new MyDoubleClickListener());
90  		tableCmp.getTableViewer().addSelectionChangedListener(new MySelectionChangedListener());
91  
92  		parent.pack();
93  		return dialogArea;
94  	}
95  
96  	public String getSelected() {
97  		if (selectedNode != null)
98  			return ConnectJcrUtils.get(selectedNode, ResourcesNames.RESOURCES_TAG_CODE);
99  		else
100 			return null;
101 	}
102 
103 	protected void configureShell(Shell shell) {
104 		super.configureShell(shell);
105 		shell.setText(title);
106 	}
107 
108 	class MySelectionChangedListener implements ISelectionChangedListener {
109 		@Override
110 		public void selectionChanged(SelectionChangedEvent event) {
111 			if (event.getSelection().isEmpty()) {
112 				selectedNode = null;
113 				return;
114 			}
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 				selectedNode = null;
127 				return;
128 			}
129 
130 			Object obj = ((IStructuredSelection) evt.getSelection()).getFirstElement();
131 			if (obj instanceof Node) {
132 				selectedNode = (Node) obj;
133 				okPressed();
134 			}
135 		}
136 	}
137 }