1 19 package org.netbeans.modules.tasklist.core; 20 21 import java.beans.PropertyChangeEvent ; 22 import java.beans.PropertyChangeListener ; 23 import java.lang.ref.WeakReference ; 24 import javax.swing.SwingUtilities ; 25 import org.netbeans.modules.tasklist.core.*; 26 import org.openide.awt.Actions; 27 28 import org.openide.util.HelpCtx; 29 import org.openide.util.actions.BooleanStateAction; 30 import org.openide.util.actions.Presenter; 31 import org.openide.windows.Mode; 32 import org.openide.windows.TopComponent; 33 import org.openide.windows.WindowManager; 34 35 38 public abstract class ToggleViewAction extends BooleanStateAction implements 39 PropertyChangeListener , Presenter.Menu { 40 41 private static final long serialVersionUID = 1; 42 43 private boolean block; 44 private String mode = "output"; private WeakReference activated; 46 47 public javax.swing.JMenuItem getMenuPresenter() { 48 return new Actions.MenuItem(this, true); 49 } 50 51 public void setBooleanState(final boolean value) { 52 super.setBooleanState(value); 53 if (block) 54 return; 55 block = true; 56 57 SwingUtilities.invokeLater(new Runnable () { 59 public void run() { 60 toggleView(value); 61 block = false; 62 } 63 }); 64 } 65 66 71 protected abstract TopComponent getView(); 72 73 80 protected boolean isViewOpened() { 81 return getView().isOpened(); 82 } 83 84 89 private void toggleView(boolean visible) { 90 if (visible == isViewOpened()) 91 return; 92 93 TopComponent view = getView(); 94 WindowManager wm = WindowManager.getDefault(); 95 if (!visible) { 96 Mode mode = wm.findMode(view); 97 if (mode != null) 98 this.mode = mode.getName(); 99 view.close(); 100 if (activated != null) { 101 TopComponent act = (TopComponent) activated.get(); 102 if (act != null) 103 act.requestActive(); 104 } 105 } else { 106 TopComponent act = WindowManager.getDefault(). 107 getRegistry().getActivated(); 108 if (act == null) 109 activated = null; 110 else 111 activated = new WeakReference (act); 112 Mode mode = wm.findMode(this.mode); 113 if (mode != null) { 114 mode.dockInto(view); 115 } 116 view.open(); 117 view.requestVisible(); 118 view.requestActive(); 119 } 120 } 121 122 protected void initialize() { 123 super.initialize(); 124 WindowManager.getDefault().addPropertyChangeListener(this); 125 WindowManager.getDefault().getRegistry().addPropertyChangeListener(this); 126 putProperty(PROP_BOOLEAN_STATE, Boolean.valueOf(isViewOpened())); 127 } 128 129 public HelpCtx getHelpCtx() { 130 return HelpCtx.DEFAULT_HELP; 131 } 132 133 public void propertyChange(PropertyChangeEvent e) { 134 String p = e.getPropertyName(); 135 if (p.equals(TopComponent.Registry.PROP_OPENED)) { 136 super.setBooleanState(isViewOpened()); 137 } 138 } 139 } 140 | Popular Tags |