KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > retouche > source > WorkingCopy


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.retouche.source;
21
22
23 import java.io.IOException JavaDoc;
24 import java.io.PrintWriter JavaDoc;
25 import java.io.Writer JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import javax.swing.text.BadLocationException JavaDoc;
30 import javax.swing.text.Document JavaDoc;
31 import javax.swing.text.Position.Bias;
32 import org.netbeans.api.gsf.Error;
33 import org.netbeans.api.gsf.Index;
34 import org.netbeans.api.gsf.OffsetRange;
35 import org.netbeans.api.gsf.Parser;
36 import org.netbeans.api.gsf.ParserResult;
37 import org.netbeans.api.gsf.PositionManager;
38 import org.netbeans.api.gsf.ColoringAttributes;
39 import org.netbeans.api.retouche.source.CompilationUnitTree;
40 import org.netbeans.api.retouche.source.ModificationResult.Difference;
41 import org.netbeans.api.retouche.source.ParserTaskImpl;
42 import org.netbeans.modules.gsf.Language;
43 import org.openide.cookies.EditorCookie;
44 import org.openide.filesystems.FileObject;
45 import org.openide.filesystems.FileUtil;
46 import org.openide.loaders.DataObject;
47 import org.openide.text.CloneableEditorSupport;
48
49 /**
50  * This file is originally from Retouche, the Java Support
51  * infrastructure in NetBeans. I have modified the file as little
52  * as possible to make merging Retouche fixes back as simple as
53  * possible.
54  *
55  *XXX: extends CompilationController now, finish method delegation
56  *
57  * @author Dusan Balek, Petr Hrebejk
58  */

59 public class WorkingCopy extends CompilationController {
60     
61     private CompilationInfo delegate;
62 // private CommandEnvironment ce;
63
// private ChangeSet changes;
64
private boolean afterCommit = false;
65 // //private WorkingCopyContext wcc;
66
// private TreeMaker treeMaker;
67

68     WorkingCopy(final CompilationInfo delegate) throws IOException JavaDoc {
69         super(delegate);
70         assert delegate != null;
71         this.delegate = delegate;
72 // wcc = new WorkingCopyContext();
73
}
74
75 // private synchronized void init() throws ReattributionException {
76
// final CompilationUnitTree tree = getCompilationUnit();
77
// if (tree != null) {
78
// Context context = getContext();
79
// ASTService model = ASTService.instance(context);
80
// model.setRoot(TreeFactory.instance(context).Root(new ArrayList<CompilationUnitTree>()));
81
// UndoListService.instance(context).reset();
82
// model.addSourceTree(tree);
83
// }
84
//
85
// ParserTaskImpl task = this.delegate.getParserTask();
86
// ce = new DefaultEnvironment(
87
// task, getCompilationUnit(), Source.instance(task.getContext()).name, wcc);
88
// this.treeMaker = new TreeMaker(this, ce.getTreeMaker());
89
// this.changes = new ChangeSet(ce, "<no-description>");
90
// }
91
//
92
// private Context getContext() {
93
// return getParserTask().getContext();
94
// }
95
//
96
// // API of the class --------------------------------------------------------
97
//
98
// CommandEnvironment getCommandEnvironment() {
99
// return ce;
100
// }
101
//
102
// @Override
103
// public Source.Phase toPhase(Source.Phase phase) throws IOException {
104
// Source.Phase result = super.toPhase(phase);
105
//
106
// if (result.compareTo(Source.Phase.PARSED) >= 0) {
107
// try {
108
// init();
109
// } catch (ReattributionException ex) {
110
// IOException ioe = new IOException();
111
// ioe.initCause(ex);
112
// throw ioe;
113
// }
114
// }
115
//
116
// return result;
117
// }
118
//
119
// @Override
120
// public TreeUtilities getTreeUtilities() {
121
// return this.delegate.getTreeUtilities();
122
// }
123
//
124
// @Override
125
// public ElementUtilities getElementUtilities() {
126
// return this.delegate.getElementUtilities();
127
// }
128
//
129
// public synchronized TreeMaker getTreeMaker() {
130
// if (treeMaker == null)
131
// throw new IllegalStateException("Cannot call getTreeMaker before toPhase.");
132
// return treeMaker;
133
// }
134
//
135
// void run(Transformer t) {
136
// if (afterCommit)
137
// throw new IllegalStateException ("The run method can't be called on a WorkingCopy instance after the commit"); //NOI18N
138
// t.attachTo(ce);
139
// t.apply();
140
// }
141
//
142
// void run(Transformer t, Tree tree) {
143
// if (afterCommit)
144
// throw new IllegalStateException ("The run method can't be called on a WorkingCopy instance after the commit"); //NOI18N
145
// t.attachTo(ce);
146
// t.apply(tree);
147
// }
148
//
149
// public synchronized void rewrite(Tree oldTree, Tree newTree) {
150
// if (changes == null)
151
// throw new IllegalStateException("Cannot call rewrite before toPhase.");
152
//
153
// changes.rewrite(oldTree, newTree);
154
// }
155
//
156
/**
157      * Returns the current phase of the {@link Source}.
158      *
159      * @return {@link Source.Phase} the state which was reached by the {@link Source}.
160      */

161     @Override JavaDoc
162     public Phase getPhase() {
163         return this.delegate.getPhase();
164     }
165     
166     @Override JavaDoc
167     public CompilationUnitTree getCompilationUnit() {
168         return this.delegate.getCompilationUnit();
169     }
170
171     @Override JavaDoc
172     public ParserResult getParserResult() {
173         return this.delegate.getParserResult();
174     }
175     
176     /**
177      * Returns the content of the file represented by the {@link Source}.
178      *
179      * @return String the java source
180      */

181     @Override JavaDoc
182     public String JavaDoc getText() {
183         return this.delegate.getText();
184     }
185
186 // /**@inheritDoc*/
187
// @Override
188
// public TokenHierarchy getTokenHiearchy() {
189
// return this.delegate.getTokenHiearchy();
190
// }
191
//
192
// /**
193
// * Returns the errors in the file represented by the {@link Source}.
194
// * @return an list of {@link Diagnostic}
195
// */
196
@Override JavaDoc
197     public List JavaDoc<Error JavaDoc/*Diagnostic*/> getDiagnostics() {
198         return this.delegate.getDiagnostics();
199     }
200
201 // @Override
202
// public Trees getTrees() {
203
// return this.delegate.getTrees();
204
// }
205
//
206
// @Override
207
// public Types getTypes() {
208
// return this.delegate.getTypes();
209
// }
210
//
211
// @Override
212
// public Elements getElements() {
213
// return this.delegate.getElements();
214
// }
215
//
216
@Override JavaDoc
217     public Source getSource() {
218         return this.delegate.getSource();
219     }
220
221     @Override JavaDoc
222     public ClasspathInfo getClasspathInfo() {
223         return this.delegate.getClasspathInfo();
224     }
225     
226     @Override JavaDoc
227     public PositionManager getPositionManager() {
228         return this.delegate.getPositionManager();
229     }
230
231     @Override JavaDoc
232     public FileObject getFileObject() {
233         return this.delegate.getFileObject();
234     }
235
236     @Override JavaDoc
237     public Parser getParser() {
238         return this.delegate.getParser();
239     }
240
241
242 // @Override
243
// public Document getDocument() throws IOException {
244
// return this.delegate.getDocument();
245
// }
246
//
247
// Package private methods -------------------------------------------------
248

249     @Override JavaDoc
250     void setPhase(final Phase phase) {
251         throw new UnsupportedOperationException JavaDoc ("WorkingCopy supports only read interface"); //NOI18N
252
}
253
254     @Override JavaDoc
255     void setCompilationUnit(final CompilationUnitTree compilationUnit) {
256         throw new UnsupportedOperationException JavaDoc ("WorkingCopy supports only read interface"); //NOI18N
257
}
258
259     @Override JavaDoc
260     public void setParserResult(final ParserResult parserResult) {
261         throw new UnsupportedOperationException JavaDoc ("WorkingCopy supports only read interface"); //NOI18N
262
}
263
264     @Override JavaDoc
265     public void setPositionManager(final PositionManager positions) {
266         throw new UnsupportedOperationException JavaDoc ("WorkingCopy supports only read interface"); //NOI18N
267
}
268
269     @Override JavaDoc
270     public void setParser(final Parser parser) {
271         throw new UnsupportedOperationException JavaDoc ("WorkingCopy supports only read interface"); //NOI18N
272
}
273
274     @Override JavaDoc
275     ParserTaskImpl getParserTask() {
276         return this.delegate.getParserTask();
277     }
278     
279     List JavaDoc<Difference> getChanges() throws IOException JavaDoc {
280         if (afterCommit)
281             throw new IllegalStateException JavaDoc("The commit method can be called only once on a WorkingCopy instance"); //NOI18N
282
afterCommit = true;
283 // try {
284
// RootTree newRoot = changes.commit(ce.getRootNode());
285
//
286
// if (changes.hasChanges()) {
287
// getCommandEnvironment().getModel().setRoot(newRoot);
288
// }
289
//
290
// Commit save = new Commit(this);
291
// save.attachTo(ce);
292
// save.commit();
293
// return wcc.diffs;
294
// } catch (QueryException qe) {
295
// return null;
296
// } catch (ReattributionException qe) {
297
return null;
298 // }
299
}
300 //
301
// // Innerclasses ------------------------------------------------------------
302
//
303
// private class WorkingCopyContext extends DefaultApplicationContext {
304
//
305
// private ArrayList<Difference> diffs = new ArrayList<Difference>();
306
//
307
// public PrintWriter getOutputWriter(String title) {
308
// // Sink any log output so it isn't displayed.
309
// return new PrintWriter(new Writer() {
310
// public void write(char[] cbuf, int off, int len) throws IOException {}
311
// public void flush() throws IOException {}
312
// public void close() throws IOException {}
313
// }, true);
314
// }
315
//
316
// public SourceRewriter getSourceRewriter(JavaFileObject sourcefile) throws IOException {
317
// return new Rewriter();
318
// }
319
//
320
// private class Rewriter implements SourceRewriter {
321
//
322
// private int offset = 0;
323
// private CloneableEditorSupport ces;
324
//
325
// private Rewriter() throws IOException {
326
// FileObject fo = getFileObject();
327
// if (fo != null) {
328
// DataObject dObj = DataObject.find(fo);
329
// ces = dObj != null ? (CloneableEditorSupport)dObj.getCookie(EditorCookie.class) : null;
330
// }
331
// if (ces == null)
332
// throw new IOException("Could not find CloneableEditorSupport for " + FileUtil.getFileDisplayName (fo)); //NOI18N
333
// }
334
//
335
// public void writeTo(String s) throws IOException, BadLocationException {
336
// Difference diff = diffs.size() > 0 ? diffs.get(diffs.size() - 1) : null;
337
// if (diff != null && diff.getKind() == Difference.Kind.REMOVE && diff.getEndPosition().getOffset() == offset) {
338
// diff.kind = Difference.Kind.CHANGE;
339
// diff.newText = s;
340
// } else {
341
// diffs.add(new Difference(Difference.Kind.INSERT, ces.createPositionRef(offset, Bias.Forward), ces.createPositionRef(offset, Bias.Forward), null, s));
342
// }
343
// }
344
//
345
// public void skipThrough(SourceReader in, int pos) throws IOException, BadLocationException {
346
// char[] buf = in.getCharsTo(pos);
347
// Difference diff = diffs.size() > 0 ? diffs.get(diffs.size() - 1) : null;
348
// if (diff != null && diff.getKind() == Difference.Kind.INSERT && diff.getStartPosition().getOffset() == offset) {
349
// diff.kind = Difference.Kind.CHANGE;
350
// diff.oldText = new String(buf);
351
// } else {
352
// diffs.add(new Difference(Difference.Kind.REMOVE, ces.createPositionRef(offset, Bias.Forward), ces.createPositionRef(offset + buf.length, Bias.Forward), new String(buf), null));
353
// }
354
// offset += buf.length;
355
// }
356
//
357
// public void copyTo(SourceReader in, int pos) throws IOException {
358
// char[] buf = in.getCharsTo(pos);
359
// offset += buf.length;
360
// }
361
//
362
// public void copyRest(SourceReader in) throws IOException {
363
// }
364
//
365
// public void close(boolean save) {
366
// }
367
// }
368
// }
369

370     @Override JavaDoc
371     public void setLanguage(final Language language) {
372         throw new UnsupportedOperationException JavaDoc ("WorkingCopy supports only read interface"); //NOI18N
373
}
374
375     @Override JavaDoc
376     public Language getLanguage() {
377         return this.delegate.getLanguage();
378     }
379
380     @Override JavaDoc
381     public Index getIndex() {
382         return this.delegate.getIndex();
383     }
384 }
385
Popular Tags