1 19 20 package org.netbeans.modules.refactoring.spi; 21 22 import java.awt.Container ; 23 import java.io.IOException ; 24 import java.io.InputStreamReader ; 25 import java.io.Reader ; 26 import javax.swing.JEditorPane ; 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 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 [] 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 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 r2 = getReader(); 103 if (r2!=null) { 104 DiffView diffView = null; 105 Reader 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 ioe) { 115 throw (RuntimeException ) new RuntimeException ().initCause(ioe); 116 } 117 } 118 119 123 public Reader getReader() { 124 return null; 125 } 126 127 private Reader getFirstReader() throws IOException { 128 PositionRef beginPos=getPosition().getBegin(); 129 CloneableEditorSupport editSupp=beginPos.getCloneableEditorSupport(); 130 return new InputStreamReader (editSupp.getInputStream()); 131 } 132 133 private String getMimeType() { 134 return getParentFile().getMIMEType(); 135 } 136 137 private static final TopComponent getTopComponent(Container 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 |