1 7 package org.ejtools.management.browser.ui; 8 9 import java.awt.GridBagConstraints ; 10 import java.awt.GridLayout ; 11 import java.awt.Insets ; 12 import java.awt.event.ActionEvent ; 13 import java.awt.event.ActionListener ; 14 import java.util.ArrayList ; 15 16 import javax.management.MBeanAttributeInfo ; 17 import javax.management.MBeanInfo ; 18 import javax.swing.JButton ; 19 import javax.swing.JLabel ; 20 import javax.swing.JList ; 21 import javax.swing.JPanel ; 22 import javax.swing.SwingConstants ; 23 24 import org.apache.log4j.Logger; 25 import org.ejtools.adwt.AwtToolkit; 26 import org.ejtools.management.browser.model.ManagedObject; 27 28 34 public class ManagedObjectEventsViewer extends ManagedObjectViewer 35 { 36 37 private static Logger logger = Logger.getLogger(ManagedObjectEventsViewer.class); 38 39 40 41 public ManagedObjectEventsViewer() 42 { 43 super(); 44 } 45 46 47 52 public ManagedObjectEventsViewer(Object bean) 53 { 54 super(bean); 55 } 56 57 58 62 public MBeanAttributeInfo [] filter(MBeanAttributeInfo [] infos) 63 { 64 ArrayList filteredInfos = new ArrayList (); 65 ArrayList includes = new ArrayList (); 66 includes.add(MO_ATTRIBUTE_EVENTTYPES); 67 68 for (int i = 0; i < infos.length; i++) 69 { 70 String name = infos[i].getName(); 71 if (includes.contains(name.toLowerCase())) 72 { 73 filteredInfos.add(infos[i]); 74 } 75 } 76 77 return (MBeanAttributeInfo []) filteredInfos.toArray(new MBeanAttributeInfo [0]); 78 } 79 80 81 86 public void setObject(Object bean) 87 { 88 try 89 { 90 this.panel.removeAll(); 91 92 if (bean == null) 93 { 94 this.validate(); 95 this.repaint(); 96 return; 97 } 98 99 this.object = (ManagedObject) bean; 100 MBeanInfo beaninfo = this.object.getMBeanInfo(); 101 102 GridBagConstraints gridbagconstraints = new GridBagConstraints (); 103 gridbagconstraints.insets = new Insets (5, 5, 5, 5); 104 gridbagconstraints.anchor = GridBagConstraints.NORTH; 105 gridbagconstraints.weightx = 1.0D; 106 gridbagconstraints.weighty = 0.0D; 107 gridbagconstraints.fill = GridBagConstraints.HORIZONTAL; 108 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 109 110 MBeanAttributeInfo [] infos = this.filter(beaninfo.getAttributes()); 111 if (infos.length == 0) 112 { 113 return; 114 } 115 116 Object value = this.object.getAttribute(infos[0].getName()); 117 String [] eventTypes = (String []) value; 118 JList list = new JList (eventTypes); 119 120 gridbagconstraints.weightx = 0.0D; 121 gridbagconstraints.gridwidth = GridBagConstraints.RELATIVE; 122 JLabel label = new JLabel (resources.getString("customizer.tab.events.text.event.types") + " : ", SwingConstants.RIGHT); 123 label.setToolTipText(resources.getString("customizer.tab.events.tooltip.event.types")); 124 label.setForeground(AwtToolkit.DARK_BLUE); 125 this.panel.add(label, gridbagconstraints); 126 127 gridbagconstraints.weightx = 1.0D; 128 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 129 this.panel.add(list, gridbagconstraints); 130 131 JPanel buttonPanel = new JPanel (new GridLayout (1, 2, 1, 1)); 132 final JButton button1 = new JButton (resources.getString("customizer.tab.events.button.listen")); 133 final JButton button2 = new JButton (resources.getString("customizer.tab.events.button.unlisten")); 134 135 button1.addActionListener( 136 new ActionListener () 137 { 138 public void actionPerformed(ActionEvent e) 139 { 140 try 141 { 142 ManagedObjectEventsViewer.this.object.registerForNotifications(); 143 if (ManagedObjectEventsViewer.this.object.isRegisteredForNotifications()) 144 { 145 button1.setEnabled(false); 146 button2.setEnabled(true); 147 } 148 } 149 catch (Exception ex) 150 { 151 logger.error("Error while performing action" + ex); 152 } 153 } 154 } 155 ); 156 157 button2.addActionListener( 158 new ActionListener () 159 { 160 public void actionPerformed(ActionEvent e) 161 { 162 try 163 { 164 ManagedObjectEventsViewer.this.object.unregisterForNotifications(); 165 if (!ManagedObjectEventsViewer.this.object.isRegisteredForNotifications()) 166 { 167 button1.setEnabled(true); 168 button2.setEnabled(false); 169 } 170 } 171 catch (Exception ex) 172 { 173 logger.error("Error while performing action" + ex); 174 } 175 } 176 } 177 ); 178 179 try 180 { 181 if (ManagedObjectEventsViewer.this.object.isRegisteredForNotifications()) 182 { 183 button1.setEnabled(false); 184 button2.setEnabled(true); 185 } 186 else 187 { 188 button1.setEnabled(true); 189 button2.setEnabled(false); 190 } 191 } 192 catch (Exception ue) 193 { 194 } 195 196 buttonPanel.add(button1); 197 buttonPanel.add(button2); 198 199 gridbagconstraints.weighty = 1.0D; 200 this.panel.add(buttonPanel, gridbagconstraints); 201 202 this.validate(); 203 this.repaint(); 204 } 205 catch (Exception e) 206 { 207 logger.error("Error while setting object" + e); 208 } 209 } 210 } 211 | Popular Tags |