1 19 20 package org.netbeans.modules.mdrxml.looks.actions; 21 22 23 import java.util.ResourceBundle ; 24 import javax.jmi.reflect.RefObject; 25 import javax.swing.JMenu ; 26 import javax.swing.JMenuItem ; 27 import org.openide.DialogDescriptor; 28 import org.openide.NotifyDescriptor; 29 import org.openide.TopManager; 30 import org.openide.nodes.*; 31 import org.openide.util.actions.NodeAction; 32 import org.openide.util.NbBundle; 33 import org.openide.util.HelpCtx; 34 import org.netbeans.api.looks.*; 35 import xmlmodel.TextNode; 36 import xmlmodel.ElementNode; 37 41 public class NewAction extends NodeAction { 42 43 private static final String ELEMENT = "ELEMENT"; private static final String TEXT = "TEXT"; private static final String ATTRIBUTE = "ATTR"; 47 private static ResourceBundle bundle; 48 private String source; 49 50 51 public NewAction() { 52 } 53 54 public void performAction(Node[] nodes) { 55 xmlmodel.ElementNode node = (xmlmodel.ElementNode) ((LookNode)nodes[0]).getRepresentedObject(); 56 if (this.source.equals(ELEMENT)) { 57 this.newElementNode(node); 58 } 59 else if (this.source.equals (ATTRIBUTE)) { 60 this.newAttributeNode(node); 61 } 62 else if (this.source.equals(TEXT)) { 63 this.newTextNode(node); 64 } 65 } 66 67 public void actionPerformed(java.awt.event.ActionEvent event) { 68 this.source = event.getActionCommand(); 69 super.actionPerformed(event); 70 } 71 72 public JMenuItem getPopupPresenter() { 73 74 if (this.isEnabled()) { 75 JMenu presenter = new JMenu (getLocalizedString("TXT_NewAction")); 76 JMenuItem mi = new JMenuItem (); 77 mi.setLabel(getLocalizedString("TXT_Element")); 78 mi.addActionListener(this); 79 mi.setActionCommand(ELEMENT); 80 presenter.add(mi); 81 mi = new JMenuItem (); 82 mi.setLabel(getLocalizedString("TXT_Attribute")); 83 mi.addActionListener(this); 84 mi.setActionCommand(ATTRIBUTE); 85 presenter.add(mi); 86 mi = new JMenuItem (); 87 mi.setLabel(getLocalizedString("TXT_Text")); 88 mi.addActionListener(this); 89 mi.setActionCommand(TEXT); 90 presenter.add(mi); 91 return presenter; 92 } 93 else { 94 JMenuItem presenter = new JMenuItem (getLocalizedString("TXT_NewAction")); 95 presenter.setEnabled (false); 96 return presenter; 97 } 98 } 99 100 public boolean enable(Node[] nodes) { 101 if (nodes == null || nodes.length != 1 || !(nodes[0] instanceof LookNode)) 102 return false; 103 return (((LookNode)nodes[0]).getRepresentedObject() instanceof ElementNode) && 104 ! (((LookNode)nodes[0]).getRepresentedObject() instanceof TextNode); 105 } 106 107 public String getName() { 108 return getLocalizedString("TXT_NewAction"); 109 } 110 111 public HelpCtx getHelpCtx() { 112 return HelpCtx.DEFAULT_HELP; 113 } 114 115 protected static String getLocalizedString(String msg) { 116 if (bundle == null) { 117 bundle = NbBundle.getBundle(NewAction.class); 118 } 119 return bundle.getString(msg); 120 } 121 122 protected void newElementNode(xmlmodel.ElementNode en) { 123 NotifyDescriptor.InputLine dd = new NotifyDescriptor.InputLine(getLocalizedString("TXT_ElementName"), getLocalizedString("TXT_NewElement")); 124 if (TopManager.getDefault().notify(dd) == DialogDescriptor.OK_OPTION) { 125 String name = dd.getInputText(); 126 xmlmodel.XmlmodelPackage pkg = (xmlmodel.XmlmodelPackage) ((RefObject)en).refImmediatePackage(); 127 en.getNodes().add(pkg.getElementNode().createElementNode(name)); 128 } 129 } 130 131 protected void newTextNode(xmlmodel.ElementNode en) { 132 NewTextPanel ntp = new NewTextPanel(); 133 DialogDescriptor dd = new DialogDescriptor(ntp, getLocalizedString("TXT_NewText")); 134 java.awt.Dialog dlg = TopManager.getDefault().createDialog(dd); 135 dlg.setVisible(true); 136 if (dd.getValue() == DialogDescriptor.OK_OPTION) { 137 String name = ntp.getText(); 138 xmlmodel.XmlmodelPackage pkg = (xmlmodel.XmlmodelPackage) ((RefObject)en).refImmediatePackage(); 139 en.getNodes().add(pkg.getTextNode().createTextNode(name)); 140 } 141 } 142 143 protected void newAttributeNode(xmlmodel.ElementNode en) { 144 NewAttributePanel nap = new NewAttributePanel(); 145 DialogDescriptor dd = new DialogDescriptor(nap, NbBundle.getMessage (NewAction.class,"TXT_NewAttribute")); 146 java.awt.Dialog dlg = TopManager.getDefault().createDialog(dd); 147 dlg.setVisible(true); 148 if (dd.getValue() == DialogDescriptor.OK_OPTION) { 149 String attrName = nap.getAttributeName(); 150 String attrValue = nap.getAttributeValue(); 151 xmlmodel.XmlmodelPackage pkg = (xmlmodel.XmlmodelPackage) ((RefObject)en).refImmediatePackage(); 152 en.getNodes().add(pkg.getAttributeNode().createAttributeNode(attrName,attrValue)); 153 } 154 } 155 156 } 157 | Popular Tags |