/******************************************************************************* * Copyright (c) 2000, 2011 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ package org.eclipse.jdt.internal.ui.search; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; import org.eclipse.jface.viewers.StyledString; import org.eclipse.jface.viewers.StyledString.Styler; import org.eclipse.search.ui.text.Match; import org.eclipse.jdt.core.IBuffer; import org.eclipse.jdt.core.ITypeRoot; import org.eclipse.jdt.core.formatter.IndentManipulation; import org.eclipse.jdt.internal.corext.util.Messages; import org.eclipse.jdt.ui.JavaUI; import org.eclipse.jdt.internal.ui.search.IOccurrencesFinder.OccurrenceLocation; import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels; import org.eclipse.jdt.internal.ui.viewsupport.ColoringLabelProvider; public class JavaElementLine { private final ITypeRoot fElement; private final String fLineContents; private final int fLineNumber; private final int fLineStartOffset; private int fFlags; /** * @param element either an ICompilationUnit or an IClassFile * @param lineNumber the line number * @param lineStartOffset the start offset of the line * @throws CoreException thrown when accessing of the buffer failed */ public JavaElementLine(ITypeRoot element, int lineNumber, int lineStartOffset) throws CoreException { fElement= element; fFlags= 0; IBuffer buffer= element.getBuffer(); if (buffer == null) { throw new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, Messages.format( SearchMessages.JavaElementLine_error_nobuffer, BasicElementLabels.getFileName(element)))); } int length= buffer.getLength(); int i= lineStartOffset; char ch= buffer.getChar(i); while (lineStartOffset < length && IndentManipulation.isIndentChar(ch)) { ch= buffer.getChar(++i); } fLineStartOffset= i; StringBuffer buf= new StringBuffer(); while (i < length && !IndentManipulation.isLineDelimiterChar(ch)) { if (Character.isISOControl(ch)) { buf.append(' '); } else { buf.append(ch); } i++; if (i < length) ch= buffer.getChar(i); } fLineContents= buf.toString(); fLineNumber= lineNumber; } public void setFlags(int flags) { fFlags= flags; } public int getFlags() { return fFlags; } public ITypeRoot getJavaElement() { return fElement; } public int getLine() { return fLineNumber; } public String getLineContents() { return fLineContents; } public int getLineStartOffset() { return fLineStartOffset; } public void generated_2430045153647211890(OccurrenceLocation loc) { setFlags(getFlags() | loc.getFlags()); } public String generated_4416462867979027888(OccurrencesSearchLabelProvider occurrencessearchlabelprovider) { return occurrencessearchlabelprovider.getLineNumberLabel(this) + getLineContents(); } public StyledString generated_739450509809147788(OccurrencesSearchLabelProvider occurrencessearchlabelprovider) { String lineNumberString= occurrencessearchlabelprovider.getLineNumberLabel(this); Styler highlightStyle= ColoringLabelProvider.HIGHLIGHT_STYLE; StyledString res= new StyledString(); res.append(lineNumberString, StyledString.QUALIFIER_STYLER); res.append(getLineContents()); Match[] matches= occurrencessearchlabelprovider.getPage().getInput().getMatches(this); for (int i= 0; i < matches.length; i++) { OccurrenceMatch curr= (OccurrenceMatch) matches[i]; int offset= curr.getOriginalOffset() - getLineStartOffset() + lineNumberString.length(); int length= curr.getOriginalLength(); if (offset >= 0 && (offset + length <= res.length())) { if ((curr.getFlags() & IOccurrencesFinder.F_WRITE_OCCURRENCE) != 0) { res.setStyle(offset, length, ColoringLabelProvider.HIGHLIGHT_WRITE_STYLE); } else { res.setStyle(offset, length, highlightStyle); } } } return res; } }