private boolean hasBlankLineAbove() { if (mOut.length() < 2 * mLineSeparator.length()) { return false; } return SdkUtils.endsWith(mOut, mLineSeparator) && SdkUtils.endsWith(mOut, mOut.length() - mLineSeparator.length(), mLineSeparator); }
private void addLayoutFileChanges(IProject project, CompositeChange result) { try { // Update references in XML resource files IFolder resFolder = project.getFolder(SdkConstants.FD_RESOURCES); IResource[] folders = resFolder.members(); for (IResource folder : folders) { String folderName = folder.getName(); ResourceFolderType folderType = ResourceFolderType.getFolderType(folderName); if (folderType != ResourceFolderType.LAYOUT) { continue; } if (!(folder instanceof IFolder)) { continue; } IResource[] files = ((IFolder) folder).members(); for (int i = 0; i < files.length; i++) { IResource member = files[i]; if ((member instanceof IFile) && member.exists()) { IFile file = (IFile) member; String fileName = member.getName(); if (SdkUtils.endsWith(fileName, DOT_XML)) { addXmlFileChanges(file, result, false); } } } } } catch (CoreException e) { RefactoringUtil.log(e); } }
/** * Creates a new {@link XmlPrettyPrinter} * * @param prefs the preferences to format with * @param style the style to format with * @param lineSeparator the line separator to use, such as "\n" (can be null, in which case the * system default is looked up via the line.separator property) */ public XmlPrettyPrinter(XmlFormatPreferences prefs, XmlFormatStyle style, String lineSeparator) { mPrefs = prefs; mStyle = style; if (lineSeparator == null) { lineSeparator = SdkUtils.getLineSeparator(); } mLineSeparator = lineSeparator; }
/** * Pretty prints the given node using default styles * * @param node the node, usually a document, to be printed * @param endWithNewline if true, ensure that the printed output ends with a newline * @return the resulting formatted string */ @NonNull public static String prettyPrint(@NonNull Node node, boolean endWithNewline) { return prettyPrint( node, XmlFormatPreferences.defaults(), XmlFormatStyle.get(node), SdkUtils.getLineSeparator(), endWithNewline); }
private static void formatFile(@NonNull XmlFormatPreferences prefs, File file, boolean stdout) { if (file.isDirectory()) { File[] files = file.listFiles(); if (files != null) { for (File child : files) { formatFile(prefs, child, stdout); } } } else if (file.isFile() && SdkUtils.endsWithIgnoreCase(file.getName(), DOT_XML)) { XmlFormatStyle style = null; if (file.getName().equals(SdkConstants.ANDROID_MANIFEST_XML)) { style = XmlFormatStyle.MANIFEST; } else { File parent = file.getParentFile(); if (parent != null) { String parentName = parent.getName(); ResourceFolderType folderType = ResourceFolderType.getFolderType(parentName); if (folderType == ResourceFolderType.LAYOUT) { style = XmlFormatStyle.LAYOUT; } else if (folderType == ResourceFolderType.VALUES) { style = XmlFormatStyle.RESOURCE; } } } try { String xml = Files.toString(file, Charsets.UTF_8); Document document = XmlUtils.parseDocumentSilently(xml, true); if (document == null) { System.err.println("Could not parse " + file); System.exit(1); return; } if (style == null) { style = XmlFormatStyle.get(document); } boolean endWithNewline = xml.endsWith("\n"); int firstNewLine = xml.indexOf('\n'); String lineSeparator = firstNewLine > 0 && xml.charAt(firstNewLine - 1) == '\r' ? "\r\n" : "\n"; String formatted = XmlPrettyPrinter.prettyPrint(document, prefs, style, lineSeparator, endWithNewline); if (stdout) { System.out.println(formatted); } else { Files.write(formatted, file, Charsets.UTF_8); } } catch (IOException e) { System.err.println("Could not read " + file); System.exit(1); } } }
public String toCamelCase() { return SdkUtils.constantNameToCamelCase(name()); }
/** * Called by {@link NewXmlFileWizard} to initialize the page with the selection received by the * wizard -- typically the current user workbench selection. * * <p>Things we expect to find out from the selection: * * <ul> * <li>The project name, valid if it's an android nature. * <li>The current folder, valid if it's a folder under /res * <li>An existing filename, in which case the user will be asked whether to override it. * </ul> * * <p>The selection can also be set to a {@link Pair} of {@link IProject} and a workspace resource * path (where the resource path does not have to exist yet, such as res/anim/). * * @param selection The selection when the wizard was initiated. */ private boolean initializeFromSelection(IStructuredSelection selection) { if (selection == null) { return false; } // Find the best match in the element list. In case there are multiple selected elements // select the one that provides the most information and assign them a score, // e.g. project=1 + folder=2 + file=4. IProject targetProject = null; String targetWsFolderPath = null; String targetFileName = null; int targetScore = 0; for (Object element : selection.toList()) { if (element instanceof IAdaptable) { IResource res = (IResource) ((IAdaptable) element).getAdapter(IResource.class); IProject project = res != null ? res.getProject() : null; // Is this an Android project? try { if (project == null || !project.hasNature(AdtConstants.NATURE_DEFAULT)) { continue; } } catch (CoreException e) { // checking the nature failed, ignore this resource continue; } int score = 1; // we have a valid project at least IPath wsFolderPath = null; String fileName = null; assert res != null; // Eclipse incorrectly thinks res could be null, so tell it no if (res.getType() == IResource.FOLDER) { wsFolderPath = res.getProjectRelativePath(); } else if (res.getType() == IResource.FILE) { if (SdkUtils.endsWithIgnoreCase(res.getName(), DOT_XML)) { fileName = res.getName(); } wsFolderPath = res.getParent().getProjectRelativePath(); } // Disregard this folder selection if it doesn't point to /res/something if (wsFolderPath != null && wsFolderPath.segmentCount() > 1 && SdkConstants.FD_RESOURCES.equals(wsFolderPath.segment(0))) { score += 2; } else { wsFolderPath = null; fileName = null; } score += fileName != null ? 4 : 0; if (score > targetScore) { targetScore = score; targetProject = project; targetWsFolderPath = wsFolderPath != null ? wsFolderPath.toString() : null; targetFileName = fileName; } } else if (element instanceof Pair<?, ?>) { // Pair of Project/String @SuppressWarnings("unchecked") Pair<IProject, String> pair = (Pair<IProject, String>) element; targetScore = 1; targetProject = pair.getFirst(); targetWsFolderPath = pair.getSecond(); targetFileName = ""; } } if (targetProject == null) { // Try to figure out the project from the active editor IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); if (window != null) { IWorkbenchPage page = window.getActivePage(); if (page != null) { IEditorPart activeEditor = page.getActiveEditor(); if (activeEditor instanceof AndroidXmlEditor) { Object input = ((AndroidXmlEditor) activeEditor).getEditorInput(); if (input instanceof FileEditorInput) { FileEditorInput fileInput = (FileEditorInput) input; targetScore = 1; IFile file = fileInput.getFile(); targetProject = file.getProject(); IPath path = file.getParent().getProjectRelativePath(); targetWsFolderPath = path != null ? path.toString() : null; } } } } } if (targetProject == null) { // If we didn't find a default project based on the selection, check how many // open Android projects we can find in the current workspace. If there's only // one, we'll just select it by default. IJavaProject[] projects = AdtUtils.getOpenAndroidProjects(); if (projects != null && projects.length == 1) { targetScore = 1; targetProject = projects[0].getProject(); } } // Now set the UI accordingly if (targetScore > 0) { mValues.project = targetProject; mValues.folderPath = targetWsFolderPath; mProjectButton.setSelectedProject(targetProject); mFileNameTextField.setText(targetFileName != null ? targetFileName : ""); // $NON-NLS-1$ // If the current selection context corresponds to a specific file type, // select it. if (targetWsFolderPath != null) { int pos = targetWsFolderPath.lastIndexOf(WS_SEP_CHAR); if (pos >= 0) { targetWsFolderPath = targetWsFolderPath.substring(pos + 1); } String[] folderSegments = targetWsFolderPath.split(RES_QUALIFIER_SEP); if (folderSegments.length > 0) { mValues.configuration = FolderConfiguration.getConfig(folderSegments); String folderName = folderSegments[0]; selectTypeFromFolder(folderName); } } } return true; }
/** * Paints the preview at the given x/y position * * @param gc the graphics context to paint it into * @param x the x coordinate to paint the preview at * @param y the y coordinate to paint the preview at */ void paint(GC gc, int x, int y) { mTitleHeight = paintTitle(gc, x, y, true /*showFile*/); y += mTitleHeight; y += 2; int width = getWidth(); int height = getHeight(); if (mThumbnail != null && mError == null) { gc.drawImage(mThumbnail, x, y); if (mActive) { int oldWidth = gc.getLineWidth(); gc.setLineWidth(3); gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_LIST_SELECTION)); gc.drawRectangle(x - 1, y - 1, width + 2, height + 2); gc.setLineWidth(oldWidth); } } else if (mError != null) { if (mThumbnail != null) { gc.drawImage(mThumbnail, x, y); } else { gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_WIDGET_BORDER)); gc.drawRectangle(x, y, width, height); } gc.setClipping(x, y, width, height); Image icon = IconFactory.getInstance().getIcon("renderError"); // $NON-NLS-1$ ImageData data = icon.getImageData(); int prevAlpha = gc.getAlpha(); int alpha = 96; if (mThumbnail != null) { alpha -= 32; } gc.setAlpha(alpha); gc.drawImage(icon, x + (width - data.width) / 2, y + (height - data.height) / 2); String msg = mError; Density density = mConfiguration.getDensity(); if (density == Density.TV || density == Density.LOW) { msg = "Broken rendering library; unsupported DPI. Try using the SDK manager " + "to get updated layout libraries."; } int charWidth = gc.getFontMetrics().getAverageCharWidth(); int charsPerLine = (width - 10) / charWidth; msg = SdkUtils.wrap(msg, charsPerLine, null); gc.setAlpha(255); gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_BLACK)); gc.drawText(msg, x + 5, y + HEADER_HEIGHT, true); gc.setAlpha(prevAlpha); gc.setClipping((Region) null); } else { gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_WIDGET_BORDER)); gc.drawRectangle(x, y, width, height); Image icon = IconFactory.getInstance().getIcon("refreshPreview"); // $NON-NLS-1$ ImageData data = icon.getImageData(); int prevAlpha = gc.getAlpha(); gc.setAlpha(96); gc.drawImage(icon, x + (width - data.width) / 2, y + (height - data.height) / 2); gc.setAlpha(prevAlpha); } if (mActive) { int left = x; int prevAlpha = gc.getAlpha(); gc.setAlpha(208); Color bg = mCanvas.getDisplay().getSystemColor(SWT.COLOR_WHITE); gc.setBackground(bg); gc.fillRectangle(left, y, x + width - left, HEADER_HEIGHT); gc.setAlpha(prevAlpha); y += 2; // Paint icons gc.drawImage(CLOSE_ICON, left, y); left += CLOSE_ICON_WIDTH; gc.drawImage(ZOOM_IN_ICON, left, y); left += ZOOM_IN_ICON_WIDTH; gc.drawImage(ZOOM_OUT_ICON, left, y); left += ZOOM_OUT_ICON_WIDTH; gc.drawImage(EDIT_ICON, left, y); left += EDIT_ICON_WIDTH; } }