TimRegisterGui.java

00001 /*
00002  * TimRegisterGui.java
00003  *
00004  * Created on December 16, 2005, 5:22 PM
00005  */
00006 
00007 package DisplayGUI;
00008 import Sct_SctApi.*;
00009 import GuiComponents.System.*;
00010 
00015 public class TimRegisterGui extends javax.swing.JFrame implements displayParams, TimRegisterInfo, java.awt.event.ActionListener {
00016 
00017         private javax.swing.JCheckBox[] checkBox = new javax.swing.JCheckBox[16];
00018 
00019         private String[] bits = {"0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"};
00020 
00021         private String[] tims;
00022         private int selection;
00023 
00024         java.util.regex.Pattern sp = java.util.regex.Pattern.compile("Partition(\\d+)\\.Crate(\\d+)");
00025 
00028     public TimRegisterGui(Object[] timlist, int selection) {
00029                 super("TIM Registers");
00030                 tims = new String[timlist.length];
00031                 for(int i=0;i<timlist.length;i++) tims[i]=new String((String)timlist[i]);
00032                 this.selection = selection;
00033         initComponents();
00034                 pack();
00035                 setRegister(0);
00036 
00037                 addWindowListener(new java.awt.event.WindowAdapter() {
00038             public void windowClosing(java.awt.event.WindowEvent evt) {
00039                 closeDialog(evt);
00040             }
00041         });
00042     }
00043     
00049     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
00050     private void initComponents() {
00051         java.awt.GridBagConstraints gridBagConstraints;
00052 
00053         title = new javax.swing.JLabel();
00054         selectLabel = new javax.swing.JLabel();
00055         jComboBox1 = new javax.swing.JComboBox();
00056                 jComboBox2 = new javax.swing.JComboBox(tims);
00057         jButton1 = new javax.swing.JButton();
00058         jButton2 = new javax.swing.JButton();
00059         jButton3 = new javax.swing.JButton();
00060 
00061                 getContentPane().setBackground(backgroundColor);
00062         getContentPane().setLayout(new java.awt.GridBagLayout());
00063 
00064                 javax.swing.JLabel label = new javax.swing.JLabel("TIM:");
00065                 label.setFont(displayScaleFont);
00066         gridBagConstraints = new java.awt.GridBagConstraints();
00067         gridBagConstraints.gridx = 0;
00068         gridBagConstraints.gridy = 0;
00069         gridBagConstraints.gridwidth = 2;
00070                 gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
00071                 gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 2);
00072         //gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
00073                 //gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
00074         getContentPane().add(label, gridBagConstraints);
00075 
00076 
00077                 jComboBox2.setSelectedIndex(selection);
00078                 jComboBox2.setFont(displayScaleFont);
00079                 jComboBox2.setBackground(backgroundColor);
00080         gridBagConstraints = new java.awt.GridBagConstraints();
00081         gridBagConstraints.gridx = 2;
00082         gridBagConstraints.gridy = 0;
00083                 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
00084         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
00085                 gridBagConstraints.insets = new java.awt.Insets(5, 2, 5, 5);
00086         //gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
00087                 //gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
00088         getContentPane().add(jComboBox2, gridBagConstraints);
00089                 jComboBox2.addActionListener(this);
00090 
00091         selectLabel.setText("Register:");
00092         selectLabel.setFocusCycleRoot(true);
00093                 selectLabel.setFont(displayScaleFont);
00094         gridBagConstraints = new java.awt.GridBagConstraints();
00095         gridBagConstraints.gridx = 0;
00096         gridBagConstraints.gridy = 1;
00097         gridBagConstraints.gridwidth = 2;
00098         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
00099         gridBagConstraints.weightx = 0.2;
00100         gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 2); // top, left, bottom, right
00101         getContentPane().add(selectLabel, gridBagConstraints);
00102 
00103                 jComboBox1.setFont(displayScaleFont);
00104                 jComboBox1.setBackground(backgroundColor);
00105                 for(int i=0;i<timRegisterNames.length;i++) jComboBox1.addItem(timRegisterNames[i]);
00106         gridBagConstraints = new java.awt.GridBagConstraints();
00107         gridBagConstraints.gridx = 2;
00108         gridBagConstraints.gridy = 1;
00109         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
00110         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
00111         gridBagConstraints.weightx = 0.7;
00112         gridBagConstraints.insets = new java.awt.Insets(5, 2, 5, 5);
00113         getContentPane().add(jComboBox1, gridBagConstraints);
00114                 jComboBox1.addActionListener(this);
00115 
00116                 for(int i=0;i<16;i++) {
00117                         javax.swing.JLabel indexLabel = new javax.swing.JLabel(Integer.toString(i));
00118                         indexLabel.setFont(displayScaleFont);
00119                 gridBagConstraints = new java.awt.GridBagConstraints();
00120                 gridBagConstraints.gridx = 0;
00121                 gridBagConstraints.gridy = i+2;
00122                 gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
00123                 if(i==0) gridBagConstraints.weightx = 0.1;
00124                 getContentPane().add(indexLabel, gridBagConstraints);
00125 
00126                         checkBox[i] = new javax.swing.JCheckBox();
00127 //            checkBox[i].setText("Description XXXXXXXXXXXXXXX "+Integer.toString(i));
00128                         checkBox[i].setFont(displayScaleFont);
00129                         checkBox[i].setBackground(backgroundColor);
00130                 gridBagConstraints = new java.awt.GridBagConstraints();
00131                 gridBagConstraints.gridx = 1;
00132                 gridBagConstraints.gridy = i+2;
00133                 gridBagConstraints.gridwidth = 2;
00134                 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
00135                 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
00136                 getContentPane().add(checkBox[i], gridBagConstraints);
00137                         }
00138 
00139 
00140                 javax.swing.JPanel buttonPanel = new javax.swing.JPanel();
00141                 buttonPanel.setLayout(new javax.swing.BoxLayout(buttonPanel,javax.swing.BoxLayout.X_AXIS));
00142                 buttonPanel.setBackground(backgroundColor);
00143 
00144         jButton1.setText("Read");
00145                 jButton1.setFont(displayScaleFont);
00146                 jButton1.addActionListener(new java.awt.event.ActionListener() {
00147             public void actionPerformed(java.awt.event.ActionEvent evt) {
00148                                 rwRegister(true);
00149                                 updateBitString();
00150             }
00151         });
00152         jButton2.setText("Write");
00153                 jButton2.setFont(displayScaleFont);
00154                 jButton2.addActionListener(new java.awt.event.ActionListener() {
00155             public void actionPerformed(java.awt.event.ActionEvent evt) {
00156                                 rwRegister(false);
00157             }
00158         });
00159                 jButton3.setText("Close");
00160                 jButton3.setFont(displayScaleFont);
00161                 jButton3.addActionListener(new java.awt.event.ActionListener() {
00162             public void actionPerformed(java.awt.event.ActionEvent evt) {
00163                 closeDialog(null);
00164             }
00165         });
00166                 buttonPanel.add(jButton1);
00167                 buttonPanel.add(javax.swing.Box.createHorizontalGlue());
00168                 buttonPanel.add(jButton2);
00169                 buttonPanel.add(javax.swing.Box.createHorizontalGlue());
00170                 buttonPanel.add(jButton3);
00171 
00172         gridBagConstraints = new java.awt.GridBagConstraints();
00173         gridBagConstraints.gridx = 0;
00174         gridBagConstraints.gridy = 18;
00175         gridBagConstraints.gridwidth = 3;
00176         gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
00177                 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
00178         gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
00179         getContentPane().add(buttonPanel, gridBagConstraints);
00180 
00181         gridBagConstraints = new java.awt.GridBagConstraints();
00182         gridBagConstraints.gridx = 0;
00183         gridBagConstraints.gridy = 19;
00184         gridBagConstraints.gridwidth = 3;
00185                 gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
00186                 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
00187         gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
00188 
00189                 javax.swing.JPanel p2 = new javax.swing.JPanel();
00190                 p2.setBackground(backgroundColor);
00191                 p2.setLayout(new javax.swing.BoxLayout(p2,javax.swing.BoxLayout.X_AXIS));
00192                 javax.swing.JLabel from = new javax.swing.JLabel("From ");
00193                 from.setFont(displayScaleFont);
00194                 p2.add(from);
00195                 jComboBox3 = new javax.swing.JComboBox(bits);
00196                 jComboBox3.setFont(displayScaleFont);
00197                 jComboBox3.setBackground(backgroundColor);
00198                 p2.add(jComboBox3);
00199                 javax.swing.JLabel to = new javax.swing.JLabel(" to ");
00200                 to.setFont(displayScaleFont);
00201                 p2.add(to);
00202                 jComboBox4 = new javax.swing.JComboBox(bits);
00203                 jComboBox4.setFont(displayScaleFont);
00204                 jComboBox4.setBackground(backgroundColor);
00205                 p2.add(jComboBox4);
00206                 jButton4 = new javax.swing.JButton("Eval");
00207                 jButton4.setFont(displayScaleFont);
00208                 p2.add(javax.swing.Box.createRigidArea(new java.awt.Dimension(5, 0)));
00209                 p2.add(jButton4);
00210 
00211                 jButton4.addActionListener(new java.awt.event.ActionListener() {
00212             public void actionPerformed(java.awt.event.ActionEvent evt) {
00213                                 updateBitString();
00214             }
00215         });
00216 
00217                 javax.swing.JPanel p1 = new javax.swing.JPanel();
00218                 p1.setLayout(new javax.swing.BoxLayout(p1,javax.swing.BoxLayout.Y_AXIS));
00219                 p1.setBackground(backgroundColor);
00220                 p1.add(p2);
00221                 p1.add(javax.swing.Box.createRigidArea(new java.awt.Dimension(0, 10)));
00222                 bitResult = new javax.swing.JLabel("0000000000000000 = 65000 = 0xffff      ");
00223                 bitResult.setFont(displayScaleFont);
00224                 p1.add(bitResult);
00225                 p1.setBorder(new javax.swing.border.TitledBorder(null, "BitPattern Evaluation", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 0, 11), new java.awt.Color(0, 0, 153)));
00226 
00227                 getContentPane().add(p1, gridBagConstraints);
00228 
00229     }
00230 
00231         private void rwRegister(boolean read) {
00232                 try {
00233         Sct_SctApi.SctApiIPC api = SystemInterface.getInstance().getSctApi();
00234                 if(api==null) {
00235                 javax.swing.JOptionPane.showMessageDialog(null,"Read/Write failed: SctApi is not available.");
00236                 return;
00237                 }
00238                 String tim = (String)jComboBox2.getSelectedItem();
00239                 java.util.regex.Matcher matcher = sp.matcher(tim);
00240                 if(!matcher.matches()) {
00241                         javax.swing.JOptionPane.showMessageDialog(null,"Software error - invalid TIM descriptor");
00242                         return;
00243                         }
00244                 int partition = Integer.parseInt(tim.substring(matcher.start(1),matcher.end(1)));
00245                 int crate = Integer.parseInt(tim.substring(matcher.start(2),matcher.end(2)));
00246                 int registerOffset = 2 * jComboBox1.getSelectedIndex();
00247                 if(read) {
00248                         short data = api.timReadRegister(partition,crate,(short)registerOffset);
00249 //                      System.out.println("Read data "+data);
00250                         for(int i=0;i<16;i++) checkBox[i].setSelected((data & 1<<i) > 0);
00251                         }
00252                 else {
00253                         short data = 0;
00254                         for(int i=0;i<16;i++) {
00255                                 int bit = checkBox[i].isSelected() ? 1 : 0;
00256                                 data |= (bit<<i);
00257                                 }
00258 //                      System.out.println("Write data "+data);
00259                         api.timWriteRegister(partition,crate,(short)registerOffset,data);
00260                         }
00261                 }catch(Exception e) {javax.swing.JOptionPane.showMessageDialog(null,"Read/write failed due to exception: "+e.toString());}
00262 
00263         }
00264 
00265         private void setRegister(int register) {
00266                 jComboBox1.setSelectedIndex(register);
00267                 for(int i=0;i<16;i++) {
00268                         checkBox[i].setText(registerContentLabel[register][i]);
00269                         checkBox[i].setEnabled(isRW[register][i]);
00270                         }
00271                 rwRegister(true);  // re-read the register
00272                 setBitStrings();
00273                 updateBitString();
00274                 }
00275 
00276         public static void main(String args[]) {
00277                 String[] tims = new String[1];
00278                 if(args.length==0) tims[0] = new String("Partition0.Crate0");
00279                 else if(args.length==2) {
00280                         try {
00281                                 int p1 = Integer.valueOf(args[0]);
00282                                 int p2 = Integer.valueOf(args[1]);
00283                                 tims[0] = new String("Partition"+args[0]+".Crate"+args[1]);
00284                                 }
00285                         catch(Exception e) {
00286                                 javax.swing.JOptionPane.showMessageDialog(null,"Invalid partition and crate. Using Partition0.Crate0 by default.");
00287                                 tims[0] = new String("Partition0.Crate0");
00288                                 }
00289                         }
00290                 else {
00291                         System.out.println("Invalid arguments.... use runtimgui <partition number> <crate number>");
00292                         System.exit(0);
00293                         }
00294         new TimRegisterGui(tims,0).setVisible(true);
00295     }
00296 
00297     public void actionPerformed(java.awt.event.ActionEvent e) {
00298         javax.swing.JComboBox cb = (javax.swing.JComboBox)e.getSource();
00299         if(cb == jComboBox1 || cb==jComboBox2) { // register change
00300                         int item = jComboBox1.getSelectedIndex();
00301                         setRegister(item);
00302                         }
00303     }
00304 
00305     private void closeDialog(java.awt.event.WindowEvent evt) {
00306         setVisible(false);
00307         dispose();
00308     }
00309 
00310         private void updateBitString() {
00311                 int firstbit = jComboBox3.getSelectedIndex();
00312                 int lastbit = jComboBox4.getSelectedIndex();
00313                 if(lastbit<firstbit) {
00314                         javax.swing.JOptionPane.showMessageDialog(null,"Last bit must be more or equal to first bit");
00315                         return;
00316                         }
00317         int data=0;
00318                 int ibit=0;
00319                 StringBuffer sb = new StringBuffer();
00320                 for(int i=firstbit;i<=lastbit;i++) {
00321                                         int databit = checkBox[i].isSelected() ? 1 : 0;
00322                                         data |= (databit << ibit);
00323                                         ibit++;
00324                                         sb.insert(0,databit);
00325                                         }
00326                 bitResult.setText(sb.toString()+" = "+Integer.toString(data) + " = 0x"+Integer.toHexString(data));
00327                 }
00328 
00329         private void setBitStrings() {
00330                 jComboBox3.setSelectedIndex(0);
00331                 jComboBox4.setSelectedIndex(15);
00332                 }
00333     
00334     
00335     // Variables declaration - do not modify
00336     private javax.swing.JButton jButton1;
00337     private javax.swing.JButton jButton2;
00338     private javax.swing.JButton jButton3, jButton4;
00339     private javax.swing.JComboBox jComboBox1,jComboBox2,jComboBox3,jComboBox4;
00340     private javax.swing.JLabel selectLabel;
00341         private javax.swing.JLabel title, bitResult;
00342     // End of variables declaration
00343     
00344 }

Generated on Mon Feb 6 14:12:20 2006 for SCT DAQ/DCS Software - Java by  doxygen 1.4.6