View Javadoc
1   package org.argeo.cms.ui.forms;
2   
3   import java.util.Observable;
4   import java.util.Observer;
5   
6   import javax.jcr.Node;
7   
8   import org.argeo.cms.ui.CmsEditable;
9   import org.argeo.cms.ui.util.CmsUiUtils;
10  import org.eclipse.swt.SWT;
11  import org.eclipse.swt.events.SelectionEvent;
12  import org.eclipse.swt.events.SelectionListener;
13  import org.eclipse.swt.widgets.Button;
14  import org.eclipse.swt.widgets.Composite;
15  
16  /** Add life cycle management abilities to an editable form page */
17  public class FormEditorHeader implements SelectionListener, Observer {
18  	private static final long serialVersionUID = 7392898696542484282L;
19  
20  	// private final Node context;
21  	private final CmsEditable cmsEditable;
22  	private Button publishBtn;
23  
24  	// Should we provide here the ability to switch from read only to edition
25  	// mode?
26  	// private Button editBtn;
27  	// private boolean readOnly;
28  
29  	// TODO add information about the current node status, typically if it is
30  	// dirty or not
31  
32  	private Composite parent;
33  	private Composite display;
34  	private Object layoutData;
35  
36  	public FormEditorHeader(Composite parent, int style, Node context,
37  			CmsEditable cmsEditable) {
38  		this.cmsEditable = cmsEditable;
39  		this.parent = parent;
40  		// readOnly = SWT.READ_ONLY == (style & SWT.READ_ONLY);
41  		// this.context = context;
42  		if (this.cmsEditable instanceof Observable)
43  			((Observable) this.cmsEditable).addObserver(this);
44  		refresh();
45  	}
46  
47  	public void setLayoutData(Object layoutData) {
48  		this.layoutData = layoutData;
49  		if (display != null && !display.isDisposed())
50  			display.setLayoutData(layoutData);
51  	}
52  
53  	protected void refresh() {
54  		if (display != null && !display.isDisposed())
55  			display.dispose();
56  
57  		display = new Composite(parent, SWT.NONE);
58  		display.setLayoutData(layoutData);
59  
60  		CmsUiUtils.style(display, FormStyle.header.style());
61  		display.setBackgroundMode(SWT.INHERIT_FORCE);
62  
63  		display.setLayout(CmsUiUtils.noSpaceGridLayout());
64  
65  		publishBtn = createSimpleBtn(display, getPublishButtonLabel());
66  		display.moveAbove(null);
67  		parent.layout();
68  	}
69  
70  	private Button createSimpleBtn(Composite parent, String label) {
71  		Button button = new Button(parent, SWT.FLAT | SWT.PUSH);
72  		button.setText(label);
73  		CmsUiUtils.style(button, FormStyle.header.style());
74  		button.addSelectionListener(this);
75  		return button;
76  	}
77  
78  	private String getPublishButtonLabel() {
79  		// Rather check if the current node differs from what has been
80  		// previously committed
81  		// For the time being, we always reach here, the underlying CmsEditable
82  		// is always editing.
83  		if (cmsEditable.isEditing())
84  			return " Publish ";
85  		else
86  			return " Edit ";
87  	}
88  
89  	@Override
90  	public void widgetSelected(SelectionEvent e) {
91  		if (e.getSource() == publishBtn) {
92  			// For the time being, the underlying CmsEditable
93  			// is always editing when we reach this point
94  			if (cmsEditable.isEditing()) {
95  				// we always leave the node in a check outed state
96  				cmsEditable.stopEditing();
97  				cmsEditable.startEditing();
98  			} else {
99  				cmsEditable.startEditing();
100 			}
101 		}
102 	}
103 
104 	@Override
105 	public void widgetDefaultSelected(SelectionEvent e) {
106 	}
107 
108 	@Override
109 	public void update(Observable o, Object arg) {
110 		if (o == cmsEditable) {
111 			refresh();
112 		}
113 	}
114 }