sysmodelmgr/com.symbian.smt.gui/src/com/symbian/smt/gui/FileValidationHelper.java
changeset 0 522a326673b6
equal deleted inserted replaced
-1:000000000000 0:522a326673b6
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 
       
    17 
       
    18 package com.symbian.smt.gui;
       
    19 
       
    20 import org.eclipse.core.resources.IFile;
       
    21 import org.eclipse.core.resources.IMarker;
       
    22 import org.eclipse.core.resources.IResource;
       
    23 import org.eclipse.core.runtime.CoreException;
       
    24 import org.eclipse.jface.dialogs.MessageDialog;
       
    25 import org.eclipse.ui.IWorkbenchPage;
       
    26 import org.eclipse.ui.IWorkbenchWindow;
       
    27 import org.eclipse.ui.PartInitException;
       
    28 import org.eclipse.ui.PlatformUI;
       
    29 
       
    30 /**
       
    31  * @author barbararosi-schwartz
       
    32  * 
       
    33  */
       
    34 public class FileValidationHelper {
       
    35 
       
    36 	private static void createErrorMarker(IResource resource,
       
    37 			String errorMessage) {
       
    38 		IMarker marker;
       
    39 		try {
       
    40 			marker = resource.createMarker(IMarker.PROBLEM);
       
    41 
       
    42 			if (marker.exists()) {
       
    43 				marker.setAttribute(IMarker.MESSAGE, errorMessage);
       
    44 				marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
       
    45 				marker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_HIGH);
       
    46 			}
       
    47 		} catch (CoreException e) {
       
    48 			Logger.log("Cannot create error marker for resource ["
       
    49 					+ resource.getName() + "].", e);
       
    50 		}
       
    51 	}
       
    52 
       
    53 	private static void deleteErrorMarkers(IResource resource) {
       
    54 		try {
       
    55 			resource
       
    56 					.deleteMarkers(IMarker.PROBLEM, false, IResource.DEPTH_ZERO);
       
    57 		} catch (CoreException e) {
       
    58 			Logger.log("Cannot delete error marker for resource ["
       
    59 					+ resource.getName() + "].", e);
       
    60 		}
       
    61 	}
       
    62 
       
    63 	private static void showProblemsView() {
       
    64 		IWorkbenchWindow window = PlatformUI.getWorkbench()
       
    65 				.getActiveWorkbenchWindow();
       
    66 
       
    67 		if (window == null) {
       
    68 			return;
       
    69 		}
       
    70 
       
    71 		IWorkbenchPage page = window.getActivePage();
       
    72 
       
    73 		if (page == null) {
       
    74 			return;
       
    75 		}
       
    76 
       
    77 		try {
       
    78 			page.showView("org.eclipse.ui.views.ProblemView");
       
    79 		} catch (PartInitException e) {
       
    80 			String message = "Could not open the Problems View. Reason: "
       
    81 					+ e.getMessage();
       
    82 			String title = "Error";
       
    83 
       
    84 			MessageDialog.openError(window.getShell(), title, message);
       
    85 		}
       
    86 	}
       
    87 
       
    88 	private static void communicateCoreException(CoreException e) {
       
    89 		Logger.log(e.getMessage(), e);
       
    90 		
       
    91 		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
       
    92 
       
    93 		if (window == null) {
       
    94 			return;
       
    95 		}
       
    96 
       
    97 		String message = "Problem encountered in locating errors in the workspace. Reason: "
       
    98 				+ e.getMessage();
       
    99 		String title = "Error";
       
   100 		
       
   101 		MessageDialog.openError(window.getShell(), title, message);
       
   102 	}
       
   103 	
       
   104 	public static void validateResourceFile(IFile file) {
       
   105 		ResourcesEnums resourceType = ManageResources.getResourceType(file);
       
   106 
       
   107 		if (resourceType != null) {
       
   108 			ResourceFileValidator validator = new ResourceFileValidator(
       
   109 					resourceType);
       
   110 			String errorMessage;
       
   111 			
       
   112 			try {
       
   113 				String url = ManageResources.getResourceUrl(file);
       
   114 			
       
   115 				if (url == null) {
       
   116 					errorMessage = validator.validateXml(file.getLocation()
       
   117 							.toOSString());
       
   118 				}
       
   119 				else {
       
   120 					errorMessage = validator.validateXml(url);
       
   121 				}
       
   122 			
       
   123 				deleteErrorMarkers(file);
       
   124 	
       
   125 				if (errorMessage != null) {
       
   126 					createErrorMarker(file, errorMessage);
       
   127 					PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
       
   128 						public void run() {
       
   129 							showProblemsView();
       
   130 						}
       
   131 					});
       
   132 					
       
   133 				}
       
   134 			} catch (CoreException e) {
       
   135 				communicateCoreException(e);
       
   136 			}
       
   137 		}
       
   138 	}
       
   139 
       
   140 	public static void validateSysDefFile(IFile file) {
       
   141 		XmlFileValidator validator = new XmlFileValidator();
       
   142 		String errorMessage;
       
   143 		
       
   144 		try {
       
   145 			String url = ManageResources.getResourceUrl(file);
       
   146 			
       
   147 			if (url == null) {
       
   148 				errorMessage = validator.validateXml(file.getLocation()
       
   149 					.toOSString());
       
   150 			}
       
   151 			else {
       
   152 				errorMessage = validator.validateXml(url);
       
   153 			}
       
   154 			
       
   155 			deleteErrorMarkers(file);
       
   156 			
       
   157 			if (errorMessage != null) {
       
   158 				createErrorMarker(file, errorMessage);
       
   159 				PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().getDisplay().asyncExec(new Runnable() {
       
   160 					public void run() {
       
   161 						showProblemsView();
       
   162 					}
       
   163 				});
       
   164 			}
       
   165 		} catch (CoreException e) {
       
   166 			communicateCoreException(e);
       
   167 		}
       
   168 	}
       
   169 }