001: package org.enhydra.kelp.ant.xmlc;
002:
003: //AddInCore
004: //import org.enhydra.kelp.common.xmlc.CoreXMLCTool;
005: //import org.enhydra.kelp.common.xmlc.XMLCInnerPanel;
006: import org.enhydra.kelp.common.AbstractTool;
007: import org.enhydra.kelp.common.event.SelectionPanelEvent;
008: import org.enhydra.kelp.common.event.SelectionPanelListener;
009: import org.enhydra.kelp.common.node.OtterProject;
010: import org.enhydra.kelp.common.node.OtterFileNode;
011: import org.enhydra.kelp.common.swing.AddinInnerPanel;
012:
013: //AddInAnt
014: import org.enhydra.kelp.ant.node.AntProject;
015: import org.enhydra.kelp.ant.swing.HTMLViewer;
016:
017: //ToolBox
018: import org.enhydra.tool.common.InnerPanel;
019: import org.enhydra.tool.common.SwingUtil;
020: import org.enhydra.tool.common.ButtonPanel;
021: import org.enhydra.tool.common.event.FirstFocusEvent;
022: import org.enhydra.tool.common.event.FirstFocusListener;
023:
024: // Standard imports
025: import java.io.File;
026: import java.awt.Component;
027: import java.awt.Dialog;
028: import java.awt.Frame;
029: import java.awt.event.ActionListener;
030: import java.lang.ref.WeakReference;
031: import java.net.URL;
032: import java.util.ResourceBundle;
033:
034: import org.enhydra.kelp.common.node.OtterDocumentNode;
035:
036: /**
037: * <p>Title: </p>
038: * <p>Description: XMLC tool use AntProject for read,write properties and
039: * AntXMLCBuilder for running XML Compiler. </p>
040: * <p>Copyright: Copyright (c) 2003</p>
041: * <p>Company: </p>
042: * @author Damir Milovic
043: * @version 1.0
044: */
045:
046: public class AntXMLCTool extends AbstractTool implements
047: SelectionPanelListener, FirstFocusListener {
048:
049: static ResourceBundle addinRes = ResourceBundle
050: .getBundle("org.enhydra.kelp.common.Res");
051: private AntXMLCInnerPanel innerPanel = null;
052: private AntXMLCButtonPanel buttonPanel = null;
053: private WeakReference compileRef = null;
054: private static AntProject project = null;
055:
056: public AntXMLCTool() {
057: super ();
058: innerPanel = new AntXMLCInnerPanel();
059: buttonPanel = new AntXMLCButtonPanel();
060: innerPanel.addFirstFocusListener(this );
061: innerPanel.getSelectionPanel().addSelectionPanelListener(this );
062: }
063:
064: public static void main(String[] args) {
065: SwingUtil.setLookAndFeelToSystem();
066: AntXMLCTool tool = null;
067:
068: if (args.length > 0) {
069: // check path
070: File root = new File(args[0]);
071: if (!root.exists()) {
072: System.err.println("ERROR: directory'" + args[0]
073: + "' don't exist!");
074: return;
075: }
076: try {
077: project = new AntProject(args[0]);
078: } catch (Exception ex) {
079: ex.printStackTrace();
080: return;
081: }
082:
083: } else {
084: return;
085: //FIXME TODO get current directory
086: }
087: tool = new AntXMLCTool();
088: tool.setProject(project); //DACHA
089: tool.showDialog(new Frame());
090: //tool.showDialog(null);
091: }
092:
093: /**
094: * Override
095: */
096: public void build() {
097: ((AntXMLCInnerPanel) getInnerPanel()).selectOutputTab();
098: ((AntXMLCInnerPanel) getInnerPanel()).save();
099: ((AntXMLCInnerPanel) getInnerPanel()).clearOutput();
100: buildImpl();
101: }
102:
103: private void buildImpl() {
104: try {
105: AntXMLCBuilder builder = new AntXMLCBuilder(
106: ((AntXMLCInnerPanel) getInnerPanel())
107: .getWriteListeners());
108: builder.setProject(getProject());
109: builder.build();
110: } catch (Exception ex) {
111: ex.printStackTrace();
112: }
113: }
114:
115: public static final String getDefaultTitle() {
116: return "XML Compiler";
117: }
118:
119: // implements AbstractTool
120: public String 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 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:
161: // implements SelectionPanelListener
162: public void onSelection(SelectionPanelEvent event) {
163: boolean enable = event.getSelected().size() > 0;
164: //First set to project changed documents
165: OtterFileNode[] fileNodes = new OtterFileNode[0];
166: fileNodes = innerPanel.getNodes();
167: OtterDocumentNode[] docNodes = new OtterDocumentNode[fileNodes.length];
168: for (int i = 0; i < fileNodes.length; i++) {
169: docNodes[i] = (OtterDocumentNode) fileNodes[i];
170: }
171: project.setAllDocuments(docNodes);
172: //refrsh Compile button
173: refreshCompile(enable);
174: }
175:
176: public void showHelp(Dialog parent) {
177: URL helpURL = this .getClass().getClassLoader().getResource(
178: "using_ant_kelp.html");
179: HTMLViewer.createAndShow(parent, "Kelp Help", helpURL);
180:
181: }
182:
183: //
184: // PROTECTED
185: //
186: // implements org.enhydra.kelp.common.tool.AbstractTool
187:
188: /**
189: * Get source files that can be selected for compilation.
190: */
191: protected OtterFileNode[] getNodes() {
192: return innerPanel.getNodes();
193: }
194:
195: /**
196: * Set the source files that can be selected for compilation
197: */
198: protected void setNodes(OtterFileNode[] n) {
199: innerPanel.setNodes(n);
200: //refreshCompile();
201: }
202:
203: protected String[] getSelectText() {
204: String[] s = { "Documents Available", "Documents Selected" };
205: return s;
206: }
207:
208: // implements org.enhydra.kelp.common.tool.AbstractTool
209: protected String getBuildText() {
210: return addinRes.getString("buttonMake_Text");
211: }
212:
213: // implements org.enhydra.kelp.common.tool.AbstractTool
214: protected String getProgressTitle() {
215: return addinRes.getString("XMLC_Progress");
216: }
217:
218: // implements org.enhydra.kelp.common.tool.AbstractTool
219: protected AddinInnerPanel getPropertyPanel() {
220: return innerPanel;
221: }
222:
223: private Component getCompile() {
224: Component comp = null;
225:
226: if (compileRef == null) {
227: comp = ButtonPanel.findButton(getInnerPanel()
228: .getTopLevelAncestor(),
229: AntXMLCButtonPanel.COMMAND_COMPILE);
230: if (comp != null) {
231: compileRef = new WeakReference(comp);
232: }
233: } else {
234: comp = (Component) compileRef.get();
235: }
236: return comp;
237: }
238:
239: //Called when user change selection
240: private void refreshCompile() {
241:
242: boolean enable = false;
243: OtterFileNode[] nodes = new OtterFileNode[0];
244: nodes = innerPanel.getNodes();
245: if (nodes != null) {
246:
247: for (int i = 0; i < nodes.length; i++) {
248: if (nodes[i].isSelected()) {
249: enable = true;
250: break;
251: }
252: }
253: }
254: refreshCompile(enable);
255: }
256:
257: private void refreshCompile(boolean enable) {
258: Component compileButton = getCompile();
259:
260: if (compileButton != null) {
261: compileButton.setEnabled(enable);
262: }
263: }
264:
265: }
|