1 19 20 package org.netbeans.core.multiview; 21 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.lang.ref.WeakReference ; 25 import javax.swing.AbstractAction ; 26 import javax.swing.ButtonGroup ; 27 import javax.swing.JComponent ; 28 import javax.swing.JMenu ; 29 import javax.swing.JMenuItem ; 30 import javax.swing.JRadioButtonMenuItem ; 31 import org.netbeans.core.api.multiview.MultiViewHandler; 32 import org.netbeans.core.api.multiview.MultiViewPerspective; 33 import org.netbeans.core.api.multiview.MultiViews; 34 import org.openide.awt.DynamicMenuContent; 35 import org.openide.awt.Mnemonics; 36 import org.openide.text.CloneableEditorSupport; 37 import org.openide.util.NbBundle; 38 import org.openide.util.actions.Presenter; 39 import org.openide.windows.Mode; 40 import org.openide.windows.TopComponent; 41 import org.openide.windows.WindowManager; 42 43 47 public class EditorsAction extends AbstractAction 48 implements Presenter.Menu { 49 50 public EditorsAction() { 51 super(NbBundle.getMessage(EditorsAction.class, "CTL_EditorsAction")); 52 } 53 54 public void actionPerformed(ActionEvent ev) { 55 assert false; } 57 58 public JMenuItem getMenuPresenter() { 59 JMenu menu = new UpdatingMenu(); 60 String label = NbBundle.getMessage(EditorsAction.class, "CTL_EditorsAction"); 61 Mnemonics.setLocalizedText(menu, label); 62 return menu; 63 } 64 65 private static final class UpdatingMenu extends JMenu implements DynamicMenuContent { 66 67 public JComponent [] synchMenuPresenters(JComponent [] items) { 68 return getMenuPresenters(); 69 } 70 71 public JComponent [] getMenuPresenters() { 72 Mode mode = WindowManager.getDefault().findMode(CloneableEditorSupport.EDITOR_MODE); 73 removeAll(); 74 if (mode != null) { 75 TopComponent tc = mode.getSelectedTopComponent(); 76 if (tc != null) { 77 setEnabled(true); 78 MultiViewHandler handler = MultiViews.findMultiViewHandler(tc); 79 if (handler != null) { 80 final WeakReference handlerRef = new WeakReference (handler); 81 ButtonGroup group = new ButtonGroup (); 82 MultiViewPerspective[] pers = handler.getPerspectives(); 83 for (int i = 0; i < pers.length; i++) { 84 MultiViewPerspective thisPers = pers[i]; 85 final WeakReference persRef = new WeakReference (thisPers); 86 87 JRadioButtonMenuItem item = new JRadioButtonMenuItem (thisPers.getDisplayName()); 88 item.addActionListener(new ActionListener () { 89 public void actionPerformed(ActionEvent event) { 90 MultiViewHandler handler = (MultiViewHandler)handlerRef.get(); 92 MultiViewPerspective thisPers = (MultiViewPerspective)persRef.get(); 93 if (handler != null && thisPers != null) { 94 handler.requestActive(thisPers); 95 } 96 } 97 }); 98 if (thisPers.getDisplayName().equals(handler.getSelectedPerspective().getDisplayName())) { 99 item.setSelected(true); 100 } 101 group.add(item); 102 add(item); 103 } 104 } else { JRadioButtonMenuItem but = new JRadioButtonMenuItem (NbBundle.getMessage(EditorsAction.class, "EditorsAction.source")); 106 but.setSelected(true); 107 add(but); 108 } 109 } else { setEnabled(false); 111 } 112 } else { setEnabled(false); 114 } 115 return new JComponent [] {this}; 116 } 117 118 } 119 120 } 121 | Popular Tags |