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.Actions; 27 import org.openide.util.NbBundle; 28 import org.openide.util.WeakListeners; 29 import org.openide.windows.TopComponent; 30 31 import javax.swing.*; 32 import java.awt.*; 33 import java.beans.PropertyChangeEvent ; 34 import java.beans.PropertyChangeListener ; 35 36 37 41 public class MaximizeWindowAction extends AbstractAction { 42 43 private final PropertyChangeListener propListener; 44 private TopComponent topComponent; 45 private boolean isPopup; 46 47 public MaximizeWindowAction() { 48 propListener = new PropertyChangeListener () { 49 public void propertyChange(PropertyChangeEvent evt) { 50 String propName = evt.getPropertyName(); 51 if(WindowManagerImpl.PROP_MAXIMIZED_MODE.equals(propName) 52 || WindowManagerImpl.PROP_EDITOR_AREA_STATE.equals(evt.getPropertyName()) 53 || WindowManagerImpl.PROP_ACTIVE_MODE.equals(evt.getPropertyName())) { 54 updateState(); 55 } 56 if (TopComponent.Registry.PROP_ACTIVATED.equals(propName)) { 58 updateState(); 59 } 60 } 61 }; 62 TopComponent.Registry registry = TopComponent.getRegistry(); 63 registry.addPropertyChangeListener(WeakListeners.propertyChange(propListener, registry)); 64 WindowManagerImpl wm = WindowManagerImpl.getInstance(); 65 wm.addPropertyChangeListener(WeakListeners.propertyChange(propListener, wm)); 66 67 updateState(); 68 } 69 76 public MaximizeWindowAction (TopComponent tc) { 77 topComponent = tc; 78 propListener = null; 79 isPopup = true; 80 updateState(); 81 } 82 83 84 public void actionPerformed (java.awt.event.ActionEvent ev) { 85 WindowManagerImpl wm = WindowManagerImpl.getInstance(); 86 TopComponent curTC = getTCToWorkWith(); 87 88 if(wm.isDocked(curTC)) { 89 ModeImpl mode = (ModeImpl)wm.findMode(curTC); 91 String tcID = wm.findTopComponentID( curTC ); 92 93 if( mode.getKind() == Constants.MODE_KIND_SLIDING ) { 94 wm.userToggledTopComponentSlideInMaximize( tcID ); 96 } else if( null != mode ) { 97 ModeImpl previousMax = wm.getCurrentMaximizedMode(); 98 if( null != previousMax ) { 99 if( previousMax.getKind() == Constants.MODE_KIND_EDITOR && mode.getKind() == Constants.MODE_KIND_VIEW ) { 100 wm.switchMaximizedMode( mode ); 101 } else { 102 wm.switchMaximizedMode( null ); 103 } 104 } else { 105 wm.switchMaximizedMode( mode ); 106 } 107 } else { 108 wm.switchMaximizedMode( null ); 109 } 110 } else { 111 ModeImpl curMax = (ModeImpl)wm.findMode(curTC); 113 if (curMax != null) { 114 if(curMax.getFrameState() == Frame.NORMAL) { 115 curMax.setFrameState(Frame.MAXIMIZED_BOTH); 116 } else { 117 curMax.setFrameState(Frame.NORMAL); 118 } 119 } 120 } 121 122 updateState(); 123 } 124 125 128 private void updateState() { 129 if (SwingUtilities.isEventDispatchThread()) { 130 doUpdateState(); 131 } else { 132 SwingUtilities.invokeLater(new Runnable () { 133 public void run() { 134 doUpdateState(); 135 } 136 }); 137 } 138 } 139 140 142 private void doUpdateState() { 143 WindowManagerImpl wm = WindowManagerImpl.getInstance(); 144 TopComponent active = getTCToWorkWith(); 145 Object param = active == null ? "" : active.getName(); boolean maximize; 147 ModeImpl activeMode = (ModeImpl)wm.findMode(active); 148 if (activeMode == null) { 149 return; 150 } 151 152 if (wm.isDocked(active)) { 153 maximize = wm.getCurrentMaximizedMode() != activeMode; 154 } else { 155 maximize = activeMode.getFrameState() == Frame.NORMAL; 156 } 157 158 if (activeMode != null && activeMode.getKind() == Constants.MODE_KIND_SLIDING) { 159 maximize = null != active && !wm.isTopComponentMaximizedWhenSlidedIn( wm.findTopComponentID( active ) ); 160 } 161 162 String label; 163 if(maximize) { 164 label = NbBundle.getMessage(MaximizeWindowAction.class, "CTL_MaximizeWindowAction", param); 165 } else { 166 label = NbBundle.getMessage(MaximizeWindowAction.class, "CTL_UnmaximizeWindowAction", param); 167 } 168 putValue(Action.NAME, (isPopup ? Actions.cutAmpersand(label) : label)); 169 170 setEnabled(activeMode != null ); 171 } 172 173 private TopComponent getTCToWorkWith () { 174 if (topComponent != null) { 175 return topComponent; 176 } 177 return TopComponent.getRegistry().getActivated(); 178 } 179 180 182 public void putValue(String key, Object newValue) { 183 if (Action.ACCELERATOR_KEY.equals(key)) { 184 ActionUtils.putSharedAccelerator("MaximizeWindow", newValue); 185 } else { 186 super.putValue(key, newValue); 187 } 188 } 189 190 192 public Object getValue(String key) { 193 if (Action.ACCELERATOR_KEY.equals(key)) { 194 return ActionUtils.getSharedAccelerator("MaximizeWindow"); 195 } else { 196 return super.getValue(key); 197 } 198 } 199 200 } 201 202 | Popular Tags |