1 19 20 package org.netbeans.core.windows.actions; 21 22 23 import org.netbeans.core.windows.Constants; 24 import org.netbeans.core.windows.ModeImpl; 25 import org.netbeans.core.windows.WindowManagerImpl; 26 import org.openide.awt.JMenuPlus; 27 import org.openide.util.NbBundle; 28 import org.openide.util.WeakListeners; 29 import org.openide.util.actions.Presenter; 30 import org.openide.windows.TopComponent; 31 import org.openide.windows.WindowManager; 32 33 import javax.swing.*; 34 import java.awt.event.ActionEvent ; 35 import java.beans.PropertyChangeEvent ; 36 import java.beans.PropertyChangeListener ; 37 import java.util.Iterator ; 38 import java.util.Set ; 39 40 41 46 public class AttachWindowAction extends AbstractAction 47 implements Presenter.Menu { 48 49 50 private static final String PROP_ACTION_CHANGED = "actionChanged"; 53 private final PropertyChangeListener propListener; 54 55 56 public AttachWindowAction() { 57 propListener = new PropertyChangeListener () { 58 public void propertyChange(PropertyChangeEvent evt) { 59 String propName = evt.getPropertyName(); 60 if(TopComponent.Registry.PROP_ACTIVATED.equals(propName) 61 || WindowManager.PROP_MODES.equals(propName) 62 || WindowManagerImpl.PROP_EDITOR_AREA_STATE.equals(propName)) { 63 updateState(); 64 } 65 } 66 }; 67 TopComponent.Registry registry = TopComponent.getRegistry(); 68 registry.addPropertyChangeListener(WeakListeners.propertyChange(propListener, registry)); 69 70 WindowManagerImpl.getInstance().addPropertyChangeListener( 71 WeakListeners.propertyChange(propListener, WindowManagerImpl.getInstance())); 72 73 updateState(); 74 } 75 76 public void actionPerformed(ActionEvent evt) {} 77 78 private void updateState() { 79 TopComponent active = TopComponent.getRegistry().getActivated(); 80 Object param = active == null ? "" : active.getName(); putValue(Action.NAME, NbBundle.getMessage(MaximizeWindowAction.class, "CTL_AttachWindowAction", param)); 82 83 if(WindowManagerImpl.getInstance().getEditorAreaState() == Constants.EDITOR_AREA_SEPARATED) { 86 setEnabled(false); 87 } else { 88 setEnabled(active != null); 89 } 90 91 firePropertyChange(PROP_ACTION_CHANGED, null, null); 93 } 94 95 96 97 public JMenuItem getMenuPresenter() { 98 JMenuItem mi = new LazyPopup(this); 99 org.openide.awt.Actions.connect(mi, this, false); 100 return mi; 101 } 102 103 104 106 private static class LazyPopup extends JMenuPlus implements PropertyChangeListener { 107 108 109 private boolean created = false; 110 111 112 113 public LazyPopup(Action action) { 114 setText((String )action.getValue(Action.NAME)); 115 116 action.addPropertyChangeListener(WeakListeners.propertyChange(this, action)); 117 } 118 119 120 121 public JPopupMenu getPopupMenu() { 122 if(!created) { 123 createMenuItems(); 124 } 125 126 return super.getPopupMenu(); 127 } 128 129 130 private void createMenuItems() { 131 created = true; 132 removeAll(); 133 134 ModeImpl editorMode = (ModeImpl)WindowManagerImpl.getInstance().findMode("editor"); if(editorMode != null) { 136 add(new ModeMenu(editorMode, NbBundle.getMessage(AttachWindowAction.class, "CTL_Documents"), true)); 137 add(new JSeparator()); 138 } 139 140 Set modeSet = WindowManagerImpl.getInstance().getModes(); 141 142 for(Iterator it = modeSet.iterator(); it.hasNext(); ) { 143 ModeImpl mode = (ModeImpl)it.next(); 144 145 if("editor".equals(mode.getName())) { continue; 147 } 148 149 if(!mode.getOpenedTopComponents().isEmpty()) { 150 TopComponent tc = mode.getSelectedTopComponent(); 151 if(tc != null) { 152 String name = tc.getName(); 153 add(new ModeMenu(mode, name == null ? "null" : name, false)); } 155 } 156 } 157 } 158 159 160 public void propertyChange(PropertyChangeEvent evt) { 161 if(Action.NAME.equals(evt.getPropertyName())) { 162 setText((String )evt.getNewValue()); 163 } else if(PROP_ACTION_CHANGED.equals(evt.getPropertyName())) { created = false; 165 } 166 } 167 } 169 170 171 private static class ModeMenu extends JMenu { 172 public ModeMenu(ModeImpl mode, String name, boolean editor) { 173 super(name); 174 175 TopComponent active = TopComponent.getRegistry().getActivated(); 176 if(mode.canContain(active)) { 177 if(!editor) { 178 add(new AttachAction(mode, null)); 179 add(new JSeparator()); 180 } 181 182 add(new AttachAction(mode, Constants.TOP)); 183 add(new AttachAction(mode, Constants.LEFT)); 184 add(new AttachAction(mode, Constants.BOTTOM)); 185 add(new AttachAction(mode, Constants.RIGHT)); 186 } else { 187 setEnabled(false); 188 } 189 } 190 } 192 193 194 private static class AttachAction extends AbstractAction { 195 private final ModeImpl mode; 196 private final String side; 197 198 public AttachAction(ModeImpl mode, String side) { 199 this.mode = mode; 200 this.side = side; 201 202 String key; 203 if(side == null) { 204 key = "CTL_SideAsLastTab"; TopComponent active = TopComponent.getRegistry().getActivated(); 206 if(mode.getOpenedTopComponents().contains(active)) { 207 setEnabled(false); 208 } 209 } else if(side == Constants.TOP) { 210 key = "CTL_SideTop"; } else if(side == Constants.LEFT) { 212 key = "CTL_SideLeft"; } else if(side == Constants.BOTTOM) { 214 key = "CTL_SideBottom"; } else if(side == Constants.RIGHT) { 216 key = "CTL_SideRight"; } else { 218 return; 219 } 220 221 putValue(Action.NAME, NbBundle.getMessage(AttachWindowAction.class, key)); 222 } 223 224 public void actionPerformed(ActionEvent evt) { 225 WindowManagerImpl wm = WindowManagerImpl.getInstance(); 226 ModeImpl activeMode = wm.getActiveMode(); 227 if(activeMode == null) { 228 return; 229 } 230 231 TopComponent selected = activeMode.getSelectedTopComponent(); 232 if(selected == null || !mode.canContain(selected)) { 233 return; 234 } 235 236 if(side == null) { 237 mode.dockInto(selected); 238 selected.open(); 239 selected.requestActive(); 240 } else { 241 wm.attachTopComponentToSide(selected, mode, side); 242 } 243 } 244 } 246 } 247 248 | Popular Tags |