1 19 package org.netbeans.modules.web.struts.editor; 20 21 import java.awt.Dialog ; 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.util.ArrayList ; 25 import javax.swing.JButton ; 26 import javax.swing.JMenu ; 27 import javax.swing.JPanel ; 28 import javax.swing.JPopupMenu ; 29 import javax.swing.JSeparator ; 30 import javax.swing.text.Document ; 31 import javax.swing.text.JTextComponent ; 32 import org.netbeans.editor.BaseAction; 33 import org.netbeans.editor.BaseDocument; 34 import org.netbeans.modules.editor.NbEditorUtilities; 35 import org.netbeans.modules.web.struts.StrutsConfigDataObject; 36 import org.netbeans.modules.web.struts.config.model.Action; 37 import org.netbeans.modules.web.struts.config.model.ActionMappings; 38 import org.netbeans.modules.web.struts.config.model.FormBean; 39 import org.netbeans.modules.web.struts.config.model.FormBeans; 40 import org.netbeans.modules.web.struts.config.model.FormProperty; 41 import org.netbeans.modules.web.struts.config.model.Forward; 42 import org.netbeans.modules.web.struts.config.model.GlobalExceptions; 43 import org.netbeans.modules.web.struts.config.model.GlobalForwards; 44 import org.netbeans.modules.web.struts.config.model.StrutsConfig; 45 import org.netbeans.modules.web.struts.config.model.StrutsException; 46 import org.netbeans.modules.web.struts.dialogs.AddDialog; 47 import org.netbeans.modules.web.struts.dialogs.AddExceptionDialogPanel; 48 import org.netbeans.modules.web.struts.dialogs.AddFIActionPanel; 49 import org.netbeans.modules.web.struts.dialogs.AddActionPanel; 50 import org.netbeans.modules.web.struts.dialogs.AddFormBeanPanel; 51 import org.netbeans.modules.web.struts.dialogs.AddFormPropertyPanel; 52 import org.netbeans.modules.web.struts.dialogs.AddForwardDialogPanel; 53 import org.netbeans.modules.web.struts.dialogs.ValidatingPanel; 54 import org.openide.DialogDescriptor; 55 import org.openide.ErrorManager; 56 import org.openide.loaders.DataObject; 57 import org.openide.util.HelpCtx; 58 import org.openide.util.NbBundle; 59 import org.openide.util.actions.Presenter; 60 import org.openide.util.actions.SystemAction; 61 62 63 67 public final class StrutsPopupAction extends SystemAction implements Presenter.Popup { 68 69 private ArrayList actions = null; 70 71 public String getName() { 72 return NbBundle.getMessage(StrutsPopupAction.class, "org-netbeans-modules-web-struts-editor-StrutsPopupAction.instance"); } 74 75 public void actionPerformed(ActionEvent ev) { 76 } 78 79 public HelpCtx getHelpCtx() { 80 return HelpCtx.DEFAULT_HELP; 81 } 82 83 public javax.swing.JMenuItem getPopupPresenter() { 84 return new SubMenu(getName()); 85 } 86 87 public class SubMenu extends JMenu { 88 89 public SubMenu(String s){ 90 super(s); 91 } 92 93 94 public JPopupMenu getPopupMenu() { 95 JPopupMenu pm = super.getPopupMenu(); 96 pm.removeAll(); 97 pm.add(new AddActionAction()); 98 pm.add(new AddForwardInlcudeAction()); 99 pm.add(new AddForwardAction()); 100 pm.add(new AddExceptionAction()); 101 pm.add(new JSeparator ()); 102 pm.add(new AddFormBeanAction()); 103 pm.add(new AddFormPropertyAction()); 104 pm.pack(); 105 return pm; 106 } 107 } 108 109 public static class AddFormBeanAction extends BaseAction{ 110 public AddFormBeanAction(){ 111 super(NbBundle.getBundle(StrutsPopupAction.class).getString("add-form-bean-action")); } 113 114 public void actionPerformed(ActionEvent evt, JTextComponent target) { 115 Document doc = target.getDocument(); 116 DataObject data = NbEditorUtilities.getDataObject(doc); 117 AddFormBeanPanel dialogPanel = new AddFormBeanPanel((StrutsConfigDataObject)data); 118 AddDialog dialog = new AddDialog(dialogPanel, 119 NbBundle.getMessage(StrutsPopupAction.class,"TTL_AddFormBean"), new HelpCtx(AddFormBeanPanel.class)); 121 dialog.disableAdd(); java.awt.Dialog d = org.openide.DialogDisplayer.getDefault().createDialog(dialog); 123 d.setVisible(true); 124 if (dialog.getValue().equals(dialog.ADD_OPTION)) { 125 try { 127 StrutsConfig config = ((StrutsConfigDataObject)data).getStrutsConfig(); 128 if (config==null) return; FormBeans beans = config.getFormBeans(); 130 if (beans==null) { 131 beans = new FormBeans(); 132 config.setFormBeans(beans); 133 } 134 FormBean bean = new FormBean(); 135 bean.setAttributeValue("type",dialogPanel.getFormBeanClass()); bean.setAttributeValue("name",dialogPanel.getFormName()); beans.addFormBean(bean); 138 target.setCaretPosition(StrutsEditorUtilities.writeBean((BaseDocument)doc, bean, "form-bean", "form-beans")); } catch (java.io.IOException ex) { 141 ErrorManager.getDefault().notify(ex); 142 } 143 } 144 145 } 146 } 147 148 public static class AddActionAction extends BaseAction { 149 public AddActionAction(){ 150 super(NbBundle.getBundle(StrutsPopupAction.class).getString("add-action-action")); } 152 153 public void actionPerformed(ActionEvent evt, JTextComponent target) { 154 Document doc = target.getDocument(); 155 DataObject data = NbEditorUtilities.getDataObject(doc); 156 AddActionPanel dialogPanel = new AddActionPanel((StrutsConfigDataObject)data); 157 AddDialog dialog = new AddDialog(dialogPanel, 158 NbBundle.getMessage(StrutsPopupAction.class,"TTL_AddAction"), new HelpCtx(AddActionPanel.class)); 160 dialog.disableAdd(); java.awt.Dialog d = org.openide.DialogDisplayer.getDefault().createDialog(dialog); 162 d.setVisible(true); 163 if (dialog.getValue().equals(dialog.ADD_OPTION)) { 164 try { 166 StrutsConfig config = ((StrutsConfigDataObject)data).getStrutsConfig(); 167 if (config==null) return; ActionMappings mappings = config.getActionMappings(); 169 if (mappings==null) { 170 mappings = new ActionMappings(); 171 config.setActionMappings(mappings); 172 } 173 Action action = new Action(); 174 action.setAttributeValue("type",dialogPanel.getActionClass()); action.setAttributeValue("path",dialogPanel.getActionPath()); if (dialogPanel.isActionFormUsed()){ 177 action.setAttributeValue("name",dialogPanel.getFormName()); action.setAttributeValue("input",dialogPanel.getInput()); action.setAttributeValue("validate",dialogPanel.getValidate()); action.setAttributeValue("scope",dialogPanel.getScope()); action.setAttributeValue("attribute",dialogPanel.getAttribute()); } 183 action.setAttributeValue("parameter",dialogPanel.getParameter()); mappings.addAction(action); 185 target.setCaretPosition(StrutsEditorUtilities.writeBean((BaseDocument)doc, action, "action", "action-mappings")); } catch (java.io.IOException ex) {} 187 } 188 } 189 } 190 191 public static class AddForwardInlcudeAction extends BaseAction { 192 public AddForwardInlcudeAction(){ 193 super(NbBundle.getBundle(StrutsPopupAction.class).getString("add-forward-include-action-action")); } 195 196 public void actionPerformed(ActionEvent evt, JTextComponent target) { 197 Document doc = target.getDocument(); 198 DataObject data = NbEditorUtilities.getDataObject(doc); 199 200 AddFIActionPanel dialogPanel = new AddFIActionPanel((StrutsConfigDataObject)data); 201 AddDialog dialog = new AddDialog(dialogPanel, 202 NbBundle.getMessage(StrutsPopupAction.class,"TTL_Forward-Include"), new HelpCtx(AddFIActionPanel.class)); 204 dialog.disableAdd(); java.awt.Dialog d = org.openide.DialogDisplayer.getDefault().createDialog(dialog); 206 d.setVisible(true); 207 if (dialog.getValue().equals(dialog.ADD_OPTION)) { 208 try { 209 StrutsConfig config = ((StrutsConfigDataObject)data).getStrutsConfig(); 210 if (config==null) return; ActionMappings mappings = config.getActionMappings(); 212 if (mappings==null) { 213 mappings = new ActionMappings(); 214 config.setActionMappings(mappings); 215 } 216 Action action = new Action(); 217 action.setAttributeValue("path",dialogPanel.getActionPath()); if (dialogPanel.isForward()) { 219 action.setAttributeValue("forward",dialogPanel.getResource()); } else { 221 action.setAttributeValue("include",dialogPanel.getResource()); } 223 mappings.addAction(action); 224 target.setCaretPosition(StrutsEditorUtilities.writeBean((BaseDocument)doc, action, "action", "action-mappings")); 225 } catch (java.io.IOException ex) {} 226 } 227 } 228 } 229 230 public static class AddFormPropertyAction extends BaseAction { 231 public AddFormPropertyAction(){ 232 super(NbBundle.getBundle(StrutsPopupAction.class).getString("add-form-property-action")); } 234 235 public void actionPerformed(ActionEvent evt, JTextComponent target) { 236 Document doc = target.getDocument(); 237 DataObject data = NbEditorUtilities.getDataObject(doc); 238 String formName = StrutsEditorUtilities.getActionFormBeanName((BaseDocument)doc, target.getCaretPosition()); 239 AddFormPropertyPanel dialogPanel = new AddFormPropertyPanel((StrutsConfigDataObject)data, formName); 240 AddDialog dialog = new AddDialog(dialogPanel, 241 NbBundle.getMessage(StrutsPopupAction.class,"TTL_AddFormProperty"), new HelpCtx(AddFormPropertyPanel.class)); 243 dialog.disableAdd(); java.awt.Dialog d = org.openide.DialogDisplayer.getDefault().createDialog(dialog); 245 d.setVisible(true); 246 if (dialog.getValue().equals(dialog.ADD_OPTION)) { 247 try { 249 StrutsConfig config = ((StrutsConfigDataObject)data).getStrutsConfig(); 250 if (config==null) return; FormProperty prop = new FormProperty(); 252 prop.setAttributeValue("name",dialogPanel.getPropertyName()); prop.setAttributeValue("type",dialogPanel.getPropertyType()); if (dialogPanel.isArray()) { 255 prop.setAttributeValue("size",dialogPanel.getArraySize()); } else { 257 prop.setAttributeValue("initial",dialogPanel.getInitValue()); } 259 FormBean[] beans = config.getFormBeans().getFormBean(); 260 for (int i=0;i<beans.length;i++) { 261 if (dialogPanel.getFormName().equals(beans[i].getAttributeValue("name"))) { beans[i].addFormProperty(prop); 263 beans[i].setAttributeValue("dynamic","true"); target.setCaretPosition(StrutsEditorUtilities.writePropertyIntoBean((BaseDocument) doc, prop, beans[i].getAttributeValue("name"))); break; 266 } 267 } 268 269 270 } catch (java.io.IOException ex) {} 271 } 272 } 273 } 274 275 public static class AddForwardAction extends BaseAction{ 276 public AddForwardAction(){ 277 super(NbBundle.getBundle(StrutsPopupAction.class).getString("add-forward-action")); } 279 280 public void actionPerformed(ActionEvent evt, JTextComponent target) { 281 Document doc = target.getDocument(); 282 DataObject data = NbEditorUtilities.getDataObject(doc); 283 try { 284 StrutsConfig config = ((StrutsConfigDataObject)data).getStrutsConfig(); 285 if (config==null) return; String actionPath = StrutsEditorUtilities.getActionPath((BaseDocument)doc, target.getCaretPosition()); 287 String targetActionPath=null; 288 289 if (actionPath != null && data instanceof StrutsConfigDataObject){ 290 Action[] actions = config.getActionMappings().getAction(); 291 String path; 292 for (int i = 0; i < actions.length; i++){ 293 path = actions[i].getAttributeValue("path"); if (path != null && path.equals(actionPath) 295 && actions[i].getAttributeValue("type") != null){ targetActionPath=path; 297 break; 298 } 299 } 300 } 301 302 AddForwardDialogPanel dialogPanel=null; 303 if (targetActionPath==null) 304 dialogPanel = new AddForwardDialogPanel((StrutsConfigDataObject)data); 305 else 306 dialogPanel = new AddForwardDialogPanel((StrutsConfigDataObject)data, targetActionPath); 307 AddDialog dialog = new AddDialog(dialogPanel, 308 NbBundle.getMessage(StrutsPopupAction.class,"TTL_AddForward"), new HelpCtx(AddForwardDialogPanel.class)); 310 dialog.disableAdd(); java.awt.Dialog d = org.openide.DialogDisplayer.getDefault().createDialog(dialog); 312 d.setVisible(true); 313 if (dialog.getValue().equals(dialog.ADD_OPTION)) { 314 Forward forward = new Forward(); 315 forward.setAttributeValue("name",dialogPanel.getForwardName()); forward.setAttributeValue("path",dialogPanel.getForwardTo()); forward.setAttributeValue("redirect",dialogPanel.getRedirect()); if (dialogPanel.isGlobal()) { 319 GlobalForwards forwards = config.getGlobalForwards(); 320 if (forwards==null) { 321 forwards = new GlobalForwards(); 322 config.setGlobalForwards(forwards); 323 } 324 forwards.addForward(forward); 325 target.setCaretPosition(StrutsEditorUtilities.writeBean((BaseDocument)doc, forward, "forward", "global-forwards")); } else { 327 Action[] actions = config.getActionMappings().getAction(); 328 for (int i=0;i<actions.length;i++) { 329 if (dialogPanel.getLocationAction().equals(actions[i].getAttributeValue("path"))) { actions[i].addForward(forward); 331 break; 332 } 333 } 334 target.setCaretPosition(StrutsEditorUtilities.writeForwardIntoAction((BaseDocument)doc, forward, dialogPanel.getLocationAction())); 335 } 336 337 } 338 } catch (java.io.IOException ex) { 339 ErrorManager.getDefault().notify(ex); 340 } 341 } 342 } 343 344 public static class AddExceptionAction extends BaseAction{ 345 public AddExceptionAction(){ 346 super(NbBundle.getBundle(StrutsPopupAction.class).getString("add-exception-action")); } 348 349 public void actionPerformed(ActionEvent evt, JTextComponent target) { 350 Document doc = target.getDocument(); 351 DataObject data = NbEditorUtilities.getDataObject(doc); 352 try { 353 StrutsConfig config = ((StrutsConfigDataObject)data).getStrutsConfig(); 354 if (config==null) return; String actionPath = StrutsEditorUtilities.getActionPath((BaseDocument)doc, target.getCaretPosition()); 356 String targetActionPath=null; 357 358 if (actionPath != null && data instanceof StrutsConfigDataObject){ 359 Action[] actions = config.getActionMappings().getAction(); 360 String path; 361 for (int i = 0; i < actions.length; i++){ 362 path = actions[i].getAttributeValue("path"); if (path != null && path.equals(actionPath) 364 && actions[i].getAttributeValue("type") != null){ targetActionPath=path; 366 break; 367 } 368 } 369 } 370 371 AddExceptionDialogPanel dialogPanel=null; 372 if (targetActionPath==null) 373 dialogPanel = new AddExceptionDialogPanel((StrutsConfigDataObject)data); 374 else 375 dialogPanel = new AddExceptionDialogPanel((StrutsConfigDataObject)data, targetActionPath); 376 377 AddDialog dialog = new AddDialog(dialogPanel, 378 NbBundle.getMessage(StrutsPopupAction.class,"TTL_AddException"), new HelpCtx(AddExceptionDialogPanel.class)); 380 dialog.disableAdd(); java.awt.Dialog d = org.openide.DialogDisplayer.getDefault().createDialog(dialog); 382 d.setVisible(true); 383 if (dialog.getValue().equals(dialog.ADD_OPTION)) { 384 StrutsException exception = new StrutsException(); 386 exception.setAttributeValue("bundle",dialogPanel.getResourceBundle()); exception.setAttributeValue("type",dialogPanel.getExceptionType()); exception.setAttributeValue("key",dialogPanel.getExceptionKey()); exception.setAttributeValue("path",dialogPanel.getForwardTo()); exception.setAttributeValue("scope",dialogPanel.getScope()); if (dialogPanel.isGlobal()) { 392 GlobalExceptions exceptions = config.getGlobalExceptions(); 393 if (exceptions==null) { 394 exceptions = new GlobalExceptions(); 395 config.setGlobalExceptions(exceptions); 396 } 397 exceptions.addStrutsException(exception); 398 target.setCaretPosition(StrutsEditorUtilities.writeBean((BaseDocument) doc, exception, "exception", "global-exceptions")); } else { 400 Action[] actions = config.getActionMappings().getAction(); 401 for (int i=0;i<actions.length;i++) { 402 if (dialogPanel.getLocationAction().equals(actions[i].getAttributeValue("path"))) { actions[i].addStrutsException(exception); 404 break; 405 } 406 } 407 target.setCaretPosition(StrutsEditorUtilities.writeExceptionIntoAction((BaseDocument)doc, exception, dialogPanel.getLocationAction())); 408 } 409 } 410 } catch (java.io.IOException ex) {} 411 } 412 } 413 414 } 415 | Popular Tags |