View Javadoc
1   package org.argeo.people.e4.parts;
2   
3   import java.io.InputStream;
4   
5   import javax.annotation.PreDestroy;
6   import javax.jcr.Node;
7   import javax.jcr.Property;
8   
9   import org.apache.commons.io.IOUtils;
10  import org.argeo.cms.ui.eclipse.forms.FormToolkit;
11  import org.argeo.connect.ConnectNames;
12  import org.argeo.connect.e4.parts.AbstractConnectCTabEditor;
13  import org.argeo.connect.ui.ConnectImages;
14  import org.argeo.connect.ui.ConnectUiUtils;
15  import org.argeo.eclipse.ui.EclipseUiUtils;
16  import org.eclipse.swt.SWT;
17  import org.eclipse.swt.graphics.Image;
18  import org.eclipse.swt.layout.GridData;
19  import org.eclipse.swt.layout.GridLayout;
20  import org.eclipse.swt.widgets.Composite;
21  import org.eclipse.swt.widgets.Display;
22  import org.eclipse.swt.widgets.Label;
23  
24  /**
25   * Slightly modifies AbstractPeopleEditor main layout adding a place for an
26   * image on the left part of the header
27   */
28  public abstract class AbstractPeopleWithImgEditor extends AbstractConnectCTabEditor {
29  
30  	// A corresponding picture that must be explicitly disposed
31  	protected Image itemPicture = null;
32  
33  	@Override
34  	public void init() {
35  
36  		InputStream is = null;
37  		try {
38  			if (getNode().hasNode(ConnectNames.CONNECT_PHOTO)) {
39  				Node imageNode = getNode().getNode(ConnectNames.CONNECT_PHOTO).getNode(Node.JCR_CONTENT);
40  				is = imageNode.getProperty(Property.JCR_DATA).getBinary().getStream();
41  				itemPicture = new Image(Display.getCurrent(), is);
42  			} else
43  				itemPicture = null;
44  		} catch (Exception e) {
45  		} finally {
46  			IOUtils.closeQuietly(is);
47  		}
48  	}
49  
50  	@Override
51  	protected void createMainLayout(Composite parent) {
52  		parent.setLayout(EclipseUiUtils.noSpaceGridLayout());
53  
54  		FormToolkit toolkit = getManagedForm().getToolkit();
55  
56  		// Internal main Layout
57  		// The header
58  		Composite header = toolkit.createComposite(parent, SWT.NO_FOCUS | SWT.NO_SCROLL | SWT.NO_TRIM);
59  
60  		GridLayout gl;
61  		if (displayImage())
62  			gl = ConnectUiUtils.noSpaceGridLayout(3);
63  		else
64  			gl = ConnectUiUtils.noSpaceGridLayout(2);
65  
66  		// So that the buttons are not too close to the right border of the
67  		// composite.
68  		gl.marginRight = 5;
69  		header.setLayout(gl);
70  		header.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
71  
72  		if (displayImage()) {
73  			// the image
74  			Composite imgCmp = toolkit.createComposite(header, SWT.NO_FOCUS | SWT.NO_SCROLL | SWT.NO_TRIM);
75  			imgCmp.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false));
76  			populateImagePart(imgCmp);
77  		}
78  		// header content
79  		Composite left = toolkit.createComposite(header, SWT.NO_FOCUS | SWT.NO_SCROLL | SWT.NO_TRIM);
80  		left.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
81  		populateHeader(left);
82  
83  		// header buttons
84  		Composite right = toolkit.createComposite(header, SWT.NO_FOCUS | SWT.NO_SCROLL | SWT.NO_TRIM);
85  		GridData gd = new GridData(SWT.CENTER, SWT.TOP, false, false);
86  		gd.verticalIndent = 5;
87  		right.setLayoutData(gd);
88  		populateButtonsComposite(right);
89  
90  		// the body
91  		Composite body = toolkit.createComposite(parent, SWT.NO_FOCUS);
92  		body.setLayout(EclipseUiUtils.noSpaceGridLayout());
93  		body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
94  		populateBody(body);
95  	}
96  
97  	protected void populateImagePart(Composite parent) {
98  		GridLayout gl = new GridLayout(2, false);
99  		gl.marginTop = gl.verticalSpacing = 0;
100 		gl.horizontalSpacing = 0;
101 		gl.marginWidth = 10;
102 		gl.marginBottom = 8;
103 		parent.setLayout(gl);
104 
105 		Label image = getManagedForm().getToolkit().createLabel(parent, "", SWT.NO_FOCUS);
106 		image.setBackground(parent.getBackground());
107 		GridData gd = new GridData(SWT.LEFT, SWT.TOP, false, false);
108 		if (getPicture() != null) {
109 			image.setImage(getPicture());
110 			gd.horizontalIndent = 5;
111 			gd.verticalIndent = 5;
112 		} else {
113 			gd.widthHint = 10;
114 		}
115 		image.setLayoutData(gd);
116 
117 	}
118 
119 	protected Image getPicture() {
120 		return itemPicture;
121 	}
122 
123 	@PreDestroy
124 	public void dispose() {
125 		// Free the resources.
126 		if (itemPicture != null && !itemPicture.equals(ConnectImages.NO_PICTURE))
127 			itemPicture.dispose();
128 		super.dispose();
129 	}
130 
131 	/**
132 	 * Overwrite to display no image in the header
133 	 * 
134 	 * @return
135 	 */
136 	protected boolean displayImage() {
137 		return false;
138 	}
139 }