KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > core > wseditor > support > EditWSAttributesCookieImpl


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 /*
21  * EditWSAttributesCookieImpl.java
22  *
23  * Created on April 12, 2006, 10:13 AM
24  *
25  * To change this template, choose Tools | Template Manager
26  * and open the template in the editor.
27  */

28
29 package org.netbeans.modules.websvc.core.wseditor.support;
30
31 import java.awt.Component JavaDoc;
32 import java.awt.Dialog JavaDoc;
33 import java.awt.event.ActionEvent JavaDoc;
34 import java.awt.event.ActionListener JavaDoc;
35 import java.awt.event.WindowAdapter JavaDoc;
36 import java.awt.event.WindowEvent JavaDoc;
37 import java.awt.event.WindowListener JavaDoc;
38 import java.util.Collection JavaDoc;
39 import java.util.HashMap JavaDoc;
40 import java.util.HashSet JavaDoc;
41 import java.util.Map JavaDoc;
42 import java.util.Set JavaDoc;
43 import java.util.WeakHashMap JavaDoc;
44 import javax.swing.SwingUtilities JavaDoc;
45 import org.netbeans.modules.websvc.api.jaxws.project.config.JaxWsModel;
46 import org.netbeans.modules.websvc.core.wseditor.spi.WSEditor;
47 import org.netbeans.modules.websvc.core.wseditor.spi.WSEditorProvider;
48 import org.netbeans.modules.websvc.core.wseditor.spi.WSEditorProviderRegistry;
49 import org.openide.DialogDescriptor;
50 import org.openide.DialogDisplayer;
51 import org.openide.NotifyDescriptor;
52 import org.openide.nodes.Node;
53 import org.openide.util.HelpCtx;
54 import org.openide.util.Lookup;
55 import org.openide.util.WeakListeners;
56
57 /**
58  *
59  * @author rico
60  */

61 public class EditWSAttributesCookieImpl implements EditWSAttributesCookie{
62     
63     /** Creates a new instance of EditWSAttributesCookieImpl */
64     public EditWSAttributesCookieImpl(Node node, JaxWsModel jaxWsModel) {
65         this.node = node;
66         this.jaxWsModel = jaxWsModel;
67     }
68     
69     public void openWSAttributesEditor(){
70         tc = cachedTopComponents.get(node);
71         if(tc == null){
72             //populate the editor registry if needed
73
populateWSEditorProviderRegistry();
74             //get all providers
75
providers =
76                     WSEditorProviderRegistry.getDefault().getEditorProviders();
77             tc = new EditWSAttributesTopComponent();
78             cachedTopComponents.put(this, tc);
79         }
80         populatePanels();
81         SwingUtilities.invokeLater(new Runnable JavaDoc(){
82             public void run(){
83                 tc.addTabs(editors, node, jaxWsModel );
84                 DialogDescriptor dialogDesc = new DialogDescriptor(tc, node.getName());
85                 dialogDesc.setHelpCtx(new HelpCtx(EditWSAttributesCookieImpl.class));
86                 Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog(dialogDesc);
87                 dialog.setVisible(true);
88                 if(dialogDesc.getValue() == NotifyDescriptor.OK_OPTION){
89                     for(WSEditor editor : editors){
90                         editor.save(node, jaxWsModel);
91                     }
92                 } else{
93                     for(WSEditor editor: editors){
94                         editor.cancel(node, jaxWsModel);
95                     }
96                 }
97             }
98         });
99     }
100     
101     
102     
103     class DialogWindowListener extends WindowAdapter JavaDoc{
104         Set JavaDoc<WSEditor> editors;
105         public DialogWindowListener(Set JavaDoc<WSEditor> editors){
106             this.editors = editors;
107         }
108         public void windowClosing(WindowEvent JavaDoc e){
109             for(WSEditor editor: editors){
110                 editor.cancel(node, jaxWsModel);
111             }
112         }
113     }
114     
115     public Set JavaDoc getWSEditorProviders(){
116         return providers;
117     }
118     
119     private void populatePanels(){
120         editors = new HashSet JavaDoc<WSEditor>();
121         for(WSEditorProvider provider : providers){
122             if(provider.enable(node)){
123                 //for each provider, create a WSAttributesEditor
124
WSEditor editor = provider.createWSEditor();
125                 editors.add(editor);
126             }
127         }
128     }
129     
130     private void populateWSEditorProviderRegistry(){
131         WSEditorProviderRegistry registry = WSEditorProviderRegistry.getDefault();
132         if(registry.getEditorProviders().isEmpty()){
133             Lookup.Result results = Lookup.getDefault().
134                     lookup(new Lookup.Template(WSEditorProvider.class));
135             Collection JavaDoc<WSEditorProvider> services = results.allInstances();
136             //System.out.println("###number of editors: " + services.size());
137
for(WSEditorProvider provider : services){
138                 registry.register(provider);
139             }
140         }
141     }
142     
143     private Set JavaDoc<WSEditorProvider> providers;
144     private Set JavaDoc<WSEditor> editors;
145     private static Map JavaDoc<EditWSAttributesCookie, EditWSAttributesTopComponent> cachedTopComponents
146             = new WeakHashMap JavaDoc<EditWSAttributesCookie, EditWSAttributesTopComponent>();
147     private EditWSAttributesTopComponent tc;
148     private Node node;
149     private JaxWsModel jaxWsModel;
150     private DialogWindowListener windowListener;
151 }
152
Popular Tags