Merge branch 'develop' into remoting-4.0
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / sessions / SessionsViewPart.java
1 // $Id$
2 /**
3 * Copyright (C) 2015 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.taxeditor.view.sessions;
11
12 import java.text.DecimalFormat;
13 import java.util.Collection;
14
15 import net.sf.ehcache.Cache;
16 import net.sf.ehcache.CacheManager;
17 import net.sf.ehcache.statistics.LiveCacheStatistics;
18
19 import org.eclipse.jface.action.IMenuManager;
20 import org.eclipse.jface.action.IToolBarManager;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.SelectionAdapter;
23 import org.eclipse.swt.events.SelectionEvent;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Button;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Label;
29 import org.eclipse.swt.widgets.Table;
30 import org.eclipse.swt.widgets.TableColumn;
31 import org.eclipse.swt.widgets.TableItem;
32 import org.eclipse.swt.widgets.Text;
33 import org.eclipse.ui.part.ViewPart;
34 import org.eclipse.wb.swt.SWTResourceManager;
35
36 import eu.etaxonomy.cdm.api.cache.CdmCacher;
37 import eu.etaxonomy.taxeditor.model.AbstractUtility;
38 import eu.etaxonomy.taxeditor.remoting.cache.CdmRemoteCacheManager;
39 import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
40 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
41 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManagerObserver;
42 import eu.etaxonomy.taxeditor.store.CdmStore;
43
44 /**
45 * @author cmathew
46 * @date 16 Feb 2015
47 *
48 */
49 public class SessionsViewPart extends ViewPart implements ICdmEntitySessionManagerObserver {
50
51 public static final String ID = "eu.etaxonomy.taxeditor.view.sessions.SessionsViewPart"; //$NON-NLS-1$
52 public static final String OPEN_INSPECT_SESSIONS_DIALOG_ID = "eu.etaxonomy.taxeditor.store.open.InspectSessionsDialog";
53 private Table tableSessions;
54
55 private final String[] titles = { "Owner", "In Memory", "On Disk"};
56
57 private final ICdmEntitySessionManager cdmEntitySessionManager;
58 private Text txtNoOfSessions;
59 private Text txtNoOfCaches;
60 private Text txtDefaultInMemory;
61 private Text txtCdmModelInMemory;
62 private Text txtCdmModelOnDisk;
63 private Text txtDefaultOnDisk;
64
65 public SessionsViewPart() {
66 this.cdmEntitySessionManager = CdmStore.getCurrentSessionManager();
67 if(cdmEntitySessionManager != null) {
68 cdmEntitySessionManager.addSessionObserver(this);
69 }
70 }
71
72 /**
73 * Create contents of the view part.
74 * @param parent
75 */
76 @Override
77 public void createPartControl(Composite parent) {
78 Composite container = new Composite(parent, SWT.NONE);
79 container.setLayout(new GridLayout(5, false));
80 {
81 Label lblNoOfSessions = new Label(container, SWT.NONE);
82 lblNoOfSessions.setFont(SWTResourceManager.getFont("Ubuntu", 10, SWT.NORMAL));
83 lblNoOfSessions.setAlignment(SWT.RIGHT);
84 GridData gd_lblNoOfSessions = new GridData(SWT.RIGHT, SWT.FILL, false, false, 1, 1);
85 gd_lblNoOfSessions.widthHint = 115;
86 gd_lblNoOfSessions.minimumHeight = 30;
87 lblNoOfSessions.setLayoutData(gd_lblNoOfSessions);
88 lblNoOfSessions.setText("No. of Sessions : ");
89 }
90 {
91 txtNoOfSessions = new Text(container, SWT.BORDER);
92 txtNoOfSessions.setEditable(false);
93 txtNoOfSessions.setBackground(SWTResourceManager.getColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));
94 GridData gd_txtNoOfSessions = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
95 gd_txtNoOfSessions.widthHint = 50;
96 txtNoOfSessions.setLayoutData(gd_txtNoOfSessions);
97 }
98 new Label(container, SWT.NONE);
99 new Label(container, SWT.NONE);
100 new Label(container, SWT.NONE);
101 {
102 Label lblNoOfCaches = new Label(container, SWT.CENTER);
103 lblNoOfCaches.setAlignment(SWT.RIGHT);
104 lblNoOfCaches.setFont(SWTResourceManager.getFont("Ubuntu", 10, SWT.NORMAL));
105 GridData gd_lblNoOfCaches = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1);
106 gd_lblNoOfCaches.widthHint = 150;
107 lblNoOfCaches.setLayoutData(gd_lblNoOfCaches);
108 lblNoOfCaches.setText("No. of entity caches : ");
109 }
110 {
111 txtNoOfCaches = new Text(container, SWT.BORDER);
112 txtNoOfCaches.setEditable(false);
113 GridData gd_txtNoOfCaches = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
114 gd_txtNoOfCaches.widthHint = 50;
115 txtNoOfCaches.setLayoutData(gd_txtNoOfCaches);
116 }
117 new Label(container, SWT.NONE);
118 new Label(container, SWT.NONE);
119 new Label(container, SWT.NONE);
120
121 {
122 Label lblDefaultCache = new Label(container, SWT.CENTER);
123 lblDefaultCache.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
124 lblDefaultCache.setText("Default Cache, ");
125 lblDefaultCache.setFont(SWTResourceManager.getFont("Ubuntu", 10, SWT.NORMAL));
126 lblDefaultCache.setAlignment(SWT.RIGHT);
127 }
128 {
129 Label lblDefaultInMemory = new Label(container, SWT.CENTER);
130 lblDefaultInMemory.setText("in Memory : ");
131 lblDefaultInMemory.setFont(SWTResourceManager.getFont("Ubuntu", 10, SWT.NORMAL));
132 lblDefaultInMemory.setAlignment(SWT.RIGHT);
133 lblDefaultInMemory.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
134 }
135 {
136 txtDefaultInMemory = new Text(container, SWT.BORDER);
137 txtDefaultInMemory.setEditable(false);
138 GridData gd_txtDefaultInMemory = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
139 gd_txtDefaultInMemory.widthHint = 130;
140 txtDefaultInMemory.setLayoutData(gd_txtDefaultInMemory);
141 }
142 {
143 Label lblDefaultOnDisk = new Label(container, SWT.CENTER);
144 lblDefaultOnDisk.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
145 lblDefaultOnDisk.setText("on Disk : ");
146 lblDefaultOnDisk.setFont(SWTResourceManager.getFont("Ubuntu", 10, SWT.NORMAL));
147 lblDefaultOnDisk.setAlignment(SWT.RIGHT);
148 }
149 {
150 txtDefaultOnDisk = new Text(container, SWT.BORDER);
151 txtDefaultOnDisk.setEditable(false);
152 txtDefaultOnDisk.setText("");
153 GridData gd_txtDefaultOnDisk = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
154 gd_txtDefaultOnDisk.widthHint = 130;
155 txtDefaultOnDisk.setLayoutData(gd_txtDefaultOnDisk);
156 }
157 {
158 Label lblCdmModelCache = new Label(container, SWT.CENTER);
159 lblCdmModelCache.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
160 lblCdmModelCache.setText("CDM Model Cache, ");
161 lblCdmModelCache.setFont(SWTResourceManager.getFont("Ubuntu", 10, SWT.NORMAL));
162 lblCdmModelCache.setAlignment(SWT.RIGHT);
163 }
164 {
165 Label lblCdmModelInMemory = new Label(container, SWT.CENTER);
166 lblCdmModelInMemory.setText("in Memory : ");
167 lblCdmModelInMemory.setFont(SWTResourceManager.getFont("Ubuntu", 10, SWT.NORMAL));
168 lblCdmModelInMemory.setAlignment(SWT.RIGHT);
169 GridData gd_lblCdmModelInMemory = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1);
170 gd_lblCdmModelInMemory.widthHint = 86;
171 lblCdmModelInMemory.setLayoutData(gd_lblCdmModelInMemory);
172 }
173 {
174 txtCdmModelInMemory = new Text(container, SWT.BORDER);
175 txtCdmModelInMemory.setEditable(false);
176 GridData gd_txtCdmModelInMemory = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
177 gd_txtCdmModelInMemory.widthHint = 130;
178 txtCdmModelInMemory.setLayoutData(gd_txtCdmModelInMemory);
179 }
180 {
181 Label lblCdmModelOnDisk = new Label(container, SWT.CENTER);
182 lblCdmModelOnDisk.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
183 lblCdmModelOnDisk.setText("on Disk : ");
184 lblCdmModelOnDisk.setFont(SWTResourceManager.getFont("Ubuntu", 10, SWT.NORMAL));
185 lblCdmModelOnDisk.setAlignment(SWT.RIGHT);
186 }
187 {
188 txtCdmModelOnDisk = new Text(container, SWT.BORDER);
189 txtCdmModelOnDisk.setEditable(false);
190 txtCdmModelOnDisk.setText("");
191 GridData gd_txtCdmModelOnDisk = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
192 gd_txtCdmModelOnDisk.widthHint = 130;
193 txtCdmModelOnDisk.setLayoutData(gd_txtCdmModelOnDisk);
194 }
195 {
196 Button btnRefresh = new Button(container, SWT.NONE);
197 btnRefresh.addSelectionListener(new SelectionAdapter() {
198 @Override
199 public void widgetSelected(SelectionEvent e) {
200 changed();
201 }
202 });
203 GridData gd_btnRefresh = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
204 gd_btnRefresh.widthHint = 100;
205 btnRefresh.setLayoutData(gd_btnRefresh);
206 btnRefresh.setText("Refresh");
207 }
208 {
209 Button btnInspectActiveSession = new Button(container, SWT.NONE);
210 btnInspectActiveSession.addSelectionListener(new SelectionAdapter() {
211 @Override
212 public void widgetSelected(SelectionEvent e) {
213 AbstractUtility.executeCommand(OPEN_INSPECT_SESSIONS_DIALOG_ID, this, null);
214 }
215 });
216 btnInspectActiveSession.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 2, 1));
217 btnInspectActiveSession.setText("Inspect Active Session");
218 }
219 new Label(container, SWT.NONE);
220 new Label(container, SWT.NONE);
221
222 {
223 tableSessions = new Table(container, SWT.BORDER | SWT.FULL_SELECTION);
224 GridData gd_tableSessions = new GridData(SWT.FILL, SWT.FILL, true, true, 5, 1);
225 gd_tableSessions.widthHint = 769;
226 tableSessions.setLayoutData(gd_tableSessions);
227 tableSessions.setHeaderVisible(true);
228 tableSessions.setLinesVisible(true);
229 }
230
231 createActions();
232 initializeToolBar();
233 initializeMenu();
234 }
235
236 /**
237 * Create the actions.
238 */
239 private void createActions() {
240
241
242 for (String title : titles) {
243 TableColumn column = new TableColumn(tableSessions, SWT.NULL);
244 column.setWidth(200);
245 column.setText(title);
246 }
247 changed();
248
249 }
250
251
252 @Override
253 public void changed() {
254 if(tableSessions.isDisposed()) {
255 return;
256 }
257 tableSessions.removeAll();
258 if(cdmEntitySessionManager != null) {
259 Collection<ICdmEntitySession> sessions = cdmEntitySessionManager.getSessions();
260 txtNoOfSessions.setText(String.valueOf(sessions.size()));
261 for(ICdmEntitySession session : sessions) {
262 if(session.getOwner() != null) {
263 TableItem item = new TableItem(tableSessions, SWT.NULL);
264 item.setText(session.getOwner().toString());
265 String activePrefix = "";
266 if(session.isActive()) {
267 activePrefix = "*";
268 }
269 item.setText(0, activePrefix + session.getOwner().toString());
270 LiveCacheStatistics cacheStatistics = session.getCacheStatistics();
271 item.setText(1, getInMemoryStats(cacheStatistics));
272 item.setText(2, getOnDiskStats(cacheStatistics));
273 }
274 }
275 for (int i=0; i<titles.length; i++) {
276 tableSessions.getColumn (i).pack ();
277 }
278 }
279
280 CdmRemoteCacheManager instance = CdmRemoteCacheManager.getInstance();
281 int cdmEnityCacheSize = CacheManager.create().getCacheNames().length;
282 txtNoOfCaches.setText(String.valueOf(cdmEnityCacheSize));
283
284 Cache defaultCache = CacheManager.create().getCache(CdmCacher.DEFAULT_CACHE_NAME);
285 txtDefaultInMemory.setText("");
286 txtDefaultOnDisk.setText("");
287 if(defaultCache != null) {
288 LiveCacheStatistics liveCacheStatistics = defaultCache.getLiveCacheStatistics();
289 txtDefaultInMemory.setText(getInMemoryStats(liveCacheStatistics));
290 txtDefaultOnDisk.setText(getOnDiskStats(liveCacheStatistics));
291 }
292
293 Cache cdmModelGetMethodsCache = instance.getCdmModelGetMethodsCache();
294 txtCdmModelInMemory.setText("");
295 txtCdmModelOnDisk.setText("");
296 if(cdmModelGetMethodsCache != null) {
297 LiveCacheStatistics liveCacheStatistics = cdmModelGetMethodsCache.getLiveCacheStatistics();
298 txtCdmModelInMemory.setText(getInMemoryStats(liveCacheStatistics));
299 txtCdmModelOnDisk.setText(getOnDiskStats(liveCacheStatistics));
300 }
301
302 }
303
304 private String getInMemoryStats(LiveCacheStatistics statistics) {
305 if(statistics != null) {
306 long localHeapSize = statistics.getLocalHeapSize();
307
308 long localHeapSizeInBytes = statistics.getLocalHeapSizeInBytes();
309 String localHeapSizeInBytesStr =
310 (new DecimalFormat("#.##")).format(localHeapSizeInBytes/1048576.0);
311 return String.valueOf(localHeapSize) + " objects / " + localHeapSizeInBytesStr + " Mb";
312 }
313 return "";
314 }
315
316 private String getOnDiskStats(LiveCacheStatistics statistics) {
317 if(statistics != null) {
318 long localDiskSize = statistics.getLocalDiskSize();
319
320 long localDiskSizeInBytes = statistics.getLocalDiskSizeInBytes();
321 String localDiskSizeInBytesStr =
322 (new DecimalFormat("#.##")).format(localDiskSizeInBytes/1048576.0);
323 return String.valueOf(localDiskSize) + " objects / " + localDiskSizeInBytesStr + " Mb";
324 }
325 return "";
326 }
327
328 /**
329 * Initialize the toolbar.
330 */
331 private void initializeToolBar() {
332 IToolBarManager toolbarManager = getViewSite().getActionBars().getToolBarManager();
333 }
334
335 /**
336 * Initialize the menu.
337 */
338 private void initializeMenu() {
339 IMenuManager menuManager = getViewSite().getActionBars().getMenuManager();
340 }
341
342 @Override
343 public void setFocus() {
344 // Set the focus
345 }
346
347 }