ee2636285f27d927650386d6e5f649e53e1d433d
[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 EntityCacherDebugResult ecdr;
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 ecdr = activeSession.debug();
79 }
80
81 /**
82 * Open the dialog.
83 * @return the result
84 */
85 public Object open() {
86
87 createContents();
88 setDebugInfoText();
89 treeViewer.setContentProvider(new SessionsTreeContentProvider());
90 treeViewer.setLabelProvider(new SessionsTreeLabelProvider());
91 treeViewer.setInput(getRootElements());
92 shlInspectSessions.open();
93 shlInspectSessions.layout();
94 Display display = getParent().getDisplay();
95 while (!shlInspectSessions.isDisposed()) {
96 if (!display.readAndDispatch()) {
97 display.sleep();
98 }
99 }
100 return result;
101 }
102
103 /**
104 * Create contents of the dialog.
105 */
106 private void createContents() {
107 shlInspectSessions = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.RESIZE);
108 shlInspectSessions.setSize(641, 631);
109 shlInspectSessions.setText("Inspect Sessions");
110 shlInspectSessions.setLayout(new GridLayout(1, false));
111
112 sashForm = new SashForm(shlInspectSessions, SWT.VERTICAL);
113 sashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
114
115 PatternFilter filter = new PatternFilter();
116 FilteredTree tree = new FilteredTree(sashForm, SWT.MULTI | SWT.H_SCROLL | SWT.BORDER
117 | SWT.V_SCROLL, filter, true);
118 treeViewer = tree.getViewer();
119 //treeViewer = new TreeViewer(sashForm, SWT.BORDER);
120 //Tree tree = treeViewer.getTree();
121
122 compositeDebug = new Composite(sashForm, SWT.NONE);
123 compositeDebug.setLayout(new GridLayout(1, false));
124
125 lblDebugInformation = new Label(compositeDebug, SWT.NONE);
126 lblDebugInformation.setAlignment(SWT.CENTER);
127 lblDebugInformation.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1));
128 lblDebugInformation.setFont(SWTResourceManager.getFont("Ubuntu", 10, SWT.NORMAL));
129 lblDebugInformation.setText("Debug Information");
130
131 txtDebugInfo = new Text(compositeDebug, SWT.BORDER | SWT.READ_ONLY | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL | SWT.MULTI);
132 txtDebugInfo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
133 sashForm.setWeights(new int[] {338, 184});
134
135 btnClose = new Button(shlInspectSessions, SWT.NONE);
136 btnClose.addSelectionListener(new SelectionAdapter() {
137 @Override
138 public void widgetSelected(SelectionEvent e) {
139 shlInspectSessions.dispose();
140 }
141 });
142 GridData gd_btnClose = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
143 gd_btnClose.widthHint = 70;
144 btnClose.setLayoutData(gd_btnClose);
145 btnClose.setText("Close");
146
147 }
148
149 private void setDebugInfoText() {
150 txtDebugInfo.setText(ecdr.toString());
151 }
152 private CdmEntityInfo[] getRootElements() {
153
154 List<CdmEntityInfo> rootElemnts = ecdr.getRootElements();
155 return rootElemnts.toArray(new CdmEntityInfo[rootElemnts.size()]);
156 }
157
158 class SessionsTreeContentProvider implements ITreeContentProvider {
159
160 @Override
161 public void dispose() {
162
163 }
164
165 @Override
166 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
167
168 @Override
169 public Object[] getElements(Object inputElement) {
170 return (CdmEntityInfo[])inputElement;
171 }
172
173 @Override
174 public Object[] getChildren(Object parentElement) {
175 List<CdmEntityInfo> children = ((CdmEntityInfo)parentElement).getChildren();
176 return children.toArray();
177 }
178
179 @Override
180 public Object getParent(Object element) {
181 CdmEntityInfo cei = (CdmEntityInfo)element;
182 return cei.getParent();
183 }
184
185 @Override
186 public boolean hasChildren(Object element) {
187 List<CdmEntityInfo> children = ((CdmEntityInfo)element).getChildren();
188 if(children != null && !children.isEmpty()) {
189 return true;
190 } else {
191 return false;
192 }
193 }
194
195 public CdmModelFieldPropertyFromClass getFromCdmlibModelCache(String className) {
196 Element e = cdmlibModelCache.get(className);
197 if (e == null) {
198 return null;
199 } else {
200 return (CdmModelFieldPropertyFromClass) e.getObjectValue();
201 }
202 }
203 }
204
205 class SessionsTreeLabelProvider extends StyledCellLabelProvider implements ILabelProvider {
206
207 @Override
208 public void update(ViewerCell cell) {
209 CdmEntityInfo cei = (CdmEntityInfo)cell.getElement();
210 StyledString text = new StyledString();
211 if(cei != null) {
212 text.append(cei.getLabel());
213 }
214 cell.setText(text.toString());
215 cell.setStyleRanges(text.getStyleRanges());
216 super.update(cell);
217 }
218
219 @Override
220 public Image getImage(Object element) {
221 // TODO Auto-generated method stub
222 return null;
223 }
224
225 @Override
226 public String getText(Object element) {
227 CdmEntityInfo cei = (CdmEntityInfo)element;
228 return cei.getLabel();
229 }
230 }
231 }