KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > jsf > editor > JSFPopupAction


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.jsf.editor;
20 import java.awt.event.ActionEvent JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import javax.swing.JMenu JavaDoc;
24 import javax.swing.JPopupMenu JavaDoc;
25 import javax.swing.JSeparator JavaDoc;
26 import javax.swing.text.JTextComponent JavaDoc;
27 import org.netbeans.editor.BaseAction;
28 import org.netbeans.editor.BaseDocument;
29 import org.netbeans.modules.editor.NbEditorUtilities;
30 import org.netbeans.modules.web.jsf.JSFConfigDataObject;
31 import org.netbeans.modules.web.jsf.JSFConfigUtilities;
32 import org.netbeans.modules.web.jsf.JSFConfigUtilities;
33 import org.netbeans.modules.web.jsf.api.ConfigurationUtils;
34 import org.netbeans.modules.web.jsf.api.facesmodel.JSFConfigModel;
35 import org.netbeans.modules.web.jsf.dialogs.AddDialog;
36 import org.netbeans.modules.web.jsf.dialogs.AddManagedBeanDialog;
37 import org.netbeans.modules.web.jsf.dialogs.AddNavigationCaseDialog;
38 import org.netbeans.modules.web.jsf.dialogs.AddNavigationRuleDialog;
39 import org.netbeans.modules.web.jsf.api.facesmodel.FacesConfig;
40 import org.netbeans.modules.web.jsf.api.facesmodel.ManagedBean;
41 import org.netbeans.modules.web.jsf.api.facesmodel.NavigationRule;
42 import org.netbeans.modules.web.jsf.api.facesmodel.NavigationCase;
43 import org.openide.ErrorManager;
44 import org.openide.util.HelpCtx;
45 import org.openide.util.NbBundle;
46 import org.openide.util.actions.Presenter;
47 import org.openide.util.actions.SystemAction;
48
49
50 /**
51  *
52  * @author Petr Pisl
53  */

54 public final class JSFPopupAction extends SystemAction implements Presenter.Popup {
55     
56     private ArrayList JavaDoc actions = null;
57     static private String JavaDoc END_LINE = System.getProperty("line.separator"); //NOI18N
58
protected final static int MANAGED_BEAN_TYPE = 1;
59     protected final static int NAVIGATION_RULE_TYPE = 2;
60     
61     public String JavaDoc getName() {
62         return NbBundle.getMessage(JSFPopupAction.class, "org-netbeans-modules-web-jsf-editor-JSFPopupAction.instance"); // NOI18N
63
}
64     
65     public void actionPerformed(ActionEvent JavaDoc ev) {
66         // do nothing - should never be called
67
}
68     
69     public HelpCtx getHelpCtx() {
70         return HelpCtx.DEFAULT_HELP;
71     }
72     
73     public javax.swing.JMenuItem JavaDoc getPopupPresenter() {
74         return new SubMenu(getName());
75     }
76     
77     public class SubMenu extends JMenu JavaDoc {
78         
79         public SubMenu(String JavaDoc s){
80             super(s);
81         }
82         
83         /** Gets popup menu. Overrides superclass. Adds lazy menu items creation. */
84         public JPopupMenu JavaDoc getPopupMenu() {
85             JPopupMenu JavaDoc pm = super.getPopupMenu();
86             pm.removeAll();
87             pm.add(new AddNavigationRuleAction());
88             pm.add(new AddNavigationCaseAction());
89             pm.add(new JSeparator JavaDoc());
90             pm.add(new AddManagedBeanAction());
91             pm.pack();
92             return pm;
93         }
94     }
95     
96     public static class AddManagedBeanAction extends BaseAction{
97         public AddManagedBeanAction(){
98             super(NbBundle.getBundle(JSFPopupAction.class).getString("add-managed-bean-action")); //NOI18N
99
}
100         
101         public void actionPerformed(ActionEvent JavaDoc evt, JTextComponent JavaDoc target) {
102             BaseDocument doc = (BaseDocument)target.getDocument();
103             JSFConfigDataObject data = (JSFConfigDataObject)NbEditorUtilities.getDataObject(doc);
104             AddManagedBeanDialog dialogPanel = new AddManagedBeanDialog(data);
105             AddDialog dialog = new AddDialog(dialogPanel,
106                     NbBundle.getMessage(JSFPopupAction.class,"TTL_AddManagedBean"), //NOI18N
107
new HelpCtx(AddManagedBeanDialog.class));
108             dialog.disableAdd(); // disable Add button
109
java.awt.Dialog JavaDoc d = org.openide.DialogDisplayer.getDefault().createDialog(dialog);
110             d.setVisible(true);
111             if (dialog.getValue().equals(dialog.ADD_OPTION)) {
112                 try {
113                     FacesConfig facesConfig = ConfigurationUtils.getConfigModel(data.getPrimaryFile(),true).getRootComponent();
114                     ManagedBean bean = facesConfig.getModel().getFactory().createManagedBean();
115                     
116                     bean.setManagedBeanName(dialogPanel.getName());
117                     bean.setManagedBeanClass(dialogPanel.getBeanClass());
118                     bean.setManagedBeanScope(dialogPanel.getScope());
119                     if (dialogPanel.getDescription() != null &&
120                             !dialogPanel.getDescription().equals(""))
121                         bean.setDescription(END_LINE +
122                                 dialogPanel.getDescription() +
123                                 END_LINE);
124                     facesConfig.getModel().startTransaction();
125                     facesConfig.addManagedBean(bean);
126                     facesConfig.getModel().endTransaction();
127                     facesConfig.getModel().sync();
128                     target.setCaretPosition(bean.findPosition());
129                 } catch (IOException JavaDoc ex) {
130                     java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE,
131                             ex.getMessage(),
132                             ex);
133                 }
134             }
135         }
136     }
137     
138     public static class AddNavigationRuleAction extends BaseAction{
139         public AddNavigationRuleAction(){
140             super(NbBundle.getBundle(JSFPopupAction.class).getString("add-navigation-rule-action")); //NOI18N
141
}
142         
143         public void actionPerformed(ActionEvent JavaDoc evt, JTextComponent JavaDoc target) {
144             BaseDocument doc = (BaseDocument)target.getDocument();
145             JSFConfigDataObject data = (JSFConfigDataObject)NbEditorUtilities.getDataObject(doc);
146             AddNavigationRuleDialog dialogPanel = new AddNavigationRuleDialog(data);
147             AddDialog dialog = new AddDialog(dialogPanel,
148                     NbBundle.getMessage(JSFPopupAction.class,"TTL_AddNavigationRule"), //NOI18N
149
new HelpCtx(AddNavigationRuleDialog.class));
150             dialog.disableAdd(); // disable Add button
151
java.awt.Dialog JavaDoc d = org.openide.DialogDisplayer.getDefault().createDialog(dialog);
152             d.setVisible(true);
153             if (dialog.getValue().equals(dialog.ADD_OPTION)) {
154                 try {
155                     JSFConfigModel model = ConfigurationUtils.getConfigModel(data.getPrimaryFile(),true);
156                     FacesConfig facesConfig = model.getRootComponent();
157                     NavigationRule rule = facesConfig.getModel().getFactory().createNavigationRule();
158                     if (dialogPanel.getDescription() == null || "".equals(dialogPanel.getDescription())){
159                         rule.setDescription(null);
160                     }
161                     else {
162                         rule.setDescription(END_LINE + dialogPanel.getDescription() + END_LINE);
163                     }
164                     rule.setFromViewId(dialogPanel.getFromView());
165                     facesConfig.getModel().startTransaction();
166                     facesConfig.addNavigationRule(rule);
167                     facesConfig.getModel().endTransaction();
168                     facesConfig.getModel().sync();
169                     target.setCaretPosition(rule.findPosition());
170                 } catch (java.io.IOException JavaDoc ex) {
171                     ErrorManager.getDefault().notify(ex);
172                 }
173             }
174         }
175     }
176     
177     public static class AddNavigationCaseAction extends BaseAction{
178         public AddNavigationCaseAction(){
179             super(NbBundle.getBundle(JSFPopupAction.class).getString("add-navigation-case-action")); //NOI18N
180
}
181         
182         public void actionPerformed(ActionEvent JavaDoc evt, JTextComponent JavaDoc target) {
183             BaseDocument doc = (BaseDocument)target.getDocument();
184             JSFConfigDataObject data = (JSFConfigDataObject)NbEditorUtilities.getDataObject(doc);
185             AddNavigationCaseDialog dialogPanel = new AddNavigationCaseDialog(data,
186                     JSFEditorUtilities.getNavigationRule((BaseDocument)doc, target.getCaretPosition()));
187             AddDialog dialog = new AddDialog(dialogPanel,
188                     NbBundle.getMessage(JSFPopupAction.class,"TTL_AddNavigationCase"), //NOI18N
189
new HelpCtx(AddNavigationCaseDialog.class));
190             dialog.disableAdd(); // disable Add button
191
java.awt.Dialog JavaDoc d = org.openide.DialogDisplayer.getDefault().createDialog(dialog);
192             d.setVisible(true);
193             if (dialog.getValue().equals(dialog.ADD_OPTION)) {
194                 try {
195                     
196                     FacesConfig facesConfig = ConfigurationUtils.getConfigModel(data.getPrimaryFile(),true).getRootComponent();
197                     boolean newRule = false;
198                     NavigationRule rule = JSFConfigUtilities.findNavigationRule(data, dialogPanel.getRule());
199                     if (rule == null){
200                         rule = facesConfig.getModel().getFactory().createNavigationRule();
201                         rule.setFromViewId(dialogPanel.getRule());
202                         facesConfig.getModel().startTransaction();
203                         facesConfig.addNavigationRule(rule);
204                         facesConfig.getModel().endTransaction();
205                         newRule = true;
206                     }
207                     NavigationCase nCase = facesConfig.getModel().getFactory().createNavigationCase();
208                     if(dialogPanel.getFromAction() != null && !dialogPanel.getFromAction().equals("")) //NOI18N
209
nCase.setFromAction(dialogPanel.getFromAction());
210                     if(dialogPanel.getFromOutcome() != null && !dialogPanel.getFromOutcome().equals("")) //NOI18N
211
nCase.setFromOutcome(dialogPanel.getFromOutcome());
212                     nCase.setRedirected(dialogPanel.isRedirect());
213                     nCase.setToViewId(dialogPanel.getToView());
214                     if(dialogPanel.getDescription() != null && !dialogPanel.getDescription().equals("")) //NOI18N
215
nCase.setDescription(END_LINE + dialogPanel.getDescription() + END_LINE);
216                     
217                     facesConfig.getModel().startTransaction();
218                     rule.addNavigationCase(nCase);
219                     facesConfig.getModel().endTransaction();
220                     facesConfig.getModel().sync();
221                     
222                     if (newRule)
223                         target.setCaretPosition(rule.findPosition()); //NOI18N
224
else
225                         target.setCaretPosition(nCase.findPosition());
226                 } catch (java.io.IOException JavaDoc ex) {
227                     ErrorManager.getDefault().notify(ex);
228                 }
229             }
230         }
231     }
232 }
233
Popular Tags