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-2006 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 threaddemo.apps.populate;
043:
044: import java.awt.FlowLayout;
045: import java.awt.Frame;
046: import java.io.File;
047: import java.io.FileOutputStream;
048: import java.io.IOException;
049: import java.io.OutputStream;
050: import java.io.PrintStream;
051: import javax.swing.BoundedRangeModel;
052: import javax.swing.JDialog;
053: import javax.swing.JLabel;
054: import javax.swing.JOptionPane;
055: import javax.swing.JProgressBar;
056: import javax.swing.SwingUtilities;
057: import javax.swing.WindowConstants;
058: import org.openide.xml.XMLUtil;
059: import org.w3c.dom.Document;
060: import org.w3c.dom.Element;
061: import org.w3c.dom.NodeList;
062:
063: // XXX offer a Cancel button
064:
065: /**
066: * Simple app to populate a file tree with some test data.
067: * @author Jesse Glick
068: */
069: public class Populate {
070:
071: /** Static usage only. */
072: private Populate() {
073: }
074:
075: /**
076: * Run the app.
077: * @param root root of the phadhail tree to add test data to
078: * @param app application frame (may be null)
079: */
080: public static void run(final File root, Frame app) {
081: String msg = "How many files should I create?";
082: String title = "Choose Size of Test Data";
083: Integer[] sizes = new Integer[] { new Integer(5),
084: new Integer(10), new Integer(25), new Integer(50),
085: new Integer(100), new Integer(250), new Integer(500),
086: new Integer(1000), new Integer(2500),
087: new Integer(5000), new Integer(10000),
088: new Integer(25000), };
089: Integer def = new Integer(500);
090: Integer i = (Integer) JOptionPane.showInputDialog(null, msg,
091: title, JOptionPane.QUESTION_MESSAGE, null, sizes, def);
092: if (i == null) {
093: // Cancelled.
094: return;
095: }
096: final int val = i.intValue();
097: final JProgressBar progress = new JProgressBar(0, val);
098: final JDialog dialog = new JDialog(app,
099: "Creating test files...", true);
100: dialog.getContentPane().setLayout(new FlowLayout());
101: JLabel label = new JLabel("Creating files:");
102: label.setLabelFor(progress);
103: dialog.getContentPane().add(label);
104: dialog.getContentPane().add(progress);
105: dialog.pack();
106: dialog
107: .setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
108: new Thread(new Runnable() {
109: public void run() {
110: try {
111: create(root, val, progress.getModel());
112: } catch (IOException e) {
113: e.printStackTrace();
114: }
115: SwingUtilities.invokeLater(new Runnable() {
116: public void run() {
117: dialog.setVisible(false);
118: }
119: });
120: }
121: }, "Populating test files").start();
122: dialog.setVisible(true);
123: }
124:
125: private static void create(File root, int val,
126: final BoundedRangeModel progress) throws IOException {
127: for (int i = 0; i < val; i++) {
128: final int progval = i;
129: SwingUtilities.invokeLater(new Runnable() {
130: public void run() {
131: progress.setValue(progval);
132: }
133: });
134: boolean xml = i % 5 > 2;
135: String fname = "file" + i + (xml ? ".xml" : ".txt");
136: int bit = 0;
137: int x = i;
138: while (x > 0) {
139: if (x % 3 == 0) {
140: fname = "dir" + bit + File.separatorChar + fname;
141: }
142: bit++;
143: x /= 3;
144: }
145: File tomake = new File(root, "test" + File.separatorChar
146: + fname);
147: tomake.getParentFile().mkdirs();
148: if (tomake.createNewFile()) {
149: OutputStream os = new FileOutputStream(tomake);
150: try {
151: if (xml) {
152: Document doc = createXML(i);
153: XMLUtil.write(doc, os, "UTF-8");
154: } else {
155: PrintStream ps = new PrintStream(os);
156: ps.println("Sample data for file #" + i);
157: ps.close();
158: }
159: } finally {
160: os.close();
161: }
162: }
163: }
164: }
165:
166: private static Document createXML(int n) {
167: Document doc = XMLUtil.createDocument("sample", null, null,
168: null);
169: Element el = doc.createElement("file");
170: el.setAttribute("number", Integer.toString(n));
171: doc.getDocumentElement().appendChild(el);
172: int count = (int) Math.pow(n + 20, .85);
173: for (int i = 0; i < count; i++) {
174: el = doc.getDocumentElement();
175: int x = i;
176: int bit = 0;
177: while (x > 0) {
178: if (x % 3 == 0) {
179: String tagname = "tag-" + bit;
180: Element el2;
181: NodeList nl = el.getElementsByTagName(tagname);
182: if (nl.getLength() < 3) {
183: el2 = doc.createElement("tag-" + bit);
184: el.appendChild(el2);
185: } else {
186: el2 = (Element) nl.item(0);
187: }
188: el = el2;
189: }
190: bit++;
191: x /= 3;
192: }
193: Element el2 = doc.createElement("datum");
194: el2.setAttribute("number", Integer.toString(i));
195: el.appendChild(el2);
196: }
197: return doc;
198: }
199:
200: }
|