KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > spi > SimpleRefactoringElementImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.refactoring.spi;
21
22 import java.awt.Container JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.io.Reader JavaDoc;
26 import javax.swing.JEditorPane JavaDoc;
27 import org.netbeans.api.diff.Diff;
28 import org.netbeans.api.diff.DiffView;
29 import org.netbeans.api.diff.StreamSource;
30 import org.netbeans.modules.refactoring.spi.impl.ParametersPanel;
31 import org.netbeans.modules.refactoring.spi.ui.UI;
32 import org.openide.DialogDisplayer;
33 import org.openide.NotifyDescriptor;
34 import org.openide.filesystems.FileObject;
35 import org.openide.text.CloneableEditorSupport;
36 import org.openide.text.PositionBounds;
37 import org.openide.text.PositionRef;
38 import org.openide.util.NbBundle;
39 import org.openide.windows.TopComponent;
40
41 /**
42  * Default implementation of RefactoringElementImplementation interface.
43  * It contains implementations of
44  * @see RefactoringElementImplementation#isEnabled()
45  * @see RefactoringElementImplementation#setEnabled(boolean)
46  * @see RefactoringElementImplementation#getStatus()
47  * @see RefactoringElementImplementation#setStatus(int) and
48  * @see RefactoringElementImplementation#openInEditor()
49  * @see RefactoringElementImplementation#showPreview()
50  * @author Jan Becicka
51  * @see RefactoringElementImplementation
52  * @since 1.5.0
53  */

54 public abstract class SimpleRefactoringElementImpl implements RefactoringElementImplementation {
55     
56     private boolean enabled = true;
57     private int status = NORMAL;
58     
59     public boolean isEnabled() {
60         return enabled;
61     }
62     
63     public void setEnabled(boolean enabled) {
64         this.enabled = enabled;
65     }
66     
67     public int getStatus() {
68         return status;
69     }
70     
71     public void setStatus(int status) {
72         this.status = status;
73     }
74     
75     public void openInEditor() {
76         
77         PositionBounds bounds = getPosition();
78         if (bounds == null)
79             return;
80         
81         PositionRef beginPos=bounds.getBegin();
82         CloneableEditorSupport editSupp=beginPos.getCloneableEditorSupport();
83         editSupp.edit();
84         JEditorPane JavaDoc[] panes=editSupp.getOpenedPanes();
85         
86         if (panes!=null) {
87             panes[0].setCaretPosition(bounds.getEnd().getOffset());
88             panes[0].moveCaretPosition(beginPos.getOffset());
89             getTopComponent(panes[0]).requestActive();
90         } else {
91             // todo (#pf): what to do if there is no pane? -- now, there
92
// is a error message. I'm not sure, maybe this code will be
93
// never called.
94
DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
95                     NbBundle.getMessage(ParametersPanel.class,"ERR_ErrorOpeningEditor"))
96                     );
97         }
98     }
99     
100     public void showPreview() {
101         try {
102             Reader JavaDoc r2 = getReader();
103             if (r2!=null) {
104                 DiffView diffView = null;
105                 Reader JavaDoc r1 = getFirstReader();
106                 FileObject f = getParentFile();
107                 diffView = Diff.getDefault().createDiff(
108                         StreamSource.createSource(f.getName(), f.getNameExt(), getMimeType(), r1),
109                         StreamSource.createSource("Proposed refactoring", "Refactored " + f.getNameExt(), getMimeType(), r2));
110                 UI.setComponentForRefactoringPreview(diffView.getComponent());
111             } else {
112                 UI.setComponentForRefactoringPreview(null);
113             }
114         } catch (IOException JavaDoc ioe) {
115             throw (RuntimeException JavaDoc) new RuntimeException JavaDoc().initCause(ioe);
116         }
117     }
118     
119     /**
120      * return reader, which will provide state of file after refactoring
121      * default implementation will show diff between getParentFile and this reader
122      */

123     public Reader JavaDoc getReader() {
124         return null;
125     }
126     
127     private Reader JavaDoc getFirstReader() throws IOException JavaDoc {
128         PositionRef beginPos=getPosition().getBegin();
129         CloneableEditorSupport editSupp=beginPos.getCloneableEditorSupport();
130         return new InputStreamReader JavaDoc(editSupp.getInputStream());
131     }
132     
133     private String JavaDoc getMimeType() {
134         return getParentFile().getMIMEType();
135     }
136
137     private static final TopComponent getTopComponent(Container JavaDoc temp) {
138         while (!(temp instanceof TopComponent)) {
139             temp = temp.getParent();
140         }
141         return (TopComponent) temp;
142     }
143     
144     public void undoChange() {
145     }
146     
147 }
148
Popular Tags