/** * Copyright (c) 2005-2011 by Appcelerator, Inc. All Rights Reserved. * Licensed under the terms of the Eclipse Public License (EPL). * Please see the license.txt included with this distribution for details. * Any modifications to this file must keep this entire header intact. */ /* * Author: atotic * Created on Apr 14, 2004 */ package org.python.pydev.editor.model; import java.io.File; import java.util.Properties; import org.eclipse.core.resources.IFile; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Path; import org.python.pydev.core.PropertiesHelper; import org.python.pydev.editor.codecompletion.revisited.visitors.Definition; import org.python.pydev.parser.jython.SimpleNode; import com.aptana.shared_core.io.FileUtils; /** * Pointer points to a python resource inside a file system. * * You can create one of these, and use PyOpenAction to open the * right editor. */ public class ItemPointer { /** * IFile or File object (may be null) */ public final Object file; /** * Position of the 1st character */ public final Location start; /** * Position of the last character */ public final Location end; /** * The definition that originated this ItemPointer (good chance of being null). */ public final Definition definition; /** * The path within the zip file for this pointer (null if we're not dealing with a zip file) */ public final String zipFilePath; public ItemPointer(Object file) { this(file, new Location(), new Location()); } public ItemPointer(Object file, SimpleNode n) { int line = n.beginLine; int col = n.beginColumn; this.file = file; this.start = new Location(line - 1, col - 1); this.end = new Location(line - 1, col - 1); this.definition = null; this.zipFilePath = null; } public ItemPointer(Object file, Location start, Location end) { this(file, start, end, null, null); } public ItemPointer(Object file, Location start, Location end, Definition definition, String zipFilePath) { this.file = file; this.start = start; this.end = end; this.definition = definition; this.zipFilePath = zipFilePath; } @Override public String toString() { StringBuffer buffer = new StringBuffer("ItemPointer ["); buffer.append(file); buffer.append(" - "); buffer.append(start); buffer.append(" - "); buffer.append(end); buffer.append("]"); return buffer.toString(); } @Override public boolean equals(Object obj) { if (!(obj instanceof ItemPointer)) { return false; } ItemPointer i = (ItemPointer) obj; if (!i.file.equals(file)) { return false; } if (!i.start.equals(start)) { return false; } if (!i.end.equals(end)) { return false; } if (i.zipFilePath != null && zipFilePath == null) { return false; } if (zipFilePath != null) { if (!zipFilePath.equals(i.zipFilePath)) { return false; } } return true; } @Override public int hashCode() { int colLineBasedHash = (this.end.column + this.start.line + 7) * 3; if (this.file != null) { return this.file.hashCode() + colLineBasedHash; } else { return colLineBasedHash; } } /** * @return a string to be used to recreate the item pointer. Note that it doesn't provide * the definition even if it was present in this pointer. */ public String asPortableString() { IPath path; if (file instanceof IFile) { IFile f = (IFile) file; path = f.getRawLocation(); } else if (file instanceof IPath) { path = (IPath) file; } else if (file instanceof File) { String absPath = FileUtils.getFileAbsolutePath((File) file); path = Path.fromOSString(absPath); } else if (file instanceof String) { path = Path.fromOSString((String) file); } else if (file == null) { path = null; } else { throw new RuntimeException("Don't know how to handle: " + file.getClass()); } if (path == null) { return null; //the path is really needed. } Properties properties = new Properties(); properties.put("FILE_PATH", path.toPortableString()); if (start != null) { properties.put("START_LINE", String.valueOf(start.line)); properties.put("START_COL", String.valueOf(start.column)); } if (end != null) { properties.put("END_LINE", String.valueOf(end.line)); properties.put("END_COL", String.valueOf(end.column)); } if (zipFilePath != null) { properties.put("ZIP", zipFilePath); } return PropertiesHelper.createStringFromProperties(properties); } public static ItemPointer fromPortableString(String asPortableString) { Properties properties = PropertiesHelper.createPropertiesFromString(asPortableString); String filePath = (String) properties.get("FILE_PATH"); if (filePath == null) { return null; } String startLine = (String) properties.get("START_LINE"); String startCol = (String) properties.get("START_COL"); Location start; if (startLine != null && startCol != null) { start = new Location(Integer.parseInt(startLine), Integer.parseInt(startCol)); } else { start = new Location(); } String endLine = (String) properties.get("END_LINE"); String endCol = (String) properties.get("END_COL"); Location end; if (endLine != null && endCol != null) { end = new Location(Integer.parseInt(endLine), Integer.parseInt(endCol)); } else { end = new Location(); } String zip = (String) properties.get("ZIP"); return new ItemPointer(Path.fromPortableString(filePath), start, end, null, zip); } }