KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > client > tools > admin > SubscriptionPanel


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA.
18  *
19  * Initial developer(s): ScalAgent DT
20  * Contributor(s):
21  */

22 package org.objectweb.joram.client.tools.admin;
23
24 import java.awt.*;
25 import java.awt.event.*;
26 import javax.swing.*;
27 import java.util.*;
28
29 import org.objectweb.joram.client.jms.admin.*;
30 import javax.jms.*;
31
32 public class SubscriptionPanel extends JPanel {
33   private final AdminController controller;
34
35   private DefaultListModel msgListModel;
36
37   private int serverId;
38
39   private User user;
40
41   private Subscription sub;
42
43   private JLabel idLabel;
44   private JLabel countLabel;
45   private JList msgList;
46   private JTextArea msgDisplay;
47
48   public SubscriptionPanel(AdminController controller) {
49     super(new BorderLayout());
50     this.controller = controller;
51
52     JPanel form = new JPanel();
53
54     GridBagLayout gridbag = new GridBagLayout();
55     GridBagConstraints c = new GridBagConstraints();
56     c.anchor = GridBagConstraints.NORTHWEST;
57     // c.weightx = 1;
58
// c.weighty = 1;
59
c.insets = new Insets(5,5,5,5);
60     form.setLayout(gridbag);
61
62     JLabel title = new JLabel("Subscription Information");
63     title.setFont(new Font("Arial", Font.BOLD, 18));
64     title.setHorizontalAlignment(JLabel.LEFT);
65     c.gridx = 0;
66     c.gridy = 0;
67     gridbag.setConstraints(title, c);
68     form.add(title);
69
70     JLabel label = new JLabel("Subscription Name:");
71     c.gridx = 0;
72     c.gridy = 1;
73     gridbag.setConstraints(label, c);
74     form.add(label);
75
76     idLabel = new JLabel();
77     c.gridx = 1;
78     c.gridy = 1;
79     gridbag.setConstraints(idLabel, c);
80     form.add(idLabel);
81     
82     label = new JLabel("Message Count:");
83     c.gridx = 0;
84     c.gridy = 2;
85     gridbag.setConstraints(label, c);
86     form.add(label);
87     
88     countLabel = new JLabel();
89     c.gridx = 1;
90     c.gridy = 2;
91     gridbag.setConstraints(countLabel, c);
92     form.add(countLabel);
93
94     msgListModel = new DefaultListModel();
95     msgList = new JList(msgListModel);
96     msgList.setSelectionMode(
97       ListSelectionModel.SINGLE_INTERVAL_SELECTION);
98
99     JScrollPane listScroller = new JScrollPane(msgList);
100     listScroller.getViewport().setScrollMode(
101       JViewport.SIMPLE_SCROLL_MODE);
102     listScroller.setPreferredSize(new Dimension(400, 80));
103     listScroller.setMinimumSize(new Dimension(400, 80));
104     listScroller.setAlignmentX(LEFT_ALIGNMENT);
105
106     c.gridx = 0;
107     c.gridy = 3;
108     c.gridwidth = 4;
109     gridbag.setConstraints(listScroller, c);
110     form.add(listScroller);
111
112     JButton loadBt = new JButton("Load message");
113     loadBt.addActionListener(new ActionListener() {
114         public void actionPerformed(ActionEvent e) {
115           String JavaDoc selectedMsgId =
116             (String JavaDoc)msgList.getSelectedValue();
117           if (selectedMsgId != null) {
118             try {
119               loadMessage(selectedMsgId);
120             } catch (Exception JavaDoc exc) {
121               JOptionPane.showMessageDialog(
122                 AdminTool.getInstance(),
123                 exc.getMessage(), "Load message",
124                 JOptionPane.ERROR_MESSAGE);
125             }
126           }
127         }
128       });
129     c.gridx = 0;
130     c.gridy = 4;
131     c.gridwidth = 1;
132     gridbag.setConstraints(loadBt, c);
133     form.add(loadBt);
134
135     JButton deleteBt = new JButton("Delete message");
136     deleteBt.addActionListener(new ActionListener() {
137         public void actionPerformed(ActionEvent e) {
138           String JavaDoc selectedMsgId =
139             (String JavaDoc)msgList.getSelectedValue();
140           if (selectedMsgId != null) {
141             try {
142               deleteMessage(selectedMsgId);
143               msgListModel.removeElementAt(
144                 msgList.getSelectedIndex());
145             } catch (Exception JavaDoc exc) {
146               JOptionPane.showMessageDialog(
147                 AdminTool.getInstance(),
148                 exc.getMessage(), "Delete message",
149                 JOptionPane.ERROR_MESSAGE);
150             }
151           }
152         }
153       });
154     c.gridx = 1;
155     c.gridy = 4;
156     gridbag.setConstraints(deleteBt, c);
157     form.add(deleteBt);
158     
159     msgDisplay = new JTextArea();
160     JScrollPane msgScroller = new JScrollPane(msgDisplay);
161     msgScroller.getViewport().setScrollMode(
162       JViewport.SIMPLE_SCROLL_MODE);
163     msgScroller.setPreferredSize(new Dimension(400, 80));
164     msgScroller.setMinimumSize(new Dimension(400, 80));
165     msgScroller.setAlignmentX(LEFT_ALIGNMENT);
166
167     c.gridx = 0;
168     c.gridy = 5;
169     c.gridwidth = 4;
170     gridbag.setConstraints(msgScroller, c);
171     form.add(msgScroller);
172
173     add(form, BorderLayout.NORTH);
174   }
175
176   public void setSubscription(Subscription sub) {
177     this.sub = sub;
178     idLabel.setText(sub.getName());
179     setMessageCount(sub.getMessageCount());
180   }
181
182   private void setMessageCount(int msgCount) {
183     countLabel.setText("" + msgCount);
184   }
185
186   public void setUser(User user) {
187     this.user = user;
188   }
189
190   public void loadMessageIds() throws Exception JavaDoc {
191     msgListModel.removeAllElements();
192     String JavaDoc[] ids = user.getMessageIds(
193       sub.getName());
194     for (int i = 0; i < ids.length; i++) {
195       msgListModel.addElement(ids[i]);
196     }
197     setMessageCount(ids.length);
198   }
199
200   public void loadMessage(String JavaDoc msgId) throws Exception JavaDoc {
201     Message msg = user.readMessage(
202       sub.getName(), msgId);
203     msgDisplay.setText(msg.toString());
204   }
205
206   public void deleteMessage(String JavaDoc msgId) throws Exception JavaDoc {
207     user.deleteMessage(
208       sub.getName(), msgId);
209   }
210 }
211
Popular Tags