~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to j2ee/ejbcore/src/org/netbeans/modules/j2ee/ejbcore/ui/logicalview/entres/SendJMSMessageUiSupport.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.j2ee.ejbcore.ui.logicalview.entres;
 
43
 
 
44
import java.awt.Component;
 
45
import java.awt.event.ActionEvent;
 
46
import java.awt.event.ActionListener;
 
47
import java.io.IOException;
 
48
import java.util.ArrayList;
 
49
import java.util.Collections;
 
50
import java.util.Comparator;
 
51
import java.util.HashMap;
 
52
import java.util.List;
 
53
import java.util.Map;
 
54
import javax.swing.DefaultListCellRenderer;
 
55
import javax.swing.JComboBox;
 
56
import javax.swing.JList;
 
57
import javax.swing.JTextField;
 
58
import org.netbeans.api.project.Project;
 
59
import org.netbeans.api.project.ProjectUtils;
 
60
import org.netbeans.api.project.ui.OpenProjects;
 
61
import org.netbeans.modules.j2ee.api.ejbjar.EjbJar;
 
62
import org.netbeans.modules.j2ee.dd.api.ejb.EjbJarMetadata;
 
63
import org.netbeans.modules.j2ee.dd.api.ejb.EnterpriseBeans;
 
64
import org.netbeans.modules.j2ee.dd.api.ejb.MessageDriven;
 
65
import org.netbeans.modules.j2ee.deployment.common.api.ConfigurationException;
 
66
import org.netbeans.modules.j2ee.deployment.common.api.MessageDestination;
 
67
import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
 
68
import org.netbeans.modules.j2ee.ejbcore.ejb.wizard.mdb.MessageDestinationUiSupport;
 
69
import org.netbeans.modules.j2ee.ejbcore.ui.logicalview.entres.SendJMSMessageUiSupport.MdbHolder;
 
70
import org.netbeans.modules.j2ee.metadata.model.api.MetadataModel;
 
71
import org.netbeans.modules.j2ee.metadata.model.api.MetadataModelAction;
 
72
import org.openide.util.Exceptions;
 
73
import org.openide.util.NbBundle;
 
74
 
 
75
/**
 
76
 * Support for SendJmsMessagePanel class.
 
77
 * <p>
 
78
 * This class contains only static methods.
 
79
 * @author Tomas Mysik
 
80
 * @see MessageDestinationUiSupport
 
81
 */
 
82
public abstract class SendJMSMessageUiSupport extends MessageDestinationUiSupport {
 
83
    
 
84
    /**
 
85
     * Get list of message-driven beans with all required properties.
 
86
     * @return list of message-driven beans.
 
87
     */
 
88
    public static List<MdbHolder> getMdbs() {
 
89
        List<MdbHolder> mdbs = new ArrayList<MdbHolder>();
 
90
        
 
91
        Project[] openProjects = OpenProjects.getDefault().getOpenProjects();
 
92
        for (Project p : openProjects) {
 
93
            if (EjbJar.getEjbJars(p).length > 0) {
 
94
                try {
 
95
                    Map<String, String> drivens = getMdbs(p);
 
96
                    populateMdbs(mdbs, drivens, p);
 
97
                } catch (IOException ioe) {
 
98
                    Exceptions.printStackTrace(ioe);
 
99
                }
 
100
            }
 
101
        }
 
102
        
 
103
        return mdbs;
 
104
    }
 
105
    
 
106
    /**
 
107
     * Populate given combo box and text field with given message-driven beans.
 
108
     * @param mdbs message-driven beans for given combo box and text field.
 
109
     * @param comboBox combo box to populate.
 
110
     * @param textField text field to populate.
 
111
     */
 
112
    public static void populateMessageDrivenBeans(final List<MdbHolder> mdbs, final JComboBox comboBox,
 
113
            final JTextField textField) {
 
114
        assert mdbs != null;
 
115
        assert comboBox != null;
 
116
        assert textField != null;
 
117
        
 
118
        comboBox.setRenderer(new MdbHolderListCellRenderer());
 
119
        
 
120
        List<MdbHolder> sortedMdbs = new ArrayList<MdbHolder>(mdbs);
 
121
        Collections.sort(sortedMdbs, new MdbHolderComparator());
 
122
        
 
123
        comboBox.removeAllItems();
 
124
        textField.setText("");
 
125
        for (MdbHolder mdbHolder : sortedMdbs) {
 
126
            comboBox.addItem(mdbHolder);
 
127
        }
 
128
        comboBox.addActionListener(new ActionListener() {
 
129
            public void actionPerformed(ActionEvent actionEvent) {
 
130
                populateMdbTextField((JComboBox) actionEvent.getSource(), textField);
 
131
            }
 
132
        });
 
133
        populateMdbTextField(comboBox, textField);
 
134
    }
 
135
    
 
136
    private static void populateMdbTextField(final JComboBox comboBox, final JTextField textField) {
 
137
        MdbHolder selectedItem = (MdbHolder) comboBox.getSelectedItem();
 
138
        if (selectedItem != null) {
 
139
            textField.setText(selectedItem.getMessageDestination().getName());
 
140
        }
 
141
    }
 
142
    
 
143
    private static void populateMdbs(List<MdbHolder> mdbs, final Map<String, String> drivens, final Project project) {
 
144
        for (Map.Entry<String, String> mdbEntry : drivens.entrySet()) {
 
145
            J2eeModuleProvider j2eeModuleProvider = getJ2eeModuleProvider(project);
 
146
            try {
 
147
                MessageDestination messageDestination = null;
 
148
                String mdbName = mdbEntry.getKey();
 
149
                String mappedName = mdbEntry.getValue();
 
150
                String destName = j2eeModuleProvider.getConfigSupport().findMessageDestinationName(mdbName);
 
151
                if (destName == null && mappedName != null) {
 
152
                    destName = mappedName;
 
153
                }
 
154
                if (destName != null) {
 
155
                    messageDestination = j2eeModuleProvider.getConfigSupport().findMessageDestination(destName);
 
156
                    mdbs.add(new MdbHolder(mdbName, messageDestination, project));
 
157
                }
 
158
            } catch (ConfigurationException ce) {
 
159
                Exceptions.printStackTrace(ce);
 
160
            }
 
161
        }
 
162
    }
 
163
    
 
164
    private static J2eeModuleProvider getJ2eeModuleProvider(Project project) {
 
165
        return project.getLookup().lookup(J2eeModuleProvider.class);
 
166
    }
 
167
 
 
168
    // kay ejb-name and value is mapped-name
 
169
    private static Map<String, String> getMdbs(Project project) throws IOException {
 
170
        
 
171
        Map<String, String> mdbs = new HashMap<String, String>();
 
172
        
 
173
        for (EjbJar ejbModule : EjbJar.getEjbJars(project)) {
 
174
            MetadataModel<EjbJarMetadata> metadataModel = ejbModule.getMetadataModel();
 
175
            Map<String, String> mdbsInModule = metadataModel.runReadAction(new MetadataModelAction<EjbJarMetadata, Map<String, String>>() {
 
176
                public Map<String, String> run(EjbJarMetadata metadata) throws Exception {
 
177
                    Map<String, String> result = new HashMap<String, String>();
 
178
                    EnterpriseBeans eb = metadata.getRoot().getEnterpriseBeans();
 
179
                    if (eb == null) {
 
180
                        return Collections.<String, String>emptyMap();
 
181
                    }
 
182
 
 
183
                    MessageDriven[] messageDrivens = eb.getMessageDriven();
 
184
                    for (MessageDriven mdb : messageDrivens) {
 
185
                        result.put(mdb.getEjbName(), mdb.getMappedName());
 
186
                    }
 
187
                    return result;
 
188
                }
 
189
            });
 
190
            mdbs.putAll(mdbsInModule);
 
191
        }
 
192
        
 
193
        return mdbs;
 
194
    }
 
195
    
 
196
    /**
 
197
     * Holder for message-driven bean and its properties.
 
198
     */
 
199
    public static class MdbHolder {
 
200
        
 
201
        private final String mdbEjbName;
 
202
        private final MessageDestination messageDestination;
 
203
        private final Project project;
 
204
 
 
205
        /** Constructor with all properties. */
 
206
        public MdbHolder(String mdbEjbName, final MessageDestination messageDestination, final Project project) {
 
207
            assert mdbEjbName != null;
 
208
            assert messageDestination != null;
 
209
            assert project != null;
 
210
            
 
211
            this.mdbEjbName = mdbEjbName;
 
212
            this.messageDestination = messageDestination;
 
213
            this.project = project;
 
214
        }
 
215
 
 
216
        public MessageDestination getMessageDestination() {
 
217
            return messageDestination;
 
218
        }
 
219
 
 
220
        public Project getProject() {
 
221
            return project;
 
222
        }
 
223
        
 
224
        public String getMdbEjbName() {
 
225
            return mdbEjbName;
 
226
        }
 
227
        
 
228
        public String getProjectName() {
 
229
            return ProjectUtils.getInformation(project).getDisplayName();
 
230
        }
 
231
        
 
232
        @Override
 
233
        public String toString() {
 
234
            StringBuilder sb = new StringBuilder();
 
235
            sb.append(getClass().getName());
 
236
            sb.append(" [");
 
237
            sb.append(getMdbEjbName());
 
238
            sb.append(" (");
 
239
            sb.append(getMessageDestination().getType().toString());
 
240
            sb.append("), ");
 
241
            sb.append(getProjectName());
 
242
            sb.append("]");
 
243
            return sb.toString();
 
244
        }
 
245
    }
 
246
    
 
247
    // optional - create factory method for this class
 
248
    private static class MdbHolderComparator implements Comparator<MdbHolder> {
 
249
        
 
250
        public int compare(MdbHolder mdbHolder1, MdbHolder mdbHolder2) {
 
251
            
 
252
            if (mdbHolder1 == null) {
 
253
                return mdbHolder2 == null ? 0 : -1;
 
254
            }
 
255
            
 
256
            if (mdbHolder2 == null) {
 
257
                return 1;
 
258
            }
 
259
            
 
260
            String name1 = mdbHolder1.getMdbEjbName();
 
261
            String name2 = mdbHolder2.getMdbEjbName();
 
262
            if (name1 == null) {
 
263
                return name2 == null ? 0 : -1;
 
264
            }
 
265
            
 
266
            return name2 == null ? 1 : name1.compareToIgnoreCase(name2);
 
267
        }
 
268
    }
 
269
    
 
270
    // optional - create factory method for this class
 
271
    private static class MdbHolderListCellRenderer extends DefaultListCellRenderer {
 
272
 
 
273
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
 
274
                boolean cellHasFocus) {
 
275
 
 
276
            if (isSelected) {
 
277
                setBackground(list.getSelectionBackground());
 
278
                setForeground(list.getSelectionForeground());
 
279
            } else {
 
280
                setBackground(list.getBackground());
 
281
                setForeground(list.getForeground());
 
282
            }
 
283
 
 
284
            if (value instanceof MdbHolder) {
 
285
                MdbHolder mdbHolder = (MdbHolder) value;
 
286
                setText(mdbHolder.getMdbEjbName());
 
287
                // tooltip
 
288
                String type = MessageDestination.Type.QUEUE.equals(mdbHolder.getMessageDestination().getType()) ? "LBL_Queue" : "LBL_Topic"; // NOI18N
 
289
                StringBuilder sb = new StringBuilder(mdbHolder.getProjectName());
 
290
                sb.append(" : "); // NOI18N
 
291
                sb.append(mdbHolder.getMdbEjbName());
 
292
                sb.append(" ["); // NOI18N
 
293
                sb.append(NbBundle.getMessage(MessageDestinationUiSupport.class, type));
 
294
                sb.append("]"); // NOI18N
 
295
                setToolTipText(sb.toString());
 
296
            } else {
 
297
                setText(value != null ? value.toString() : ""); // NOI18N
 
298
                setToolTipText(""); // NOI18N
 
299
            }
 
300
            return this;
 
301
        }
 
302
    }
 
303
}