001: /*
002: JavaStylePlugin.java - a Java pretty printer plugin for jEdit
003: Copyright (c) 1999 Andreas "Mad" Schaefer (andreas.schaefer@madplanet.ch)
004: Copyright (c) 2000,2001 Dirk Moebius (dmoebius@gmx.net)
005: jEdit buffer options:
006: :tabSize=4:indentSize=4:noTabs=false:maxLineLen=0:
007: This program is free software; you can redistribute it and/or
008: modify it under the terms of the GNU General Public License
009: as published by the Free Software Foundation; either version 2
010: of the License, or any later version.
011: This program is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU General Public License for more details.
015: You should have received a copy of the GNU General Public License
016: along with this program; if not, write to the Free Software
017: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: */
019: package org.acm.seguin.ide.jedit;
020:
021: import org.gjt.sp.jedit.View;
022: import org.gjt.sp.jedit.Buffer;
023: import org.gjt.sp.jedit.jEdit;
024: import org.gjt.sp.jedit.textarea.JEditTextArea;
025:
026: import java.awt.Component;
027: import java.awt.Container;
028: import javax.swing.JMenu;
029: import javax.swing.JMenuItem;
030: import java.awt.event.ContainerListener;
031: import java.awt.event.ContainerEvent;
032: import javax.swing.event.MenuListener;
033: import javax.swing.event.MenuEvent;
034: import net.sourceforge.jrefactory.action.CurrentSummary;
035: import org.acm.seguin.summary.MethodSummary;
036: import org.acm.seguin.summary.FieldSummary;
037:
038: /**
039: * A plugin for pretty printing the current jEdit buffer, using the PrettyPrinter of the JREFactory suite.
040: *
041: * @author Mike Atkinson (<a href="mailto:javastyle@ladyshot.demon.co.uk"> Mike@ladyshot.demon.co.uk</a> )
042: * @author Dirk Moebius (<a href="mailto:dmoebius@gmx.net"> dmoebius@gmx.net </a> )
043: * @since 1.0
044: * @created 14 July 2003
045: * @version $Version: $
046: */
047: public class GreyOutMenuFrig {
048: private static View view = null;
049: private static Object javastyleMenuFound = null;
050:
051: private static MenuListener javastyleMenuListener = new JavastyleMenuListener();
052: private static MenuListener javastyleMenuListener2 = new JavastyleMenuListener2();
053:
054: /**
055: * Description of the Method
056: *
057: * @since v 1.0
058: */
059: public static void checkMenus() {
060: view = jEdit.getActiveView();
061: if (view == null) {
062: return;
063: }
064: javax.swing.JMenuBar menuBar = view.getJMenuBar();
065: javax.swing.MenuElement[] menus = menuBar.getSubElements();
066: for (int i = 0; i < menus.length; i++) {
067: if (menus[i] instanceof JMenu) {
068: String label = ((JMenu) menus[i]).getLabel();
069: //System.out.println("menu[" + i + "] = \"" + label + "\"");
070: if ("Plugins".equals(label)) {
071: if (javastyleMenuFound != menus[i]) {
072: addMenuListener((JMenu) menus[i],
073: javastyleMenuListener);
074: javastyleMenuFound = menus[i];
075: }
076: break;
077: }
078: }
079: }
080:
081: }
082:
083: /**
084: * Description of the Class
085: *
086: * @author Mike Atkinson MRA
087: * @since v 1.0
088: */
089: private static class JavastyleMenuListener implements MenuListener {
090: /**
091: * Description of the Method
092: *
093: * @param e Description of Parameter
094: * @since v 1.0
095: */
096: public void menuCanceled(MenuEvent e) {
097: //System.out.println("JavaStyleMenuListener: menuCanceled("+e+")");
098: }
099:
100: /**
101: * Description of the Method
102: *
103: * @param e Description of Parameter
104: * @since v 1.0
105: */
106: public void menuDeselected(MenuEvent e) {
107: //System.out.println("JavaStyleMenuListener: menuDeselected("+e+")");
108: }
109:
110: /**
111: * Description of the Method
112: *
113: * @param e Description of Parameter
114: * @since v 1.0
115: */
116: public void menuSelected(MenuEvent e) {
117: //System.out.println("JavaStyleMenuListener: menuSelected(" + e + ")");
118: JMenuItem source = (JMenuItem) e.getSource();
119: if (source instanceof JMenu) {
120: int count = ((JMenu) source).getItemCount();
121: //System.out.println(" itemCount="+count);
122: for (int i = 0; i < count; i++) {
123: Component item = ((JMenu) source)
124: .getMenuComponent(i);
125: if (item != null && item instanceof JMenu) {
126: if ("JavaStyle".equals(((JMenu) item)
127: .getLabel())) {
128: addMenuListener((JMenu) item,
129: javastyleMenuListener2);
130: } else {
131: addMenuListener((JMenu) item,
132: javastyleMenuListener);
133: }
134: }
135: }
136: }
137: }
138: }
139:
140: /**
141: * Description of the Class
142: *
143: * @author Mike Atkinson MRA
144: * @since v 1.0
145: */
146: private static class JavastyleMenuListener2 implements MenuListener {
147: /**
148: * Description of the Method
149: *
150: * @param e Description of Parameter
151: * @since v 1.0
152: */
153: public void menuCanceled(MenuEvent e) {
154: //System.out.println("JavaStyleMenuListener2: menuCanceled("+e+")");
155: }
156:
157: /**
158: * Description of the Method
159: *
160: * @param e Description of Parameter
161: * @since v 1.0
162: */
163: public void menuDeselected(MenuEvent e) {
164: //System.out.println("JavaStyleMenuListener2: menuDeselected("+e+")");
165: }
166:
167: /**
168: * Description of the Method
169: *
170: * @param e Description of Parameter
171: * @since v 1.0
172: */
173: public void menuSelected(MenuEvent e) {
174: //System.out.println("JavaStyleMenuListener2: menuSelected("+e+")");
175: JMenuItem source = (JMenuItem) e.getSource();
176: if (source instanceof JMenu) {
177: int count = ((JMenu) source).getItemCount();
178: //System.out.println(" itemCount="+count);
179: for (int i = 0; i < count; i++) {
180: Component item = ((JMenu) source)
181: .getMenuComponent(i);
182: if (item != null && item instanceof JMenuItem) {
183: String label = ((JMenuItem) item).getLabel();
184: if ("Reformat Buffer".equals(label)) {
185: //System.out.println(" grey out reformat buffer?");
186: Buffer buffer = view.getBuffer();
187: item.setEnabled(buffer.getName().endsWith(
188: ".java"));
189: } else if ("Check current buffer".equals(label)) {
190: //System.out.println(" grey out reformat buffer?");
191: Buffer buffer = view.getBuffer();
192: item.setEnabled(buffer.getName().endsWith(
193: ".java"));
194: } else if ("In Current File".equals(label)) {
195: //System.out.println(" grey out reformat buffer?");
196: Buffer buffer = view.getBuffer();
197: item.setEnabled(buffer.getName().endsWith(
198: ".java"));
199: } else if ("Refactor".equals(label)) {
200: //System.out.println(" grey out method refactorings?");
201: try {
202: CurrentSummary cs = CurrentSummary
203: .get();
204: JEditTextArea textArea = view
205: .getTextArea();
206: Object summary = cs.getCurrentSummary();
207: item.setEnabled(summary != null);
208: addMenuListener((JMenu) item,
209: javastyleMenuListener2);
210: } catch (Exception ex) {
211: item.setEnabled(false);
212: }
213:
214: } else if ("Method refactorings".equals(label)) {
215: //System.out.println(" grey out method refactorings?");
216: try {
217: CurrentSummary cs = CurrentSummary
218: .get();
219: JEditTextArea textArea = view
220: .getTextArea();
221: Object summary = cs.getCurrentSummary();
222: if (summary != null && textArea != null) {
223: //System.out.println("JavaStylePlugin.getLineNumber() -> " + textArea.getCaretLine());
224: cs.lineNo = textArea.getCaretLine();
225: item
226: .setEnabled(summary != null
227: && summary instanceof MethodSummary);
228: } else {
229: item.setEnabled(false);
230: }
231: } catch (Exception ex) {
232: item.setEnabled(false);
233: }
234:
235: } else if ("Field refactorings".equals(label)) {
236: //System.out.println(" grey out field refactorings?");
237: try {
238: CurrentSummary cs = CurrentSummary
239: .get();
240: JEditTextArea textArea = view
241: .getTextArea();
242: Object summary = cs.getCurrentSummary();
243: if (summary != null && textArea != null) {
244: //System.out.println("JavaStylePlugin.getLineNumber() -> " + textArea.getCaretLine());
245: cs.lineNo = textArea.getCaretLine();
246: item
247: .setEnabled(summary != null
248: && summary instanceof FieldSummary);
249: } else {
250: item.setEnabled(false);
251: }
252: } catch (Exception ex) {
253: item.setEnabled(false);
254: }
255: } else if (item != null
256: && item instanceof JMenu) {
257: addMenuListener((JMenu) item,
258: javastyleMenuListener2);
259: }
260: }
261: }
262: }
263: }
264: }
265:
266: public static void addMenuListener(JMenu item,
267: MenuListener menuListener) {
268: MenuListener[] listeners = item.getMenuListeners();
269: if (listeners == null) {
270: item.addMenuListener(menuListener);
271: } else {
272: for (int i = 0; i < listeners.length; i++) {
273: if (listeners[i] == menuListener) {
274: return;
275: }
276: item.addMenuListener(menuListener);
277: }
278: }
279: }
280: }
|