| 1 16 17 package org.mc4j.console.swing.editor.jmx; 18 19 import org.mc4j.console.bean.MBeanNode; 20 import org.mc4j.console.connection.ConnectionNode; 21 import org.openide.ErrorManager; 22 import org.openide.explorer.propertysheet.ExPropertyEditor; 23 import org.openide.explorer.propertysheet.InplaceEditor; 24 import org.openide.explorer.propertysheet.PropertyEnv; 25 import org.openide.explorer.propertysheet.PropertyModel; 26 import org.openide.nodes.Node; 27 28 import javax.management.ObjectName ; 29 import javax.swing.*; 30 import java.awt.*; 31 import java.awt.event.ActionEvent ; 32 import java.awt.event.ActionListener ; 33 import java.awt.event.KeyEvent ; 34 import java.beans.PropertyEditor ; 35 import java.beans.PropertyEditorSupport ; 36 import java.util.Iterator ; 37 import java.util.List ; 38 39 40 41 45 public class ObjectNameEditor extends PropertyEditorSupport implements ExPropertyEditor, InplaceEditor.Factory { 46 47 private String currentValue; 48 49 private DefaultListModel listModel; 50 private DefaultListSelectionModel selectionModel; 51 52 private PropertyEnv propertyEnv; 53 54 private ConnectionNode connectionNode; 55 56 public ObjectNameEditor() { 57 this.listModel = new DefaultListModel(); 58 this.selectionModel = new DefaultListSelectionModel(); 59 this.selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 60 } 61 62 public void setAsText(String text) throws IllegalArgumentException { 63 for (int i = 0; i < this.listModel.getSize(); i++) { 64 String name = (String ) this.listModel.get(i); 65 if (name.equals(text)) { 66 this.currentValue = name; 67 this.selectionModel.setSelectionInterval(i,i); 68 } 69 } 70 } 71 72 public String getAsText() { 73 return (this.currentValue!= null)?this.currentValue:"<null>"; 74 } 75 76 public boolean supportsCustomEditor() { 77 return true; 78 } 79 80 public Component getCustomEditor () { 81 return new ObjectNameSelector(listModel, selectionModel); 82 } 83 84 public void attachEnv(PropertyEnv env) { 85 this.propertyEnv = env; 86 env.registerInplaceEditorFactory(this); 87 88 Object bean = propertyEnv.getBeans()[0]; 89 90 if (bean != null) { 91 Node node = (Node) bean; 92 while ((!(node instanceof ConnectionNode)) && (node != null)) { 93 node = node.getParentNode(); 94 } 95 96 if (node != null) { 97 this.connectionNode = (ConnectionNode) node; 98 List objectInstanceList = this.connectionNode.getMBeanList(); 99 100 Iterator instIter = objectInstanceList.iterator(); 101 int i = 0; 102 while (instIter.hasNext()) { 103 i++; 104 String name = ((MBeanNode)instIter.next()).getEmsBean().getBeanName().getCanonicalName(); 106 108 listModel.addElement(name); 109 110 if ((this.currentValue != null) && (this.currentValue.equals(name))) { 111 this.selectionModel.setSelectionInterval(i,i); 112 } 113 } 114 } 115 } 116 117 } 118 119 public void setValue(Object value) { 120 if (value != null) { 121 this.currentValue = (String ) value; 122 firePropertyChange(); 123 124 for (int i = 0; i < this.listModel.getSize(); i++) { 125 String objectName = (String ) this.listModel.get(i); 126 127 if (objectName.equals(value)) { 128 this.selectionModel.setSelectionInterval(i,i); 129 } 130 } 131 } else { 132 this.currentValue = null; 133 } 134 } 135 136 public Object getValue() { 137 return this.currentValue; 138 } 139 140 141 public boolean isPaintable() { 142 return true; 143 } 144 145 private ObjectNameInplace renderer = null; 146 public void paintValue(Graphics gfx, Rectangle box) { 147 if (renderer == null) { 148 renderer = new ObjectNameInplace(); 149 } 150 renderer.setText(getAsText()); 151 renderer.setSize (box.width, box.height); 152 renderer.doLayout(); 153 Graphics g = gfx.create(box.x, box.y, box.width, box.height); 154 renderer.setOpaque(false); 155 renderer.paint (g); 156 g.dispose(); 157 } 158 159 160 public InplaceEditor getInplaceEditor() { 161 162 ObjectNameInplace inplace = new ObjectNameInplace(); 163 return inplace; 164 } 165 166 public void gotoBean() { 167 if (this.currentValue != null) 168 this.connectionNode.browseToMBean(this.currentValue); 169 } 170 171 172 class ObjectNameInplace extends JButton implements InplaceEditor { 173 private ConnectionNode connectionNode; 174 175 private PropertyEditor propertyEditor; 176 private PropertyEnv propertyEnv; 177 178 private PropertyModel propertyModel; 179 180 181 182 183 final KeyStroke[] cbKeyStrokes = 184 new KeyStroke[] {KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,0,false), 185 KeyStroke.getKeyStroke(KeyEvent.VK_UP,0,false), 186 KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,0,true), 187 KeyStroke.getKeyStroke(KeyEvent.VK_UP,0,true), 188 KeyStroke.getKeyStroke (KeyEvent.VK_PAGE_DOWN,0,false), 189 KeyStroke.getKeyStroke (KeyEvent.VK_PAGE_UP,0,false), 190 KeyStroke.getKeyStroke (KeyEvent.VK_PAGE_DOWN,0,true), 191 KeyStroke.getKeyStroke (KeyEvent.VK_PAGE_UP,0,true) 192 }; 193 194 195 public ObjectNameInplace() { 196 197 setText(getAsText()); 199 setToolTipText("Click to browse to managed object..."); 200 201 addActionListener(new ActionListener () { 203 public void actionPerformed(ActionEvent e) { 204 gotoBean(); 205 } 206 }); 207 } 208 209 214 215 public JComponent getComponent() { 216 return this; 217 } 218 219 public void clear() { 220 221 } 222 223 public boolean isKnownComponent(Component c) { 224 return (this.equals(c)); 225 } 226 227 public boolean supportsTextEntry() { 228 return false; 229 } 230 231 public void reset() { 232 } 233 234 public void addActionListener(ActionListener al) { 235 } 236 237 public void removeActionListener(ActionListener al) { 238 239 } 240 241 public KeyStroke[] getKeyStrokes() { 242 return cbKeyStrokes; 243 } 244 245 246 public void setValue(Object value) { 247 248 249 if ((value != null) && (value instanceof ObjectName )) { 250 currentValue = (String ) value; 251 setText(getAsText()); 252 } 253 } 254 255 public Object getValue() { 256 return currentValue; 257 } 258 259 public void connect(PropertyEditor pe, PropertyEnv env) { 260 261 gotoBean(); 263 264 this.propertyEditor = pe; 265 this.propertyEnv = env; 266 267 } 268 269 public PropertyEditor getPropertyEditor() { 270 return this.propertyEditor; 271 } 272 273 public PropertyModel getPropertyModel() { 274 return propertyModel; 275 } 276 277 public void setPropertyModel(PropertyModel pm) { 278 this.propertyModel = pm; 279 try { 280 Object value = this.propertyModel.getValue(); 281 282 if ((value != null) && (value instanceof ObjectName )) { 283 currentValue = (String ) value; 284 } 285 } catch(Exception e) { 286 ErrorManager.getDefault().notify(e); 287 } 288 } 289 } 290 } 291 | Popular Tags |