ALMA Computing Group

JDynAct.java

Go to the documentation of this file.
00001 package alma.demo.dyncomp; 00002 00003 import java.awt.BorderLayout; 00004 import java.awt.Container; 00005 import java.awt.Dimension; 00006 import java.awt.FlowLayout; 00007 import java.awt.GridLayout; 00008 import java.awt.event.ActionEvent; 00009 import java.awt.event.ActionListener; 00010 import java.awt.event.WindowEvent; 00011 import java.awt.event.WindowListener; 00012 00013 import javax.swing.JButton; 00014 import javax.swing.JComboBox; 00015 import javax.swing.JDialog; 00016 import javax.swing.JLabel; 00017 import javax.swing.JOptionPane; 00018 import javax.swing.JPanel; 00019 import javax.swing.JPopupMenu; 00020 import javax.swing.JScrollPane; 00021 import javax.swing.JTable; 00022 import javax.swing.ListSelectionModel; 00023 import javax.swing.table.TableColumn; 00024 import javax.swing.table.TableColumnModel; 00025 00026 import si.ijs.maci.ComponentSpec; 00027 00028 00035 public class JDynAct extends JDialog implements ActionListener, WindowListener 00036 { 00037 // GUI components 00038 JComboBox nameCB, idlCB, implCB, containerCB; 00039 JPanel variableAreaP; 00040 JTable activatedT; 00041 JPopupMenu popMenu; 00042 00043 // The mangerLoc and the logger 00044 private String m_managerLoc; 00045 00046 // The clients to acivate dynamic components 00047 Client theClient; 00048 00049 public JDynAct(String managerLoc) { 00050 super(); 00051 setTitle("Dynamic component activator"); 00052 buildWindow(); 00053 setBounds(50,50,10,10); 00054 pack(); 00055 addWindowListener(this); 00056 00057 m_managerLoc=managerLoc; 00058 try { 00059 theClient = new Client(null,m_managerLoc,"DynCompClient"); 00060 } catch (Exception e) { 00061 System.err.println("Error: "+e.toString()); 00062 e.printStackTrace(System.err); 00063 System.exit(-1); 00064 } 00065 00066 popMenu = new JPopupMenu(); 00067 setVisible(true); 00068 setDefaultCloseOperation(DISPOSE_ON_CLOSE); 00069 } 00070 00074 private void buildWindow() { 00075 Container rootCnt = getContentPane(); 00076 rootCnt.setLayout(new BorderLayout()); 00077 // The container with labels and combo boxes 00078 Container firstCnt = new Container(); 00079 firstCnt.setLayout(new GridLayout(4,2)); 00080 firstCnt.add(new JLabel("Name")); 00081 nameCB=new JComboBox(); 00082 nameCB.setEditable(true); 00083 nameCB.addItem("*"); 00084 nameCB.addItem("PIPPO"); 00085 nameCB.addItem("PLUTO"); 00086 firstCnt.add(nameCB); 00087 firstCnt.add(new JLabel("IDL interface")); 00088 idlCB=new JComboBox(); 00089 idlCB.addItem("*"); 00090 idlCB.addItem("IDL:alma/acsexmplLamp/Lamp:1.0"); 00091 idlCB.addItem("IDL:alma/MOUNT_ACS/Mount:1.0"); 00092 idlCB.addItem("IDL:alma/demo/HelloDemo:1.0"); 00093 idlCB.setEditable(true); 00094 firstCnt.add(idlCB); 00095 firstCnt.add(new JLabel("Implementation")); 00096 implCB=new JComboBox(); 00097 implCB.addItem("*"); 00098 implCB.addItem("acsexmplLampImpl"); 00099 implCB.addItem("acsexmplMountImpl"); 00100 implCB.addItem("alma.demo.HelloDemoImpl.HelloDemoHelper"); 00101 implCB.addItem("demoImpl.HelloDemo"); 00102 implCB.addItem("acsexmplHelloWorldClient"); 00103 implCB.setEditable(true); 00104 firstCnt.add(implCB); 00105 firstCnt.add(new JLabel("Container")); 00106 containerCB=new JComboBox(); 00107 containerCB.addItem("*"); 00108 containerCB.addItem("bilboContainer"); 00109 containerCB.addItem("frodoContainer"); 00110 containerCB.addItem("aragornContainer"); 00111 containerCB.setEditable(true); 00112 firstCnt.add(containerCB); 00113 // The container with the activate button 00114 Container secondCnt = new Container(); 00115 secondCnt.setLayout(new FlowLayout()); 00116 JButton activateB = new JButton("Activate"); 00117 activateB.addActionListener(this); 00118 secondCnt.add(activateB,"Center"); 00119 // The container with activated container 00120 MyTableModel tableModel = new MyTableModel(); 00121 activatedT = new JTable(tableModel); 00122 activatedT.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 00123 ListSelectionModel rowSM = activatedT.getSelectionModel(); 00124 JScrollPane scrollP = new JScrollPane(activatedT); 00125 activatedT.setPreferredScrollableViewportSize(new Dimension(400,90)); 00126 MyCellRendererr cellR = new MyCellRendererr(); 00127 TableColumnModel tcm = activatedT.getColumnModel(); 00128 TableColumn tc = tcm.getColumn(2); 00129 tc.setCellRenderer(cellR); 00130 MyCellEditor cellE = new MyCellEditor(this); 00131 tc.setCellEditor(cellE); 00132 Container thirdCnt = new Container(); 00133 thirdCnt.setLayout(new FlowLayout()); 00134 thirdCnt.add(scrollP,"North"); 00135 00136 // Add the container to the main container 00137 rootCnt.add(firstCnt,"North"); 00138 rootCnt.add(secondCnt,"Center"); 00139 rootCnt.add(thirdCnt,"South"); 00140 } 00141 00151 private void startDynamicComponent(String Name, String IDL, String Implementation, String Container) { 00152 boolean activated; 00153 // Check if a dynamic component with the same name was already activated 00154 MyTableModel myModel=(MyTableModel)activatedT.getModel(); 00155 if (myModel.exist(Name)) { 00156 JOptionPane.showOptionDialog( 00157 this, 00158 "The component "+Name+" already exists", 00159 "Error activating the component", 00160 JOptionPane.DEFAULT_OPTION, 00161 JOptionPane.ERROR_MESSAGE, 00162 null,null,null); 00163 return; 00164 } 00165 // Start the dynamic component.... 00166 String activatedComponentUrl=null; 00167 if (theClient.hasFreeSlot()) { 00168 ComponentSpec compSpec = new ComponentSpec(Name,IDL,Implementation,Container); 00169 try { 00170 activatedComponentUrl=theClient.getDynamicComponent(compSpec,false); 00171 } catch (Exception ce) { 00172 System.err.println("Error activating the component: "+ce.toString()); 00173 ce.printStackTrace(System.err); 00174 activated=false; 00175 } 00176 // The component is activated => add a new entry in the GUI 00177 if (activatedComponentUrl!=null) { 00178 myModel.append(Name,activatedComponentUrl); 00179 } else { 00180 JOptionPane.showOptionDialog( 00181 this, 00182 "Dynamic activation error", 00183 "Error activating the component", 00184 JOptionPane.DEFAULT_OPTION, 00185 JOptionPane.ERROR_MESSAGE, 00186 null,null,null); 00187 } 00188 } else JOptionPane.showOptionDialog( 00189 this, 00190 "No free slot available", 00191 "Error activating the component", 00192 JOptionPane.DEFAULT_OPTION, 00193 JOptionPane.ERROR_MESSAGE, 00194 null,null,null); 00195 } 00196 00199 public void actionPerformed(ActionEvent ae) { 00200 popMenu.setVisible(false); 00201 if (ae.getActionCommand().compareTo("Activate")==0) { 00202 startDynamicComponent( 00203 (String)nameCB.getSelectedItem(), 00204 (String)idlCB.getSelectedItem(), 00205 (String)implCB.getSelectedItem(), 00206 (String)containerCB.getSelectedItem()); 00207 } else if (ae.getActionCommand().split(" ")[0].compareTo("Release")==0) { 00208 String splitted[] = ae.getActionCommand().split(" "); 00209 // Get the cURL of the component 00210 MyTableModel myModel=(MyTableModel)activatedT.getModel(); 00211 String cURL=myModel.getURL(splitted[1]); 00212 if (releaseComponent(cURL)) { 00213 ((JButton)ae.getSource()).removeActionListener(this); 00214 ((MyCellEditor)activatedT.getCellEditor()).stopEditing(); 00215 ((MyTableModel)activatedT.getModel()).sort(); 00216 } 00217 } 00218 } 00219 00222 public static void main(String[] args) 00223 { 00224 00225 String managerLoc= System.getProperty("ACS.manager"); 00226 if (managerLoc==null) { 00227 System.err.println("Error getting ACS.manager property"); 00228 System.exit(-1); 00229 } 00230 new JDynAct(managerLoc); 00231 } 00232 00236 public boolean releaseComponent(String url) { 00237 if (theClient.releaseComponent(url)) { 00238 MyTableModel tm=(MyTableModel)activatedT.getModel(); 00239 tm.deleteEntry(url); 00240 return true; 00241 } else { 00242 JOptionPane.showOptionDialog( 00243 this, 00244 "Error releasing the component", 00245 "Dynamic activation error", 00246 JOptionPane.DEFAULT_OPTION, 00247 JOptionPane.ERROR_MESSAGE, 00248 null,null,null); 00249 return false; 00250 } 00251 } 00252 00255 private void cleanExit() { 00256 // Realease all the dynamic components 00257 theClient.cleanExit(); 00258 System.exit(0); 00259 } 00260 00261 public void windowActivated(WindowEvent e) {} 00262 public void windowClosing(WindowEvent e) {} 00263 public void windowDeactivated(WindowEvent e) {} 00264 public void windowDeiconified(WindowEvent e) {} 00265 public void windowIconified(WindowEvent e) {} 00266 public void windowOpened(WindowEvent e) {} 00267 public void windowClosed(WindowEvent e) { cleanExit(); } 00268 } 00269 00270 00271 00272 00273