1 19 20 package org.netbeans.modules.editor; 21 22 import java.awt.Component ; 23 import javax.swing.Action ; 24 import javax.swing.ActionMap ; 25 import javax.swing.JComponent ; 26 import javax.swing.JMenu ; 27 import javax.swing.JMenuItem ; 28 import javax.swing.JPopupMenu ; 29 import javax.swing.JSeparator ; 30 import javax.swing.text.JTextComponent ; 31 import org.netbeans.editor.BaseAction; 32 import org.netbeans.editor.BaseKit; 33 import org.netbeans.editor.Settings; 34 import org.netbeans.editor.SettingsNames; 35 import org.netbeans.editor.Utilities; 36 import org.openide.util.HelpCtx; 37 import org.openide.util.NbBundle; 38 import org.openide.awt.DynamicMenuContent; 39 import org.openide.util.actions.Presenter; 40 41 46 public class NbCodeFoldingAction extends GlobalContextAction implements Presenter.Menu{ 47 48 49 50 public NbCodeFoldingAction() { 51 } 52 53 public final HelpCtx getHelpCtx() { 54 return HelpCtx.DEFAULT_HELP; 55 } 56 57 public String getName() { 58 return NbBundle.getBundle(NbCodeFoldingAction.class).getString( 59 "Menu/View/CodeFolds"); } 61 62 public void resultChanged(org.openide.util.LookupEvent ev) { 63 } 64 65 public boolean isEnabled() { 66 return false; 67 } 68 69 72 public JMenuItem getMenuPresenter(){ 73 return new CodeFoldsMenu(getName()); 74 } 75 76 private static JTextComponent getComponent(){ 77 return Utilities.getFocusedComponent(); 78 } 79 80 public void actionPerformed (java.awt.event.ActionEvent ev){ 81 } 82 83 private BaseKit getKit(){ 84 JTextComponent component = getComponent(); 85 return (component == null) ? BaseKit.getKit(NbEditorKit.class) : Utilities.getKit(component); 86 } 87 88 private static Object getSettingValue(BaseKit kit, String settingName) { 89 return Settings.getValue(kit.getClass(), settingName); 90 } 91 92 95 private static boolean getSettingBoolean(BaseKit kit, String settingName) { 96 Boolean val = (Boolean )getSettingValue(kit, settingName); 97 return (val != null) ? val.booleanValue() : false; 98 } 99 100 101 private boolean isFoldingEnabledInSettings(BaseKit kit){ 102 return getSettingBoolean(kit, SettingsNames.CODE_FOLDING_ENABLE); 103 } 104 105 106 public class CodeFoldsMenu extends JMenu implements DynamicMenuContent { 107 public CodeFoldsMenu(){ 108 super(); 109 } 110 111 public CodeFoldsMenu(String s){ 112 super(s); 113 org.openide.awt.Mnemonics.setLocalizedText(this, s); 117 } 118 119 public JComponent [] getMenuPresenters() { 120 return new JComponent [] { this }; 121 } 122 123 public JComponent [] synchMenuPresenters(JComponent [] items) { 124 getPopupMenu(); 125 return items; 126 } 127 128 public JPopupMenu getPopupMenu(){ 129 JPopupMenu pm = super.getPopupMenu(); 130 pm.removeAll(); 131 boolean enable = false; 132 BaseKit bKit = getKit(); 133 if (bKit==null) bKit = BaseKit.getKit(NbEditorKit.class); 134 if (bKit!=null){ 135 Action action = bKit.getActionByName(NbEditorKit.generateFoldPopupAction); 136 if (action instanceof BaseAction){ 137 boolean foldingAvailable = isFoldingEnabledInSettings(bKit); 138 JTextComponent component = Utilities.getFocusedComponent(); 139 if (foldingAvailable){ 140 ActionMap contextActionmap = getContextActionMap(); 141 if (contextActionmap!=null){ 142 foldingAvailable = contextActionmap.get(BaseKit.collapseFoldAction) != null && 143 component != null; 144 145 if (!foldingAvailable){ 146 bKit = BaseKit.getKit(NbEditorKit.class); 147 if (bKit!=null){ 148 Action defaultAction = bKit.getActionByName(NbEditorKit.generateFoldPopupAction); 149 if (defaultAction instanceof BaseAction) action = defaultAction; 150 } 151 } 152 } 153 } 154 155 JMenu menu = (JMenu )((BaseAction)action).getPopupMenuItem(foldingAvailable ? component : null); 156 if (menu!=null){ 157 Component comps[] = menu.getMenuComponents(); 158 for (int i=0; i<comps.length; i++){ 159 pm.add(comps[i]); 160 if (comps[i].isEnabled() && !(comps[i] instanceof JSeparator )) { 161 enable = true; 162 } 163 } 164 } 165 } 166 } 167 setEnabled(enable); 168 pm.pack(); 169 return pm; 170 } 171 } 172 173 } 174 | Popular Tags |