1 /*------------------------------------------------------------------------------
2 Name: SimpleReaderGui.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Author: Wolfgang Kleinertz, Thomas Bodemer
6 ------------------------------------------------------------------------------*/
7 package javaclients.simplereader;
8
9 import java.awt.BorderLayout;
10 import java.awt.Component;
11 import java.awt.Dimension;
12 import java.awt.Image;
13 import java.awt.Toolkit;
14 import java.awt.event.ActionEvent;
15 import java.util.Properties;
16
17 import javax.swing.DefaultListCellRenderer;
18 import javax.swing.DefaultListModel;
19 import javax.swing.Icon;
20 import javax.swing.ImageIcon;
21 import javax.swing.JButton;
22 import javax.swing.JFrame;
23 import javax.swing.JList;
24 import javax.swing.JPanel;
25 import javax.swing.JScrollPane;
26 import javax.swing.JSplitPane;
27 import javax.swing.JTextArea;
28 import javax.swing.JTextField;
29 import javax.swing.ListSelectionModel;
30 import javax.swing.UIManager;
31 import javax.swing.event.ListSelectionEvent;
32 import javax.swing.event.ListSelectionListener;
33
34 import org.xmlBlaster.client.I_Callback;
35 import org.xmlBlaster.client.I_XmlBlasterAccess;
36 import org.xmlBlaster.client.key.SubscribeKey;
37 import org.xmlBlaster.client.key.UnSubscribeKey;
38 import org.xmlBlaster.client.key.UpdateKey;
39 import org.xmlBlaster.client.qos.ConnectQos;
40 import org.xmlBlaster.client.qos.SubscribeQos;
41 import org.xmlBlaster.client.qos.SubscribeReturnQos;
42 import org.xmlBlaster.client.qos.UnSubscribeQos;
43 import org.xmlBlaster.client.qos.UpdateQos;
44 import org.xmlBlaster.util.FileDumper;
45 import org.xmlBlaster.util.Global;
46 import org.xmlBlaster.util.XmlBlasterException;
47 import org.xmlBlaster.util.def.Constants;
48
49 /**
50 * Usage:
51 * <pre>
52 * java javaclients.simplereader.SimpleReaderGui -dumpToFile true -directoryName /tmp
53 * java javaclients.simplereader.SimpleReaderGui -xpath "//key[starts-with(@oid,'com.')]"
54 * </pre>
55 * -directoryName defaults to ${user.home}/FileDumper
56 */
57 public class SimpleReaderGui extends JFrame implements I_Callback {
58 /**
59 *
60 */
61 private static final long serialVersionUID = 8368002446068669824L;
62
63 private static final String ME = "SimpleReaderGui";
64
65 private static final String USR_LOGIN = ME;
66
67 private FileDumper fileDumper;
68 private boolean dumpToFile;
69
70 private ImageIcon image;
71
72 private I_XmlBlasterAccess xmlBlaster;
73 private SubscribeReturnQos subscribeReturnQos;
74
75 private DefaultListModel listModel = new DefaultListModel();
76 private JList jList1 = new JList();
77 private JScrollPane jScrollPane1 = new JScrollPane();
78 private JSplitPane jSplitPane1 = new JSplitPane();
79 private JPanel jPanel1 = new JPanel();
80 private JPanel jPanel2 = new JPanel();
81 private BorderLayout borderLayout1 = new BorderLayout();
82 private BorderLayout borderLayout2 = new BorderLayout();
83 private JScrollPane jScrollPane2 = new JScrollPane();
84 private JTextArea jTextArea1 = new JTextArea();
85 private JPanel jPanel4 = new JPanel();
86 private JTextField jTextField1 = new JTextField();
87 private JPanel jPanel3 = new JPanel();
88 private JButton jButton1 = new JButton();
89 private BorderLayout borderLayout3 = new BorderLayout();
90 private JButton jButton2 = new JButton();
91 private BorderLayout borderLayout4 = new BorderLayout();
92
93
94 public SimpleReaderGui(I_XmlBlasterAccess _xmlBlaster) throws Exception{
95 this.xmlBlaster = _xmlBlaster;
96 try {
97 jbInit();
98 }
99 catch(Exception e) {
100 e.printStackTrace();
101 }
102
103 this.dumpToFile = _xmlBlaster.getGlobal().getProperty().get("dumpToFile", false);
104 if (this.dumpToFile)
105 this.fileDumper = new FileDumper(_xmlBlaster.getGlobal());
106
107 // set the application icon
108 java.net.URL oUrl;
109 oUrl = this.getClass().getResource("AppIcon.gif");
110 Image img = null;
111 if (oUrl != null)
112 img = java.awt.Toolkit.getDefaultToolkit().getImage(oUrl);
113 if(img != null) {
114 this.setIconImage(img);
115 // System.out.println(img.toString());
116 } else {
117 System.out.println("AppIcon.gif not found");
118 } // -- if img != null
119 this.setTitle(ME);
120 }
121
122 public static void main(String[] args) {
123 SimpleReaderGui srGui = null;
124 try {
125 Global glob = new Global();
126 if (glob.init(args) != 0) { // Get help with -help
127 System.out.println(glob.usage());
128 System.err.println("Example:\n");
129 System.err.println("java javaclients.simplereader.SimpleReaderGui -xpath '//key' -session.name simpleReader -passwd secret -protocol SOCKET -dispatch/connection/plugin/socket/hostname localhost -dumpToFile true -directoryName ${user.home}/FileDumper\n");
130 System.exit(1);
131 }
132
133 I_XmlBlasterAccess xmlBlaster = glob.getXmlBlasterAccess();
134 srGui = new SimpleReaderGui(xmlBlaster);
135 srGui.loadImage();
136 String loginName = glob.getProperty().get("session.name", "");
137 ConnectQos qos = (loginName.length() < 1) ? new ConnectQos(glob, USR_LOGIN, null) : new ConnectQos(glob);
138 xmlBlaster.connect(qos, srGui);
139 srGui.setTitle(ME + " " + xmlBlaster.getSessionName().getNodeIdStr() + " <no subscription>");
140 }
141 catch(Exception ex) {
142 log_error( ME, ex.toString(), "");
143 ex.printStackTrace();
144 }
145 if( srGui != null ) {
146 srGui.setSize(640,480);
147 srGui.show();
148 }
149 }
150
151 public String update(String secretCallbackSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos) throws XmlBlasterException
152 {
153 MessageWrapper messageWrapper = new MessageWrapper(secretCallbackSessionId, updateKey, content, updateQos);
154 listModel.addElement(messageWrapper);
155 System.out.println("Key: "+updateKey.toXml()+" >>> Content: "+new String(content)+" >>> ---");
156 if (this.fileDumper != null) {
157 this.fileDumper.dumpMessage(updateKey.getData(), content, updateQos.getData());
158 }
159 return "";
160 }
161
162
163 private void jbInit() throws Exception {
164 jList1.setFixedCellHeight(15);
165 jList1.setModel(listModel);
166 jList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
167 jList1.setMaximumSize(new Dimension(1000, 1000));
168 jList1.setMinimumSize(new Dimension(100, 10));
169 jList1.setCellRenderer(new MyCellRenderer());
170
171 /*
172 this.setDefaultCloseOperation(EXIT_ON_CLOSE);
173 EXIT_ON_CLOSE is not working with JDK 1.2.2.
174 EXIT_ON_CLOSE should be defined in the interface javax.swing.WindowConstants
175 but it isn't.
176 The value is set to 3, therefore we set it hard here.
177 */
178 this.setDefaultCloseOperation(3);
179 jList1.addListSelectionListener(new ListSelectionListener() {
180 public void valueChanged(ListSelectionEvent evt) {
181 JList source = (JList) evt.getSource();
182 MessageWrapper selection = (MessageWrapper) source.getSelectedValue();
183 if (selection != null) {
184 String secretCallbackSessionId = selection.getSecretCallbackSessionId();
185 UpdateKey updateKey = selection.getUpdateKey();
186 byte[] content = selection.getContent();
187 UpdateQos updateQos = selection.getUpdateQos();
188 Properties props = new Properties();
189 props.put(Constants.TOXML_FORCEREADABLE, ""+true);
190 String text = (
191 new StringBuffer()
192 .append(" - - - secretCallbackSessionId: - - -\n")
193 .append(secretCallbackSessionId)
194 .append("\n - - - updateKey: - - -")
195 .append(updateKey.toXml())
196 .append("\n - - - content: - - -\n")
197 .append(new String(content))
198 .append("\n - - - updateQos: - - -")
199 .append(updateQos.getData().toXml("", props)))
200 .append("\n - - - end - - -\n")
201 .toString();
202 jTextArea1.setText(text);
203 } else {
204 jTextArea1.setText("");
205 }
206 }
207 });
208 jPanel1.setLayout(borderLayout1);
209 jPanel2.setLayout(borderLayout2);
210 jTextArea1.setMinimumSize(new Dimension(20, 23));
211 jTextArea1.setEditable(false);
212 jPanel1.setMaximumSize(new Dimension(400, 300));
213 jPanel1.setMinimumSize(new Dimension(200, 300));
214 jPanel1.setPreferredSize(new Dimension(200, 300));
215 jScrollPane2.setAutoscrolls(true);
216 jScrollPane2.setPreferredSize(new Dimension(300, 26));
217 jSplitPane1.setMinimumSize(new Dimension(234, 400));
218 jSplitPane1.setPreferredSize(new Dimension(512, 400));
219 jTextField1.setText(this.xmlBlaster.getGlobal().getProperty().get("xpath", "//key"));
220 jPanel3.setLayout(borderLayout4);
221 jPanel3.setMaximumSize(new Dimension(120, 40));
222 jPanel3.setMinimumSize(new Dimension(120, 40));
223 jPanel3.setPreferredSize(new Dimension(120, 40));
224 jButton1.setSelected(true);
225 jButton1.setText("subscribe");
226 jButton1.addActionListener(new java.awt.event.ActionListener() {
227 public void actionPerformed(ActionEvent e) {
228 jButton1_actionPerformed(e);
229 }
230 });
231 jPanel4.setLayout(borderLayout3);
232 jPanel4.setMaximumSize(new Dimension(32767, 70));
233 jPanel4.setMinimumSize(new Dimension(120, 70));
234 jPanel4.setPreferredSize(new Dimension(120, 70));
235 jScrollPane1.setAutoscrolls(true);
236 jButton2.setText("clear");
237 jButton2.addActionListener(new java.awt.event.ActionListener() {
238 public void actionPerformed(ActionEvent e) {
239 jButton2_actionPerformed(e);
240 }
241 });
242 jPanel1.add(jScrollPane1, BorderLayout.CENTER);
243 jScrollPane1.getViewport().add(jList1);
244 jPanel1.add(jPanel4, BorderLayout.NORTH);
245 jPanel3.add(jButton1, BorderLayout.WEST);
246 jPanel3.add(jButton2, BorderLayout.CENTER);
247 jPanel4.add(jPanel3, BorderLayout.CENTER);
248 jPanel4.add(jTextField1, BorderLayout.NORTH);
249 jSplitPane1.add(jPanel2, JSplitPane.RIGHT);
250 jPanel2.add(jScrollPane2, BorderLayout.CENTER);
251 jSplitPane1.add(jPanel1, JSplitPane.LEFT);
252 jScrollPane2.getViewport().add(jTextArea1, null);
253 this.getContentPane().add(jSplitPane1, BorderLayout.CENTER);
254 }
255
256 void jButton1_actionPerformed(ActionEvent e) {
257 String text = jTextField1.getText();
258 this.setTitle(ME + " " + xmlBlaster.getSessionName().getNodeIdStr() + " " + text);
259
260 if (this.subscribeReturnQos != null) {
261 try {
262 UnSubscribeKey key = new UnSubscribeKey(xmlBlaster.getGlobal(), this.subscribeReturnQos.getSubscriptionId());
263 UnSubscribeQos qos = new UnSubscribeQos(xmlBlaster.getGlobal() );
264 xmlBlaster.unSubscribe(key.toXml(), qos.toXml());
265 System.out.println(ME + " unSubscribe from " + this.subscribeReturnQos.getSubscriptionId());
266 this.subscribeReturnQos = null;
267 }
268 catch( Throwable ex ) {
269 System.err.println("error-error-error-error >>>"+ex.toString());
270 System.out.println(ME + " " + ex.getMessage());
271 ex.printStackTrace();
272 }
273 }
274
275 try {
276 SubscribeKey key = new SubscribeKey(xmlBlaster.getGlobal(), text, "XPATH");
277 SubscribeQos qos = new SubscribeQos(xmlBlaster.getGlobal() );
278 this.subscribeReturnQos = xmlBlaster.subscribe(key.toXml(), qos.toXml());
279 System.out.println(ME + " subscribe on " + text + " -> " + this.subscribeReturnQos.getSubscriptionId());
280 }
281 catch( Exception ex ) {
282 System.err.println("error-error-error-error >>>"+ex.toString());
283 System.out.println(ME + " " + ex.getMessage());
284 ex.printStackTrace();
285 }
286 }
287
288 void jButton2_actionPerformed(ActionEvent ae) {
289 try {
290 listModel.clear();
291 } catch (Exception e) {
292 System.err.println("error-error-error-error >>>"+e.toString());
293 System.out.println(ME + " " + e.getMessage());
294 e.printStackTrace();
295 }
296 }
297
298 public static void log_error(String ME, String text1, String text2) {
299 System.err.println("ME:" + ME + "text:" + text1 + text2);
300 }
301
302
303 public void loadImage() {
304 try {
305 String filename = "red.gif";
306 Image img = Toolkit.getDefaultToolkit().createImage(getClass().getResource(filename));
307 image = new ImageIcon( img );
308 }
309 catch (Exception ex) {
310 System.err.println("error-error-error-error >>>"+ex.toString());
311 System.out.println(ME + " " + ex.getMessage());
312 ex.printStackTrace();
313 }
314 }
315
316 class MyCellRenderer extends DefaultListCellRenderer {
317 /**
318 *
319 */
320 private static final long serialVersionUID = 5678672570993331767L;
321
322 public Component getListCellRendererComponent(
323 JList list,
324 Object value,
325 int index,
326 boolean isSelected,
327 boolean cellHasFocus)
328 {
329 this.setComponentOrientation(list.getComponentOrientation());
330 if (isSelected) {
331 this.setBackground(list.getSelectionBackground());
332 this.setForeground(list.getSelectionForeground());
333 }
334 else {
335 this.setBackground(list.getBackground());
336 this.setForeground(list.getForeground());
337 }
338
339 if (value instanceof Icon) {
340 setIcon((Icon)value);
341 setText("");
342 }
343 else {
344 setIcon(image);
345 setText((value == null) ? "" : ( ((MessageWrapper) value).getUpdateKey().getOid()));
346 }
347
348 this.setEnabled(list.isEnabled());
349 this.setFont(list.getFont());
350 setBorder((cellHasFocus) ? UIManager.getBorder("List.focusCellHighlightBorder") : noFocusBorder);
351
352 return this;
353 }
354 }
355
356
357 } // -- class
358
359 // -- file
syntax highlighted by Code2HTML, v. 0.9.1