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.netbeans;
053:
054: import javax.swing.*;
055: import org.openide.cookies.*;
056: import org.openide.nodes.Node;
057: import org.openide.util.HelpCtx;
058: import org.openide.util.NbBundle;
059: import org.openide.util.actions.*;
060: import org.openide.util.Lookup;
061: import org.openide.windows.WindowManager;
062: import org.openide.windows.TopComponent;
063: import org.netbeans.modules.java.JavaDataObject;
064:
065: import org.acm.seguin.ide.common.IDEPlugin;
066:
067: /**
068: * Checks for Cut and paste in the current buffer
069: *
070: *@author Mike Atkinson
071: *@created October 1, 2003
072: */
073: public class CPDCheckBufferAction extends CookieAction {
074:
075: /**
076: * Gets the helpCtx attribute of the CPDCheckBufferAction object
077: *
078: *@return The helpCtx value
079: */
080: public HelpCtx getHelpCtx() {
081: return HelpCtx.DEFAULT_HELP;
082: // (PENDING) context help
083: // return new HelpCtx (CPDCheckBufferAction.class);
084: }
085:
086: /**
087: * Gets the menuPresenter attribute of the CPDCheckBufferAction object
088: *
089: *@return The menuPresenter value
090: */
091: // public JMenuItem getMenuPresenter() {
092: // JMenuItem item = new JMenuItem(getName());
093: // item.addActionListener(this);
094: // return item;
095: // }
096:
097: /**
098: * Gets the name attribute of the CPDCheckBufferAction object
099: *
100: *@return The name value
101: */
102: public String getName() {
103: return NbBundle.getMessage(CPDCheckBufferAction.class,
104: "LBL_CPDCheckBufferAction");
105: }
106:
107: /**
108: * Description of the Method
109: *
110: *@return Description of the Return Value
111: */
112: protected Class[] cookieClasses() {
113: //return new Class[]{EditorCookie.class, org.netbeans.modules.java.JavaNode.class, org.netbeans.modules.java.JavaDataObject.class};
114: return new Class[] { JavaDataObject.class };
115: }
116:
117: /**
118: * Perform special enablement check in addition to the normal one.
119: *
120: *@param nodes Description of the Parameter
121: *@return Description of the Return Value
122: */
123: protected boolean enable(Node[] nodes) {
124: if (!super .enable(nodes)) {
125: return false;
126: }
127: // Any additional checks ...
128: return true;
129: }
130:
131: /**
132: * Get the path to the icon to be displayed in menus, etc
133: *
134: *@return Icon to be displayed in menus, etc.
135: */
136: protected String iconResource() {
137: return "org/acm/seguin/ide/netbeans/CPDCheckBufferActionIcon.gif";
138: }
139:
140: /**
141: * Perform extra initialization of this action's singleton. PLEASE do not
142: * use constructors for this purpose!
143: */
144: protected void initialize() {
145: super .initialize();
146: putProperty(CPDCheckBufferAction.SHORT_DESCRIPTION, NbBundle
147: .getMessage(CPDCheckBufferAction.class,
148: "HINT_CPDCheckBufferAction"));
149: }
150:
151: /**
152: *@return MODE_EXACTLY_ONE
153: */
154: protected int mode() {
155: return MODE_EXACTLY_ONE;
156: }
157:
158: /** this means that performAction is expecting to be run in the event thread */
159: protected boolean asynchronous() {
160: return false;
161: }
162:
163: /**
164: * Description of the Method
165: *
166: *@param nodes Description of the Parameter
167: */
168: protected void performAction(Node[] nodes) {
169: JRefactory.ensureVisible();
170: /*
171: java.util.Set modes = org.openide.windows.WindowManager.getDefault().getModes();
172: java.util.Iterator i = modes.iterator();
173: System.err.println("===Modes===");
174: while (i.hasNext()) {
175: System.err.println("mode="+i.next());
176: }
177: org.openide.windows.Mode mode = org.openide.windows.WindowManager.getDefault().findMode("editor");
178: System.err.println("===Top Components===");
179: TopComponent[] tc = mode.getTopComponents();
180: for (int n=0; n<tc.length; n++) {
181: System.err.println("tc="+tc[n]);
182: }
183: System.err.println("===Nodes===");
184: for (int n=0; n<nodes.length; n++) {
185: System.err.println("node="+nodes[n].getClass());
186: Node parent = nodes[n].getParentNode();
187: while (parent!=null) {
188: System.err.println(" parent="+parent);
189: parent = parent.getParentNode();
190: }
191: }
192: */
193: JavaDataObject cookie = (JavaDataObject) nodes[0]
194: .getCookie(JavaDataObject.class);
195: //System.err.println("cookie x="+cookie);
196: //if (cookie instanceof org.netbeans.modules.java.JavaEditor){
197: // org.openide.loaders.MultiDataObject mdo = ((org.netbeans.modules.java.JavaEditor)cookie).findDataObject();
198: //}
199: java.awt.Frame frame = org.openide.windows.WindowManager
200: .getDefault().getMainWindow();
201: try {
202: IDEPlugin.cpdBuffer(frame, cookie);
203: } catch (java.io.IOException e) {
204: e.printStackTrace();
205: }
206: }
207:
208: /**
209: * Sets the string in the IDE
210: *
211: *@param text The new StringInIDE value
212: */
213: protected void setStringInIDE(String text) {
214: //_editorPane.setText(text);
215: getCurrentEditorPane().setText(text);
216: }
217:
218: /**
219: * Gets the CurrentEditorPane attribute of the NetBeansExtractMethodDialog
220: * object
221: *
222: *@return The CurrentEditorPane value
223: */
224: private JEditorPane getCurrentEditorPane() {
225: return getCurrentEditorPane(null);
226: }
227:
228: /**
229: * Gets the CurrentEditorPane attribute of the NetBeansExtractMethodDialog
230: * object
231: *
232: *@param cookie Description of Parameter
233: *@return The CurrentEditorPane value
234: */
235: private JEditorPane getCurrentEditorPane(EditorCookie cookie) {
236: WindowManager wm = (WindowManager) Lookup.getDefault().lookup(
237: WindowManager.class);
238: TopComponent comp = wm.getRegistry().getActivated();
239: Node[] nodes = comp.getRegistry().getActivatedNodes();
240:
241: //(NOTE) This is a hack fix
242: cookie = null;
243: if (nodes.length == 1) {
244: cookie = (EditorCookie) nodes[0]
245: .getCookie(EditorCookie.class);
246: }
247:
248: JEditorPane[] panes = cookie.getOpenedPanes();
249: if (panes != null && panes.length == 1) {
250: return panes[0];
251: }
252:
253: return null;
254: }
255:
256: /**
257: * Gets the initial string from the IDE
258: *
259: *@return The file in string format
260: */
261: protected String getStringFromIDE() {
262: //return _editorPane.getText();
263: JEditorPane pane = getCurrentEditorPane();
264: return (pane == null) ? "" : pane.getText();
265: }
266: }
|