KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > builder > MultiTransformer


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.modules.java.source.builder;
21
22 import org.netbeans.modules.java.source.engine.EngineEnvironment;
23 import org.netbeans.modules.java.source.engine.EngineEnvironment;
24 import org.netbeans.modules.java.source.engine.ASTModel;
25 import org.netbeans.modules.java.source.engine.RootTree;
26 import org.netbeans.api.java.source.query.QueryEnvironment;
27 import org.netbeans.modules.java.source.engine.QueryProgress;
28 import org.netbeans.modules.java.source.engine.ReattributionException;
29 import org.netbeans.api.java.source.query.Query;
30 import org.netbeans.api.java.source.query.ResultTableModel;
31 import org.netbeans.api.java.source.transform.Transformer;
32 import org.netbeans.modules.java.source.engine.RootTree;
33
34 import java.util.ArrayList JavaDoc;
35 import java.util.List JavaDoc;
36
37 /**
38  * Execute multiple Query and Transformer instances as a single command.
39  */

40 public final class MultiTransformer extends Transformer<Object JavaDoc, Object JavaDoc> {
41     ASTModel model;
42     Query[] commands;
43     List JavaDoc<ResultTableModel> resultsList;
44     QueryProgress progressBar;
45     int done;
46     
47     public MultiTransformer(String JavaDoc name, Query[] queries, QueryProgress progress) throws Exception JavaDoc {
48         super();
49         setQueryDescription(name);
50         commands = queries;
51         resultsList = new ArrayList JavaDoc<ResultTableModel>();
52         progressBar = progress;
53         progressBar.setTotalQueries(queries.length);
54     }
55     
56     @Override JavaDoc
57     public void init() {
58         super.init();
59         for (Query q : commands)
60             q.init();
61     }
62     
63     @Override JavaDoc
64     public void attach(QueryEnvironment env) {
65         super.attach(env);
66         model = ((EngineEnvironment)env).getModel();
67         for (Query q : commands)
68             q.attach(env);
69     }
70     
71     @Override JavaDoc
72     public void release() {
73         super.release();
74         model = null;
75         for (Query q : commands)
76             q.release();
77     }
78     
79     @Override JavaDoc
80     public void destroy() {
81         super.destroy();
82         resultsList = null;
83         progressBar = null;
84         for (Query q : commands)
85             q.destroy();
86     }
87     
88     @Override JavaDoc
89     public void apply() {
90         try {
91             for (Query q : commands) {
92                 String JavaDoc name = q.getQueryDescription();
93                 progressBar.queryStarted(name);
94                 q.apply();
95                 if (q instanceof Transformer) {
96                     changes.add(((Transformer)q).getChanges());
97                 }
98                 resultsList.add(q.getResult());
99                 progressBar.queryFinished();
100             }
101             RootTree newRoot = commit((RootTree)model.getRoot());
102             if (!translationSuccessful()) {
103                 result = null; // discard any result entries
104
env.setErrorMessage(getFailureMessage(), queryDescription);
105             } else if (changes.hasChanges()) {
106                 model.setRoot(newRoot);
107             }
108             show(resultsList.toArray(new ResultTableModel[0]), getQueryDescription());
109         } catch (ReattributionException e) {
110             error(e);
111         }
112     }
113 }
114
Popular Tags