KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > console > gui > frames > jmxdesktop > OperationCallDialog


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.console.gui.frames.jmxdesktop;
26
27 import java.awt.Color JavaDoc;
28 import java.awt.GridBagConstraints JavaDoc;
29 import java.awt.GridBagLayout JavaDoc;
30 import java.awt.GridLayout JavaDoc;
31 import java.awt.HeadlessException JavaDoc;
32 import java.awt.event.ActionEvent JavaDoc;
33 import java.awt.event.ActionListener JavaDoc;
34 import java.io.PrintWriter JavaDoc;
35
36 import javax.management.MBeanOperationInfo JavaDoc;
37 import javax.management.MBeanParameterInfo JavaDoc;
38 import javax.management.ObjectName JavaDoc;
39 import javax.swing.BorderFactory JavaDoc;
40 import javax.swing.JButton JavaDoc;
41 import javax.swing.JDialog JavaDoc;
42 import javax.swing.JLabel JavaDoc;
43 import javax.swing.JPanel JavaDoc;
44 import javax.swing.JScrollPane JavaDoc;
45 import javax.swing.JTextArea JavaDoc;
46 import javax.swing.JTextField JavaDoc;
47
48 import org.objectweb.cjdbc.common.jmx.JmxConstants;
49 import org.objectweb.cjdbc.console.gui.CjdbcGui;
50 import org.objectweb.cjdbc.console.gui.constants.GuiCommands;
51 import org.objectweb.cjdbc.console.gui.constants.GuiConstants;
52 import org.objectweb.cjdbc.console.gui.jtools.JTextAreaWriter;
53
54 /**
55  * This class defines a OperationCallDialog
56  *
57  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
58  * @version 1.0
59  */

60 public class OperationCallDialog extends JDialog JavaDoc implements ActionListener JavaDoc
61 {
62
63   CjdbcGui gui;
64   ObjectName JavaDoc objectN;
65   MBeanOperationInfo JavaDoc info;
66   private JTextArea JavaDoc area;
67   private JScrollPane JavaDoc scrollPane;
68   private MBeanParameterInfo JavaDoc[] params;
69   private int length;
70   private JTextField JavaDoc[] fields;
71
72   /**
73    * Creates a new <code>OperationCallDialog</code> object
74    *
75    * @param gui the GUI
76    * @param name the object name
77    * @param info the MBean operation info
78    * @throws java.awt.HeadlessException if an error occurs
79    */

80   public OperationCallDialog(CjdbcGui gui, ObjectName JavaDoc name,
81       MBeanOperationInfo JavaDoc info) throws HeadlessException JavaDoc
82   {
83     super(gui, "Operation Call Dialog", true);
84     GuiConstants.centerComponent(this, 400, 500);
85     this.gui = gui;
86     this.objectN = name;
87     this.info = info;
88
89     // Define the panels and areas
90
GridBagLayout JavaDoc gbl = new GridBagLayout JavaDoc();
91     this.getContentPane().setLayout(gbl);
92     this.setFont(GuiConstants.CENTER_PANE_FONT);
93     GridBagConstraints JavaDoc gbc = new GridBagConstraints JavaDoc();
94     gbc.fill = GridBagConstraints.BOTH;
95     gbc.weightx = 1.0;
96     gbc.gridheight = 1;
97     gbc.gridwidth = GridBagConstraints.REMAINDER;
98     this.getContentPane().setBackground(Color.white);
99
100     JLabel JavaDoc objectName = new JLabel JavaDoc(name.toString());
101     objectName.setBorder(BorderFactory.createTitledBorder(
102         GuiConstants.LINE_BORDER, "MBean Name"));
103     gbl.setConstraints(objectName, gbc);
104     this.getContentPane().add(objectName);
105
106     scrollPane = new JScrollPane JavaDoc();
107     scrollPane.setBackground(Color.white);
108     scrollPane.setViewportBorder(BorderFactory.createTitledBorder(
109         GuiConstants.LINE_BORDER, "Operation Result"));
110     area = new JTextArea JavaDoc();
111     area.setBackground(Color.white);
112     scrollPane.getViewport().add(area);
113
114     JLabel JavaDoc operation = new JLabel JavaDoc(info.getName());
115     operation.setBorder(BorderFactory.createTitledBorder(
116         GuiConstants.LINE_BORDER, "Operation Name"));
117     gbl.setConstraints(operation, gbc);
118     this.getContentPane().add(operation);
119
120     JPanel JavaDoc operationPane = new JPanel JavaDoc();
121     operationPane.setBackground(Color.white);
122     operationPane.setBorder(BorderFactory.createTitledBorder(
123         GuiConstants.LINE_BORDER, "MBean parameters"));
124     params = info.getSignature();
125     length = params.length;
126     GridLayout JavaDoc gl = new GridLayout JavaDoc(info.getSignature().length, 2);
127     operationPane.setLayout(gl);
128     fields = new JTextField JavaDoc[length];
129     for (int i = 0; i < length; i++)
130     {
131       operationPane.add(new JLabel JavaDoc(params[i].getType()));
132       fields[i] = new JTextField JavaDoc("");
133       operationPane.add(fields[i]);
134     }
135     gbl.setConstraints(operationPane, gbc);
136     this.getContentPane().add(operationPane);
137
138     JButton JavaDoc ok = new JButton JavaDoc("Run Jmx operation");
139     ok.setBackground(new Color JavaDoc(198, 226, 255));
140     ok.setActionCommand(GuiCommands.COMMAND_CONFIRM_ACTION);
141     ok.addActionListener(this);
142     gbl.setConstraints(ok, gbc);
143     this.getContentPane().add(ok);
144
145     gbc.gridheight = GridBagConstraints.REMAINDER;
146     gbc.weighty = 2.0;
147     gbl.setConstraints(scrollPane, gbc);
148     this.getContentPane().add(scrollPane);
149
150     this.validate();
151
152   }
153
154   /**
155    * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
156    */

157   public void actionPerformed(ActionEvent JavaDoc event)
158   {
159     try
160     {
161       area.setForeground(Color.BLACK);
162       if (gui.getCurrentJmxClient().isSubjectSet() == false
163           && JmxConstants.mbeanNeedAuthentication(objectN))
164         new SetSubjectDialog(gui);
165       Object JavaDoc[] args = new Object JavaDoc[length];
166       for (int i = 0; i < length; i++)
167       {
168         args[i] = getParameter(i);
169       }
170       Object JavaDoc result = gui.getCurrentJmxClient().invokeOperation(objectN, info,
171           args);
172       if (result != null)
173         area.setText(result.toString());
174       else
175         area.setText("Command did not return a result");
176
177       area.validate();
178       scrollPane.validate();
179     }
180     catch (Exception JavaDoc e)
181     {
182       area.setForeground(Color.RED);
183       area.setText(e.getMessage());
184       JTextAreaWriter areaWriter = new JTextAreaWriter(area);
185       PrintWriter JavaDoc writer = new PrintWriter JavaDoc(areaWriter);
186       e.printStackTrace(writer);
187     }
188   }
189
190   private Object JavaDoc getParameter(int i)
191   {
192     String JavaDoc value = fields[i].getText();
193     String JavaDoc type = params[i].getType();
194     return GuiConstants.convertType(value, type);
195   }
196
197 }
198
Popular Tags