2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
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]"
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.
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.
42
package org.netbeans.modules.j2ee.ejbcore.ui.logicalview.entres;
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;
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;
76
* Support for SendJmsMessagePanel class.
78
* This class contains only static methods.
80
* @see MessageDestinationUiSupport
82
public abstract class SendJMSMessageUiSupport extends MessageDestinationUiSupport {
85
* Get list of message-driven beans with all required properties.
86
* @return list of message-driven beans.
88
public static List<MdbHolder> getMdbs() {
89
List<MdbHolder> mdbs = new ArrayList<MdbHolder>();
91
Project[] openProjects = OpenProjects.getDefault().getOpenProjects();
92
for (Project p : openProjects) {
93
if (EjbJar.getEjbJars(p).length > 0) {
95
Map<String, String> drivens = getMdbs(p);
96
populateMdbs(mdbs, drivens, p);
97
} catch (IOException ioe) {
98
Exceptions.printStackTrace(ioe);
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.
112
public static void populateMessageDrivenBeans(final List<MdbHolder> mdbs, final JComboBox comboBox,
113
final JTextField textField) {
115
assert comboBox != null;
116
assert textField != null;
118
comboBox.setRenderer(new MdbHolderListCellRenderer());
120
List<MdbHolder> sortedMdbs = new ArrayList<MdbHolder>(mdbs);
121
Collections.sort(sortedMdbs, new MdbHolderComparator());
123
comboBox.removeAllItems();
124
textField.setText("");
125
for (MdbHolder mdbHolder : sortedMdbs) {
126
comboBox.addItem(mdbHolder);
128
comboBox.addActionListener(new ActionListener() {
129
public void actionPerformed(ActionEvent actionEvent) {
130
populateMdbTextField((JComboBox) actionEvent.getSource(), textField);
133
populateMdbTextField(comboBox, textField);
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());
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);
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;
154
if (destName != null) {
155
messageDestination = j2eeModuleProvider.getConfigSupport().findMessageDestination(destName);
156
mdbs.add(new MdbHolder(mdbName, messageDestination, project));
158
} catch (ConfigurationException ce) {
159
Exceptions.printStackTrace(ce);
164
private static J2eeModuleProvider getJ2eeModuleProvider(Project project) {
165
return project.getLookup().lookup(J2eeModuleProvider.class);
168
// kay ejb-name and value is mapped-name
169
private static Map<String, String> getMdbs(Project project) throws IOException {
171
Map<String, String> mdbs = new HashMap<String, String>();
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();
180
return Collections.<String, String>emptyMap();
183
MessageDriven[] messageDrivens = eb.getMessageDriven();
184
for (MessageDriven mdb : messageDrivens) {
185
result.put(mdb.getEjbName(), mdb.getMappedName());
190
mdbs.putAll(mdbsInModule);
197
* Holder for message-driven bean and its properties.
199
public static class MdbHolder {
201
private final String mdbEjbName;
202
private final MessageDestination messageDestination;
203
private final Project project;
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;
211
this.mdbEjbName = mdbEjbName;
212
this.messageDestination = messageDestination;
213
this.project = project;
216
public MessageDestination getMessageDestination() {
217
return messageDestination;
220
public Project getProject() {
224
public String getMdbEjbName() {
228
public String getProjectName() {
229
return ProjectUtils.getInformation(project).getDisplayName();
233
public String toString() {
234
StringBuilder sb = new StringBuilder();
235
sb.append(getClass().getName());
237
sb.append(getMdbEjbName());
239
sb.append(getMessageDestination().getType().toString());
241
sb.append(getProjectName());
243
return sb.toString();
247
// optional - create factory method for this class
248
private static class MdbHolderComparator implements Comparator<MdbHolder> {
250
public int compare(MdbHolder mdbHolder1, MdbHolder mdbHolder2) {
252
if (mdbHolder1 == null) {
253
return mdbHolder2 == null ? 0 : -1;
256
if (mdbHolder2 == null) {
260
String name1 = mdbHolder1.getMdbEjbName();
261
String name2 = mdbHolder2.getMdbEjbName();
263
return name2 == null ? 0 : -1;
266
return name2 == null ? 1 : name1.compareToIgnoreCase(name2);
270
// optional - create factory method for this class
271
private static class MdbHolderListCellRenderer extends DefaultListCellRenderer {
273
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
274
boolean cellHasFocus) {
277
setBackground(list.getSelectionBackground());
278
setForeground(list.getSelectionForeground());
280
setBackground(list.getBackground());
281
setForeground(list.getForeground());
284
if (value instanceof MdbHolder) {
285
MdbHolder mdbHolder = (MdbHolder) value;
286
setText(mdbHolder.getMdbEjbName());
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());
297
setText(value != null ? value.toString() : ""); // NOI18N
298
setToolTipText(""); // NOI18N