sysmodelmgr/com.symbian.smt.gui/src/com/symbian/smt/gui/SmmPartListener.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 package com.symbian.smt.gui;
       
    17 
       
    18 import java.util.ArrayList;
       
    19 import java.util.HashMap;
       
    20 
       
    21 import org.eclipse.core.resources.IFile;
       
    22 import org.eclipse.core.runtime.CoreException;
       
    23 import org.eclipse.ui.IEditorInput;
       
    24 import org.eclipse.ui.IEditorPart;
       
    25 import org.eclipse.ui.IFileEditorInput;
       
    26 import org.eclipse.ui.IPartListener;
       
    27 import org.eclipse.ui.IPropertyListener;
       
    28 import org.eclipse.ui.IWorkbenchPart;
       
    29 import org.eclipse.ui.PlatformUI;
       
    30 
       
    31 import com.symbian.smt.gui.nature.Nature;
       
    32 
       
    33 final class SmmPartListener implements IPartListener {
       
    34 
       
    35 	private final class PropertyChangeListener implements IPropertyListener {
       
    36 
       
    37 		public void propertyChanged(Object source, int propId) {
       
    38 			if (propId == IEditorPart.PROP_DIRTY) {
       
    39 				if (source instanceof IEditorPart) {
       
    40 					IEditorPart editorPart = (IEditorPart) source;
       
    41 					IEditorInput input = editorPart.getEditorInput();
       
    42 
       
    43 					if (input instanceof IFileEditorInput) {
       
    44 						boolean isEditorDirty = editorPart.isDirty();
       
    45 						String dirty = String.valueOf(isEditorDirty);
       
    46 						IFile file = ((IFileEditorInput) input).getFile();
       
    47 
       
    48 						// If user has saved and if it is a resource file or a
       
    49 						// sys def file,
       
    50 						// need to validate the file and mark it in error
       
    51 						// if it is invalid.
       
    52 						if (!isEditorDirty) {
       
    53 							// save but do not build if validation fails.
       
    54 							if (ManageResources.isResourceFile(file)) {
       
    55 								// TODO:BRS:Remove the if test below when
       
    56 								// Shapes.xsd is available.
       
    57 								if (!file.getParent().getName()
       
    58 										.equals("Shapes")) {
       
    59 									FileValidationHelper
       
    60 											.validateResourceFile(file);
       
    61 								}
       
    62 							} else if (ManageResources
       
    63 									.isSystemDefinitionFile(file)) {
       
    64 								FileValidationHelper.validateSysDefFile(file);
       
    65 							}
       
    66 						}
       
    67 
       
    68 						if (!items.containsKey(file.getProject().getName())) {
       
    69 							String entry[] = { file.getName(), dirty };
       
    70 							ArrayList<String[]> entries = new ArrayList<String[]>();
       
    71 							entries.add(entry);
       
    72 							items.put(file.getProject().getName(), entries);
       
    73 						} else {
       
    74 							ArrayList<String[]> projectFiles = items.get(file
       
    75 									.getProject().getName());
       
    76 
       
    77 							Boolean alreadyExists = false;
       
    78 
       
    79 							for (String[] anEntry : projectFiles) {
       
    80 								if (anEntry[0].equalsIgnoreCase(file.getName())) {
       
    81 									anEntry[1] = dirty;
       
    82 									alreadyExists = true;
       
    83 								}
       
    84 							}
       
    85 
       
    86 							if (!alreadyExists) {
       
    87 								String entry[] = { file.getName(), dirty };
       
    88 								items.get(file.getProject().getName()).add(
       
    89 										entry);
       
    90 							}
       
    91 						}
       
    92 					}
       
    93 				}
       
    94 
       
    95 				// Tell it to do any decorating
       
    96 				PlatformUI.getWorkbench().getDecoratorManager().update(
       
    97 						"com.symbian.smt.gui.outofsyncdecorator");
       
    98 			}
       
    99 		}
       
   100 	}
       
   101 
       
   102 	private PropertyChangeListener listener;
       
   103 
       
   104 	private static HashMap<String, ArrayList<String[]>> items = new HashMap<String, ArrayList<String[]>>();
       
   105 
       
   106 	public SmmPartListener() {
       
   107 		listener = new PropertyChangeListener();
       
   108 	}
       
   109 
       
   110 	private void addListener(IWorkbenchPart part) {
       
   111 		if (part instanceof IEditorPart) {
       
   112 			try {
       
   113 				IEditorInput input = ((IEditorPart) part).getEditorInput();
       
   114 				if (input instanceof IFileEditorInput) {
       
   115 					IFile file = ((IFileEditorInput) input).getFile();
       
   116 
       
   117 					if (file.getProject().isOpen()) {
       
   118 						boolean ourProject = file.getProject().hasNature(
       
   119 								Nature.ID);
       
   120 						if (ourProject && !file.isDerived()) {
       
   121 							part.addPropertyListener(listener);
       
   122 						}
       
   123 					}
       
   124 				}
       
   125 			} catch (CoreException e) {
       
   126 				Logger.log(e.getMessage(), e);
       
   127 			}
       
   128 		}
       
   129 	}
       
   130 
       
   131 	public boolean isInSync(String project) {
       
   132 		// Get the files for this project
       
   133 		ArrayList<String[]> projectFiles = items.get(project);
       
   134 
       
   135 		// Iterate over the resources
       
   136 		if (projectFiles != null) {
       
   137 			for (String[] anEntry : projectFiles) {
       
   138 				if (Boolean.valueOf(anEntry[1])) {
       
   139 					// A file is out of sync, so the project it dirty
       
   140 					return false;
       
   141 				}
       
   142 			}
       
   143 		}
       
   144 
       
   145 		// No sync information exists for the project or all are in sync -
       
   146 		// return true
       
   147 		return true;
       
   148 	}
       
   149 
       
   150 	public void partActivated(IWorkbenchPart part) {
       
   151 		addListener(part);
       
   152 
       
   153 		// if (part instanceof IViewPart) {
       
   154 		// String partId = ((IViewPart) part).getViewSite().getId();
       
   155 		//
       
   156 		// if (partId.equals("org.eclipse.ui.navigator.ProjectExplorer")) {
       
   157 		// FileValidationHelper.showProblemsViewIfNeeded();
       
   158 		// }
       
   159 		// }
       
   160 	}
       
   161 
       
   162 	public void partBroughtToTop(IWorkbenchPart part) {
       
   163 		addListener(part);
       
   164 	}
       
   165 
       
   166 	public void partClosed(IWorkbenchPart part) {
       
   167 		part.removePropertyListener(listener);
       
   168 
       
   169 		if (part instanceof IEditorPart) {
       
   170 			IEditorInput input = ((IEditorPart) part).getEditorInput();
       
   171 			if (input instanceof IFileEditorInput) {
       
   172 				IFile file = ((IFileEditorInput) input).getFile();
       
   173 
       
   174 				if (items.containsKey(file.getProject().getName())) {
       
   175 					ArrayList<String[]> projectFiles = items.get(file
       
   176 							.getProject().getName());
       
   177 
       
   178 					for (String[] anEntry : projectFiles) {
       
   179 						if (anEntry[0].equalsIgnoreCase(file.getName())) {
       
   180 							projectFiles.remove(anEntry);
       
   181 							break;
       
   182 						}
       
   183 					}
       
   184 				}
       
   185 			}
       
   186 
       
   187 			PlatformUI.getWorkbench().getDecoratorManager().update(
       
   188 					"com.symbian.smt.gui.outofsyncdecorator");
       
   189 		}
       
   190 	}
       
   191 
       
   192 	public void partDeactivated(IWorkbenchPart part) {
       
   193 	}
       
   194 
       
   195 	public void partOpened(IWorkbenchPart part) {
       
   196 		addListener(part);
       
   197 	}
       
   198 }