View Javadoc
1   package org.argeo.people.web.parts;
2   
3   import java.io.InputStream;
4   
5   import javax.jcr.Node;
6   import javax.jcr.Property;
7   import javax.jcr.RepositoryException;
8   
9   import org.apache.commons.io.IOUtils;
10  import org.argeo.cms.ui.CmsUiProvider;
11  import org.argeo.connect.ConnectNames;
12  import org.argeo.connect.resources.ResourcesNames;
13  import org.argeo.connect.resources.ResourcesService;
14  import org.argeo.eclipse.ui.EclipseUiUtils;
15  import org.argeo.people.PeopleNames;
16  import org.argeo.people.PeopleService;
17  import org.argeo.people.web.PeopleWebConstants;
18  import org.argeo.people.web.providers.PersonOverviewLP;
19  import org.eclipse.jface.viewers.ILabelProvider;
20  import org.eclipse.rap.rwt.RWT;
21  import org.eclipse.swt.SWT;
22  import org.eclipse.swt.graphics.Image;
23  import org.eclipse.swt.layout.GridData;
24  import org.eclipse.swt.layout.GridLayout;
25  import org.eclipse.swt.widgets.Composite;
26  import org.eclipse.swt.widgets.Control;
27  import org.eclipse.swt.widgets.Label;
28  
29  /** Overview header for Node of type PeopleTypes.PEOPLE_PERSON */
30  public class PersonHeaderPart implements CmsUiProvider {
31  
32  	/* DEPENDENCY INJECTION */
33  	private PeopleService peopleService;
34  	private ResourcesService resourcesService;
35  	private TagLikeValuesPart tagsPart;
36  	private TagLikeValuesPart mailingListsPart;
37  
38  	public PersonHeaderPart() {
39  	}
40  
41  	public PersonHeaderPart(PeopleService peopleService) {
42  		this.peopleService = peopleService;
43  		tagsPart = new TagLikeValuesPart(ResourcesNames.CONNECT_TAGS);
44  		mailingListsPart = new TagLikeValuesPart(PeopleNames.PEOPLE_MAILING_LISTS);
45  	}
46  
47  	@Override
48  	public Control createUi(Composite parent, Node context) throws RepositoryException {
49  		InputStream is = null;
50  		Image itemPicture = null;
51  		// Initialize image
52  		// FIXME rather use a CmsImage
53  		try {
54  			if (context.hasNode(ConnectNames.CONNECT_PHOTO)) {
55  				Node imageNode = context.getNode(ConnectNames.CONNECT_PHOTO).getNode(Node.JCR_CONTENT);
56  				is = imageNode.getProperty(Property.JCR_DATA).getBinary().getStream();
57  				itemPicture = new Image(parent.getShell().getDisplay(), is);
58  			} else
59  				itemPicture = null;
60  		} catch (Exception e) {
61  		} finally {
62  			IOUtils.closeQuietly(is);
63  		}
64  
65  		if (itemPicture != null) {
66  			parent.setLayout(new GridLayout(2, false));
67  			Composite imgCmp = new Composite(parent, SWT.NO_FOCUS | SWT.NO_SCROLL | SWT.NO_TRIM);
68  			imgCmp.setLayout(EclipseUiUtils.noSpaceGridLayout());
69  			imgCmp.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false));
70  			new ImageLabel(imgCmp, SWT.NO_FOCUS, itemPicture);
71  
72  			Composite rightCmp = new Composite(parent, SWT.NO_FOCUS);
73  			rightCmp.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
74  			parent = rightCmp;
75  		}
76  		parent.setLayout(new GridLayout());
77  
78  		final Label readOnlyInfoLbl = new Label(parent, SWT.WRAP);
79  		readOnlyInfoLbl.setData(RWT.MARKUP_ENABLED, Boolean.TRUE);
80  		ILabelProvider personLP = new PersonOverviewLP(resourcesService, peopleService,
81  				PeopleWebConstants.OVERVIEW_TYPE_HEADER);
82  		readOnlyInfoLbl.setText(personLP.getText(context));
83  
84  		Composite tagsCmp = new Composite(parent, SWT.NO_FOCUS);
85  		tagsCmp.setLayoutData(EclipseUiUtils.fillWidth());
86  		tagsCmp.setLayout(EclipseUiUtils.noSpaceGridLayout());
87  		tagsPart.createUi(tagsCmp, context);
88  
89  		Composite mlCmp = new Composite(parent, SWT.NO_FOCUS);
90  		mlCmp.setLayoutData(EclipseUiUtils.fillWidth());
91  		mlCmp.setLayout(EclipseUiUtils.noSpaceGridLayout());
92  		mailingListsPart.createUi(mlCmp, context);
93  
94  		return parent;
95  	}
96  
97  	/** Will dispose the image on dispose */
98  	private class ImageLabel extends Label {
99  		private static final long serialVersionUID = 1L;
100 
101 		private final Image image;
102 
103 		private ImageLabel(Composite parent, int style, Image image) {
104 			super(parent, style);
105 			this.image = image;
106 			this.setBackground(parent.getBackground());
107 			this.setImage(image);
108 			// gd.horizontalIndent = 5;
109 			// gd.verticalIndent = 5;
110 			this.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false));
111 		}
112 
113 		@Override
114 		public void dispose() {
115 			if (image != null)
116 				image.dispose();
117 			super.dispose();
118 		}
119 	}
120 
121 	/* DEPENDENCY INJECTION */
122 	public void setResourcesService(ResourcesService resourcesService) {
123 		this.resourcesService = resourcesService;
124 	}
125 
126 	public void setPeopleService(PeopleService peopleService) {
127 		this.peopleService = peopleService;
128 	}
129 
130 	public void setTagsPart(TagLikeValuesPart tagsPart) {
131 		this.tagsPart = tagsPart;
132 	}
133 
134 	public void setMailingListsPart(TagLikeValuesPart mailingListsPart) {
135 		this.mailingListsPart = mailingListsPart;
136 	}
137 }