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