1 19 package org.netbeans.modules.web.jsf.editor; 20 import java.awt.event.ActionEvent ; 21 import java.io.IOException ; 22 import java.util.ArrayList ; 23 import javax.swing.JMenu ; 24 import javax.swing.JPopupMenu ; 25 import javax.swing.JSeparator ; 26 import javax.swing.text.JTextComponent ; 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 54 public final class JSFPopupAction extends SystemAction implements Presenter.Popup { 55 56 private ArrayList actions = null; 57 static private String END_LINE = System.getProperty("line.separator"); protected final static int MANAGED_BEAN_TYPE = 1; 59 protected final static int NAVIGATION_RULE_TYPE = 2; 60 61 public String getName() { 62 return NbBundle.getMessage(JSFPopupAction.class, "org-netbeans-modules-web-jsf-editor-JSFPopupAction.instance"); } 64 65 public void actionPerformed(ActionEvent ev) { 66 } 68 69 public HelpCtx getHelpCtx() { 70 return HelpCtx.DEFAULT_HELP; 71 } 72 73 public javax.swing.JMenuItem getPopupPresenter() { 74 return new SubMenu(getName()); 75 } 76 77 public class SubMenu extends JMenu { 78 79 public SubMenu(String s){ 80 super(s); 81 } 82 83 84 public JPopupMenu getPopupMenu() { 85 JPopupMenu pm = super.getPopupMenu(); 86 pm.removeAll(); 87 pm.add(new AddNavigationRuleAction()); 88 pm.add(new AddNavigationCaseAction()); 89 pm.add(new JSeparator ()); 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")); } 100 101 public void actionPerformed(ActionEvent evt, JTextComponent 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"), new HelpCtx(AddManagedBeanDialog.class)); 108 dialog.disableAdd(); java.awt.Dialog 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 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")); } 142 143 public void actionPerformed(ActionEvent evt, JTextComponent 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"), new HelpCtx(AddNavigationRuleDialog.class)); 150 dialog.disableAdd(); java.awt.Dialog 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 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")); } 181 182 public void actionPerformed(ActionEvent evt, JTextComponent 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"), new HelpCtx(AddNavigationCaseDialog.class)); 190 dialog.disableAdd(); java.awt.Dialog 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("")) nCase.setFromAction(dialogPanel.getFromAction()); 210 if(dialogPanel.getFromOutcome() != null && !dialogPanel.getFromOutcome().equals("")) nCase.setFromOutcome(dialogPanel.getFromOutcome()); 212 nCase.setRedirected(dialogPanel.isRedirect()); 213 nCase.setToViewId(dialogPanel.getToView()); 214 if(dialogPanel.getDescription() != null && !dialogPanel.getDescription().equals("")) 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()); else 225 target.setCaretPosition(nCase.findPosition()); 226 } catch (java.io.IOException ex) { 227 ErrorManager.getDefault().notify(ex); 228 } 229 } 230 } 231 } 232 } 233 | Popular Tags |