View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: EditorUtil.java
13   * Created: 2009-08-05
14   * Author: czerwin
15   * $Id$
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   * Editor's utilities.
46   * 
47   * @author AGH CAST Team
48   */
49  public final class EditorUtil {
50  
51  	/**
52  	 * Editor extensions ID.
53  	 */
54  	private static final String EXTENSION_POINT_ID = "pl.edu.agh.cast.editors"; //$NON-NLS-1$
55  
56  	/**
57  	 * ID of the <em>name</em> attribute of <code>pl.edu.agh.cast.editor</code> extension point element.
58  	 */
59  	public static final String EDITOR_NAME_ATTR_ID = "name"; //$NON-NLS-1$
60  
61  	/**
62  	 * ID of the <em>id</em> attribute of <code>pl.edu.agh.cast.editor</code> extension point element.
63  	 */
64  	public static final String EDITOR_ID_ATTR_ID = "id"; //$NON-NLS-1$
65  
66  	/**
67  	 * ID of the <em>typeId</em> attribute of <code>pl.edu.agh.cast.editor</code> extension point element.
68  	 */
69  	public static final String EDITOR_TYPEID_ATTR_ID = "typeId"; //$NON-NLS-1$
70  
71  	/**
72  	 * Returns all configuration elements from all extensions defined for <code>pl.edu.agh.cast.editor</code> extension
73  	 * point.
74  	 * 
75  	 * @return array of configuration elements
76  	 */
77  	public static IConfigurationElement[] getEditorConfiguration() {
78  		return Platform.getExtensionRegistry().getConfigurationElementsFor(EXTENSION_POINT_ID);
79  	}
80  
81  	/**
82  	 * Opens new editor for {@link pl.edu.agh.cast.data.model.DataSetDescriptor}.
83  	 * 
84  	 * @param descriptor
85  	 *            data set descriptor
86  	 * @return editor part
87  	 */
88  	public static IEditorPart openNewEditor(DataSetDescriptor descriptor) throws PartInitException {
89  		// find editor ID compatible with type provided in descriptor
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  		// get model
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!", //$NON-NLS-1$
103 			        descriptor.getId().toString()));
104 			MsgBoxHelper.showErrorBox(Display.getCurrent().getActiveShell(), Messages.EditorUtil_0,
105 			        Messages.EditorUtil_4);
106 			return null;
107 		}
108 
109 		// get diagram info
110 //		ProjectUtil.NewDiagramInfo diagramInfo = ProjectUtil.getInstance().getNewDiagramInfo(getActiveProject());
111 //		if (diagramInfo.cancelled) {
112 //			return null;
113 //		}
114 
115 		// create editor input, get workbench
116 		IEditorInput editorInput = new ModelEditorInput<IDataSet<? extends IElement>>(model);
117 		IWorkbenchPage workbenchPage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
118 
119 		switchToDiagramPerspective();
120 
121 //		ProjectStore modelStore = ProjectStore.getInstance();
122 //		if (!modelStore.isInitialized(project)) {
123 //			modelStore.init(project);
124 //		}
125 
126 		// determine if new editor should be opened or restored
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 	 * Method is switching to default workbench perspective.
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 	 * Finds editor compatible with provided type.
149 	 * 
150 	 * @param type
151 	 *            type which should be compatible with editor
152 	 * @return editor ID
153 	 */
154 	private static String findEditorId(Type type) {
155 		// find explicit editor ID and type ID mapping (without checking subtypes)
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 	 * Returns active project. If there is no active project message box will be shown.
167 	 * 
168 	 * @return active project
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 }