001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id$
023: */
024: package com.bostechcorp.cbesb.ui.util;
025:
026: import java.util.Collection;
027: import java.util.HashMap;
028: import java.util.Vector;
029:
030: import org.eclipse.ui.IPerspectiveDescriptor;
031: import org.eclipse.ui.IPerspectiveRegistry;
032: import org.eclipse.ui.IWorkbenchPage;
033: import org.eclipse.ui.IWorkbenchWindow;
034: import org.eclipse.ui.PlatformUI;
035: import org.eclipse.ui.part.EditorPart;
036:
037: public class PerspectiveUtil {
038:
039: public static Vector editor_vector = new Vector();
040: public static final String PERSPECTIVE_ID_COMPONENTFLOWEDIT = "com.bostechcorp.cbesb.ui.ide.componentFlowEditorPerspective";
041: public static final String PERSPECTIVE_ID_FORMATMESSAGEEDIT = "com.bostechcorp.cbesb.ui.ide.messageformatperspective";
042: public static final String PERSPECTIVE_ID_TRANSFORMEREDIT = "com.bostechcorp.cbesb.ui.ide.transformerperspective";
043:
044: public static final String PERSPECTIVE_ID_X12EDIT = "com.bostechcorp.cbesb.ui.ide.x12variantperspective";
045: public static final String PERSPECTIVE_ID_HL7EDIT = "com.bostechcorp.cbesb.ui.ide.hl7variantperspective";
046: public static final String PERSPECTIVE_ID_MAPTESTEDIT = "com.bostechcorp.cbesb.ui.ide.perspective.maptestingperspective";
047: public static final String PERSPECTIVE_ID_DATASOURCEMAPPER = "com.bostechcorp.cbesb.ui.ide.datasourcemapperperpective";
048:
049: public static Vector getVector() {
050: if (editor_vector == null) {
051: editor_vector = new Vector();
052: }
053: return editor_vector;
054: }
055:
056: public static void addEditor(EditorPart editor) {
057:
058: int size = editor_vector.size();
059: String ID = null;
060: for (int i = 0; i < size; i++) {
061: HashMap map = (HashMap) editor_vector.get(i);
062: if (map.containsKey(editor)) {
063: ID = (String) map.get(editor);
064: break;
065: }
066: }
067: EditorPart editor_added = (EditorPart) editor;
068: HashMap map = new HashMap();
069: map.put(editor_added, ID);
070: if (editor_vector.contains(map)) {
071: editor_vector.remove(map);
072:
073: }
074:
075: editor_vector.add(map);
076: goToPer(editor_vector, map, editor_added);
077: }
078:
079: public static void addEditor(EditorPart editor, String ID) {
080:
081: if (editor_vector == null) {
082: editor_vector = new Vector();
083: }
084: EditorPart editor_added = (EditorPart) editor;
085: HashMap map = new HashMap();
086: map.put(editor_added, ID);
087: if (editor_vector.contains(map)) {
088: editor_vector.remove(map);
089:
090: }
091:
092: editor_vector.add(map);
093: goToPer(editor_vector, map, editor_added);
094: }
095:
096: public static void removeEditor(EditorPart editor, String ID) {
097: boolean left = false;
098: EditorPart editor_removed = (EditorPart) editor;
099: HashMap map = new HashMap();
100: map.put(editor_removed, ID);
101: if (editor_vector.contains(map)) {
102: editor_vector.remove(map);
103:
104: }
105:
106: for (int i = 0; i < editor_vector.size(); i++) {
107: HashMap map1 = (HashMap) editor_vector.get(i);
108: if (map1.containsValue(ID)) {
109: left = true;
110: break;
111: }
112: }
113:
114: if (!left) {
115: try {
116: IPerspectiveRegistry registry = PlatformUI
117: .getWorkbench().getPerspectiveRegistry();
118: IPerspectiveDescriptor perspectiveDesc = registry
119: .findPerspectiveWithId(ID);
120: IWorkbenchWindow workbench = PlatformUI.getWorkbench()
121: .getActiveWorkbenchWindow();
122: if (workbench != null && perspectiveDesc != null
123: && null != workbench.getActivePage())
124: workbench.getActivePage().closePerspective(
125: perspectiveDesc, false, false);
126: } catch (Exception e) {
127: e.printStackTrace();
128: }
129: }
130:
131: goToPer(editor_vector, map, editor_removed);
132: }
133:
134: public static void goToPer(Vector vector, HashMap last,
135: EditorPart part) {
136: int index = vector.size();
137: if (index != 0) {
138: HashMap editorPart_last = (HashMap) vector.get(index - 1);
139: Collection list = editorPart_last.values();
140: Object[] objects = list.toArray();
141: String ID = objects[0].toString();
142:
143: showPer(ID);
144: } else {
145: try {
146: IPerspectiveRegistry registry = PlatformUI
147: .getWorkbench().getPerspectiveRegistry();
148: IPerspectiveDescriptor perspectiveDesc = registry
149: .findPerspectiveWithId(registry
150: .getDefaultPerspective());
151: IWorkbenchWindow workbench = PlatformUI.getWorkbench()
152: .getActiveWorkbenchWindow();
153: if (workbench != null && perspectiveDesc != null
154: && null != workbench.getActivePage())
155: workbench.getActivePage().setPerspective(
156: perspectiveDesc);
157: } catch (Exception e) {
158: e.printStackTrace();
159: }
160: }
161:
162: }
163:
164: public static void showPer(String part) {
165: activePerspective(part);
166: }
167:
168: public static void activeFlowEditPerspective() {
169: activePerspective(PERSPECTIVE_ID_COMPONENTFLOWEDIT);
170: }
171:
172: public static void activeFormatEditPerspective() {
173: activePerspective(PERSPECTIVE_ID_FORMATMESSAGEEDIT);
174: }
175:
176: public static void activeTransformerEditPerspective() {
177: activePerspective(PERSPECTIVE_ID_TRANSFORMEREDIT);
178: }
179:
180: public static void activeX12EditorPerspective() {
181: activePerspective(PERSPECTIVE_ID_X12EDIT);
182: }
183:
184: public static void activeMapTestingEditorPerspective() {
185: activePerspective(PERSPECTIVE_ID_MAPTESTEDIT);
186: }
187:
188: public static void activeHL7EditorPerspective() {
189: activePerspective(PERSPECTIVE_ID_HL7EDIT);
190: }
191:
192: public static void activeDataSourceMapperEditorPerspective() {
193: activePerspective(PERSPECTIVE_ID_DATASOURCEMAPPER);
194: }
195:
196: public static void activeEditorPerspective(String perspectiveID) {
197: activePerspective(perspectiveID);
198: }
199:
200: private static void activePerspective(String perspectiveID) {
201: IWorkbenchWindow workbench = PlatformUI.getWorkbench()
202: .getActiveWorkbenchWindow();
203:
204: IPerspectiveRegistry registry = PlatformUI.getWorkbench()
205: .getPerspectiveRegistry();
206:
207: IPerspectiveDescriptor perspectiveDesc = registry
208: .findPerspectiveWithId(perspectiveID);
209:
210: if (workbench != null) {
211: IWorkbenchPage activePage = workbench.getActivePage();
212: if (activePage != null) {
213: IPerspectiveDescriptor currentPerspective = activePage
214: .getPerspective();
215:
216: if (currentPerspective != null
217: && (currentPerspective.getId()
218: .equals(perspectiveID)))
219: return;
220: try {
221: activePage.setPerspective(perspectiveDesc);
222: } catch (Exception e) {
223: e.printStackTrace();
224: }
225:
226: }
227: // if (!confirmPerspectiveSwitch(workbench, perspectiveDesc)) {
228: // return;
229: // }
230: }
231: //replaceCurrentPerspective(perspectiveDesc);
232: }
233:
234: /* private static void replaceCurrentPerspective(IPerspectiveDescriptor persp) {
235: IWorkbenchWindow window = PlatformUI.getWorkbench()
236: .getActiveWorkbenchWindow();
237: if (window == null) {
238: return;
239: }
240: IWorkbenchPage page = window.getActivePage();
241: if (page == null) {
242: return;
243: }
244: page.setPerspective(persp);
245: }
246:
247: private static boolean confirmPerspectiveSwitch(IWorkbenchWindow window,
248: IPerspectiveDescriptor finalPersp) {
249: IPreferenceStore store = ComponentFlowEditorDiagramEditorPlugin.getInstance()
250: .getPreferenceStore();
251:
252: String desc = finalPersp.getDescription();
253: final String message = "Drawing a component flow diagram is typically associated with the Component Flow Editor perspective. Do you want to open this perspective now?";
254:
255: boolean result = MessageDialog
256: .openQuestion(window.getShell(),
257: "Open Associated Perspective?", message);
258: return result;
259: }*/
260: }
|