KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > group > GroupTemplateIterator


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
20 package org.netbeans.modules.group;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.NoSuchElementException JavaDoc;
29 import java.util.Set JavaDoc;
30 import javax.swing.event.ChangeListener JavaDoc;
31 import org.openide.WizardDescriptor;
32 import org.openide.WizardDescriptor.Panel;
33 import org.openide.actions.ActionManager;
34 import org.openide.loaders.DataFolder;
35 import org.openide.loaders.DataLoader;
36 import org.openide.loaders.DataObject;
37 import org.openide.loaders.TemplateWizard;
38 import org.openide.nodes.Node;
39 import org.openide.util.Lookup;
40 import org.openide.util.actions.SystemAction;
41
42 /**
43  * Template wizard iterator for handling creating group members from template.
44  * This iterator is attached to each <code>GroupShadow</code> data object to
45  * control group template instantiating.
46  */

47 class GroupTemplateIterator implements TemplateWizard.Iterator {
48
49     /** Target panel. */
50     private Panel targetPanel = null;
51
52     /** Constructor. */
53     GroupTemplateIterator() {
54     }
55
56     /**
57      * Instantiates the template using informations provided by the wizard.
58      *
59      * @param wiz the wizard
60      * @return set of data objects that has been created
61      * (should contain at least one)
62      * @exception java.io.IOException if the instantiation fails
63      */

64     public Set JavaDoc instantiate(TemplateWizard wiz) throws IOException JavaDoc {
65         String JavaDoc nam = wiz.getTargetName();
66         DataFolder folder = wiz.getTargetFolder();
67         DataObject template = wiz.getTemplate();
68
69         // new objects from all members of template group will be created
70
// (even from nested groups)
71
if (template instanceof GroupShadow) {
72             GroupShadow group = (GroupShadow) template;
73             List JavaDoc createdObjs = group.createGroupFromTemplate(folder, nam, true);
74             HashSet JavaDoc templObjs = new HashSet JavaDoc(createdObjs.size());
75
76             if (createdObjs != null) {
77                 Iterator JavaDoc it = createdObjs.iterator();
78                 while (it.hasNext()) {
79                     DataObject obj = (DataObject) it.next();
80                     if (!(obj instanceof DataFolder)
81                             && !(obj instanceof GroupShadow)) {
82                         templObjs.add(obj);
83                         Node node = obj.getNodeDelegate();
84                         SystemAction sa = node.getDefaultAction();
85                         if (sa != null) {
86                             ActionManager actionManager
87                                     = (ActionManager) Lookup.getDefault().lookup(ActionManager.class);
88                             actionManager.invokeAction(sa, new ActionEvent JavaDoc(node, ActionEvent.ACTION_PERFORMED, "")); // NOI18N
89
}
90                     }
91                 }
92             }
93             return templObjs;
94         } else {
95             DataObject obj = nam == null ?
96                          template.createFromTemplate(folder) :
97                          template.createFromTemplate(folder, nam);
98
99             return Collections.singleton(obj);
100         }
101     }
102
103     /** Initializes this instance. */
104     public void initialize(TemplateWizard wiz) {
105         targetPanel = wiz.targetChooser();
106     }
107
108     /** No-op implementation. */
109     public void uninitialize(TemplateWizard wiz) {
110         targetPanel = null;
111     }
112
113     /** Get the current panel.
114      * @return the panel */

115     public Panel current() {
116         return targetPanel;
117     }
118
119     /** Current name of the panel. */
120     public String JavaDoc name() {
121         return ""; //NOI18N
122
}
123
124     /**
125      * @return <code>false</code> - only one panel is used
126      */

127     public boolean hasNext() {
128         return false;
129     }
130
131     /**
132      * @return <code>false</code> - only one panel is used
133      */

134     public boolean hasPrevious() {
135         return false;
136     }
137
138     /**
139      * Move to the next panel.
140      * I.e. increment its index, need not actually change any GUI itself.
141      *
142      * @exception NoSuchElementException if the panel does not exist
143      */

144     public void nextPanel() {
145         throw new NoSuchElementException JavaDoc();
146     }
147
148     /**
149      * Move to the previous panel.
150      * I.e. decrement its index, need not actually change any GUI itself.
151      *
152      * @exception NoSuchElementException if the panel does not exist
153      */

154     public void previousPanel() {
155         throw new NoSuchElementException JavaDoc();
156     }
157
158     /** Dummy implementation of method <code>TemplateWizard.Iterator</code> interface method. */
159     public void addChangeListener(ChangeListener JavaDoc l) {}
160
161     /** Dummy implementation of method <code>TemplateWizard.Iterator</code> interface method. */
162     public void removeChangeListener(ChangeListener JavaDoc l) {}
163
164 }
Popular Tags