]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/javaeditor/InternalClassFileEditorInput.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / javaeditor / InternalClassFileEditorInput.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2000, 2011 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.jdt.internal.ui.javaeditor;
12
13
14import java.io.File;
15import java.io.FileOutputStream;
16import java.io.IOException;
17
18import org.eclipse.core.runtime.CoreException;
19import org.eclipse.core.runtime.IPath;
20import org.eclipse.core.runtime.Path;
21
22import org.eclipse.jface.resource.ImageDescriptor;
23
24import org.eclipse.ui.IMemento;
25import org.eclipse.ui.IPathEditorInput;
26import org.eclipse.ui.IPersistableElement;
27
28import org.eclipse.jdt.core.IClassFile;
29import org.eclipse.jdt.core.IJavaElement;
30import org.eclipse.jdt.core.JavaModelException;
31
32import org.eclipse.jdt.internal.ui.JavaPlugin;
33import org.eclipse.jdt.internal.ui.JavaPluginImages;
34
35
36/**
37 * Class file considered as editor input.
38 */
39public class InternalClassFileEditorInput implements IClassFileEditorInput, IPersistableElement, IPathEditorInput {
40
41 private IClassFile fClassFile;
42
43 private IPath fPath;
44
45 public InternalClassFileEditorInput(IClassFile classFile) {
46 fClassFile= classFile;
47 }
48
49 /*
50 * @see Object#equals(Object)
51 */
52 @Override
53 public boolean equals(Object obj) {
54 if (this == obj)
55 return true;
56 if (!(obj instanceof InternalClassFileEditorInput))
57 return false;
58 InternalClassFileEditorInput other= (InternalClassFileEditorInput) obj;
59 return other.generated_5092004675847125971(this);
60 }
61
62 /*
63 * @see Object#hashCode
64 */
65 @Override
66 public int hashCode() {
67 return fClassFile.hashCode();
68 }
69
70 /*
71 * @see IClassFileEditorInput#getClassFile()
72 */
73 public IClassFile getClassFile() {
74 return fClassFile;
75 }
76
77 /*
78 * @see IEditorInput#getPersistable()
79 */
80 public IPersistableElement getPersistable() {
81 return this;
82 }
83
84 /*
85 * @see IEditorInput#getName()
86 */
87 public String getName() {
88 return fClassFile.getElementName();
89 }
90
91 /*
92 * @see IEditorInput#getToolTipText()
93 */
94 public String getToolTipText() {
95 return fClassFile.getType().getFullyQualifiedName();
96 }
97
98 /*
99 * @see IEditorInput#getImageDescriptor()
100 */
101 public ImageDescriptor getImageDescriptor() {
102 try {
103 if (fClassFile.isClass())
104 return JavaPluginImages.DESC_OBJS_CFILECLASS;
105 return JavaPluginImages.DESC_OBJS_CFILEINT;
106 } catch (JavaModelException e) {
107 // fall through
108 }
109 return JavaPluginImages.DESC_OBJS_CFILE;
110 }
111
112 /*
113 * @see IEditorInput#exists()
114 */
115 public boolean exists() {
116 return fClassFile.exists();
117 }
118
119 /*
120 * @see IAdaptable#getAdapter(Class)
121 */
122 public Object getAdapter(Class adapter) {
123 if (adapter == IClassFile.class)
124 return fClassFile;
125 else if (adapter == IJavaElement.class)
126 return fClassFile;
127 return null;
128 }
129
130 /*
131 * @see IPersistableElement#saveState(IMemento)
132 */
133 public void saveState(IMemento memento) {
134 ClassFileEditorInputFactory.saveState(memento, this);
135 }
136
137 /*
138 * @see IPersistableElement#getFactoryId()
139 */
140 public String getFactoryId() {
141 return ClassFileEditorInputFactory.ID;
142 }
143
144 /*
145 * @see org.eclipse.ui.IPathEditorInput#getPath()
146 * @since 3.7
147 */
148 public IPath getPath() {
149 if (fPath == null)
150 fPath= writeToTempFile(fClassFile);
151 return fPath;
152 }
153
154 public boolean generated_5092004675847125971(InternalClassFileEditorInput internalclassfileeditorinput) {
155 return internalclassfileeditorinput.fClassFile.equals(fClassFile);
156 }
157
158 private static IPath writeToTempFile(IClassFile classFile) {
159 FileOutputStream writer= null;
160 try {
161 File file= File.createTempFile(classFile.getElementName(), ".class"); //$NON-NLS-1$
162 byte[] bytes= classFile.getBytes();
163 writer= new FileOutputStream(file);
164 writer.write(bytes);
165 return new Path(file.toString());
166 } catch (IOException e) {
167 JavaPlugin.log(e);
168 } catch (CoreException e) {
169 JavaPlugin.log(e.getStatus());
170 } finally {
171 if (writer != null)
172 try {
173 writer.close();
174 } catch (IOException e) {
175 JavaPlugin.log(e);
176 }
177 }
178 throw new IllegalArgumentException("Could not create temporary file."); //$NON-NLS-1$
179 }
180
181}
182
183