KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > struts > editor > StrutsPopupAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.web.struts.editor;
20
21 import java.awt.Dialog JavaDoc;
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import javax.swing.JButton JavaDoc;
26 import javax.swing.JMenu JavaDoc;
27 import javax.swing.JPanel JavaDoc;
28 import javax.swing.JPopupMenu JavaDoc;
29 import javax.swing.JSeparator JavaDoc;
30 import javax.swing.text.Document JavaDoc;
31 import javax.swing.text.JTextComponent JavaDoc;
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 /**
64  *
65  * @author Petr Pisl
66  */

67 public final class StrutsPopupAction extends SystemAction implements Presenter.Popup {
68     
69     private ArrayList JavaDoc actions = null;
70     
71     public String JavaDoc getName() {
72         return NbBundle.getMessage(StrutsPopupAction.class, "org-netbeans-modules-web-struts-editor-StrutsPopupAction.instance"); // NOI18N
73
}
74     
75     public void actionPerformed(ActionEvent JavaDoc ev) {
76         // do nothing - should never be called
77
}
78     
79     public HelpCtx getHelpCtx() {
80         return HelpCtx.DEFAULT_HELP;
81     }
82     
83     public javax.swing.JMenuItem JavaDoc getPopupPresenter() {
84         return new SubMenu(getName());
85     }
86     
87     public class SubMenu extends JMenu JavaDoc {
88         
89         public SubMenu(String JavaDoc s){
90             super(s);
91         }
92         
93         /** Gets popup menu. Overrides superclass. Adds lazy menu items creation. */
94         public JPopupMenu JavaDoc getPopupMenu() {
95             JPopupMenu JavaDoc 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 JavaDoc());
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")); //NOI18N
112
}
113         
114         public void actionPerformed(ActionEvent JavaDoc evt, JTextComponent JavaDoc target) {
115             Document JavaDoc 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"), //NOI18N
120
new HelpCtx(AddFormBeanPanel.class));
121             dialog.disableAdd(); // disable Add button
122
java.awt.Dialog JavaDoc d = org.openide.DialogDisplayer.getDefault().createDialog(dialog);
123             d.setVisible(true);
124             if (dialog.getValue().equals(dialog.ADD_OPTION)) {
125                 //TODO:implement action
126
try {
127                     StrutsConfig config = ((StrutsConfigDataObject)data).getStrutsConfig();
128                     if (config==null) return; //TODO:inform that XML file is corrupted
129
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()); //NOI18N
136
bean.setAttributeValue("name",dialogPanel.getFormName()); //NOI18N
137
beans.addFormBean(bean);
138                     //((StrutsConfigDataObject)data).write(config);
139
target.setCaretPosition(StrutsEditorUtilities.writeBean((BaseDocument)doc, bean, "form-bean", "form-beans")); //NOI18N
140
} catch (java.io.IOException JavaDoc 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")); //NOI18N
151
}
152         
153         public void actionPerformed(ActionEvent JavaDoc evt, JTextComponent JavaDoc target) {
154             Document JavaDoc 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"), //NOI18N
159
new HelpCtx(AddActionPanel.class));
160             dialog.disableAdd(); // disable Add button
161
java.awt.Dialog JavaDoc d = org.openide.DialogDisplayer.getDefault().createDialog(dialog);
162             d.setVisible(true);
163             if (dialog.getValue().equals(dialog.ADD_OPTION)) {
164                 //TODO:implement action
165
try {
166                     StrutsConfig config = ((StrutsConfigDataObject)data).getStrutsConfig();
167                     if (config==null) return; //TODO:inform that XML file is corrupted
168
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()); //NOI18N
175
action.setAttributeValue("path",dialogPanel.getActionPath()); //NOI18N
176
if (dialogPanel.isActionFormUsed()){
177                         action.setAttributeValue("name",dialogPanel.getFormName()); //NOI18N
178
action.setAttributeValue("input",dialogPanel.getInput()); //NOI18N
179
action.setAttributeValue("validate",dialogPanel.getValidate()); //NOI18N
180
action.setAttributeValue("scope",dialogPanel.getScope()); //NOI18N
181
action.setAttributeValue("attribute",dialogPanel.getAttribute()); //NOI18N
182
}
183                     action.setAttributeValue("parameter",dialogPanel.getParameter()); //NOI18N
184
mappings.addAction(action);
185                     target.setCaretPosition(StrutsEditorUtilities.writeBean((BaseDocument)doc, action, "action", "action-mappings")); //NOI18N
186
} catch (java.io.IOException JavaDoc 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")); //NOI18N
194
}
195         
196         public void actionPerformed(ActionEvent JavaDoc evt, JTextComponent JavaDoc target) {
197             Document JavaDoc 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"), //NOI18N
203
new HelpCtx(AddFIActionPanel.class));
204             dialog.disableAdd(); // disable Add button
205
java.awt.Dialog JavaDoc 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; //TODO:inform that XML file is corrupted
211
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()); //NOI18N
218
if (dialogPanel.isForward()) {
219                         action.setAttributeValue("forward",dialogPanel.getResource()); //NOI18N
220
} else {
221                         action.setAttributeValue("include",dialogPanel.getResource()); //NOI18N
222
}
223                     mappings.addAction(action);
224                     target.setCaretPosition(StrutsEditorUtilities.writeBean((BaseDocument)doc, action, "action", "action-mappings"));
225                 } catch (java.io.IOException JavaDoc 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")); //NOI18N
233
}
234         
235         public void actionPerformed(ActionEvent JavaDoc evt, JTextComponent JavaDoc target) {
236             Document JavaDoc doc = target.getDocument();
237             DataObject data = NbEditorUtilities.getDataObject(doc);
238             String JavaDoc 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"), //NOI18N
242
new HelpCtx(AddFormPropertyPanel.class));
243             dialog.disableAdd(); // disable Add button
244
java.awt.Dialog JavaDoc d = org.openide.DialogDisplayer.getDefault().createDialog(dialog);
245             d.setVisible(true);
246             if (dialog.getValue().equals(dialog.ADD_OPTION)) {
247                 //TODO:implement action
248
try {
249                     StrutsConfig config = ((StrutsConfigDataObject)data).getStrutsConfig();
250                     if (config==null) return; //TODO:inform that XML file is corrupted
251
FormProperty prop = new FormProperty();
252                     prop.setAttributeValue("name",dialogPanel.getPropertyName()); //NOI18N
253
prop.setAttributeValue("type",dialogPanel.getPropertyType()); //NOI18N
254
if (dialogPanel.isArray()) {
255                         prop.setAttributeValue("size",dialogPanel.getArraySize()); //NOI18N
256
} else {
257                         prop.setAttributeValue("initial",dialogPanel.getInitValue()); //NOI18N
258
}
259                     FormBean[] beans = config.getFormBeans().getFormBean();
260                     for (int i=0;i<beans.length;i++) {
261                         if (dialogPanel.getFormName().equals(beans[i].getAttributeValue("name"))) { //NOI18N
262
beans[i].addFormProperty(prop);
263                             beans[i].setAttributeValue("dynamic","true"); //NOI18N
264
target.setCaretPosition(StrutsEditorUtilities.writePropertyIntoBean((BaseDocument) doc, prop, beans[i].getAttributeValue("name"))); //NOI18N
265
break;
266                         }
267                     }
268                     
269                  
270                 } catch (java.io.IOException JavaDoc 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")); //NOI18N
278
}
279         
280         public void actionPerformed(ActionEvent JavaDoc evt, JTextComponent JavaDoc target) {
281             Document JavaDoc doc = target.getDocument();
282             DataObject data = NbEditorUtilities.getDataObject(doc);
283             try {
284                 StrutsConfig config = ((StrutsConfigDataObject)data).getStrutsConfig();
285                 if (config==null) return; //TODO:inform that XML file is corrupted
286
String JavaDoc actionPath = StrutsEditorUtilities.getActionPath((BaseDocument)doc, target.getCaretPosition());
287                 String JavaDoc targetActionPath=null;
288                 
289                 if (actionPath != null && data instanceof StrutsConfigDataObject){
290                     Action[] actions = config.getActionMappings().getAction();
291                     String JavaDoc path;
292                     for (int i = 0; i < actions.length; i++){
293                         path = actions[i].getAttributeValue("path"); //NOI18N
294
if (path != null && path.equals(actionPath)
295                                 && actions[i].getAttributeValue("type") != null){ //NOI18N
296
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"), //NOI18N
309
new HelpCtx(AddForwardDialogPanel.class));
310                 dialog.disableAdd(); // disable Add button
311
java.awt.Dialog JavaDoc 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()); //NOI18N
316
forward.setAttributeValue("path",dialogPanel.getForwardTo()); //NOI18N
317
forward.setAttributeValue("redirect",dialogPanel.getRedirect()); //NOI18N
318
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")); //NOI18N
326
} 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"))) { //NOI18N
330
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 JavaDoc 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")); //NOI18N
347
}
348         
349         public void actionPerformed(ActionEvent JavaDoc evt, JTextComponent JavaDoc target) {
350             Document JavaDoc doc = target.getDocument();
351             DataObject data = NbEditorUtilities.getDataObject(doc);
352             try {
353                 StrutsConfig config = ((StrutsConfigDataObject)data).getStrutsConfig();
354                 if (config==null) return; //TODO:inform that XML file is corrupted
355
String JavaDoc actionPath = StrutsEditorUtilities.getActionPath((BaseDocument)doc, target.getCaretPosition());
356                 String JavaDoc targetActionPath=null;
357                 
358                 if (actionPath != null && data instanceof StrutsConfigDataObject){
359                     Action[] actions = config.getActionMappings().getAction();
360                     String JavaDoc path;
361                     for (int i = 0; i < actions.length; i++){
362                         path = actions[i].getAttributeValue("path"); //NOI18N
363
if (path != null && path.equals(actionPath)
364                                 && actions[i].getAttributeValue("type") != null){ //NOI18N
365
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"), //NOI18N
379
new HelpCtx(AddExceptionDialogPanel.class));
380                 dialog.disableAdd(); // disable Add button
381
java.awt.Dialog JavaDoc d = org.openide.DialogDisplayer.getDefault().createDialog(dialog);
382                 d.setVisible(true);
383                 if (dialog.getValue().equals(dialog.ADD_OPTION)) {
384                     //TODO:implement action
385
StrutsException exception = new StrutsException();
386                     exception.setAttributeValue("bundle",dialogPanel.getResourceBundle()); //NOI18N
387
exception.setAttributeValue("type",dialogPanel.getExceptionType()); //NOI18N
388
exception.setAttributeValue("key",dialogPanel.getExceptionKey()); //NOI18N
389
exception.setAttributeValue("path",dialogPanel.getForwardTo()); //NOI18N
390
exception.setAttributeValue("scope",dialogPanel.getScope()); //NOI18N
391
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")); //NOI18N
399
} 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"))) { //NOI18N
403
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 JavaDoc ex) {}
411         }
412     }
413     
414 }
415
Popular Tags