KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > mdrxml > looks > actions > NewAction


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 Forte for Java, Community Edition. The Initial
16  * Developer of the Original Software is Sun Microsystems, Inc. Portions
17  * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.mdrxml.looks.actions;
21
22
23 import java.util.ResourceBundle JavaDoc;
24 import javax.jmi.reflect.RefObject;
25 import javax.swing.JMenu JavaDoc;
26 import javax.swing.JMenuItem JavaDoc;
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 /**
38  *
39  * @author Tomas Zezula
40  */

41 public class NewAction extends NodeAction {
42     
43     private static final String JavaDoc ELEMENT = "ELEMENT"; // NoI18N
44
private static final String JavaDoc TEXT = "TEXT"; //NoI18N
45
private static final String JavaDoc ATTRIBUTE = "ATTR"; // NoI18N
46

47     private static ResourceBundle JavaDoc bundle;
48     private String JavaDoc source;
49     
50     /** Creates a new instance of NewAction */
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 JavaDoc event) {
68         this.source = event.getActionCommand();
69         super.actionPerformed(event);
70     }
71     
72     public JMenuItem JavaDoc getPopupPresenter() {
73         
74         if (this.isEnabled()) {
75             JMenu JavaDoc presenter = new JMenu JavaDoc(getLocalizedString("TXT_NewAction"));
76             JMenuItem JavaDoc mi = new JMenuItem JavaDoc();
77             mi.setLabel(getLocalizedString("TXT_Element"));
78             mi.addActionListener(this);
79             mi.setActionCommand(ELEMENT);
80             presenter.add(mi);
81             mi = new JMenuItem JavaDoc();
82             mi.setLabel(getLocalizedString("TXT_Attribute"));
83             mi.addActionListener(this);
84             mi.setActionCommand(ATTRIBUTE);
85             presenter.add(mi);
86             mi = new JMenuItem JavaDoc();
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 JavaDoc presenter = new JMenuItem JavaDoc (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 JavaDoc getName() {
108         return getLocalizedString("TXT_NewAction");
109     }
110     
111     public HelpCtx getHelpCtx() {
112         return HelpCtx.DEFAULT_HELP;
113     }
114     
115     protected static String JavaDoc getLocalizedString(String JavaDoc 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 JavaDoc 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 JavaDoc dlg = TopManager.getDefault().createDialog(dd);
135         dlg.setVisible(true);
136         if (dd.getValue() == DialogDescriptor.OK_OPTION) {
137             String JavaDoc 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 JavaDoc dlg = TopManager.getDefault().createDialog(dd);
147         dlg.setVisible(true);
148         if (dd.getValue() == DialogDescriptor.OK_OPTION) {
149             String JavaDoc attrName = nap.getAttributeName();
150             String JavaDoc 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