ALMA Computing Group

JDynAct.java

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * ALMA - Atacama Large Millimeter Array
00003  * Copyright (c) ESO - European Southern Observatory, 2011
00004  * (in the framework of the ALMA collaboration).
00005  * All rights reserved.
00006  * 
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  * 
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00015  * Lesser General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00020  *******************************************************************************/
00021 package alma.demo.dyncomp;
00022 
00023 import java.awt.BorderLayout;
00024 import java.awt.Container;
00025 import java.awt.Dimension;
00026 import java.awt.FlowLayout;
00027 import java.awt.GridLayout;
00028 import java.awt.event.ActionEvent;
00029 import java.awt.event.ActionListener;
00030 import java.awt.event.WindowEvent;
00031 import java.awt.event.WindowListener;
00032 
00033 import javax.swing.JButton;
00034 import javax.swing.JComboBox;
00035 import javax.swing.JDialog;
00036 import javax.swing.JLabel;
00037 import javax.swing.JOptionPane;
00038 import javax.swing.JPanel;
00039 import javax.swing.JPopupMenu;
00040 import javax.swing.JScrollPane;
00041 import javax.swing.JTable;
00042 import javax.swing.ListSelectionModel;
00043 import javax.swing.table.TableColumn;
00044 import javax.swing.table.TableColumnModel;
00045 
00046 import si.ijs.maci.ComponentSpec;
00047 
00048 
00055 public class JDynAct extends JDialog implements ActionListener,  WindowListener 
00056 {
00057         // GUI components
00058         JComboBox nameCB, idlCB, implCB, containerCB;
00059         JPanel variableAreaP;
00060         JTable activatedT;
00061         JPopupMenu popMenu;
00062 
00063         // The mangerLoc and the logger
00064         private String m_managerLoc;
00065 
00066         // The clients to acivate dynamic components
00067         Client theClient;
00068         
00069         public JDynAct(String managerLoc) {
00070                 super();
00071                 setTitle("Dynamic component activator");
00072                 buildWindow();
00073                 setBounds(50,50,10,10);
00074                 pack();
00075                 addWindowListener(this);
00076                 
00077                 m_managerLoc=managerLoc;
00078                 try {
00079                         theClient =  new Client(null,m_managerLoc,"DynCompClient");
00080                 } catch (Exception e) {
00081                         System.err.println("Error: "+e.toString());
00082                         e.printStackTrace(System.err);
00083                         System.exit(-1);
00084                 }
00085 
00086                 popMenu = new JPopupMenu();
00087                 setVisible(true);
00088                 setDefaultCloseOperation(DISPOSE_ON_CLOSE);
00089         }
00090         
00094         private void buildWindow() {
00095                 Container rootCnt = getContentPane();
00096                 rootCnt.setLayout(new BorderLayout());
00097                 // The container with labels and combo boxes
00098                 Container firstCnt = new Container();
00099                 firstCnt.setLayout(new GridLayout(4,2));
00100                 firstCnt.add(new JLabel("Name"));
00101                 nameCB=new JComboBox();
00102                 nameCB.setEditable(true);
00103                 nameCB.addItem("*");
00104                 nameCB.addItem("PIPPO");
00105                 nameCB.addItem("PLUTO");
00106                 firstCnt.add(nameCB);
00107                 firstCnt.add(new JLabel("IDL interface"));
00108                 idlCB=new JComboBox();
00109                 idlCB.addItem("*");
00110                 idlCB.addItem("IDL:alma/acsexmplLamp/Lamp:1.0");
00111                 idlCB.addItem("IDL:alma/MOUNT_ACS/Mount:1.0");
00112                 idlCB.addItem("IDL:alma/demo/HelloDemo:1.0");
00113                 idlCB.setEditable(true);
00114                 firstCnt.add(idlCB);
00115                 firstCnt.add(new JLabel("Implementation"));
00116                 implCB=new JComboBox();
00117                 implCB.addItem("*");
00118                 implCB.addItem("acsexmplLampImpl");
00119                 implCB.addItem("acsexmplMountImpl");
00120                 implCB.addItem("alma.demo.HelloDemoImpl.HelloDemoHelper");
00121                 implCB.addItem("demoImpl.HelloDemo");
00122                 implCB.addItem("acsexmplHelloWorldClient");
00123                 implCB.setEditable(true);
00124                 firstCnt.add(implCB);
00125                 firstCnt.add(new JLabel("Container"));
00126                 containerCB=new JComboBox();
00127                 containerCB.addItem("*");
00128                 containerCB.addItem("bilboContainer");
00129                 containerCB.addItem("frodoContainer");
00130                 containerCB.addItem("aragornContainer");
00131                 containerCB.setEditable(true);
00132                 firstCnt.add(containerCB);
00133                 // The container with the activate button
00134                 Container secondCnt = new Container();
00135                 secondCnt.setLayout(new FlowLayout());
00136                 JButton activateB = new JButton("Activate");
00137                 activateB.addActionListener(this);
00138                 secondCnt.add(activateB,"Center");
00139                 // The container with activated container
00140                 MyTableModel tableModel = new MyTableModel();
00141                 activatedT = new JTable(tableModel);
00142                 activatedT.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
00143                 ListSelectionModel rowSM = activatedT.getSelectionModel();
00144                 JScrollPane scrollP = new JScrollPane(activatedT);
00145                 activatedT.setPreferredScrollableViewportSize(new Dimension(400,90));
00146                 MyCellRendererr cellR = new  MyCellRendererr();
00147                 TableColumnModel tcm = activatedT.getColumnModel();
00148                 TableColumn tc = tcm.getColumn(2);
00149                 tc.setCellRenderer(cellR);
00150                 MyCellEditor cellE = new MyCellEditor(this);
00151                 tc.setCellEditor(cellE);
00152                 Container thirdCnt = new Container();
00153                 thirdCnt.setLayout(new FlowLayout());
00154                 thirdCnt.add(scrollP,"North");
00155 
00156                 // Add the container to the main container
00157                 rootCnt.add(firstCnt,"North");
00158                 rootCnt.add(secondCnt,"Center");
00159                 rootCnt.add(thirdCnt,"South");
00160         }
00161 
00171         private void startDynamicComponent(String Name, String IDL, String Implementation, String Container) {
00172                 boolean activated;
00173                 // Check if a dynamic component with the same name was already activated
00174                 MyTableModel myModel=(MyTableModel)activatedT.getModel();
00175                 if (myModel.exist(Name)) {
00176                          JOptionPane.showOptionDialog(
00177                                 this,
00178                                 "The component "+Name+" already exists",
00179                                 "Error activating the component",
00180                                 JOptionPane.DEFAULT_OPTION,
00181                                 JOptionPane.ERROR_MESSAGE,
00182                                 null,null,null);
00183                         return;
00184                 }
00185                 // Start the dynamic component....
00186                 String activatedComponentUrl=null;
00187                 if (theClient.hasFreeSlot()) {
00188                         ComponentSpec compSpec = new ComponentSpec(Name,IDL,Implementation,Container);
00189                         try {
00190                                 activatedComponentUrl=theClient.getDynamicComponent(compSpec,false);
00191                         } catch (Exception ce) {
00192                                 System.err.println("Error activating the component: "+ce.toString());
00193                                 ce.printStackTrace(System.err);
00194                                 activated=false;
00195                         }
00196                         // The component is activated => add a new entry in the GUI
00197                         if (activatedComponentUrl!=null) {
00198                                 myModel.append(Name,activatedComponentUrl);
00199                         } else {
00200                          JOptionPane.showOptionDialog(
00201                                 this,
00202                                 "Dynamic activation error",
00203                                 "Error activating the component",
00204                                 JOptionPane.DEFAULT_OPTION,
00205                                 JOptionPane.ERROR_MESSAGE,
00206                                 null,null,null);
00207                         }
00208                 } else JOptionPane.showOptionDialog(
00209                                 this,
00210                                 "No free slot available",
00211                                 "Error activating the component",
00212                                 JOptionPane.DEFAULT_OPTION,
00213                                 JOptionPane.ERROR_MESSAGE,
00214                                 null,null,null);
00215         }
00216         
00219         public void actionPerformed(ActionEvent ae) {
00220                 popMenu.setVisible(false);
00221                 if (ae.getActionCommand().compareTo("Activate")==0) {
00222                         startDynamicComponent(
00223                                 (String)nameCB.getSelectedItem(),
00224                                 (String)idlCB.getSelectedItem(),
00225                                 (String)implCB.getSelectedItem(),
00226                                 (String)containerCB.getSelectedItem());
00227                 } else if (ae.getActionCommand().split(" ")[0].compareTo("Release")==0) {
00228                         String splitted[] = ae.getActionCommand().split(" ");
00229                         // Get the cURL of the component
00230                         MyTableModel myModel=(MyTableModel)activatedT.getModel();
00231                         String cURL=myModel.getURL(splitted[1]);
00232                         if (releaseComponent(cURL)) {
00233                                 ((JButton)ae.getSource()).removeActionListener(this);
00234                                 ((MyCellEditor)activatedT.getCellEditor()).stopEditing();
00235                                 ((MyTableModel)activatedT.getModel()).sort();
00236                         }
00237                 } 
00238         }
00239 
00242         public static void main(String[] args)
00243         {
00244 
00245                 String managerLoc= System.getProperty("ACS.manager");
00246                 if (managerLoc==null) {
00247                         System.err.println("Error getting ACS.manager property");
00248                         System.exit(-1);
00249                 }
00250                 new JDynAct(managerLoc);
00251         }
00252 
00256         public boolean releaseComponent(String url) {
00257                 if (theClient.releaseComponent(url)) {
00258                         MyTableModel tm=(MyTableModel)activatedT.getModel();
00259                         tm.deleteEntry(url);
00260                         return true;
00261                 } else {
00262                          JOptionPane.showOptionDialog(
00263                                 this,
00264                                 "Error releasing the component",
00265                                 "Dynamic activation error",
00266                                 JOptionPane.DEFAULT_OPTION,
00267                                 JOptionPane.ERROR_MESSAGE,
00268                                 null,null,null);
00269                         return false;
00270                 }
00271         }
00272 
00275         private void cleanExit() {
00276                 // Realease all the dynamic components
00277                 theClient.cleanExit();
00278                 System.exit(0);
00279         }
00280 
00281         public void windowActivated(WindowEvent e) {}
00282         public void windowClosing(WindowEvent e) {}
00283         public void windowDeactivated(WindowEvent e) {}
00284         public void windowDeiconified(WindowEvent e) {}
00285         public void windowIconified(WindowEvent e) {}
00286         public void windowOpened(WindowEvent e) {}
00287         public void windowClosed(WindowEvent e) { cleanExit(); }
00288 }
00289 
00290 
00291 
00292 
00293