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;
043:
044: import java.awt.BorderLayout;
045: import java.awt.Component;
046: import java.awt.EventQueue;
047: import java.awt.event.ActionEvent;
048: import java.awt.event.ActionListener;
049: import java.awt.event.WindowAdapter;
050: import java.awt.event.WindowEvent;
051: import java.io.File;
052: import javax.swing.BoxLayout;
053: import javax.swing.ButtonGroup;
054: import javax.swing.JButton;
055: import javax.swing.JFrame;
056: import javax.swing.JPanel;
057: import javax.swing.JRadioButton;
058: import javax.swing.WindowConstants;
059: import threaddemo.apps.index.IndexApp;
060: import threaddemo.apps.populate.Populate;
061: import threaddemo.apps.refactor.Refactor;
062: import threaddemo.model.Phadhail;
063: import threaddemo.model.PhadhailEvent;
064: import threaddemo.model.PhadhailListener;
065: import threaddemo.model.PhadhailNameEvent;
066: import threaddemo.model.Phadhails;
067: import threaddemo.views.PhadhailViews;
068:
069: /**
070: * Demonstrate various models and views for big data sets.
071: * @author Jesse Glick
072: */
073: public final class Main extends JFrame {
074:
075: private static JFrame mainFrame;
076:
077: public static void main(String[] args) {
078: File root;
079: if (args.length == 1) {
080: root = new File(args[0]);
081: if (!root.exists()) {
082: root.mkdirs();
083: }
084: } else {
085: root = File.listRoots()[0];
086: }
087: mainFrame = new Main(root);
088: mainFrame
089: .setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
090: mainFrame.setLocation(0, 0);
091: // #35804: needs to happen in EQ, since it grabs tree lock etc.
092: EventQueue.invokeLater(new Runnable() {
093: public void run() {
094: mainFrame.pack();
095: mainFrame.setVisible(true);
096: }
097: });
098: }
099:
100: private final File root;
101: private final JRadioButton synchButton, monitoredButton,
102: lockedButton, eventHybridLockedButton, spunButton,
103: swungButton, nodeButton, lookNodeButton, lookButton,
104: rawButton;
105:
106: private Main(File root) {
107: super ("Thread Demo [" + root.getAbsolutePath() + "]");
108: this .root = root;
109: getContentPane().setLayout(
110: new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
111: JPanel modelPanel1 = new JPanel();
112: JPanel modelPanel2 = new JPanel();
113: ButtonGroup modelGroup = new ButtonGroup();
114: synchButton = new JRadioButton("Synchronous", true);
115: synchButton.setMnemonic('y');
116: monitoredButton = new JRadioButton("Monitored", false);
117: monitoredButton.setMnemonic('m');
118: lockedButton = new JRadioButton("Locked", false);
119: lockedButton.setMnemonic('k');
120: eventHybridLockedButton = new JRadioButton(
121: "Event-Hybrid-Locked", false);
122: eventHybridLockedButton.setMnemonic('e');
123: spunButton = new JRadioButton("Spun", false);
124: spunButton.setMnemonic('u');
125: swungButton = new JRadioButton("Swung", false);
126: swungButton.setMnemonic('w');
127: modelGroup.add(synchButton);
128: modelGroup.add(monitoredButton);
129: modelGroup.add(lockedButton);
130: modelGroup.add(eventHybridLockedButton);
131: modelGroup.add(spunButton);
132: modelGroup.add(swungButton);
133: modelPanel1.add(synchButton);
134: modelPanel1.add(monitoredButton);
135: modelPanel1.add(lockedButton);
136: modelPanel2.add(eventHybridLockedButton);
137: modelPanel2.add(spunButton);
138: modelPanel2.add(swungButton);
139: getContentPane().add(modelPanel1);
140: getContentPane().add(modelPanel2);
141: JPanel viewPanel = new JPanel();
142: ButtonGroup viewGroup = new ButtonGroup();
143: nodeButton = new JRadioButton("Node", false);
144: nodeButton.setMnemonic('n');
145: lookNodeButton = new JRadioButton("Look Node", true);
146: lookNodeButton.setMnemonic('o');
147: lookButton = new JRadioButton("Look", false);
148: lookButton.setMnemonic('l');
149: rawButton = new JRadioButton("Raw", false);
150: rawButton.setMnemonic('r');
151: viewGroup.add(nodeButton);
152: viewGroup.add(lookNodeButton);
153: viewGroup.add(lookButton);
154: viewGroup.add(rawButton);
155: viewPanel.add(rawButton);
156: viewPanel.add(nodeButton);
157: viewPanel.add(lookNodeButton);
158: viewPanel.add(lookButton);
159: getContentPane().add(viewPanel);
160: JButton showB = new JButton("Show");
161: showB.addActionListener(new ActionListener() {
162: public void actionPerformed(ActionEvent ev) {
163: showView();
164: }
165: });
166: JButton popB = new JButton("Populate Test Files");
167: popB.setMnemonic('p');
168: popB.addActionListener(new ActionListener() {
169: public void actionPerformed(ActionEvent ev) {
170: Populate.run(Main.this .root, Main.this );
171: }
172: });
173: JPanel bPanel = new JPanel();
174: bPanel.add(showB);
175: bPanel.add(popB);
176: getContentPane().add(bPanel);
177: getContentPane().add(new Monitor());
178: getRootPane().setDefaultButton(showB);
179: }
180:
181: private void showView() {
182: // Clear caches first!
183: System.gc();
184: System.runFinalization();
185: System.gc();
186: final Phadhail model;
187: final String modelType;
188: if (synchButton.isSelected()) {
189: model = Phadhails.synchronous(root);
190: modelType = "Synchronous";
191: } else if (monitoredButton.isSelected()) {
192: model = Phadhails.monitored(root);
193: modelType = "Monitored";
194: } else if (lockedButton.isSelected()) {
195: model = Phadhails.locked(root);
196: modelType = "Locked";
197: } else if (eventHybridLockedButton.isSelected()) {
198: model = Phadhails.eventHybridLocked(root);
199: modelType = "Event-Hybrid-Locked";
200: } else if (spunButton.isSelected()) {
201: model = Phadhails.spun(root);
202: modelType = "Spun";
203: } else {
204: assert swungButton.isSelected();
205: model = Phadhails.swung(root);
206: modelType = "Swung";
207: }
208: Component view;
209: final String viewType;
210: if (nodeButton.isSelected()) {
211: view = PhadhailViews.nodeView(model);
212: viewType = "Node";
213: } else if (lookNodeButton.isSelected()) {
214: view = PhadhailViews.lookNodeView(model);
215: viewType = "Look Node";
216: } else if (lookButton.isSelected()) {
217: view = PhadhailViews.lookView(model);
218: viewType = "Look";
219: } else {
220: assert rawButton.isSelected();
221: view = PhadhailViews.rawView(model);
222: viewType = "Raw";
223: }
224: final JFrame frame = new JFrame();
225: // For the benefit of Swung model which will produce the root path asynch:
226: final PhadhailListener l = new PhadhailListener() {
227: public void nameChanged(PhadhailNameEvent ev) {
228: frame.setTitle(modelType + " " + viewType + ": "
229: + model.getPath());
230: }
231:
232: public void childrenChanged(PhadhailEvent ev) {
233: }
234: };
235: l.nameChanged(null);
236: model.addPhadhailListener(l);
237: frame.getContentPane().add(view);
238: JButton indexB = new JButton("View Index");
239: indexB.setMnemonic('i');
240: indexB.addActionListener(new ActionListener() {
241: public void actionPerformed(ActionEvent ev) {
242: viewIndex(model);
243: }
244: });
245: JButton refactorB = new JButton("Refactor");
246: refactorB.setMnemonic('r');
247: refactorB.addActionListener(new ActionListener() {
248: public void actionPerformed(ActionEvent ev) {
249: Refactor.run(model, frame);
250: }
251: });
252: JPanel bPanel = new JPanel();
253: bPanel.add(indexB);
254: bPanel.add(refactorB);
255: frame.getContentPane().add(bPanel, BorderLayout.SOUTH);
256: frame.addWindowListener(new WindowAdapter() {
257: public void windowClosing(WindowEvent ev) {
258: model.removePhadhailListener(l);
259: frame.removeWindowListener(this );
260: // Just to make sure the view is collected:
261: frame.getContentPane().removeAll();
262: }
263: });
264: frame
265: .setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
266: frame.pack();
267: frame.setSize(500, 500);
268: frame.setLocation(mainFrame.getX() + mainFrame.getWidth(), 0);
269: frame.setVisible(true);
270: }
271:
272: private void viewIndex(Phadhail model) {
273: new IndexApp(model).setVisible(true);
274: }
275:
276: }
|