KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > ant > xmlc > AntXMLCTool


1 package org.enhydra.kelp.ant.xmlc;
2
3 //AddInCore
4
//import org.enhydra.kelp.common.xmlc.CoreXMLCTool;
5
//import org.enhydra.kelp.common.xmlc.XMLCInnerPanel;
6
import org.enhydra.kelp.common.AbstractTool;
7 import org.enhydra.kelp.common.event.SelectionPanelEvent;
8 import org.enhydra.kelp.common.event.SelectionPanelListener;
9 import org.enhydra.kelp.common.event.WriteListener;
10 import org.enhydra.kelp.common.node.OtterProject;
11 import org.enhydra.kelp.common.node.OtterFileNode;
12 import org.enhydra.kelp.common.swing.AddinInnerPanel;
13 import org.enhydra.kelp.common.properties.XMLCProjectPropertyPanel;
14
15 //AddInAnt
16
import org.enhydra.kelp.ant.node.AntProject;
17 import org.enhydra.kelp.ant.swing.HTMLViewer;
18
19
20 //ToolBox
21
import org.enhydra.tool.common.PathHandle;
22 import org.enhydra.tool.common.InnerPanel;
23 import org.enhydra.tool.common.SwingUtil;
24 import org.enhydra.tool.common.ButtonPanel;
25 import org.enhydra.tool.common.event.FirstFocusEvent;
26 import org.enhydra.tool.common.event.FirstFocusListener;
27
28 // Standard imports
29
import java.io.File JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.awt.Component JavaDoc;
32 import java.awt.Dialog JavaDoc;
33 import java.awt.Frame JavaDoc;
34 import java.awt.event.ActionListener JavaDoc;
35 import java.lang.ref.WeakReference JavaDoc;
36 import java.net.URL JavaDoc;
37 import java.util.ResourceBundle JavaDoc;
38
39 import javax.swing.JDialog JavaDoc;
40 import javax.swing.JEditorPane JavaDoc;
41
42 import org.enhydra.kelp.common.node.OtterDocumentNode;
43 import org.enhydra.kelp.ant.node.AntDocumentNode;
44 /**
45  * <p>Title: </p>
46  * <p>Description: XMLC tool use AntProject for read,write properties and
47  * AntXMLCBuilder for running XML Compiler. </p>
48  * <p>Copyright: Copyright (c) 2003</p>
49  * <p>Company: </p>
50  * @author Damir Milovic
51  * @version 1.0
52  */

53
54 public class AntXMLCTool extends AbstractTool
55                          implements SelectionPanelListener, FirstFocusListener {
56
57     static ResourceBundle JavaDoc addinRes =
58         ResourceBundle.getBundle("org.enhydra.kelp.common.Res");
59     private AntXMLCInnerPanel innerPanel = null;
60     private AntXMLCButtonPanel buttonPanel = null;
61     private WeakReference JavaDoc compileRef = null;
62     private static AntProject project = null;
63
64   public AntXMLCTool() {
65     super();
66     innerPanel = new AntXMLCInnerPanel();
67     buttonPanel = new AntXMLCButtonPanel();
68     innerPanel.addFirstFocusListener(this);
69     innerPanel.getSelectionPanel().addSelectionPanelListener(this);
70   }
71   public static void main(String JavaDoc[] args) {
72         SwingUtil.setLookAndFeelToSystem();
73         AntXMLCTool tool = null;
74
75         if(args.length > 0){
76           // check path
77
File JavaDoc root = new File JavaDoc(args[0]);
78             if(!root.exists()){
79               System.err.println("ERROR: directory'"+args[0]+"' don't exist!");
80               return;
81             }
82             try {
83                 project = new AntProject(args[0]);
84             }
85             catch (Exception JavaDoc ex) {
86                 ex.printStackTrace();
87                 return;
88             }
89
90         }else{
91             return;
92           //FIXME TODO get current directory
93
}
94         tool = new AntXMLCTool();
95         tool.setProject(project); //DACHA
96
tool.showDialog(new Frame JavaDoc());
97         //tool.showDialog(null);
98
}
99   /**
100    * Override
101    */

102   public void build() {
103     ((AntXMLCInnerPanel)getInnerPanel()).selectOutputTab();
104     ((AntXMLCInnerPanel)getInnerPanel()).save();
105     ((AntXMLCInnerPanel)getInnerPanel()).clearOutput();
106     buildImpl();
107   }
108
109   private void buildImpl() {
110     AntXMLCBuilder builder = new AntXMLCBuilder(((AntXMLCInnerPanel)getInnerPanel()).getWriteListeners());
111     builder.setProject(getProject());
112     builder.build();
113   }
114
115     public static final String JavaDoc getDefaultTitle() {
116         return "XML Compiler";
117     }
118
119     // implements AbstractTool
120
public String JavaDoc getTitle() {
121         return AntXMLCTool.getDefaultTitle();
122     }
123
124     // implements AbstractTool
125
public InnerPanel getInnerPanel() {
126         return innerPanel;
127     }
128
129     // implements AbstractTool
130
public ButtonPanel getButtonPanel() {
131         return buttonPanel;
132     }
133
134     public ActionListener JavaDoc createButtonListener() {
135         return (new AntXMLCButtonListener(this));
136     }
137
138     // overrides AbstractTool
139
public void setProject(OtterProject project) {
140         super.setProject(project);
141         setNodes(project.getAllDocuments());
142     }
143
144     // overrides AbstractTool
145
public void clearAll() {
146         super.clearAll();
147        innerPanel.removeFirstFocusListener(this);
148         if (compileRef != null) {
149             compileRef.clear();
150         }
151         innerPanel = null;
152         buttonPanel = null;
153         compileRef = null;
154     }
155
156     // implements FirstFocusListener
157
public void onFirstFocus(FirstFocusEvent event) {
158       refreshCompile();
159     }
160     // implements SelectionPanelListener
161
public void onSelection(SelectionPanelEvent event) {
162         boolean enable = event.getSelected().size() > 0;
163         //First set to project changed documents
164
OtterFileNode[] fileNodes = new OtterFileNode[0];
165         fileNodes = innerPanel.getNodes();
166         OtterDocumentNode[] docNodes = new OtterDocumentNode[fileNodes.length];
167         for(int i=0; i<fileNodes.length;i++){
168             docNodes[i] = (OtterDocumentNode) fileNodes[i];
169         }
170         project.setAllDocuments(docNodes);
171         //refrsh Compile button
172
refreshCompile(enable);
173     }
174     
175     public void showHelp(Dialog JavaDoc parent){
176         URL JavaDoc helpURL = this.getClass().getClassLoader().getResource("using_ant_kelp.html");
177         HTMLViewer.createAndShow(parent, "Kelp Help", helpURL);
178
179     }
180     //
181
// PROTECTED
182
//
183
// implements org.enhydra.kelp.common.tool.AbstractTool
184

185     /**
186      * Get source files that can be selected for compilation.
187      */

188     protected OtterFileNode[] getNodes() {
189         return innerPanel.getNodes();
190     }
191
192     /**
193      * Set the source files that can be selected for compilation
194      */

195     protected void setNodes(OtterFileNode[] n) {
196         innerPanel.setNodes(n);
197         //refreshCompile();
198
}
199
200     protected String JavaDoc[] getSelectText() {
201         String JavaDoc[] s = {
202             "Documents Available", "Documents Selected"
203         };
204         return s;
205     }
206
207     // implements org.enhydra.kelp.common.tool.AbstractTool
208
protected String JavaDoc getBuildText() {
209         return addinRes.getString("buttonMake_Text");
210     }
211
212     // implements org.enhydra.kelp.common.tool.AbstractTool
213
protected String JavaDoc getProgressTitle() {
214         return addinRes.getString("XMLC_Progress");
215     }
216
217     // implements org.enhydra.kelp.common.tool.AbstractTool
218
protected AddinInnerPanel getPropertyPanel() {
219         return innerPanel;
220     }
221
222     private Component JavaDoc getCompile() {
223         Component JavaDoc comp = null;
224
225         if (compileRef == null) {
226             comp = ButtonPanel.findButton(getInnerPanel().getTopLevelAncestor(),
227                                        AntXMLCButtonPanel.COMMAND_COMPILE);
228             if (comp != null) {
229                 compileRef = new WeakReference JavaDoc(comp);
230             }
231         } else {
232             comp = (Component JavaDoc) compileRef.get();
233         }
234         return comp;
235     }
236     //Called when user change selection
237
private void refreshCompile() {
238
239          boolean enable = false;
240         OtterFileNode[] nodes = new OtterFileNode[0];
241         nodes = innerPanel.getNodes();
242         if(nodes != null){
243
244             for (int i = 0; i < nodes.length; i++) {
245                 if (nodes[i].isSelected()) {
246                     enable = true;
247                     break;
248                 }
249             }
250         }
251         refreshCompile(enable);
252     }
253
254     private void refreshCompile(boolean enable) {
255         Component JavaDoc compileButton = getCompile();
256
257         if (compileButton != null) {
258             compileButton.setEnabled(enable);
259         }
260     }
261
262 }
Popular Tags