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: package org.netbeans.modules.refactoring.spi.impl;
042:
043: import java.io.IOException;
044: import java.io.Reader;
045: import java.io.Writer;
046: import java.util.HashMap;
047: import java.util.WeakHashMap;
048: import java.util.logging.Level;
049: import java.util.logging.Logger;
050: import javax.swing.text.BadLocationException;
051: import javax.swing.text.Document;
052: import org.netbeans.api.diff.DiffController;
053: import org.netbeans.api.diff.Difference;
054: import org.netbeans.api.diff.StreamSource;
055: import org.netbeans.modules.refactoring.api.impl.SPIAccessor;
056: import org.netbeans.modules.refactoring.spi.SimpleRefactoringElementImplementation;
057: import org.netbeans.modules.refactoring.spi.ui.UI;
058: import org.openide.filesystems.FileObject;
059: import org.openide.text.CloneableEditorSupport;
060: import org.openide.util.Lookup;
061: import org.openide.util.NbBundle;
062: import org.openide.util.lookup.Lookups;
063:
064: /**
065: *
066: * @author Jan Becicka
067: */
068: public class PreviewManager {
069:
070: private class Pair {
071: DiffController dc;
072: NewDiffSource source;
073:
074: Pair(DiffController dc, NewDiffSource source) {
075: this .dc = dc;
076: this .source = source;
077: }
078: }
079:
080: private static PreviewManager manager;
081: private WeakHashMap<RefactoringPanel, HashMap<FileObject, Pair>> map = new WeakHashMap();
082:
083: private PreviewManager() {
084: }
085:
086: public static PreviewManager getDefault() {
087: if (manager == null)
088: manager = new PreviewManager();
089: return manager;
090: }
091:
092: public void clean(RefactoringPanel panel) {
093: map.remove(panel);
094: }
095:
096: private Pair getPair(SimpleRefactoringElementImplementation element) {
097: RefactoringPanel current = RefactoringPanelContainer
098: .getRefactoringComponent().getCurrentPanel();
099: HashMap<FileObject, Pair> m = map.get(current);
100: if (m != null) {
101: Pair pair = m.get(element.getParentFile());
102: if (pair != null)
103: return pair;
104: }
105: NewDiffSource nds;
106: try {
107: DiffController diffView = DiffController.create(
108: new OldDiffSource(element),
109: nds = new NewDiffSource(element));
110: if (m == null) {
111: m = new HashMap<FileObject, Pair>();
112: map.put(current, m);
113: }
114: Pair p = new Pair(diffView, nds);
115: m.put(element.getParentFile(), p);
116: return p;
117: } catch (IOException ioe) {
118: throw (RuntimeException) new RuntimeException()
119: .initCause(ioe);
120: }
121: }
122:
123: public void refresh(SimpleRefactoringElementImplementation element) {
124: try {
125: String newText = SPIAccessor.DEFAULT
126: .getNewFileContent(element);
127: if (newText == null) {
128: UI.setComponentForRefactoringPreview(null);
129: return;
130: }
131: Pair p = getPair(element);
132: UI.setComponentForRefactoringPreview(p.dc.getJComponent());
133: p.source.setNewText(newText);
134: if (element.getPosition() != null)
135: p.dc.setLocation(DiffController.DiffPane.Base,
136: DiffController.LocationType.LineNumber, element
137: .getPosition().getBegin().getLine());
138: } catch (IOException ioe) {
139: throw (RuntimeException) new RuntimeException()
140: .initCause(ioe);
141: }
142: }
143:
144: private class OldDiffSource extends StreamSource {
145: private FileObject file;
146:
147: OldDiffSource(SimpleRefactoringElementImplementation r) {
148: this .file = r.getParentFile();
149: }
150:
151: public String getName() {
152: if (file.isFolder()) {
153: return NbBundle.getMessage(PreviewManager.class,
154: "LBL_FileDoesNotExist");
155: }
156: return file.getName();
157: }
158:
159: public String getTitle() {
160: if (file.isFolder()) {
161: return NbBundle.getMessage(PreviewManager.class,
162: "LBL_FileDoesNotExist");
163: }
164: return file.getNameExt();
165: }
166:
167: public String getMIMEType() {
168: return file.getMIMEType();
169: }
170:
171: public Reader createReader() throws IOException {
172: return null;
173: }
174:
175: public Writer createWriter(Difference[] conflicts)
176: throws IOException {
177: return null;
178: }
179:
180: public Lookup getLookup() {
181: return Lookups.singleton(file);
182: }
183:
184: }
185:
186: private class NewDiffSource extends StreamSource {
187: private SimpleRefactoringElementImplementation element;
188:
189: NewDiffSource(SimpleRefactoringElementImplementation r) {
190: this .element = r;
191: }
192:
193: public String getName() {
194: return NbBundle.getMessage(PreviewManager.class,
195: "LBL_ProposedRefactoring");
196: }
197:
198: public String getTitle() {
199: if (element.getParentFile().isFolder()) {
200: return NbBundle.getMessage(PreviewManager.class,
201: "LBL_NewFile");
202: }
203: return NbBundle.getMessage(PreviewManager.class,
204: "LBL_Refactored", element.getParentFile()
205: .getNameExt());
206: }
207:
208: public String getMIMEType() {
209: if (element.getParentFile().isFolder()) {
210: //this is hack, all folders are text/x-java
211: return "text/x-java"; //NOI18N
212: }
213: return element.getParentFile().getMIMEType();
214: }
215:
216: public Reader createReader() throws IOException {
217: return null;
218: }
219:
220: public Writer createWriter(Difference[] conflicts)
221: throws IOException {
222: return null;
223: }
224:
225: private Document internal;
226:
227: private Document getDocument() {
228: if (internal == null) {
229: internal = CloneableEditorSupport.getEditorKit(
230: getMIMEType()).createDefaultDocument();
231: }
232: return internal;
233: }
234:
235: public Lookup getLookup() {
236: return Lookups.singleton(getDocument());
237: }
238:
239: public void setNewText(String r) {
240: try {
241: internal.remove(0, internal.getLength());
242: internal.insertString(0, r, null);
243: } catch (BadLocationException ex) {
244: Logger.getLogger(getClass().getName()).log(
245: Level.SEVERE, ex.getMessage(), ex);
246: }
247: }
248: }
249: }
|