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