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.refactor;
043:
044: import java.awt.Dimension;
045: import java.awt.FlowLayout;
046: import java.awt.Frame;
047: import java.awt.event.ActionEvent;
048: import java.awt.event.ActionListener;
049: import java.io.IOException;
050: import java.util.ArrayList;
051: import java.util.HashMap;
052: import java.util.Iterator;
053: import java.util.List;
054: import java.util.Map;
055: import java.util.logging.Level;
056: import java.util.logging.Logger;
057: import javax.swing.BoundedRangeModel;
058: import javax.swing.DefaultBoundedRangeModel;
059: import javax.swing.JButton;
060: import javax.swing.JDialog;
061: import javax.swing.JLabel;
062: import javax.swing.JProgressBar;
063: import javax.swing.SwingUtilities;
064: import javax.swing.WindowConstants;
065: import org.openide.cookies.SaveCookie;
066: import org.w3c.dom.Document;
067: import org.w3c.dom.Element;
068: import org.w3c.dom.Node;
069: import org.w3c.dom.NodeList;
070: import threaddemo.data.DomProvider;
071: import threaddemo.data.PhadhailLookups;
072: import threaddemo.locking.LockAction;
073: import threaddemo.model.Phadhail;
074:
075: /**
076: * Simulates some big model-based refactoring of files.
077: * In this case, increments the number in all tag-nnn elements in XML files.
078: * @author Jesse Glick
079: */
080: public class Refactor {
081:
082: private static final Logger logger = Logger
083: .getLogger(Refactor.class.getName());
084:
085: /** No instances. */
086: private Refactor() {
087: }
088:
089: /**
090: * Begin a refactoring session.
091: * Call from event thread; will proceed in its own thread.
092: * @param root the root of the tree of phadhails to work on
093: * @param app owner app, or null
094: */
095: public static void run(final Phadhail root, Frame app) {
096: final Map<Phadhail, DomProvider> data = collectData(root);
097: final BoundedRangeModel progress = new DefaultBoundedRangeModel();
098: progress.setMinimum(0);
099: progress.setMaximum(data.size());
100: progress.setValue(0);
101: final JProgressBar progressBar = new JProgressBar(progress);
102: progressBar.setStringPainted(true);
103: Dimension d = progressBar.getPreferredSize();
104: d.width = 500;
105: progressBar.setPreferredSize(d);
106: final JDialog dialog = new JDialog(app, "Refactoring...", false);
107: JLabel label = new JLabel("Progress:");
108: label.setLabelFor(progressBar);
109: final boolean[] cancelled = new boolean[] { false };
110: JButton cancel = new JButton("Cancel");
111: cancel.addActionListener(new ActionListener() {
112: public void actionPerformed(ActionEvent ev) {
113: cancelled[0] = true;
114: }
115: });
116: dialog.getContentPane().setLayout(new FlowLayout());
117: dialog.getContentPane().add(label);
118: dialog.getContentPane().add(progressBar);
119: dialog.getContentPane().add(cancel);
120: dialog.pack();
121: dialog
122: .setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
123: dialog.setVisible(true);
124: new Thread(new Runnable() {
125: public void run() {
126: Iterator<Map.Entry<Phadhail, DomProvider>> it = data
127: .entrySet().iterator();
128: while (it.hasNext()) {
129: Map.Entry<Phadhail, DomProvider> e = it.next();
130: if (cancelled[0]) {
131: break;
132: }
133: // Avoid keeping a reference to the old data, since we have
134: // cached DomProvider's and such heavyweight stuff open on them:
135: it.remove();
136: final Phadhail ph = e.getKey();
137: logger.log(Level.FINER, "Refactoring {0}", ph);
138: final DomProvider p = e.getValue();
139: ph.lock().read(new Runnable() {
140: public void run() {
141: final String path = ph.getPath();
142: SwingUtilities.invokeLater(new Runnable() {
143: public void run() {
144: progress.setValue(progress
145: .getValue() + 1);
146: progressBar.setString(path);
147: }
148: });
149: }
150: });
151: ph.lock().write(new Runnable() {
152: public void run() {
153: SaveCookie s = (SaveCookie) PhadhailLookups
154: .getLookup(ph).lookup(
155: SaveCookie.class);
156: refactor(p);
157: if (s == null) {
158: // Was unmodified before, so save it now.
159: s = (SaveCookie) PhadhailLookups
160: .getLookup(ph).lookup(
161: SaveCookie.class);
162: if (s != null) {
163: try {
164: s.save();
165: } catch (IOException e) {
166: e.printStackTrace();
167: }
168: }
169: }
170: }
171: });
172: }
173: SwingUtilities.invokeLater(new Runnable() {
174: public void run() {
175: dialog.setVisible(false);
176: }
177: });
178: }
179: }, "Refactoring").start();
180: }
181:
182: private static Map<Phadhail, DomProvider> collectData(
183: final Phadhail root) {
184: return root.lock().read(
185: new LockAction<Map<Phadhail, DomProvider>>() {
186: private final Map<Phadhail, DomProvider> data = new HashMap<Phadhail, DomProvider>();
187:
188: public Map<Phadhail, DomProvider> run() {
189: collect(root);
190: return data;
191: }
192:
193: private void collect(Phadhail ph) {
194: if (ph.hasChildren()) {
195: for (Phadhail child : ph.getChildren()) {
196: collect(child);
197: }
198: } else {
199: DomProvider p = (DomProvider) PhadhailLookups
200: .getLookup(ph).lookup(
201: DomProvider.class);
202: if (p != null) {
203: data.put(ph, p);
204: }
205: }
206: }
207: });
208: }
209:
210: private static void refactor(DomProvider p) {
211: final Document doc;
212: try {
213: doc = p.getDocument();
214: } catch (IOException e) {
215: e.printStackTrace();
216: return;
217: }
218: NodeList nl = doc.getElementsByTagName("*");
219: final List<Element> l = new ArrayList<Element>();
220: for (int i = 0; i < nl.getLength(); i++) {
221: l.add((Element) nl.item(i));
222: }
223: p.isolatingChange(new Runnable() {
224: public void run() {
225: for (Element el : l) {
226: String tagname = el.getTagName();
227: if (tagname.startsWith("tag-")) {
228: int n = Integer.parseInt(tagname.substring(4));
229: tagname = "tag-" + (n + 1);
230: Element el2 = doc.createElement(tagname);
231: Node parent = el.getParentNode();
232: parent.insertBefore(el2, el);
233: NodeList nl2 = el.getChildNodes();
234: while (nl2.getLength() > 0) {
235: el2.appendChild(nl2.item(0));
236: }
237: parent.removeChild(el);
238: }
239: }
240: }
241: });
242: /*
243: org.apache.xml.serialize.XMLSerializer ser = new org.apache.xml.serialize.XMLSerializer(System.err, new org.apache.xml.serialize.OutputFormat(doc, "UTF-8", true));
244: try {
245: ser.serialize(doc);
246: } catch (IOException e) {
247: e.printStackTrace();
248: }
249: */
250: }
251:
252: }
|