Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 769c71af8bd4f8ca896a5452558a42c5a0381026 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*****************************************************************************
 * Copyright (c) 2014 CEA LIST.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *  CEA LIST - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.dev.types.view;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.eclipse.gmf.runtime.emf.type.core.ClientContextManager;
import org.eclipse.gmf.runtime.emf.type.core.ElementTypeRegistry;
import org.eclipse.gmf.runtime.emf.type.core.IClientContext;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.emf.type.core.edithelper.IEditHelperAdvice;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.papyrus.dev.types.providers.ElementTypesContentProvider;
import org.eclipse.papyrus.dev.types.providers.ElementTypesDetailsContentProvider;
import org.eclipse.papyrus.dev.types.providers.ElementTypesDetailsLabelProvider;
import org.eclipse.papyrus.dev.types.providers.ElementTypesLabelProvider;
import org.eclipse.papyrus.infra.types.core.utils.AdviceComparator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.dialogs.FilteredTree;
import org.eclipse.ui.dialogs.PatternFilter;
import org.eclipse.ui.part.ViewPart;

public class RegistredElementTypesView extends ViewPart {

	FilteredTree detailsFilteredTree = null;
	SashForm sash = null;
	FilteredTree elementTypesFilteredTree = null;
	Combo combo = null;
	ElementTypesDetailsContentProvider elementTypesDetailsContentProvider;


	@Override
	public void createPartControl(Composite parent) {
		parent.setLayout(new GridLayout(1, true));

		combo = new Combo(parent, SWT.NONE);
		final List<String> itemsList = new ArrayList<>();
		List<IClientContext> contexts = new ArrayList<IClientContext>(ClientContextManager.getInstance().getClientContexts());

		int index = -1;
		int i = 0;
		for (IClientContext context : contexts) {
			itemsList.add(context.getId());
			if (context.getId().equals(ClientContextManager.getDefaultClientContext().getId())) {
				index = i;
			}
			i++;
		}
		String[] items = new String[itemsList.size()];
		items = itemsList.toArray(items);
		combo.setItems(items);
		if (index != -1) {
			combo.select(index);
		}
		combo.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				int index = combo.getSelectionIndex();

				String clientContexId = itemsList.get(index);

				if (clientContexId != null) {
					IClientContext clientContex = ClientContextManager.getInstance().getClientContext(clientContexId);
					if (clientContex != null) {
						IElementType[] elementTypes = ElementTypeRegistry.getInstance().getElementTypes(clientContex);
						elementTypesDetailsContentProvider.setContextID(clientContex.getId());
						elementTypesFilteredTree.getViewer().setInput(elementTypes);
					}
				}

			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {


			}
		});

		Button exportButton = new Button(parent, SWT.NONE);
		exportButton.setText("Export registry");
		exportButton.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseUp(MouseEvent e) {
				FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.SAVE);
				String dest = dialog.open();
				if (dest != null) {
					File file = new File(dest);
					if (file != null) {
						BufferedWriter writer = null;
						try {
							writer = new BufferedWriter(new FileWriter(file));

							for (String clientContexId : itemsList) {
								writer.write(clientContexId + "\n");
								IElementType[] elementTypes = ElementTypeRegistry.getInstance().getElementTypes(ClientContextManager.getInstance().getClientContext(clientContexId));
								for (int j = 0; j < elementTypes.length; j++) {
									IElementType elementType = elementTypes[j];
									writer.write("\t" + elementType.getId() + "\n");


									IEditHelperAdvice[] advices = ElementTypeRegistry.getInstance().getEditHelperAdvice(elementType);
									List<IEditHelperAdvice> advicesList = Arrays.asList(advices);
									Collections.sort(advicesList, new AdviceComparator(elementType, clientContexId));
									for (IEditHelperAdvice advice : advicesList) {
										writer.write("\t\t" + advice.getClass().getName() + "\n");
									}
								}
							}

							writer.flush();

						} catch (IOException e1) {
							e1.printStackTrace();
						} finally {
							try {
								writer.close();
							} catch (Exception e1) {
								e1.printStackTrace();
							}
						}

					}
				}
			}
		});

		sash = new SashForm(parent, SWT.HORIZONTAL);
		sash.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		elementTypesFilteredTree = new FilteredTree(sash, SWT.BORDER, new PatternFilter(), true);
		elementTypesFilteredTree.getViewer().setLabelProvider(new ElementTypesLabelProvider());
		elementTypesFilteredTree.getViewer().setContentProvider(new ElementTypesContentProvider());
		detailsFilteredTree = new FilteredTree(sash, SWT.BORDER, new PatternFilter(), true);
		detailsFilteredTree.getViewer().setLabelProvider(new ElementTypesDetailsLabelProvider());
		elementTypesDetailsContentProvider = new ElementTypesDetailsContentProvider();
		detailsFilteredTree.getViewer().setContentProvider(elementTypesDetailsContentProvider);

		if (index != -1) {
			combo.select(index);
		}

		elementTypesFilteredTree.getViewer().addSelectionChangedListener(new ISelectionChangedListener() {

			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				if (event.getSelection() instanceof IStructuredSelection) {
					Object selection = ((IStructuredSelection) event.getSelection()).getFirstElement();
					if (selection instanceof IElementType) {
						elementTypesDetailsContentProvider.setTypeID(((IElementType) selection).getId());
						detailsFilteredTree.getViewer().setInput(selection);
					}
				}

			}
		});
	}

	@Override
	public void setFocus() {

	}
}

Back to the top