001: /* ====================================================================
002: * The JRefactory License, Version 1.0
003: *
004: * Copyright (c) 2001 JRefactory. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by the
021: * JRefactory (http://www.sourceforge.org/projects/jrefactory)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. The names "JRefactory" must not be used to endorse or promote
026: * products derived from this software without prior written
027: * permission. For written permission, please contact seguin@acm.org.
028: *
029: * 5. Products derived from this software may not be called "JRefactory",
030: * nor may "JRefactory" appear in their name, without prior written
031: * permission of Chris Seguin.
032: *
033: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
034: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
035: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
036: * DISCLAIMED. IN NO EVENT SHALL THE CHRIS SEGUIN OR
037: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
038: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
039: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
040: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
041: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
042: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
043: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
044: * SUCH DAMAGE.
045: * ====================================================================
046: *
047: * This software consists of voluntary contributions made by many
048: * individuals on behalf of JRefactory. For more information on
049: * JRefactory, please see
050: * <http://www.sourceforge.org/projects/jrefactory>.
051: */
052: package org.acm.seguin.ide.elixir;
053:
054: import com.elixirtech.fx.*;
055: import com.elixirtech.ide.edit.*;
056: import com.elixirtech.ui.LineEditorPane;
057: import java.awt.Component;
058: import java.awt.Container;
059: import javax.swing.JPanel;
060: import javax.swing.text.JTextComponent;
061: import javax.swing.JOptionPane;
062: import org.acm.seguin.awt.ExceptionPrinter;
063: import org.acm.seguin.refactor.Refactoring;
064: import org.acm.seguin.refactor.method.ExtractMethodRefactoring;
065: import org.acm.seguin.refactor.RefactoringException;
066: import net.sourceforge.jrefactory.uml.refactor.ExtractMethodDialog;
067:
068: /**
069: * ExtractMethod for the elixir editor.
070: *
071: *@author Chris Seguin
072: *@created October 18, 2001
073: *@date May 31, 1999
074: */
075: public class ElixirExtractMethod extends ExtractMethodDialog {
076: private BasicViewManager bvm;
077:
078: /**
079: * Create an ElixirPrettyPrinter object
080: *
081: *@exception RefactoringException Description of the Exception
082: */
083: public ElixirExtractMethod() throws RefactoringException {
084: super (FrameManager.current().getFrame());
085: }
086:
087: /**
088: * Sets the string in the IDE
089: *
090: *@param value The new file contained in a string
091: */
092: protected void setStringInIDE(String value) {
093: bvm.setContentsString(value);
094: }
095:
096: /**
097: * Gets the SelectionFromIDE attribute of the ElixirExtractMethod object
098: *
099: *@return The SelectionFromIDE value
100: */
101: protected String getSelectionFromIDE() {
102: try {
103: Object view = bvm.getView();
104: System.out.println("View is a : "
105: + view.getClass().getName());
106: JPanel panel = (JPanel) view;
107:
108: Component editor = searchPanels(panel, " ");
109:
110: if (editor instanceof JTextComponent) {
111: JTextComponent comp = (JTextComponent) editor;
112: return comp.getSelectedText();
113: }
114:
115: System.out.println("Not a text component");
116: return null;
117: } catch (Throwable thrown) {
118: thrown.printStackTrace(System.out);
119: }
120: return null;
121: }
122:
123: /**
124: * Gets the initial string from the IDE
125: *
126: *@return The file in string format
127: */
128: protected String getStringFromIDE() {
129: FrameManager fm = FrameManager.current();
130: bvm = (BasicViewManager) fm.getViewSite()
131: .getCurrentViewManager();
132: if (bvm == null) {
133: return null;
134: }
135:
136: return bvm.getContentsString();
137: }
138:
139: /**
140: * Reformats the current source code
141: */
142: public static void extractMethod() {
143: try {
144: System.out.println("extract method #1");
145: ElixirExtractMethod eem = new ElixirExtractMethod();
146: System.out.println("extract method #2");
147: eem.show();
148: System.out.println("extract method #3");
149: } catch (RefactoringException re) {
150: JOptionPane.showMessageDialog(null, re.getMessage(),
151: "Refactoring Exception", JOptionPane.ERROR_MESSAGE);
152: }
153: }
154:
155: /**
156: * Remove \r from buffer
157: *
158: *@param input Description of Parameter
159: *@return a string containing the results
160: */
161: public String removeCR(String input) {
162: StringBuffer buffer = new StringBuffer();
163: int last = input.length();
164:
165: for (int ndx = 0; ndx < last; ndx++) {
166: char ch = input.charAt(ndx);
167: if (ch == '\r') {
168: // Do nothing
169: } else {
170: buffer.append(ch);
171: }
172: }
173:
174: return buffer.toString();
175: }
176:
177: /**
178: * Useful program that searches through the different panels to find the
179: * text panel that we can get the selected code from.
180: *
181: *@param jPanel Description of Parameter
182: *@param prefix Description of Parameter
183: *@return Description of the Returned Value
184: */
185: private Component searchPanels(Container jPanel, String prefix) {
186: int last = jPanel.getComponentCount();
187: for (int ndx = 0; ndx < last; ndx++) {
188: Component next = jPanel.getComponent(ndx);
189: System.out.println(prefix + ":" + ndx + " "
190: + next.getClass().getName());
191: if (next instanceof LineEditorPane) {
192: return next;
193: }
194: if (next instanceof Container) {
195: Component result = searchPanels((Container) next,
196: prefix + ":" + ndx);
197: if (result != null) {
198: return result;
199: }
200: }
201: }
202: return null;
203: }
204: }
205: // EOF
|