001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.uml.ui.support.messaging;
043:
044: import java.io.BufferedWriter;
045: import java.io.File;
046: import java.io.FileWriter;
047: import java.io.IOException;
048: import java.util.Enumeration;
049:
050: import javax.swing.JFileChooser;
051: import javax.swing.JTree;
052: import javax.swing.tree.TreeNode;
053:
054: public class TreeSaver {
055:
056: private JTree m_Tree;
057: private String m_DefExt;
058: private String m_LogFile;
059:
060: public TreeSaver(JTree tree) {
061: super ();
062: m_LogFile = "Results.txt";
063: m_DefExt = "txt";
064: m_Tree = tree;
065:
066: }
067:
068: public void setDefaultExtension(String ext) {
069: m_DefExt = ext;
070: }
071:
072: public String getDefaultExtension() {
073: return m_DefExt;
074: }
075:
076: public void setLogFile(String file) {
077: m_LogFile = file;
078: }
079:
080: public String getLogFile() {
081: return m_LogFile;
082: }
083:
084: /**
085: * Saves the tree information to a specific file
086: *
087: * @param file The filename for the saved data
088: *
089: * @see TreeSaver::RetrieveFileLocation
090: */
091: public String save() {
092: return this .save("");
093: }
094:
095: public String save(String pFile) {
096:
097: String filePath = pFile;
098:
099: try {
100: if (filePath.length() == 0) {
101:
102: filePath = retrieveFileLocation();
103: }
104:
105: if (filePath.length() > 0) {
106:
107: BufferedWriter file = new BufferedWriter(
108: new FileWriter(filePath));
109:
110: // File file = _wfopen(filePath.c_str(), _T("w+"));
111:
112: if (file != null) {
113: saveTree(file, m_Tree);
114:
115: file.close();
116: }
117: }
118: } catch (Exception e) {
119: e.printStackTrace();
120: }
121:
122: return filePath;
123: }
124:
125: /**
126: * Asks the user to supply a directory location for where the data file should
127: * be stored.
128: */
129: protected String retrieveFileLocation() {
130: String retValue = "";
131:
132: JFileChooser fc = new JFileChooser();
133:
134: File f = null;
135: try {
136: f = new File(new File(m_LogFile).getCanonicalPath());
137: } catch (IOException e) {
138: e.printStackTrace();
139: }
140:
141: fc.setSelectedFile(f);
142:
143: if (fc.showSaveDialog(fc) == JFileChooser.APPROVE_OPTION) {
144: File file = fc.getSelectedFile();
145: if (file != null) {
146: retValue = file.getAbsolutePath();
147: }
148: }
149:
150: return retValue;
151: }
152:
153: /**
154: * Given a tree control this function will save the contents to a file
155: *
156: * @param pFile The file that should be opened and saved to
157: * @param tree The tree control that should be saved.
158: *
159: * @see TreeSaver::OutputItem
160: */
161: protected void saveTree(BufferedWriter pFile, JTree tree) {
162:
163: TreeNode item = (TreeNode) tree.getModel().getRoot();
164:
165: if (pFile != null) {
166: outputItem(tree, pFile, item, "");
167: }
168:
169: }
170:
171: /**
172: * Given a tree control this function will save the contents to a file
173: *
174: * @param tree The tree control that should be saved.
175: * @param pFile The file that should be opened and saved to
176: * @param item The parent HTREEITEM that is being saved
177: * @param tabs Controls indent so that the file indent level reflects the child items
178: * indent in the tree.
179: */
180: protected void outputItem(JTree tree, BufferedWriter pFile,
181: TreeNode item, String tabs) {
182:
183: if (pFile != null) {
184:
185: if (item != null) {
186:
187: // USES_CONVERSION;
188:
189: String sText = tabs;
190:
191: sText += item.toString();
192: sText += System.getProperty("line.separator");
193:
194: // Output the item text
195: // const int len = sText.GetLength() * sizeof(WCHAR);
196: // LPSTR szTemp = new char[len + 2];
197: // WideCharToMultiByte(CP_ACP, 0, sText, -1, szTemp, len, NULL, NULL);
198: // fprintf(pFile, szTemp);
199: // delete[] szTemp;
200:
201: try {
202: pFile.write(sText);
203: } catch (IOException e) {
204: e.printStackTrace();
205: }
206:
207: if (item.getChildCount() >= 0) {
208: for (Enumeration e = item.children(); e
209: .hasMoreElements();) {
210: TreeNode n = (TreeNode) e.nextElement();
211: outputItem(tree, pFile, n, tabs + "\t");
212: }
213: }
214: }
215: }
216: }
217:
218: }
|