crashanalysis/crashanalyser/com.nokia.s60tools.crashanalyser/src/com/nokia/s60tools/crashanalyser/model/EditorHandler.java
changeset 0 5ad7ad99af01
child 4 615035072f7e
equal deleted inserted replaced
-1:000000000000 0:5ad7ad99af01
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 package com.nokia.s60tools.crashanalyser.model;
       
    19 
       
    20 import java.util.List;
       
    21 import org.eclipse.ui.IWorkbench;
       
    22 import org.eclipse.ui.IWorkbenchPage;
       
    23 import org.eclipse.ui.IWorkbenchWindow;
       
    24 import org.eclipse.ui.PlatformUI;
       
    25 import org.eclipse.ui.IEditorReference;
       
    26 import org.eclipse.ui.IEditorPart;
       
    27 import com.nokia.s60tools.crashanalyser.files.*;
       
    28 
       
    29 /**
       
    30  * Handles opening and closing of Crash Visualizer editors
       
    31  *
       
    32  */
       
    33 public final class EditorHandler {
       
    34 	private EditorHandler() {
       
    35 		// not meant to be implemented
       
    36 	}
       
    37 	
       
    38 	/**
       
    39 	 * Closes all open editors which are Crash Analyser editors
       
    40 	 */
       
    41 	public static void closeAllEditors() {
       
    42 		try {
       
    43 	    	IWorkbench workbench = PlatformUI.getWorkbench();   
       
    44 	    	if (workbench == null)
       
    45 	    		return;
       
    46 	    	
       
    47 	    	IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
       
    48 	    	if (window == null)
       
    49 	    		return;
       
    50 	    	
       
    51 	    	String editorId = "com.nokia.s60tools.crashanalyser.ui.editors.MultiPageEditor";
       
    52 	    	IWorkbenchPage page = window.getActivePage();
       
    53 	    	if (page == null)
       
    54 	    		return;
       
    55 	    	
       
    56 	    	IEditorReference[] editorReferences = page.getEditorReferences();
       
    57 	    	if (editorReferences == null || editorReferences.length < 1)
       
    58 	    		return;
       
    59 	    	
       
    60 	    	// go throug all editors
       
    61     		for (int i = 0; i < editorReferences.length; i++) {
       
    62     			IEditorReference ref = editorReferences[i];
       
    63     			String openEditorId = ref.getId();
       
    64     			// if editor is Crash Analyser editor, close it
       
    65     	    	if (editorId.compareToIgnoreCase(openEditorId) == 0) {
       
    66 	    			page.closeEditor(ref.getEditor(true), true);
       
    67     	    	}
       
    68     		}
       
    69 	    	
       
    70 		} catch (Exception e) {
       
    71 			e.printStackTrace();
       
    72 		}
       
    73 	}
       
    74 	
       
    75 	/**
       
    76 	 * Checks if given file is already open in editor area. If it is, that editor is
       
    77 	 * brought as top most editor.
       
    78 	 * @param file file which is to be checked
       
    79 	 * @return true if editor was found and brought to top most, false if editor was not found
       
    80 	 * or an error occurred
       
    81 	 */
       
    82 	static boolean bringEditorOnTopIfFileAlreadyOpen(CrashAnalyserFile file) {
       
    83 		try {
       
    84 			if (file == null)
       
    85 				return false;
       
    86 			
       
    87 			// get workbench
       
    88 	    	IWorkbench workbench = PlatformUI.getWorkbench();
       
    89 	    	if (workbench == null)
       
    90 	    		return false;
       
    91 	    	
       
    92 	    	// get workbench window
       
    93 	    	IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
       
    94 	    	if (window == null)
       
    95 	    		return false;
       
    96 	    	
       
    97 	    	String editorId = "com.nokia.s60tools.crashanalyser.ui.editors.MultiPageEditor";
       
    98 	    	
       
    99 	    	// get workbench page
       
   100 	    	IWorkbenchPage page = window.getActivePage();
       
   101 	    	if (page == null)
       
   102 	    		return false;
       
   103 	    	
       
   104 	    	// get open editors
       
   105 	    	IEditorReference[] editorReferences = page.getEditorReferences();
       
   106 	    	if (editorReferences == null || editorReferences.length < 1)
       
   107 	    		return false;
       
   108 	    	
       
   109 	    	// go through all open editors
       
   110     		for (int i = 0; i < editorReferences.length; i++) {
       
   111     			IEditorReference ref = editorReferences[i];
       
   112     			String openEditorId = ref.getId();
       
   113     			// if editor is Crash Analyser editor
       
   114     	    	if (editorId.compareToIgnoreCase(openEditorId) == 0) {
       
   115     	    		IEditorPart part = ref.getEditor(true);
       
   116     	    		if (part == null)
       
   117     	    			continue;
       
   118     	    		
       
   119     	    		String title = part.getTitle();
       
   120 	    			if (title.equalsIgnoreCase(file.getFileName())) {
       
   121 	    				page.bringToTop(part);
       
   122 	    				return true;
       
   123 	    			}
       
   124     	    	}
       
   125     		}
       
   126 	    	
       
   127 		} catch (Exception e) {
       
   128 			e.printStackTrace();
       
   129 		}
       
   130 		
       
   131 		return false;
       
   132 	}
       
   133 	
       
   134 	/**
       
   135 	 * Closes all given editors
       
   136 	 */
       
   137 	public static void closeEditors(List<CrashFileBundle> files) {
       
   138 		try {
       
   139 			if (files == null || files.isEmpty())
       
   140 				return;
       
   141 			
       
   142 			// get workbench
       
   143 	    	IWorkbench workbench = PlatformUI.getWorkbench();
       
   144 	    	if (workbench == null)
       
   145 	    		return;
       
   146 	    	
       
   147 	    	// get workbench window
       
   148 	    	IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
       
   149 	    	if (window == null)
       
   150 	    		return;
       
   151 	    	
       
   152 	    	String editorId = "com.nokia.s60tools.crashanalyser.ui.editors.MultiPageEditor";
       
   153 	    	
       
   154 	    	// get workbench page
       
   155 	    	IWorkbenchPage page = window.getActivePage();
       
   156 	    	if (page == null)
       
   157 	    		return;
       
   158 	    	
       
   159 	    	// get open editors
       
   160 	    	IEditorReference[] editorReferences = page.getEditorReferences();
       
   161 	    	if (editorReferences == null || editorReferences.length < 1)
       
   162 	    		return;
       
   163 	    	
       
   164 	    	// go through all open editors
       
   165     		for (int i = 0; i < editorReferences.length; i++) {
       
   166     			IEditorReference ref = editorReferences[i];
       
   167     			String openEditorId = ref.getId();
       
   168     			// if editor is Crash Analyser editor
       
   169     	    	if (editorId.compareToIgnoreCase(openEditorId) == 0) {
       
   170     	    		IEditorPart part = ref.getEditor(true);
       
   171     	    		if (part == null)
       
   172     	    			continue;
       
   173     	    		
       
   174     	    		String title = part.getTitle();
       
   175     	    		boolean close = false;
       
   176     	    		
       
   177     	    		// go through all given files and close editor for those
       
   178     	    		for (int j = 0; j < files.size(); j++) {
       
   179     	    			CrashFileBundle file = files.get(j);
       
   180     	    			if (title.equalsIgnoreCase(file.getAnalyzeFileName())) {
       
   181     	    				close = true;
       
   182     	    				break;
       
   183     	    			}
       
   184     	    		}
       
   185     	    		if (close)
       
   186     	    			page.closeEditor(ref.getEditor(true), true);
       
   187     	    	}
       
   188     		}
       
   189 	    	
       
   190 		} catch (Exception e) {
       
   191 			e.printStackTrace();
       
   192 		}
       
   193 	}
       
   194 	
       
   195 	/**
       
   196 	 * Opens up given crash analyser file to a Crash Analyser editor.
       
   197 	 * @param file file to be opened
       
   198 	 * @return true if success, false if not
       
   199 	 */
       
   200 	private static boolean openEditor(CrashAnalyserFile file) {
       
   201 		try {
       
   202 			if (bringEditorOnTopIfFileAlreadyOpen(file))
       
   203 				return true;
       
   204 			
       
   205 	    	IWorkbench workbench = PlatformUI.getWorkbench();      
       
   206 	    	if (workbench == null)
       
   207 	    		return false;
       
   208 	    	
       
   209 	    	IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
       
   210 	    	if (window == null)
       
   211 	    		return false;
       
   212 	    	
       
   213 	    	String editorId = "com.nokia.s60tools.crashanalyser.ui.editors.MultiPageEditor";
       
   214 	    	IWorkbenchPage page = window.getActivePage();
       
   215 	    	if (page == null)
       
   216 	    		return false;
       
   217 	    	
       
   218     		if (file instanceof CrashFile)
       
   219     			page.openEditor((CrashFile)file,editorId );
       
   220     		else if (file instanceof SummaryFile)
       
   221     			page.openEditor((SummaryFile)file,editorId );
       
   222     		else
       
   223     			return false;
       
   224     		
       
   225     		return true;
       
   226 
       
   227 		} catch (Exception e) {
       
   228 			e.printStackTrace();
       
   229 		}
       
   230 		return false;
       
   231 	}
       
   232 	
       
   233 	/**
       
   234 	 * Opens given full crash file to a Crash Analyser editor.
       
   235 	 * @param file file to be opened
       
   236 	 * @return true if success, false if not
       
   237 	 */
       
   238 	public static boolean openCrashAnalyserEditor(CrashFile file) {
       
   239 		return openEditor(file);
       
   240 	}
       
   241 	
       
   242 	/**
       
   243 	 * Opens given summary crash file to a Crash Analyser editor.
       
   244 	 * @param file file to be opened
       
   245 	 * @return true if success, false if not
       
   246 	 */
       
   247 	public static boolean openCrashAnalyserEditor(SummaryFile file) {
       
   248 		return openEditor(file);
       
   249 	}
       
   250 }