Merge branch 'release/5.23.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / sessions / InspectSessionsDialog.java
1 /**
2 * Copyright (C) 2015 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.taxeditor.view.sessions;
10
11 import java.util.List;
12
13 import org.eclipse.jface.viewers.ILabelProvider;
14 import org.eclipse.jface.viewers.ITreeContentProvider;
15 import org.eclipse.jface.viewers.StyledCellLabelProvider;
16 import org.eclipse.jface.viewers.StyledString;
17 import org.eclipse.jface.viewers.TreeViewer;
18 import org.eclipse.jface.viewers.Viewer;
19 import org.eclipse.jface.viewers.ViewerCell;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.custom.SashForm;
22 import org.eclipse.swt.events.SelectionAdapter;
23 import org.eclipse.swt.events.SelectionEvent;
24 import org.eclipse.swt.graphics.Image;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.layout.GridLayout;
27 import org.eclipse.swt.widgets.Button;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Dialog;
30 import org.eclipse.swt.widgets.Display;
31 import org.eclipse.swt.widgets.Label;
32 import org.eclipse.swt.widgets.Shell;
33 import org.eclipse.swt.widgets.Text;
34 import org.eclipse.ui.dialogs.FilteredTree;
35 import org.eclipse.ui.dialogs.PatternFilter;
36 import org.eclipse.wb.swt.SWTResourceManager;
37
38 import eu.etaxonomy.cdm.cache.CdmModelFieldPropertyFromClass;
39 import eu.etaxonomy.cdm.cache.CdmRemoteCacheManager;
40 import eu.etaxonomy.cdm.cache.EntityCacherDebugResult;
41 import eu.etaxonomy.cdm.cache.EntityCacherDebugResult.CdmEntityInfo;
42 import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
43 import eu.etaxonomy.taxeditor.store.CdmStore;
44 import net.sf.ehcache.Cache;
45 import net.sf.ehcache.Element;
46
47 /**
48 * @author cmathew
49 * @date 17 Feb 2015
50 */
51 public class InspectSessionsDialog extends Dialog {
52
53 protected Object result;
54 protected Shell shlInspectSessions;
55 private Text txtDebugInfo;
56 private Label lblDebugInformation;
57
58 private final Cache cdmlibModelCache;
59
60 private final ICdmEntitySession activeSession;
61
62 private TreeViewer treeViewer;
63 private Button btnClose;
64 private EntityCacherDebugResult entityCacherDebugResult;
65 private SashForm sashForm;
66 private Composite compositeDebug;
67
68 /**
69 * Create the dialog.
70 * @param parent
71 * @param style
72 */
73 public InspectSessionsDialog(Shell parent, int style) {
74 super(parent, style);
75 setText("Inspect Active Session");
76 cdmlibModelCache = CdmRemoteCacheManager.getInstance().getCdmModelGetMethodsCache();
77 activeSession = CdmStore.getCurrentSessionManager().getActiveSession();
78 if (activeSession != null){
79 //TODO
80 entityCacherDebugResult = activeSession.debug(false);
81 }
82 }
83
84 /**
85 * Open the dialog.
86 */
87 public Object open() {
88
89 createContents();
90 setDebugInfoText();
91 treeViewer.setContentProvider(new SessionsTreeContentProvider());
92 treeViewer.setLabelProvider(new SessionsTreeLabelProvider());
93 treeViewer.setInput(getRootElements());
94 shlInspectSessions.open();
95 shlInspectSessions.layout();
96 Display display = getParent().getDisplay();
97 while (!shlInspectSessions.isDisposed()) {
98 if (!display.readAndDispatch()) {
99 display.sleep();
100 }
101 }
102 return result;
103 }
104
105 /**
106 * Create contents of the dialog.
107 */
108 private void createContents() {
109 shlInspectSessions = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.RESIZE);
110 shlInspectSessions.setSize(641, 631);
111 shlInspectSessions.setText("Inspect Sessions");
112 shlInspectSessions.setLayout(new GridLayout(1, false));
113
114 sashForm = new SashForm(shlInspectSessions, SWT.VERTICAL);
115 sashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
116
117 PatternFilter filter = new PatternFilter();
118 FilteredTree tree = new FilteredTree(sashForm, SWT.MULTI | SWT.H_SCROLL | SWT.BORDER
119 | SWT.V_SCROLL, filter, true);
120 treeViewer = tree.getViewer();
121 //treeViewer = new TreeViewer(sashForm, SWT.BORDER);
122 //Tree tree = treeViewer.getTree();
123
124 compositeDebug = new Composite(sashForm, SWT.NONE);
125 compositeDebug.setLayout(new GridLayout(1, false));
126
127 lblDebugInformation = new Label(compositeDebug, SWT.NONE);
128 lblDebugInformation.setAlignment(SWT.CENTER);
129 lblDebugInformation.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1));
130 lblDebugInformation.setFont(SWTResourceManager.getFont("Ubuntu", 10, SWT.NORMAL));
131 lblDebugInformation.setText("Debug Information");
132
133 txtDebugInfo = new Text(compositeDebug, SWT.BORDER | SWT.READ_ONLY | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL | SWT.MULTI);
134 txtDebugInfo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
135 sashForm.setWeights(new int[] {338, 184});
136
137 btnClose = new Button(shlInspectSessions, SWT.NONE);
138 btnClose.addSelectionListener(new SelectionAdapter() {
139 @Override
140 public void widgetSelected(SelectionEvent e) {
141 shlInspectSessions.dispose();
142 }
143 });
144 GridData gd_btnClose = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
145 gd_btnClose.widthHint = 70;
146 btnClose.setLayoutData(gd_btnClose);
147 btnClose.setText("Close");
148 }
149
150 private void setDebugInfoText() {
151 txtDebugInfo.setText(entityCacherDebugResult == null? "No session" : entityCacherDebugResult.toString());
152 }
153 private CdmEntityInfo[] getRootElements() {
154
155 List<CdmEntityInfo> rootElemnts = entityCacherDebugResult.getRootElements();
156 return rootElemnts.toArray(new CdmEntityInfo[rootElemnts.size()]);
157 }
158
159 class SessionsTreeContentProvider implements ITreeContentProvider {
160
161 @Override
162 public void dispose() {
163
164 }
165
166 @Override
167 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
168
169 @Override
170 public Object[] getElements(Object inputElement) {
171 return (CdmEntityInfo[])inputElement;
172 }
173
174 @Override
175 public Object[] getChildren(Object parentElement) {
176 List<CdmEntityInfo> children = ((CdmEntityInfo)parentElement).getChildren();
177 return children.toArray();
178 }
179
180 @Override
181 public Object getParent(Object element) {
182 CdmEntityInfo cei = (CdmEntityInfo)element;
183 return cei.getParent();
184 }
185
186 @Override
187 public boolean hasChildren(Object element) {
188 List<CdmEntityInfo> children = ((CdmEntityInfo)element).getChildren();
189 if(children != null && !children.isEmpty()) {
190 return true;
191 } else {
192 return false;
193 }
194 }
195
196 public CdmModelFieldPropertyFromClass getFromCdmlibModelCache(String className) {
197 Element e = cdmlibModelCache.get(className);
198 if (e == null) {
199 return null;
200 } else {
201 return (CdmModelFieldPropertyFromClass) e.getObjectValue();
202 }
203 }
204 }
205
206 class SessionsTreeLabelProvider extends StyledCellLabelProvider implements ILabelProvider {
207
208 @Override
209 public void update(ViewerCell cell) {
210 CdmEntityInfo cei = (CdmEntityInfo)cell.getElement();
211 StyledString text = new StyledString();
212 if(cei != null) {
213 text.append(cei.getLabel());
214 }
215 cell.setText(text.toString());
216 cell.setStyleRanges(text.getStyleRanges());
217 super.update(cell);
218 }
219
220 @Override
221 public Image getImage(Object element) {
222 // TODO Auto-generated method stub
223 return null;
224 }
225
226 @Override
227 public String getText(Object element) {
228 CdmEntityInfo cei = (CdmEntityInfo)element;
229 return cei.getLabel();
230 }
231 }
232 }