KickJava   Java API By Example, From Geeks To Geeks.

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


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.event.ActionEvent JavaDoc;
32 import java.awt.event.ActionListener JavaDoc;
33
34 import javax.management.MBeanAttributeInfo JavaDoc;
35 import javax.management.ObjectName JavaDoc;
36 import javax.swing.BorderFactory JavaDoc;
37 import javax.swing.JButton JavaDoc;
38 import javax.swing.JDialog JavaDoc;
39 import javax.swing.JLabel JavaDoc;
40 import javax.swing.JPanel JavaDoc;
41 import javax.swing.JScrollPane JavaDoc;
42 import javax.swing.JTextArea JavaDoc;
43 import javax.swing.JTextField JavaDoc;
44
45 import org.objectweb.cjdbc.console.gui.CjdbcGui;
46 import org.objectweb.cjdbc.console.gui.constants.GuiCommands;
47 import org.objectweb.cjdbc.console.gui.constants.GuiConstants;
48
49 /**
50  * This class defines a AttributeChangeFrame
51  *
52  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
53  * @version 1.0
54  */

55 public class AttributeChangeDialog extends JDialog JavaDoc implements ActionListener JavaDoc
56 {
57
58   private static final Color JavaDoc BUTTON_COLOR = new Color JavaDoc(198, 226, 255);
59   private static final Color JavaDoc WARNING_COLOR = new Color JavaDoc(238, 169, 184);
60   private JScrollPane JavaDoc scrollPane;
61   private JTextArea JavaDoc area;
62   private MBeanAttributeInfo JavaDoc info;
63   private CjdbcGui gui;
64   private ObjectName JavaDoc objectName;
65   private JTextField JavaDoc newValue;
66
67   /**
68    * Creates a new <code>AttributeChangeDialog</code> object
69    *
70    * @param gui The GUI
71    * @param name Object name
72    * @param info MBean attribute info
73    */

74   public AttributeChangeDialog(CjdbcGui gui, ObjectName JavaDoc name,
75       MBeanAttributeInfo JavaDoc info)
76   {
77
78     super(gui, "Attribute Change", true);
79
80     this.info = info;
81     this.gui = gui;
82     this.objectName = name;
83     GuiConstants.centerComponent(this, 400, 300);
84
85     this.setFont(GuiConstants.CENTER_PANE_FONT);
86
87     // Define the panels and areas
88
GridBagLayout JavaDoc gbl = new GridBagLayout JavaDoc();
89     this.getContentPane().setLayout(gbl);
90     GridBagConstraints JavaDoc gbc = new GridBagConstraints JavaDoc();
91     gbc.fill = GridBagConstraints.BOTH;
92     gbc.weightx = 1.0;
93     gbc.gridheight = 1;
94     gbc.gridwidth = GridBagConstraints.REMAINDER;
95     this.getContentPane().setBackground(Color.white);
96
97     JLabel JavaDoc object = new JLabel JavaDoc(name.toString());
98     object.setBorder(BorderFactory.createTitledBorder(GuiConstants.LINE_BORDER,
99         "MBean Name"));
100     gbl.setConstraints(object, gbc);
101     this.getContentPane().add(object);
102
103     JLabel JavaDoc attribute = new JLabel JavaDoc(info.getName());
104     attribute.setBorder(BorderFactory.createTitledBorder(
105         GuiConstants.LINE_BORDER, "Attribute Name (" + info.getType() + ")"));
106     gbl.setConstraints(attribute, gbc);
107     this.getContentPane().add(attribute);
108
109     JPanel JavaDoc values = new JPanel JavaDoc();
110     values.setBorder(BorderFactory.createTitledBorder(GuiConstants.LINE_BORDER,
111         "Attribute Value"));
112     values.setBackground(Color.white);
113     values.setLayout(new GridLayout JavaDoc(2, 2));
114     values.add(new JLabel JavaDoc("Old Value"));
115
116     JTextField JavaDoc oldValue = new JTextField JavaDoc();
117     Object JavaDoc attributeValue = null;
118     try
119     {
120       attributeValue = gui.getCurrentJmxClient().getAttributeValue(name,
121           info.getName());
122     }
123     catch (Exception JavaDoc e)
124     {
125       oldValue.setForeground(WARNING_COLOR);
126       attributeValue = "<error>";
127     }
128     finally
129     {
130       if (attributeValue == null)
131         attributeValue = "";
132     }
133     oldValue.setText(attributeValue.toString());
134     oldValue.setEditable(false);
135     values.add(oldValue);
136     values.add(new JLabel JavaDoc("New Value"));
137     newValue = new JTextField JavaDoc();
138     if (!info.isWritable())
139     {
140       newValue.setText(" ");
141       newValue.setEnabled(false);
142     }
143     values.add(newValue);
144     gbl.setConstraints(values, gbc);
145     this.getContentPane().add(values);
146
147     JButton JavaDoc ok;
148     if (!info.isWritable())
149     {
150       ok = new JButton JavaDoc("Cannot change attribute value");
151       ok.setBackground(WARNING_COLOR);
152       ok.setEnabled(false);
153     }
154     else
155     {
156       ok = new JButton JavaDoc("Change attribute value");
157       ok.setBackground(BUTTON_COLOR);
158       ok.setEnabled(true);
159     }
160     ok.setActionCommand(GuiCommands.COMMAND_CONFIRM_ACTION);
161     ok.addActionListener(this);
162     gbl.setConstraints(ok, gbc);
163     this.getContentPane().add(ok);
164
165     scrollPane = new JScrollPane JavaDoc();
166     scrollPane.setBackground(Color.white);
167     scrollPane.setViewportBorder(BorderFactory.createTitledBorder(
168         GuiConstants.LINE_BORDER, "Attribute Change Result"));
169     area = new JTextArea JavaDoc();
170     area.setBackground(Color.white);
171     scrollPane.getViewport().add(area);
172     gbc.gridheight = GridBagConstraints.REMAINDER;
173     gbc.weighty = 2.0;
174     gbl.setConstraints(scrollPane, gbc);
175     this.getContentPane().add(scrollPane);
176
177     this.validate();
178
179   }
180
181   /**
182    * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
183    */

184   public void actionPerformed(ActionEvent JavaDoc e)
185   {
186     if (!info.isWritable())
187     {
188       area.setForeground(WARNING_COLOR);
189       area.setText("This attribute cannot be changed");
190     }
191     else
192     {
193       try
194       {
195         Object JavaDoc o = GuiConstants.convertType(newValue.getText(), info.getType());
196         gui.getCurrentJmxClient().setAttributeValue(objectName, info.getName(),
197             o);
198         area.setForeground(Color.BLACK);
199         area.setText("Attribute value chaned");
200       }
201       catch (Exception JavaDoc e1)
202       {
203         area.setForeground(WARNING_COLOR);
204         area.setText(e1.getMessage());
205       }
206     }
207   }
208 }
209
Popular Tags