1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package pl.edu.agh.cast.editor;
19
20 import org.eclipse.core.resources.IProject;
21 import org.eclipse.core.runtime.IConfigurationElement;
22 import org.eclipse.core.runtime.Platform;
23 import org.eclipse.swt.widgets.Display;
24 import org.eclipse.ui.IEditorDescriptor;
25 import org.eclipse.ui.IEditorInput;
26 import org.eclipse.ui.IEditorPart;
27 import org.eclipse.ui.IPerspectiveDescriptor;
28 import org.eclipse.ui.IWorkbenchPage;
29 import org.eclipse.ui.PartInitException;
30 import org.eclipse.ui.PlatformUI;
31
32 import pl.edu.agh.cast.Activator;
33 import pl.edu.agh.cast.CastApplication;
34 import pl.edu.agh.cast.CoreServiceLocator;
35 import pl.edu.agh.cast.data.model.DataSetDescriptor;
36 import pl.edu.agh.cast.data.model.IDataSet;
37 import pl.edu.agh.cast.data.model.IElement;
38 import pl.edu.agh.cast.data.model.Type;
39 import pl.edu.agh.cast.editor.input.ModelEditorInput;
40 import pl.edu.agh.cast.ui.DefaultPerspective;
41 import pl.edu.agh.cast.ui.util.MsgBoxHelper;
42 import pl.edu.agh.cast.util.Messages;
43
44
45
46
47
48
49 public final class EditorUtil {
50
51
52
53
54 private static final String EXTENSION_POINT_ID = "pl.edu.agh.cast.editors";
55
56
57
58
59 public static final String EDITOR_NAME_ATTR_ID = "name";
60
61
62
63
64 public static final String EDITOR_ID_ATTR_ID = "id";
65
66
67
68
69 public static final String EDITOR_TYPEID_ATTR_ID = "typeId";
70
71
72
73
74
75
76
77 public static IConfigurationElement[] getEditorConfiguration() {
78 return Platform.getExtensionRegistry().getConfigurationElementsFor(EXTENSION_POINT_ID);
79 }
80
81
82
83
84
85
86
87
88 public static IEditorPart openNewEditor(DataSetDescriptor descriptor) throws PartInitException {
89
90 String editorId = findEditorId(descriptor.getType());
91 IEditorDescriptor editorDescriptor = PlatformUI.getWorkbench().getEditorRegistry().findEditor(editorId);
92 if (editorDescriptor == null) {
93 MsgBoxHelper.showInfoBox(Display.getCurrent().getActiveShell(), Messages.EditorUtil_2,
94 Messages.EditorUtil_3);
95 return null;
96 }
97
98
99 IDataSet<? extends IElement> model = CoreServiceLocator.getPersistenceProvider().getDataSet(descriptor);
100 if (model == null) {
101 Activator.getLogger().error(
102 String.format("Inconsistent database. Data set for UUID %s was not found!",
103 descriptor.getId().toString()));
104 MsgBoxHelper.showErrorBox(Display.getCurrent().getActiveShell(), Messages.EditorUtil_0,
105 Messages.EditorUtil_4);
106 return null;
107 }
108
109
110
111
112
113
114
115
116 IEditorInput editorInput = new ModelEditorInput<IDataSet<? extends IElement>>(model);
117 IWorkbenchPage workbenchPage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
118
119 switchToDiagramPerspective();
120
121
122
123
124
125
126
127 IEditorPart editorPart = workbenchPage.findEditor(editorInput);
128 if (editorPart == null) {
129 editorPart = workbenchPage.openEditor(editorInput, editorId);
130 } else {
131 workbenchPage.activate(editorPart);
132 }
133
134 return editorPart;
135 }
136
137
138
139
140 public static void switchToDiagramPerspective() {
141 IPerspectiveDescriptor diagramPerspective = PlatformUI.getWorkbench().getPerspectiveRegistry()
142 .findPerspectiveWithId(DefaultPerspective.ID);
143
144 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().setPerspective(diagramPerspective);
145 }
146
147
148
149
150
151
152
153
154 private static String findEditorId(Type type) {
155
156 for (IConfigurationElement conf : getEditorConfiguration()) {
157 if (conf.getAttribute(EDITOR_TYPEID_ATTR_ID).equals(type.toString())) {
158 return conf.getAttribute(EDITOR_ID_ATTR_ID);
159 }
160 }
161
162 return null;
163 }
164
165
166
167
168
169
170 @SuppressWarnings("unused")
171 private static IProject getActiveProject() {
172 IProject project = CastApplication.getActiveProject();
173
174 if (project == null) {
175 MsgBoxHelper.showErrorBox(Display.getCurrent().getActiveShell(), Messages.AbstractOpenEditorAction_0,
176 Messages.AbstractOpenEditorAction_1);
177 }
178
179 return project;
180 }
181
182 }