Project

General

Profile

Download (7.81 KB) Statistics
| Branch: | Tag: | Revision:
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
        //TODO
79
        ecdr = activeSession.debug(false);
80
    }
81

    
82
    /**
83
     * Open the dialog.
84
     * @return the result
85
     */
86
    public Object open() {
87

    
88
        createContents();
89
        setDebugInfoText();
90
        treeViewer.setContentProvider(new SessionsTreeContentProvider());
91
        treeViewer.setLabelProvider(new SessionsTreeLabelProvider());
92
        treeViewer.setInput(getRootElements());
93
        shlInspectSessions.open();
94
        shlInspectSessions.layout();
95
        Display display = getParent().getDisplay();
96
        while (!shlInspectSessions.isDisposed()) {
97
            if (!display.readAndDispatch()) {
98
                display.sleep();
99
            }
100
        }
101
        return result;
102
    }
103

    
104
    /**
105
     * Create contents of the dialog.
106
     */
107
    private void createContents() {
108
        shlInspectSessions = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.RESIZE);
109
        shlInspectSessions.setSize(641, 631);
110
        shlInspectSessions.setText("Inspect Sessions");
111
        shlInspectSessions.setLayout(new GridLayout(1, false));
112

    
113
        sashForm = new SashForm(shlInspectSessions, SWT.VERTICAL);
114
        sashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
115

    
116
        PatternFilter filter = new PatternFilter();
117
        FilteredTree tree = new FilteredTree(sashForm, SWT.MULTI | SWT.H_SCROLL | SWT.BORDER
118
                | SWT.V_SCROLL, filter, true);
119
        treeViewer = tree.getViewer();
120
        //treeViewer = new TreeViewer(sashForm, SWT.BORDER);
121
        //Tree tree = treeViewer.getTree();
122

    
123
        compositeDebug = new Composite(sashForm, SWT.NONE);
124
        compositeDebug.setLayout(new GridLayout(1, false));
125

    
126
        lblDebugInformation = new Label(compositeDebug, SWT.NONE);
127
        lblDebugInformation.setAlignment(SWT.CENTER);
128
        lblDebugInformation.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1));
129
        lblDebugInformation.setFont(SWTResourceManager.getFont("Ubuntu", 10, SWT.NORMAL));
130
        lblDebugInformation.setText("Debug Information");
131

    
132
        txtDebugInfo = new Text(compositeDebug, SWT.BORDER | SWT.READ_ONLY | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL | SWT.MULTI);
133
        txtDebugInfo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
134
        sashForm.setWeights(new int[] {338, 184});
135

    
136
        btnClose = new Button(shlInspectSessions, SWT.NONE);
137
        btnClose.addSelectionListener(new SelectionAdapter() {
138
            @Override
139
            public void widgetSelected(SelectionEvent e) {
140
                shlInspectSessions.dispose();
141
            }
142
        });
143
        GridData gd_btnClose = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
144
        gd_btnClose.widthHint = 70;
145
        btnClose.setLayoutData(gd_btnClose);
146
        btnClose.setText("Close");
147

    
148
    }
149

    
150
    private void setDebugInfoText() {
151
        txtDebugInfo.setText(ecdr.toString());
152
    }
153
    private CdmEntityInfo[] getRootElements() {
154

    
155
        List<CdmEntityInfo> rootElemnts = ecdr.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
}
(2-2/3)