KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mc4j > console > swing > editor > jmx > ObjectNameEditor


1 /*
2  * Copyright 2002-2004 Greg Hinkle
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
29 import javax.swing.*;
30 import java.awt.*;
31 import java.awt.event.ActionEvent JavaDoc;
32 import java.awt.event.ActionListener JavaDoc;
33 import java.awt.event.KeyEvent JavaDoc;
34 import java.beans.PropertyEditor JavaDoc;
35 import java.beans.PropertyEditorSupport JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.List JavaDoc;
38
39
40
41 /**
42  * @author Greg Hinkle (ghinkle@users.sourceforge.net), January 2002
43  * @version $Revision: 570 $($Author: ghinkl $ / $Date: 2006-04-12 15:14:16 -0400 (Wed, 12 Apr 2006) $)
44  */

45 public class ObjectNameEditor extends PropertyEditorSupport JavaDoc implements ExPropertyEditor, InplaceEditor.Factory {
46
47     private String JavaDoc 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 JavaDoc text) throws IllegalArgumentException JavaDoc {
63         for (int i = 0; i < this.listModel.getSize(); i++) {
64             String JavaDoc name = (String JavaDoc) 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 JavaDoc 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 JavaDoc 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 JavaDoc objectInstanceList = this.connectionNode.getMBeanList();
99
100                 Iterator JavaDoc instIter = objectInstanceList.iterator();
101                 int i = 0;
102                 while (instIter.hasNext()) {
103                     i++;
104                     //ObjectInstance instance = (ObjectInstance)
105
String JavaDoc name = ((MBeanNode)instIter.next()).getEmsBean().getBeanName().getCanonicalName();
106 // ObjectName name = ((MBeanNode)instIter.next()).getObjectName(); //instance.getObjectName();
107

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 JavaDoc value) {
120         if (value != null) {
121             this.currentValue = (String JavaDoc) value;
122             firePropertyChange();
123
124             for (int i = 0; i < this.listModel.getSize(); i++) {
125                 String JavaDoc objectName = (String JavaDoc) 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 JavaDoc 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             /*Keystrokes this inplace editor wants to consume */
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         /** Creates a new instance of ObjectNameLink */
195         public ObjectNameInplace() {
196
197             //setForeground(Color.red);
198
setText(getAsText());
199             setToolTipText("Click to browse to managed object...");
200
201             // TODO GH: this doesn't work
202
addActionListener(new ActionListener JavaDoc() {
203                 public void actionPerformed(ActionEvent JavaDoc e) {
204                     gotoBean();
205                 }
206             });
207         }
208
209         // Show the button in red and underlined?
210
// public void setText(String text) {
211
// super.setText("<html><u>"+text+"</u></html>");
212
// }
213

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 JavaDoc al) {
235         }
236
237         public void removeActionListener(ActionListener JavaDoc al) {
238
239         }
240
241         public KeyStroke[] getKeyStrokes() {
242             return cbKeyStrokes;
243         }
244
245
246         public void setValue(Object JavaDoc value) {
247
248
249             if ((value != null) && (value instanceof ObjectName JavaDoc)) {
250                 currentValue = (String JavaDoc) value;
251                 setText(getAsText());
252             }
253         }
254
255         public Object JavaDoc getValue() {
256             return currentValue;
257         }
258
259         public void connect(PropertyEditor pe, PropertyEnv env) {
260
261             // Just go on activation, since the action listener doesn't work.
262
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 JavaDoc value = this.propertyModel.getValue();
281
282                 if ((value != null) && (value instanceof ObjectName JavaDoc)) {
283                     currentValue = (String JavaDoc) value;
284                 }
285             } catch(Exception JavaDoc e) {
286                 ErrorManager.getDefault().notify(e);
287             }
288         }
289     }
290 }
291
Popular Tags