1 19 20 package org.netbeans.modules.xml.xam.ui.actions; 21 22 import java.awt.event.ActionEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 import javax.swing.Action ; 27 import javax.swing.JMenuItem ; 28 import javax.swing.event.ChangeListener ; 29 import javax.swing.event.EventListenerList ; 30 import org.netbeans.modules.xml.xam.Component; 31 import org.netbeans.modules.xml.xam.ui.XAMUtils; 32 import org.netbeans.modules.xml.xam.ui.cookies.GotoCookie; 33 import org.netbeans.modules.xml.xam.ui.cookies.ViewComponentCookie; 34 import org.openide.awt.Actions; 35 import org.openide.nodes.Node; 36 import org.openide.util.HelpCtx; 37 import org.openide.util.Lookup; 38 import org.openide.util.NbBundle; 39 import org.openide.util.actions.CookieAction; 40 import org.openide.util.actions.Presenter; 41 import org.openide.windows.WindowManager; 42 43 53 public class GoToAction extends CookieAction { 54 55 private static final long serialVersionUID = 1L; 56 private static ActSubMenuModel model = new ActSubMenuModel(null); 57 58 public String getName() { 59 return model.createName(); 60 } 61 62 68 private static GotoType[] getGotoTypes(Node[] activatedNodes) { 69 List <GotoType> types = new ArrayList <GotoType>(); 70 if (activatedNodes != null || activatedNodes.length == 1) { 71 Node node = activatedNodes[0]; 72 GotoCookie cookie = node.getCookie(GotoCookie.class); 73 if (cookie != null) { 74 for (GotoType type : cookie.getGotoTypes()) { 75 Component comp = type.getComponent(node); 76 ViewComponentCookie.View view = type.getView(); 78 if (XAMUtils.getViewCookie(comp, view) != null) { 79 types.add(type); 80 } 81 } 82 } 83 } 84 return types.toArray(new GotoType[types.size()]); 85 } 86 87 public HelpCtx getHelpCtx() { 88 return HelpCtx.DEFAULT_HELP; 89 } 90 91 protected void performAction(org.openide.nodes.Node[] activatedNodes) { 92 model.performActionAt(0); 93 } 94 95 public JMenuItem getMenuPresenter() { 96 return new Actions.SubMenu(this, model, false); 97 } 98 99 public JMenuItem getPopupPresenter() { 100 return new Actions.SubMenu(this, model, true); 101 } 102 103 protected int mode() { 104 return MODE_EXACTLY_ONE; 105 } 106 107 protected Class [] cookieClasses() { 108 return new Class [] { 109 GotoCookie.class 110 }; 111 } 112 113 protected boolean asynchronous() { 114 return false; 115 } 116 117 public Action createContextAwareInstance(Lookup actionContext) { 118 return new DelegateAction(this, actionContext); 119 } 120 121 122 private static class ActSubMenuModel extends EventListenerList implements Actions.SubMenuModel { 123 static final long serialVersionUID = -4273674308662494596L; 124 126 ActSubMenuModel(Lookup lookup) { 127 } 130 131 private Node[] nodes() { 132 return WindowManager.getDefault().getRegistry().getCurrentNodes(); 149 } 150 151 private String createName() { 152 GotoType[] types = getGotoTypes(nodes()); 153 if (types != null && types.length == 1) { 154 return NbBundle.getMessage(GoToAction.class, 155 "LBL_GoTo_Name", types[0].getName()); 156 } else { 157 return NbBundle.getMessage(GoToAction.class, "LBL_GoTo"); 158 } 159 } 160 161 public int getCount() { 162 return getGotoTypes(nodes()).length; 163 } 164 165 public String getLabel(int index) { 166 GotoType[] types = getGotoTypes(nodes()); 167 if (types.length <= index) { 168 return null; 169 } else { 170 return types[index].getName(); 171 } 172 } 173 174 public HelpCtx getHelpCtx(int index) { 175 GotoType[] types = getGotoTypes(nodes()); 176 if (types.length <= index) { 177 return null; 178 } else { 179 return types[index].getHelpCtx(); 180 } 181 } 182 183 public void performActionAt(int index) { 184 Node[] nodes = nodes(); 185 GotoType[] types = getGotoTypes(nodes); 186 if (types.length > index) { 187 types[index].show(nodes[0]); 188 } 189 } 190 191 193 public void addChangeListener(ChangeListener l) { 194 add(ChangeListener .class, l); 195 } 196 197 199 public void removeChangeListener(ChangeListener l) { 200 remove(ChangeListener .class, l); 201 } 202 } 203 204 209 private static final class DelegateAction implements 210 Action , Presenter.Menu, Presenter.Popup { 211 212 private final CookieAction delegate; 213 214 215 private final ActSubMenuModel model; 216 217 public DelegateAction(CookieAction a, Lookup actionContext) { 218 this.delegate = a; 219 this.model = new ActSubMenuModel(actionContext); 220 } 221 222 223 public String toString() { 224 return super.toString() + "[delegate=" + delegate + "]"; } 226 227 229 public void actionPerformed(ActionEvent e) { 230 model.performActionAt(0); 231 } 232 233 public void addPropertyChangeListener(PropertyChangeListener listener) { 234 } 235 236 public void removePropertyChangeListener(PropertyChangeListener listener) { 237 } 238 239 public void putValue(String key, Object o) { 240 } 241 242 public Object getValue(String key) { 243 if (Action.NAME.equals(key)) { 244 return model.createName(); 245 } else { 246 return delegate.getValue(key); 247 } 248 } 249 250 public boolean isEnabled() { 251 return model.getCount() > 0; 252 } 253 254 public void setEnabled(boolean b) { 255 } 256 257 public JMenuItem getMenuPresenter() { 258 return new Actions.SubMenu(this, model, false); 259 } 260 261 public JMenuItem getPopupPresenter() { 262 return new Actions.SubMenu(this, model, true); 263 } 264 } 265 } 266 | Popular Tags |