KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > console > gui > objects > BackendObject


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.objects;
26
27 import java.awt.Color JavaDoc;
28 import java.awt.datatransfer.DataFlavor JavaDoc;
29 import java.awt.datatransfer.Transferable JavaDoc;
30 import java.awt.datatransfer.UnsupportedFlavorException JavaDoc;
31 import java.awt.dnd.DnDConstants JavaDoc;
32 import java.awt.dnd.DragSource JavaDoc;
33 import java.io.IOException JavaDoc;
34
35 import javax.management.InstanceNotFoundException JavaDoc;
36 import javax.swing.SwingConstants JavaDoc;
37
38 import org.objectweb.cjdbc.common.jmx.mbeans.DatabaseBackendMBean;
39 import org.objectweb.cjdbc.console.gui.CjdbcGui;
40 import org.objectweb.cjdbc.console.gui.constants.GuiConstants;
41 import org.objectweb.cjdbc.console.gui.constants.GuiIcons;
42 import org.objectweb.cjdbc.console.gui.dnd.listeners.BackendTransferListener;
43 import org.objectweb.cjdbc.console.gui.popups.BackendPopUpMenu;
44 import org.objectweb.cjdbc.console.jmx.RmiJmxClient;
45
46 /**
47  * This class defines a BackendObject
48  *
49  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
50  * @version 1.0
51  */

52 public class BackendObject extends AbstractGuiObject
53     implements
54       Transferable JavaDoc,
55       Cloneable JavaDoc
56
57 {
58   private String JavaDoc backendState;
59   private String JavaDoc database;
60   private String JavaDoc controllerName;
61   private DatabaseBackendMBean mbean;
62   private String JavaDoc user;
63   private String JavaDoc password;
64   private CjdbcGui gui;
65   private RmiJmxClient jmxClient;
66   private BackendPopUpMenu menu;
67
68   /**
69    * Access the mbean from this bob object
70    *
71    * @return <code>DatabaseBackendMBean</code> instance
72    * @throws InstanceNotFoundException if fails
73    * @throws IOException if fails
74    */

75   public DatabaseBackendMBean getMbean() throws InstanceNotFoundException JavaDoc,
76       IOException JavaDoc
77   {
78     jmxClient = (RmiJmxClient) gui.getJmxClients().get(controllerName);
79     user = gui.getGuiSession().getAuthenticatedDatabaseLogin(database);
80     password = gui.getGuiSession().getAuthenticatedDatabasePassword(database);
81     mbean = jmxClient.getDatabaseBackendProxy(database, getName(), user,
82         password);
83     return mbean;
84   }
85
86   /**
87    * Creates a new <code>BackendObject.java</code> object
88    *
89    * @param gui we are attached to
90    * @param database the virtual database this backends belongs to
91    * @param name the name of the backend of this backend object
92    * @param listener backend transfer listener for DnD
93    * @param controllerName the owner of this backend
94    * @throws IOException if cannot access MBean
95    * @throws InstanceNotFoundException if cannot locate MBean
96    */

97   public BackendObject(CjdbcGui gui, BackendTransferListener listener,
98       String JavaDoc database, String JavaDoc name, String JavaDoc controllerName)
99       throws InstanceNotFoundException JavaDoc, IOException JavaDoc
100   {
101     super();
102     this.database = database;
103     this.controllerName = controllerName;
104     this.gui = gui;
105     setText(name);
106     setName(name);
107     this.menu = new BackendPopUpMenu(gui, this);
108     setBackground(Color.white);
109     setVerticalTextPosition(SwingConstants.BOTTOM);
110
111     getMbean();
112
113     addMouseMotionListener(listener);
114     addMouseListener(listener);
115     addMouseListener(menu);
116
117     DragSource JavaDoc dragSource = DragSource.getDefaultDragSource();
118     dragSource.createDefaultDragGestureRecognizer(this, // What component
119
DnDConstants.ACTION_COPY_OR_MOVE, // What drag types?
120
listener);// the listener
121
}
122
123   /**
124    * Get the state of the backend
125    *
126    * @return state of backend as defined in gui constants , null if unknown
127    */

128   public String JavaDoc getState()
129   {
130     return backendState;
131   }
132
133   /**
134    * Set state of backend
135    *
136    * @param state string description of the state
137    */

138   public void setState(String JavaDoc state)
139   {
140     this.backendState = state;
141     if (state.equals(GuiConstants.BACKEND_STATE_ENABLED))
142     {
143       setIcon(GuiIcons.BACKEND_ENABLED_ICON);
144       menu.setEnabled(true);
145       menu.getBackendCheckpoint().setEnabled(false);
146       menu.getBackendCreate().setEnabled(true);
147       menu.getBackendRemove().setEnabled(false);
148       
149       menu.getBackendEnable().setEnabled(false);
150       menu.getBackendDisable().setEnabled(true);
151       menu.getBackendBackup().setEnabled(true);
152       menu.getBackendRestore().setEnabled(false);
153     }
154     else if (state.equals(GuiConstants.BACKEND_STATE_DISABLED))
155     {
156       setIcon(GuiIcons.BACKEND_DISABLED_ICON);
157       menu.getBackendCheckpoint().setEnabled(true);
158       menu.getBackendCreate().setEnabled(true);
159       menu.getBackendRemove().setEnabled(true);
160       
161       menu.getBackendEnable().setEnabled(true);
162       menu.getBackendDisable().setEnabled(false);
163       menu.getBackendBackup().setEnabled(true);
164       menu.getBackendRestore().setEnabled(true);
165     }
166     else if (state.equals(GuiConstants.BACKEND_STATE_DISABLING))
167     {
168       setIcon(GuiIcons.BACKEND_DISABLING_ICON);
169       menu.setEnabled(false);
170     }
171     else if (state.equals(GuiConstants.BACKEND_STATE_BACKUP))
172     {
173       setIcon(GuiIcons.BACKEND_BACKUP_ICON);
174       menu.setEnabled(false);
175     }
176     else if (state.equals(GuiConstants.BACKEND_STATE_RESTORE))
177     {
178       setIcon(GuiIcons.BACKEND_RESTORE_ICON);
179       menu.setEnabled(false);
180     }
181     else if (state.equals(GuiConstants.BACKEND_STATE_RECOVERY))
182     {
183       setIcon(GuiIcons.BACKEND_STATE_RECOVERY);
184       menu.setEnabled(false);
185     }
186   }
187
188   /**
189    * @see java.awt.datatransfer.Transferable#getTransferDataFlavors()
190    */

191   public DataFlavor JavaDoc[] getTransferDataFlavors()
192   {
193     return new DataFlavor JavaDoc[]{DataFlavor.stringFlavor, DataFlavor.plainTextFlavor};
194   }
195
196   /**
197    * @see java.awt.datatransfer.Transferable#isDataFlavorSupported(java.awt.datatransfer.DataFlavor)
198    */

199   public boolean isDataFlavorSupported(DataFlavor JavaDoc flavor)
200   {
201     if (flavor.equals(DataFlavor.stringFlavor)
202         || flavor.equals(DataFlavor.plainTextFlavor))
203       return true;
204     else
205       return false;
206   }
207
208   /**
209    * @see java.awt.datatransfer.Transferable#getTransferData(java.awt.datatransfer.DataFlavor)
210    */

211   public Object JavaDoc getTransferData(DataFlavor JavaDoc flavor)
212       throws UnsupportedFlavorException JavaDoc, IOException JavaDoc
213   {
214     return this.getText();
215   }
216
217   /**
218    * Returns the database value.
219    *
220    * @return Returns the database.
221    */

222   public String JavaDoc getDatabase()
223   {
224     return database;
225   }
226
227   /**
228    * Returns the controllerName value.
229    *
230    * @return Returns the controllerName.
231    */

232   public String JavaDoc getControllerName()
233   {
234     return controllerName;
235   }
236
237   /**
238    * Sets the controllerName value.
239    *
240    * @param controllerName The controllerName to set.
241    */

242   public void setControllerName(String JavaDoc controllerName)
243   {
244     this.controllerName = controllerName;
245   }
246
247   /**
248    * @see java.awt.Component#setName(java.lang.String)
249    */

250   public void setName(String JavaDoc name)
251   {
252     super.setName(name);
253     setText(name);
254   }
255   /**
256    * Returns the jmxClient value.
257    *
258    * @return Returns the jmxClient.
259    */

260   public RmiJmxClient getJmxClient()
261   {
262     return jmxClient;
263   }
264 }
Popular Tags