sysperfana/analyzetoolext/com.nokia.s60tools.analyzetool/src/com/nokia/s60tools/analyzetool/builder/BuilderUtil.java
changeset 15 0367d2db2c06
parent 6 f65f740e69f9
equal deleted inserted replaced
14:bb339882c6e9 15:0367d2db2c06
    21 import org.eclipse.core.resources.IProjectDescription;
    21 import org.eclipse.core.resources.IProjectDescription;
    22 import org.eclipse.core.runtime.CoreException;
    22 import org.eclipse.core.runtime.CoreException;
    23 
    23 
    24 /**
    24 /**
    25  * Add and removes AnalyzeTool build natures to project natures.
    25  * Add and removes AnalyzeTool build natures to project natures.
    26  *
    26  * 
    27  * @author kihe
    27  * @author kihe
    28  *
    28  * 
    29  */
    29  */
    30 public class BuilderUtil {
    30 public class BuilderUtil {
    31 
    31 
    32 	/**
    32 	/**
    33 	 * Constructor.
    33 	 * Constructor.
    37 		// @containsComment = 'false']
    37 		// @containsComment = 'false']
    38 	}
    38 	}
    39 
    39 
    40 	/**
    40 	/**
    41 	 * Adds AnalyzeTool build natures.
    41 	 * Adds AnalyzeTool build natures.
    42 	 *
    42 	 * 
    43 	 * @param project
    43 	 * @param project
    44 	 *            Project reference
    44 	 *            Project reference
    45 	 * @return True no errors otherwise False
    45 	 * @return True no errors otherwise False
    46 	 */
    46 	 */
    47 	private boolean addAnalysisNatures(final IProject project) {
    47 	private boolean addAnalysisNatures(final IProject project) {
    54 			IProjectDescription description = project.getDescription();
    54 			IProjectDescription description = project.getDescription();
    55 
    55 
    56 			// create array for the new natures
    56 			// create array for the new natures
    57 			String[] newNatures = new String[natures.length + 2];
    57 			String[] newNatures = new String[natures.length + 2];
    58 
    58 
    59 
    59 			// if QT nature found we must adjust pre- and post natures to
    60 			//if QT nature found we must adjust pre- and post natures to correct place
    60 			// correct place
    61 			if( description.hasNature(com.trolltech.qtcppproject.QtNature.QT_NATURE_ID) ) {
    61 			if (description
    62 
    62 					.hasNature(com.trolltech.qtcppproject.QtNature.QT_NATURE_ID)) {
    63 				//find QT nature location
    63 
       
    64 				// find QT nature location
    64 				int qtNatureIndex = 0;
    65 				int qtNatureIndex = 0;
    65 				for( int i=0; i<natures.length; i++ ) {
    66 				for (int i = 0; i < natures.length; i++) {
    66 					if( natures[i].equals(com.trolltech.qtcppproject.QtNature.QT_NATURE_ID ) ) {
    67 					if (natures[i]
       
    68 							.equals(com.trolltech.qtcppproject.QtNature.QT_NATURE_ID)) {
    67 						qtNatureIndex = i;
    69 						qtNatureIndex = i;
    68 						break;
    70 						break;
    69 					}
    71 					}
    70 				}
    72 				}
    71 
    73 
    72 				//QT nature id found and it is first nature=> now start to copy existing id and add AT id
    74 				// QT nature id found and it is first nature=> now start to copy
    73 				if( qtNatureIndex == 0 ) {
    75 				// existing id and add AT id
    74 					//add natures
    76 				if (qtNatureIndex == 0) {
       
    77 					// add natures
    75 					newNatures[0] = natures[0];
    78 					newNatures[0] = natures[0];
    76 					newNatures[1] = PreNature.NATURE_ID;
    79 					newNatures[1] = PreNature.NATURE_ID;
    77 
    80 
    78 					//copy rest of the existing natures
    81 					// copy rest of the existing natures
    79 					System.arraycopy(natures, 1, newNatures, 2, natures.length-1);
    82 					System.arraycopy(natures, 1, newNatures, 2,
       
    83 							natures.length - 1);
    80 
    84 
    81 					// add post-builder nature
    85 					// add post-builder nature
    82 					newNatures[natures.length + 1] = PostNature.NATURE_ID;
    86 					newNatures[natures.length + 1] = PostNature.NATURE_ID;
    83 				}
    87 				}
    84 				//QT nature id found but there are some other natures
    88 				// QT nature id found but there are some other natures
    85 				//before QT nature
    89 				// before QT nature
    86 				else {
    90 				else {
    87 					//copy existing natures
    91 					// copy existing natures
    88 					System.arraycopy(natures, 0, newNatures, 0, qtNatureIndex+1);
    92 					System.arraycopy(natures, 0, newNatures, 0,
    89 					newNatures[qtNatureIndex+1] = PreNature.NATURE_ID;
    93 							qtNatureIndex + 1);
    90 
    94 					newNatures[qtNatureIndex + 1] = PreNature.NATURE_ID;
    91 					//copy rest of the existing natures
    95 
    92 					System.arraycopy(natures, qtNatureIndex+1, newNatures, qtNatureIndex+2, natures.length-qtNatureIndex);
    96 					// copy rest of the existing natures
       
    97 					System.arraycopy(natures, qtNatureIndex + 1, newNatures,
       
    98 							qtNatureIndex + 2, natures.length - qtNatureIndex);
    93 
    99 
    94 					// add post-builder nature
   100 					// add post-builder nature
    95 					newNatures[natures.length + 1] = PostNature.NATURE_ID;
   101 					newNatures[natures.length + 1] = PostNature.NATURE_ID;
    96 				}
   102 				}
    97 			}
   103 			}
    98 			//no QT nature found just add pre nature first and post nature last
   104 			// no QT nature found just add pre nature first and post nature last
    99 			else {
   105 			else {
   100 
   106 
   101 				// set pre-builder nature
   107 				// set pre-builder nature
   102 				newNatures[0] = PreNature.NATURE_ID;
   108 				newNatures[0] = PreNature.NATURE_ID;
   103 
   109 
   117 			ce.printStackTrace();
   123 			ce.printStackTrace();
   118 			return false;
   124 			return false;
   119 		}
   125 		}
   120 	}
   126 	}
   121 
   127 
   122 
       
   123 
       
   124 	/**
   128 	/**
   125 	 * Disable AnalyzeTool build natures.
   129 	 * Disable AnalyzeTool build natures.
   126 	 *
   130 	 * 
   127 	 * @param project
   131 	 * @param project
   128 	 *            Project reference
   132 	 *            Project reference
   129 	 */
   133 	 */
   130 	public final void disableNatures(final IProject project) {
   134 	public final void disableNatures(final IProject project) {
   131 		try {
   135 		try {
   139 				boolean foundPreNature = description
   143 				boolean foundPreNature = description
   140 						.hasNature(PreNature.NATURE_ID);
   144 						.hasNature(PreNature.NATURE_ID);
   141 				boolean foundPostNature = description
   145 				boolean foundPostNature = description
   142 						.hasNature(PostNature.NATURE_ID);
   146 						.hasNature(PostNature.NATURE_ID);
   143 
   147 
   144 				//either pre- or post builder nature found => remove it
   148 				// either pre- or post builder nature found => remove it
   145 				if (foundPreNature || foundPostNature) {
   149 				if (foundPreNature || foundPostNature) {
   146 					removeNature(project, 1);
   150 					removeNature(project, 1);
   147 				}
   151 				}
   148 
   152 			}
   149 			}
   153 		} catch (CoreException ce) {
   150 		} catch (CoreException ce) {
   154 			ce.printStackTrace();
   151 			ce.printStackTrace();
   155 		}
   152 		}
       
   153 
       
   154 	}
   156 	}
   155 
   157 
   156 	/**
   158 	/**
   157 	 * Add AnalyzeTool custom builder nature to project builder natures.
   159 	 * Add AnalyzeTool custom builder nature to project builder natures.
   158 	 *
   160 	 * 
   159 	 * @param project
   161 	 * @param project
   160 	 *            Project reference
   162 	 *            Project reference
   161 	 * @return True if natures are added otherwise False
   163 	 * @return True if natures are added otherwise False
   162 	 */
   164 	 */
   163 	public final boolean enableNatures(final IProject project) {
   165 	public final boolean enableNatures(final IProject project) {
   164 		try {
   166 		try {
   165 			// check is nature enable
   167 			// check is nature enable
   166 			if( isNatureEnabled(project)) {
   168 			if (isNatureEnabled(project)) {
   167 				return true;
   169 				return true;
   168 			}
   170 			}
   169 			// get project description
   171 			// get project description
   170 			IProjectDescription description = project.getDescription();
   172 			IProjectDescription description = project.getDescription();
   171 
   173 
   172 			// find natures
   174 			// find natures
   173 			boolean foundPreNature = description
   175 			boolean foundPreNature = description.hasNature(PreNature.NATURE_ID);
   174 					.hasNature(PreNature.NATURE_ID);
       
   175 			boolean foundPostNature = description
   176 			boolean foundPostNature = description
   176 					.hasNature(PostNature.NATURE_ID);
   177 					.hasNature(PostNature.NATURE_ID);
   177 
   178 
   178 			// only one analyzetool nature found => remove it
   179 			// only one analyzetool nature found => remove it
   179 			if (foundPostNature || foundPreNature) {
   180 			if (foundPostNature || foundPreNature) {
   189 		}
   190 		}
   190 	}
   191 	}
   191 
   192 
   192 	/**
   193 	/**
   193 	 * Checks is AnalyzeTool custom nature enabled.
   194 	 * Checks is AnalyzeTool custom nature enabled.
   194 	 *
   195 	 * 
   195 	 * @param projRef
   196 	 * @param projRef
   196 	 *            Project reference
   197 	 *            Project reference
   197 	 * @return True both pre or post nature enabled otherwise False
   198 	 * @return True both pre or post nature enabled otherwise False
   198 	 */
   199 	 */
   199 	public final boolean isNatureEnabled(final IProject projRef) {
   200 	public final boolean isNatureEnabled(final IProject projRef) {
   200 		boolean preNatureFound = false;
   201 		boolean preNatureFound = false;
   201 		boolean postNatureFound = false;
   202 		boolean postNatureFound = false;
   202 
   203 
   203 		//check project validity
   204 		// check project validity
   204 		if (projRef == null || !projRef.isOpen()) {
   205 		if (projRef == null || !projRef.isOpen()) {
   205 			return false;
   206 			return false;
   206 		}
   207 		}
   207 
   208 
   208 		try {
   209 		try {
   214 		}
   215 		}
   215 		if (preNatureFound && postNatureFound) {
   216 		if (preNatureFound && postNatureFound) {
   216 			return true;
   217 			return true;
   217 		}
   218 		}
   218 		return false;
   219 		return false;
   219 
       
   220 	}
   220 	}
   221 
   221 
   222 	/**
   222 	/**
   223 	 * Removes AnalyzeTool custom builder natures.
   223 	 * Removes AnalyzeTool custom builder natures.
   224 	 *
   224 	 * 
   225 	 * @param project
   225 	 * @param project
   226 	 *            Project reference
   226 	 *            Project reference
   227 	 * @param count
   227 	 * @param count
   228 	 *            How many natures to remove
   228 	 *            How many natures to remove
   229 	 */
   229 	 */
   233 			String[] natures = desc.getNatureIds();
   233 			String[] natures = desc.getNatureIds();
   234 			String[] newNatures = new String[natures.length - count];
   234 			String[] newNatures = new String[natures.length - count];
   235 
   235 
   236 			int index = 0;
   236 			int index = 0;
   237 
   237 
   238 			//thru natures
   238 			// thru natures
   239 			//if pre- or post nature found skip it
   239 			// if pre- or post nature found skip it
   240 			for (int i = 0; i < natures.length; i++) {
   240 			for (int i = 0; i < natures.length; i++) {
   241 				if (!natures[i].equals(PreNature.NATURE_ID)
   241 				if (!natures[i].equals(PreNature.NATURE_ID)
   242 						&& !natures[i].equals(PostNature.NATURE_ID)) {
   242 						&& !natures[i].equals(PostNature.NATURE_ID)) {
   243 					newNatures[index] = natures[i];
   243 					newNatures[index] = natures[i];
   244 					index++;
   244 					index++;
   248 			project.setDescription(desc, null);
   248 			project.setDescription(desc, null);
   249 
   249 
   250 		} catch (CoreException ce) {
   250 		} catch (CoreException ce) {
   251 			ce.printStackTrace();
   251 			ce.printStackTrace();
   252 		}
   252 		}
   253 
       
   254 	}
   253 	}
   255 }
   254 }