package org.cipres.kepler.registry; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import java.io.File; import org.cipres.guigen.ServiceCommandPanel; import org.cipres.kepler.*; import org.apache.log4j.Logger; public class ActorIterator extends JDialog { // TODO: assign a proper hash value to this static final long serialVersionUID = 1L; static Logger logger = Logger.getLogger(ActorIterator.class.getName()); // top-level elements JPanel contentPane; BorderLayout borderLayout1 = new BorderLayout(); Border border1; JTabbedPane jTabPaneActorParamSetters = new JTabbedPane(); JPanel pnlUpdate = new JPanel(); JLabel lblIterations = new JLabel("Iterations"); JTextField txtIterations = new JTextField(); JButton btnSetIterations = new JButton("Reset Iterations"); JButton btnSetParams = new JButton("Set Parameters"); JButton btnCancel = new JButton("Cancel"); GridBagLayout gridBagLayout2 = new GridBagLayout(); ServiceCommandPanel[] serviceCommandPanels; private String[][] constantParamValues; // used to store any params set in // all panels // the 1st dimension is number params // the 2nd dimension is two, with elemnt zero // for param name and element one for param value // this array is used when new iteration panels // are added private File guiXmlFile; // baseFile is used to insure that all files within the Iteration have the // base filename: [prefix]_Iter[iteration number]_[base filename] private File baseFile; private String outfilePrefix; /*************************************************************************** * CONSTRUCTORS **************************************************************************/ public ActorIterator(File guiXmlFile, int numIterations) throws Exception { this.init(guiXmlFile, numIterations); } public ActorIterator(File guiXmlFile) throws Exception { this.init(guiXmlFile,1); } private void init(File guiXmlFile, int numIterations) throws Exception { enableEvents(AWTEvent.WINDOW_EVENT_MASK); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.setModal(true); this.guiXmlFile = guiXmlFile; serviceCommandPanels = new ServiceCommandPanel[numIterations]; for (int i = 0; i < numIterations; i++) { serviceCommandPanels[i] = getNewServicePanel(); } jbInit(); } /*************************************************************************** * PUBLIC METHODS **************************************************************************/ /** * Sets a parameters value in all panels
* * @param parameterName - * name of the parameter to set * @param value - * the value to assign the parameter * @throws Exception */ public void setParameterValueInAll(String parameterName, String value) throws Exception { if (constantParamValues == null) { constantParamValues = new String[1][2]; } else { String[][] constantParamValuesNew = new String[constantParamValues.length + 1][2]; // add existing vals to constantParamValuesNew for (int i = 0; i < constantParamValues.length; i++) { constantParamValuesNew[1][0] = constantParamValues[i][0]; constantParamValuesNew[1][1] = constantParamValues[i][1]; } constantParamValues = constantParamValuesNew; } // add new vals constantParamValues[constantParamValues.length - 1][0] = parameterName; constantParamValues[constantParamValues.length - 1][1] = value; // set the new value in all panels for (int i = 0; i < serviceCommandPanels.length; i++) { serviceCommandPanels[i].setParameterValue(parameterName, value); } } /** * Sets a parameters value in a panel
* * @param parameterName - * name of the parameter to set * @param value - * the value to assign the parameter * @param index - * the index of the panel to assign the parameter value * @throws Exception */ public void setParameterValue(String parameterName, String value, int index) throws Exception { serviceCommandPanels[index].setParameterValue(parameterName, value); } /** * Returns the value of a parameter from an iteration panel
* * @param parameterName - * name of the parameter * @param index - * the index of the panel * @return the value assigned to the parameter * @throws Exception */ public String getParameterValue(String parameterName, int index) throws Exception { return serviceCommandPanels[index].getParameterValue(parameterName); } /** * Sets a unique outfile name as the value for the 'outfile' parameter in * all panels using the following pattern: [filenamePrefix]_Iter[iteration * number]_[base filename] where [base filename] is a timestamp-derived * string
* * @param filenamePrefix - * a common prefix to prepend to the outfile name name assigned * to each iteration panel; typically the name of the actor * @throws Exception */ public void setOutfileParamInAll(String filenamePrefix) throws Exception { this.outfilePrefix = filenamePrefix; for (int i = 0; i < serviceCommandPanels.length; i++) { setOutfileParam(filenamePrefix, i); } } /** * @param filenamePrefix - * a common prefix to append to the outfile name assigned to each * iteration panel; typically the name of the actor * @param index - * the index of the panel * @throws Exception */ public void setOutfileParam(String filenamePrefix, int index) throws Exception { serviceCommandPanels[index].setParameterValue("outfile", getOutfileName(filenamePrefix, index)); } /** * @return - the array of ServiceCommandPanels */ public ServiceCommandPanel[] getServiceCommandPanels() { return this.serviceCommandPanels; } /** * @param index - * the index of the panel * @return the ServiceCommandPanel at the specified index */ public ServiceCommandPanel getServiceCommandPanel(int index) { return this.serviceCommandPanels[index]; } /** * @return a String array where each element contains the commands for an * iteration */ public String[] getCommandBlocks() { String actorCommands[] = new String[this.serviceCommandPanels.length]; for (int i = 0; i < serviceCommandPanels.length; i++) { actorCommands[i] = serviceCommandPanels[i].getCmdBlock(); } return actorCommands; } /** * Returns the commands for the iteration at the specified index * * @param index - * the index of the panel * @return */ public String getCommandBlock(int index) { return serviceCommandPanels[index].getCmdBlock(); } /*************************************************************************** * PRIVATE METHODS **************************************************************************/ private void jbInit() throws Exception { // top-level components contentPane = (JPanel) this.getContentPane(); border1 = BorderFactory.createEtchedBorder(Color.white, new Color(148, 145, 140)); contentPane.setBorder(BorderFactory.createLineBorder(Color.black)); contentPane.setLayout(borderLayout1); btnSetParams.setToolTipText("\"Click to update settings\""); txtIterations.setMaximumSize(new Dimension(30, 18)); txtIterations.setMinimumSize(new Dimension(30, 18)); txtIterations.setPreferredSize(new Dimension(30, 18)); txtIterations.setSize(txtIterations.getPreferredSize()); pnlUpdate.setLayout(gridBagLayout2); lblIterations.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12)); contentPane.add(jTabPaneActorParamSetters, BorderLayout.CENTER); contentPane.add(pnlUpdate, BorderLayout.SOUTH); // add iteration panels int j; for (int i = 0; i < serviceCommandPanels.length; i++) { j = i + 1; jTabPaneActorParamSetters.add(serviceCommandPanels[i] .getServicePanel(), "Iteration " + j, jTabPaneActorParamSetters.getTabCount()); } this.txtIterations.setText(Integer.toString(jTabPaneActorParamSetters .getTabCount())); pnlUpdate.add(btnSetParams, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 0, 5, 10), 0, 0)); pnlUpdate.add(btnCancel, new GridBagConstraints(4, 0, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 0, 5, 10), 0, 0)); btnCancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Component c = (Component) e.getSource(); if (c.hasFocus()) { serviceCommandPanels = null; close(); } } }); btnSetParams.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Component c = (Component) e.getSource(); if (c.hasFocus()) { close(); } } }); // Iteration setter components pnlUpdate.add(lblIterations, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 15, 5, 0), 0, 0)); pnlUpdate.add(txtIterations, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0)); pnlUpdate.add(btnSetIterations, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 0, 5, 40), 0, 0)); btnSetIterations.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Component c = (Component) e.getSource(); if (c.hasFocus()) { setIterations(); } } }); this.pack(); } // end jbInit private void setIterations() { int numPanels = 0; try { numPanels = Integer.parseInt(txtIterations.getText()); if (numPanels < 1) { JOptionPane .showMessageDialog(this, "The entry for number of iterations must be a positive integer"); numPanels = serviceCommandPanels.length; // reset to current // value this.txtIterations.setText(Integer .toString(jTabPaneActorParamSetters.getTabCount())); } if (numPanels != serviceCommandPanels.length) { ServiceCommandPanel[] newPanels; if (numPanels > serviceCommandPanels.length) { // add panels newPanels = new ServiceCommandPanel[numPanels]; // add existing panels to newPanels for (int i = 0; i < serviceCommandPanels.length; i++) { newPanels[i] = serviceCommandPanels[i]; } // add new panels, set any param values stored in array // constantParamValues for (int i = serviceCommandPanels.length; i < newPanels.length; i++) { newPanels[i] = getNewServicePanel(); for (int j = 0; j < constantParamValues.length; j++) { newPanels[i].setParameterValue( constantParamValues[j][0], constantParamValues[j][1]); } } } else { // remove panels // use subset chooser to allow user to select which panels // to remove DisplayObject[] inObjs = new DisplayObject[serviceCommandPanels.length]; int k = 1; for (int i = 0; i < inObjs.length; i++) { inObjs[i] = new DisplayObject(serviceCommandPanels[i], "Iteration " + k); k++; } SubsetChooser ssc = new SubsetChooser(inObjs, "Select Iteration Panels to retain"); DisplayObject[] outObjs = ssc.showSubsetChooserAsDialog(); if (outObjs.length < 1) { JOptionPane.showMessageDialog(this, "At least one iteration panel must retained"); this.txtIterations.setText(Integer .toString(jTabPaneActorParamSetters .getTabCount())); newPanels = this.serviceCommandPanels; } else { newPanels = new ServiceCommandPanel[outObjs.length]; for (int i = 0; i < outObjs.length; i++) { newPanels[i] = (ServiceCommandPanel) outObjs[i] .getObject(); } } } this.serviceCommandPanels = newPanels; // rebuild panels jTabPaneActorParamSetters.removeAll(); int j = 1; for (int i = 0; i < serviceCommandPanels.length; i++) { jTabPaneActorParamSetters.add(serviceCommandPanels[i] .getServicePanel(), "Iteration " + j, jTabPaneActorParamSetters.getTabCount()); j++; } this.setOutfileParamInAll(this.outfilePrefix); this.txtIterations.setText(Integer .toString(jTabPaneActorParamSetters.getTabCount())); this.validate(); } } catch (NumberFormatException e) { JOptionPane .showMessageDialog(this, "The entry for number of iterations must be a positive integer"); this.txtIterations.setText(Integer .toString(jTabPaneActorParamSetters.getTabCount())); } catch (Exception e) { e.printStackTrace(); } } private ServiceCommandPanel getNewServicePanel() throws Exception { ServiceCommandPanel s = new ServiceCommandPanel(guiXmlFile); s.setHideNotifyButton(true); s.setHideCancelButton(true); return s; } // TODO: these methods are never used locally /* private void setTabTitles() { int j = 1; for (int i = 0; i < jTabPaneActorParamSetters.getComponentCount(); i++) { jTabPaneActorParamSetters.setTitleAt(i, "Iteration " + j); j++; } } private void removeIterationPanel(int index) { jTabPaneActorParamSetters.remove(index); ServiceCommandPanel[] newPanels = new ServiceCommandPanel[serviceCommandPanels.length - 1]; int j = 0; for (int i = 0; i < serviceCommandPanels.length; i++) { if (i != index) { newPanels[j] = serviceCommandPanels[i]; } j++; } this.serviceCommandPanels = newPanels; this.validate(); } */ private void close() { this.dispose(); } private String getOutfileName(String filenamePrefix, int index) throws Exception { if (baseFile == null) { baseFile = new File(Globals.getInstance().getUniqueTmpFilename( null, null)); } int j = index + 1; String fname = baseFile.getParent() + Globals.getInstance().getDirSep(); if (filenamePrefix != null) { return fname + filenamePrefix + "_Iter" + j + "_" + baseFile.getName(); } else { return fname + "_Iter" + j + "_" + baseFile.getName(); } } }