View Javadoc
1   package org.argeo.connect.e4.parts;
2   
3   import org.argeo.connect.ui.util.LazyCTabControl;
4   import org.argeo.eclipse.ui.EclipseUiUtils;
5   import org.eclipse.swt.SWT;
6   import org.eclipse.swt.custom.CTabFolder;
7   import org.eclipse.swt.custom.CTabItem;
8   import org.eclipse.swt.widgets.Composite;
9   import org.eclipse.swt.widgets.Control;
10  
11  /**
12   * Extends the <code>AbstractEntityEditor</code> Form adding a
13   * <code>CTabFolder</code> in the bottom part. Insures the presence of a
14   * corresponding people services and manage a life cycle of the JCR session that
15   * is bound to it. It provides a header with some meta informations and a body
16   * to add tabs with further details.
17   */
18  public abstract class AbstractConnectCTabEditor extends AbstractConnectEditor {
19  
20  	// Manage tab Folder
21  	private CTabFolder folder;
22  	protected String CTAB_INSTANCE_ID = "CTabId";
23  
24  	/** Overwrite to populate the CTabFolder */
25  	protected abstract void populateTabFolder(CTabFolder tabFolder);
26  
27  	/**
28  	 * Children class must not override this class or rather directly use the
29  	 * AbstractEntityEditor
30  	 */
31  	@Override
32  	protected final void populateBody(Composite parent) {
33  		parent.setLayout(EclipseUiUtils.noSpaceGridLayout());
34  		folder = createCTabFolder(parent, SWT.NO_FOCUS | SWT.BORDER);
35  		populateTabFolder(folder);
36  		folder.setSelection(0);
37  	}
38  
39  	/* MANAGE TAB FOLDER */
40  	protected CTabFolder createCTabFolder(Composite parent, int style) {
41  		CTabFolder tabFolder = new CTabFolder(parent, style);
42  		tabFolder.setLayoutData(EclipseUiUtils.fillAll());
43  		return tabFolder;
44  	}
45  
46  	@Override
47  	protected void addEditButtons(final Composite parent) {
48  //		if (ConnectJcrUtils.isNodeType(getNode(), NodeType.MIX_VERSIONABLE)) {
49  //			final Button showHistoryBtn = getFormToolkit().createButton(parent, "History", SWT.PUSH);
50  //			showHistoryBtn.setLayoutData(new RowData(60, 20));
51  //			showHistoryBtn.addSelectionListener(new SelectionAdapter() {
52  //				private static final long serialVersionUID = 1L;
53  //
54  //				@Override
55  //				public void widgetSelected(SelectionEvent e) {
56  //					// History panel
57  //					String tooltip = "History of information about " + JcrUtils.get(getNode(), Property.JCR_TITLE);
58  //					HistoryLog historyLogCmp = new HistoryLog(folder, SWT.NO_FOCUS, AbstractConnectCTabEditor.this,
59  //							getUserAdminService(), getNode());
60  //					historyLogCmp.setLayoutData(EclipseUiUtils.fillAll());
61  //					addLazyTabToFolder(folder, historyLogCmp, "History", HistoryLog.CTAB_ID, tooltip);
62  //					if (!showHistoryBtn.isDisposed()) {
63  //						Composite par = showHistoryBtn.getParent();
64  //						showHistoryBtn.dispose();
65  //						par.layout(true, true);
66  //					}
67  //					openTabItem(HistoryLog.CTAB_ID);
68  //					historyLogCmp.refreshPartControl();
69  //				}
70  //			});
71  //		}
72  	}
73  
74  	protected Composite addTabToFolder(CTabFolder tabFolder, int style, String label, String id, String tooltip) {
75  		CTabItem item = new CTabItem(tabFolder, style);
76  		item.setData(CTAB_INSTANCE_ID, id);
77  		item.setText(label);
78  		item.setToolTipText(tooltip);
79  		Composite innerPannel = getManagedForm().getToolkit().createComposite(tabFolder, SWT.V_SCROLL);
80  		// must set control
81  		item.setControl(innerPannel);
82  		return innerPannel;
83  	}
84  
85  	/**
86  	 * 
87  	 * @param tabFolder
88  	 * @param style
89  	 * @param label
90  	 * @param id
91  	 * @param tooltip
92  	 * @param afterTabId
93  	 *            the tab will be added after the tab that has this Id if such a
94  	 *            tab exists of at first place if null.
95  	 * @return
96  	 */
97  	protected Composite addTabToFolder(CTabFolder tabFolder, int style, String label, String id, String tooltip,
98  			String afterTabId) {
99  		// retrieve index of the existing tab
100 		CTabItem[] items = folder.getItems();
101 		int i = 0;
102 		if (afterTabId != null)
103 			loop: for (CTabItem item : items) {
104 				String currId = (String) item.getData(CTAB_INSTANCE_ID);
105 				i++;
106 				if (currId != null && currId.equals(afterTabId))
107 					break loop;
108 			}
109 
110 		CTabItem item;
111 		if (i == items.length)
112 			item = new CTabItem(tabFolder, style);
113 		else
114 			item = new CTabItem(tabFolder, style, i);
115 		item.setData(CTAB_INSTANCE_ID, id);
116 		item.setText(label);
117 		item.setToolTipText(tooltip);
118 		Composite innerPannel = getManagedForm().getToolkit().createComposite(tabFolder, SWT.V_SCROLL);
119 		// must set control
120 		item.setControl(innerPannel);
121 		return innerPannel;
122 	}
123 
124 	protected void addLazyTabToFolder(CTabFolder tabFolder, LazyCTabControl contentCmp, String label, String id,
125 			String tooltip) {
126 		addLazyTabToFolder(tabFolder, contentCmp, label, id, tooltip, null);
127 	}
128 
129 	protected void addLazyTabToFolder(CTabFolder tabFolder, LazyCTabControl contentCmp, String label, String id,
130 			String tooltip, String afterTabId) {
131 		// Retrieve index of the existing tab
132 		CTabItem[] items = folder.getItems();
133 		int i = 0;
134 		if (afterTabId != null)
135 			loop: for (CTabItem item : items) {
136 				String currId = (String) item.getData(CTAB_INSTANCE_ID);
137 				i++;
138 				if (currId != null && currId.equals(afterTabId))
139 					break loop;
140 			}
141 		else
142 			// put last
143 			i = items.length;
144 
145 		CTabItem item;
146 		if (i == items.length)
147 			item = new CTabItem(tabFolder, SWT.NO_FOCUS);
148 		else
149 			item = new CTabItem(tabFolder, SWT.NO_FOCUS, i);
150 		item.setData(CTAB_INSTANCE_ID, id);
151 		item.setText(label);
152 		item.setToolTipText(tooltip);
153 		// must set control
154 		item.setControl(contentCmp);
155 	}
156 
157 	/** Opens the corresponding tab if it has been defined */
158 	public void openTabItem(String id) {
159 		CTabItem[] items = folder.getItems();
160 		for (CTabItem item : items) {
161 			String currId = (String) item.getData(CTAB_INSTANCE_ID);
162 			if (currId != null && currId.equals(id)) {
163 				folder.setSelection(item);
164 				return;
165 			}
166 		}
167 	}
168 
169 	/** Retrieves a tab by ID if it has been defined */
170 	public CTabItem getTabItemById(String id) {
171 		CTabItem[] items = folder.getItems();
172 		for (CTabItem item : items) {
173 			String currId = (String) item.getData(CTAB_INSTANCE_ID);
174 			if (currId != null && currId.equals(id)) {
175 				return item;
176 			}
177 		}
178 		return null;
179 	}
180 
181 	/* UTILITES */
182 	protected boolean checkControl(Control control) {
183 		return control != null && !control.isDisposed();
184 	}
185 }