00001
00006 package DisplayGUI;
00007 import org.omg.CORBA.IntHolder;
00008 import ipc.*;
00009
00010
00011 import java.util.*;
00012 import GuiComponents.System.*;
00013
00014 public class ConfigurationInterface implements displayParams, SCTInfo {
00015 private static ConfigurationInterface instance = new ConfigurationInterface();
00016 private int maxWidth=0;
00017 private int midX=0, midY=0;
00018 private int moduleCount=0;
00019 private java.awt.Polygon polygon;
00020
00021 ModuleCell[][][] moduleCells;
00022 double[] radius = {0.,0.,0.,0.,0.,0.};
00023 int[] xPoints = {0,0,0,0};
00024 int[] yPoints = {0,0,0,0};
00025 long fillsn=3000;
00026
00027
00028 private SortedMap[] serialNumberMap = new TreeMap[sctViews];
00029
00030 private Map snMap;
00031 private boolean dcsAvailable;
00032
00033 sctConf.Configuration h;
00034
00035 private ConfigurationInterface() {
00036 Refresh();
00037
00038
00039 }
00040
00041 public static ConfigurationInterface getInstance() {
00042 return instance;
00043 }
00044
00045 public void setWindowSize(int maxWidth, int maxHeight) {
00046 this.maxWidth=maxWidth;
00047 midX=maxWidth/2;
00048 midY=maxHeight/2;
00049 maxWidth*=0.95;
00050 radius[0]=0.35 * (double)maxWidth/2.;
00051 radius[1]=0.52 * (double)maxWidth/2.;
00052 radius[2]= 0.5 * (double)maxWidth/2.;
00053 radius[3]= 0.76* (double)maxWidth/2.;
00054 radius[4]= 0.74 * (double)maxWidth/2.;
00055 radius[5]= maxWidth/2.;
00056 }
00057
00058 public void Refresh() {
00059 for(int i=0;i<sctViews;i++) serialNumberMap[i]=new TreeMap();
00060 moduleCells = new ModuleCell[sctViews][][];
00061 snMap = new HashMap();
00062 dcsAvailable=false;
00063 moduleCount=0;
00064 }
00065
00066 public boolean isConfigured() {
00067 return (moduleCount>0);
00068 }
00069
00070 public void getConfiguration() {
00071 getConfiguration(-1);
00072 }
00073
00074 public void getConfiguration(int moduleGroup) {
00075 h=null;
00076 h = SystemInterface.getInstance().getConfigurationService();
00077 if(h==null) return;
00078 try {
00079 int[] parts = h.listPartitions();
00080 for(int p=0; p<parts.length; p++) {
00081
00082 int[] crates = h.listCratesInPartition(parts[p]);
00083 for(int c=0; c<crates.length; c++) {
00084
00085 int[] rods = h.listRodsInCrate(parts[p], crates[c]);
00086 for(int r=0; r<rods.length; r++) {
00087
00088
00089
00090
00091 int[] MURs = h.listMURSInRod(parts[p], crates[c], rods[r]);
00092 for(int mur=0; mur<MURs.length; mur++) {
00093
00094 String[] modules = h.listModulesInMUR(parts[p], MURs[mur]);
00095
00096 for(int m=0; m<modules.length; m++) {
00097
00098
00099 IntHolder MUR = new IntHolder(), num = new IntHolder();
00100 h.translateFromSN(modules[m], MUR, num);
00101
00102 IntHolder partition = new IntHolder(), crate = new IntHolder(), rodno=new IntHolder(), channel=new IntHolder() ;
00103 h.translateToROD(MUR.value, num.value, partition, crate, rodno, channel);
00104
00105 SNInfo snInfo = (SNInfo) snMap.get(modules[m]);
00106 if(snInfo==null) snInfo = new SNInfo();
00107 snInfo.set(SNInfo.PARTITION,Integer.toString(partition.value));
00108 snInfo.set(SNInfo.ROD_CRATE,Integer.toString(crate.value));
00109 snInfo.set(SNInfo.ROD_NUMBER,Integer.toString(rodno.value));
00110 snInfo.set(SNInfo.ROD_CHANNEL,Integer.toString(channel.value));
00111 snMap.put(modules[m],snInfo);
00112
00113 try {
00114 moduleGroup = h.getModuleGroup(modules[m]);
00115 }catch(Exception groupE){
00116 System.err.println("SctGUI::ConfigurationInterface - can't retrieve moduleGroup : "+groupE.toString());
00117 moduleGroup=0;
00118 }
00119
00120 IntHolder barrel = new IntHolder(), row = new IntHolder(), number = new IntHolder();
00121 try {
00122 h.translateToBarrel(MUR.value, num.value, barrel, row, number);
00123 defineBarrelModuleGraphicsCell(barrel.value, row.value, number.value, MUR.value, moduleGroup, modules[m]);
00124 } catch(sctConf.ConfigurationException eConfig) {
00125 h.translateToEndcap(MUR.value, num.value, barrel, row, number);
00126 defineEndCapModuleGraphicsCell(barrel.value, row.value, number.value, MUR.value, moduleGroup, modules[m]);
00127 }
00128 try {
00129 h.translateToPowerSupply(MUR.value, num.value,partition,crate,channel);
00130 definePowerSupplyMapping(crate.value,channel.value,modules[m]);
00131 }catch(Exception ee) {}
00132
00133
00134 }
00135 }
00136 }
00137 }
00138 }
00139 } catch(sctConf.ConfigurationException eConfig) {
00140 System.err.println("Configuration exception retrieving configuration " + eConfig.detail);
00141 eConfig.printStackTrace();
00142 } catch(Exception e) {
00143 System.err.println("Non-configuration exception retrieving configuration ..." + e);
00144 e.printStackTrace();
00145 }
00146
00147 writeLookUpTable();
00148
00149
00150 }
00151
00152 public void definePowerSupplyMapping(int crate, int channel, String sn) {
00153
00154 dcsAvailable=true;
00155 SNInfo snInfo = (SNInfo) snMap.get(sn);
00156 if(snInfo==null) snInfo = new SNInfo();
00157
00158 String crateString = Integer.toString(crate);
00159 snInfo.set(SNInfo.DCS_CRATE,crateString);
00160 String channelString = Integer.toString(channel);
00161 snInfo.set(SNInfo.DCS_CHANNEL,channelString);
00162
00163 if(crateString.length()==1) crateString="0"+crateString;
00164 if(channelString.length()==1) channelString="0"+channelString;
00165 snInfo.set(SNInfo.DCS_HEADER,"Crate"+crateString+".Channel"+channelString+".");
00166 snInfo.set(SNInfo.IV_FILE_HEADER,"CRATE"+crateString+"CHAN"+channelString);
00167
00168 snMap.put(sn,snInfo);
00169
00170 }
00171 public String getSNInfo(String sn, int index) {
00172 SNInfo snInfo = (SNInfo) snMap.get(sn);
00173 if(snInfo==null) return "-1";
00174 return snInfo.get(index);
00175 }
00176
00177 public boolean isDCSAvailable() {
00178 return dcsAvailable;
00179 }
00180
00181
00182 public String addModule(int barrel, int row, int number, int MUR, int moduleGroup, String serialNo) {
00183
00184 for(int i=0;i<sctViews;i++) {
00185 if(serialNumberMap[i].containsKey(serialNo)) return "Module "+serialNo+" is already defined in "+SCTViewNames[i];
00186 }
00187
00188 for (Iterator i=serialNumberMap[barrel].entrySet().iterator(); i.hasNext(); ) {
00189 Map.Entry e = (Map.Entry) i.next();
00190 String thisSerialNo = (String)e.getKey();
00191 ModuleCell thisCell = (ModuleCell)e.getValue();
00192 if(thisCell.getRow()==row && thisCell.getModulePosition()==number) return "That location already taken by "+thisCell.getSerialNo();
00193 }
00194 int thisSCTView = barrel;
00195
00196 try {
00197 moduleGroup = h.getModuleGroup(serialNo);
00198 }catch(Exception ee){System.err.println("SctGUI::ConfigurationInterface - can't retrieve moduleGroup : "+ee.toString());}
00199
00200 if(barrel<4) {
00201 thisSCTView+=3;
00202 defineBarrelModuleGraphicsCell(thisSCTView, row, number, MUR, moduleGroup, serialNo);
00203 }
00204 else if(barrel>12) {
00205 thisSCTView-=12;
00206 defineEndCapModuleGraphicsCell(thisSCTView, row, number, MUR, moduleGroup, serialNo);
00207 }
00208 else {
00209 thisSCTView-=3;
00210 thisSCTView*=-1;
00211 defineEndCapModuleGraphicsCell(thisSCTView, row, number, MUR, moduleGroup, serialNo);
00212 }
00213 return null;
00214 }
00215
00216 private void defineBarrelModuleGraphicsCell(int barrel, int row, int number, int MUR, int moduleGroup, String serialNo) {
00217 for(int i=0;i<sctViews;i++) {
00218 if(serialNumberMap[i].containsKey(serialNo)) {
00219 System.out.println("** Module "+serialNo+" defined more than once!");
00220 return;
00221 }
00222 }
00223 if(barrel < 3 || barrel > 6) {
00224 System.out.println("** Module "+serialNo+" reported to be in nonexistant barrel " + barrel + "!");
00225 return;
00226 }
00227
00228 int thisSCTView=barrel-3;
00229 int moduleIndex = number;
00230 moduleIndex+=6;
00231 if(moduleIndex>5) moduleIndex--;
00232 int thisRow=row;
00233 if(moduleCells[thisSCTView]==null) moduleCells[thisSCTView] = new ModuleCell[barrelRows[thisSCTView]][];
00234 if(moduleCells[thisSCTView][thisRow]==null) moduleCells[thisSCTView][thisRow] = new ModuleCell[12];
00235
00236
00237
00238
00239 int xloc=maxWidth/2 - number*boxWidth;
00240
00241 int yloc = yOffSet + (yOffSetCount[thisSCTView] + barrelRows[thisSCTView] - row - 1)*boxHeight;
00242
00243
00244 int[] xPoints = {xloc,xloc,xloc+boxWidth,xloc+boxWidth};
00245 int[] yPoints = {yloc,yloc+boxHeight,yloc+boxHeight,yloc};
00246
00247 moduleCells[thisSCTView][thisRow][moduleIndex] = new ModuleCell(serialNo,thisSCTView,row,number,MUR,moduleGroup,xPoints,yPoints,4);
00248 serialNumberMap[thisSCTView].put(serialNo,moduleCells[thisSCTView][thisRow][moduleIndex]);
00249 moduleCount++;
00250 }
00251
00252 private void defineEndCapModuleGraphicsCell(int disk, int quadrant, int number, int MUR, int moduleGroup, String serialNo) {
00253 for(int i=0;i<sctViews;i++) {
00254 if(serialNumberMap[i].containsKey(serialNo)) {
00255 System.out.println("** Module "+serialNo+" defined more than once!");
00256 return;
00257 }
00258 }
00259
00260
00261 int thisSCTView = (disk<0) ? 3-disk : 12+disk;
00262 if(moduleCells[thisSCTView]==null) moduleCells[thisSCTView] = new ModuleCell[4][];
00263 if(moduleCells[thisSCTView][quadrant]==null) moduleCells[thisSCTView][quadrant] = new ModuleCell[33];
00264
00265 calculatePolygon(thisSCTView,quadrant,number);
00266
00267 moduleCells[thisSCTView][quadrant][number] = new ModuleCell(serialNo,thisSCTView,quadrant,number,MUR,moduleGroup,xPoints,yPoints,4);
00268 serialNumberMap[thisSCTView].put(serialNo,moduleCells[thisSCTView][quadrant][number]);
00269 moduleCount++;
00270 }
00271
00272
00273
00274 public ModuleCell getCell(int sctView, int x, int y) {
00275
00276
00277
00278 if(sctView<4) {
00279
00280 int yIndex = ((yOffSetCount[sctView]+barrelRows[sctView])*boxHeight - y + yOffSet)/boxHeight;
00281 if(yIndex<0 || yIndex>barrelRows[sctView]-1) return null;
00282 int diff = x -maxWidth/2;
00283
00284 int xIndex = diff/boxWidth;
00285 if(diff>0 && xIndex==0) return null;
00286
00287
00288 xIndex*=-1;
00289 xIndex+=6;
00290
00291
00292
00293
00294 if(xIndex<0 || xIndex>11) return null;
00295 if(moduleCells[sctView]!=null && moduleCells[sctView][yIndex]!=null) {
00296 if(moduleCells[sctView][yIndex][xIndex]!=null) return moduleCells[sctView][yIndex][xIndex];
00297 }
00298 }
00299 else {
00300
00301
00302 for (Iterator i=serialNumberMap[sctView].entrySet().iterator(); i.hasNext(); ) {
00303 Map.Entry e = (Map.Entry) i.next();
00304 ModuleCell thisCell = (ModuleCell)e.getValue();
00305 if(thisCell.contains(x,y)) return thisCell;
00306 }
00307 }
00308 return null;
00309 }
00310
00311 public Hashtable getSerialNumberMap(int sctView) {
00312 return new Hashtable(serialNumberMap[sctView]);
00313 }
00314 public SortedMap[] getSerialNumberMap() {
00315 return serialNumberMap;
00316 }
00317 public int getNumberOfModules(int sctView) {
00318 return serialNumberMap[sctView].size();
00319 }
00320 public int getTotalNumberOfModules() {
00321 return moduleCount;
00322 }
00323 public Set getSortedVector(int sctView) {
00324 return serialNumberMap[sctView].keySet();
00325 }
00326 public ModuleCell getModuleCell(int sctView, String serialNo) {
00327 return (ModuleCell)serialNumberMap[sctView].get(serialNo);
00328 }
00329
00330 public void drawSCTViewOutline(java.awt.Graphics2D g2,int selection) {
00331 g2.setColor(java.awt.Color.lightGray);
00332 g2.setFont(displayTextFont);
00333 java.awt.FontMetrics metrics = g2.getFontMetrics();
00334 int rowNo=0;
00335
00336 switch(selection) {
00337 case Barrel3:
00338 case Barrel4:
00339 case Barrel5:
00340 int x1 = midX-100;
00341 int x2 = midX+120;
00342 int yy = yOffSetCount[selection];
00343 int y1 = yOffSet +((yy-2)*boxHeight);
00344 g2.drawLine(x1,y1,x2,y1);
00345 g2.drawLine(x1,y1,x1+5,y1-5);
00346 g2.drawLine(x1,y1,x1+5,y1+5);
00347 g2.drawLine(x2,y1,x2-5,y1-5);
00348 g2.drawLine(x2,y1,x2-5,y1+5);
00349 g2.drawString("Z+",x1-20,y1);
00350 g2.drawString("Z-",x2+10,y1);
00351 case Barrel6:
00352 int ilow = yOffSetCount[selection];
00353
00354 int ihigh = ilow+barrelRows[selection];
00355 int midX= maxWidth/2;
00356
00357
00358 for(int y=ihigh-1;y>=ilow;y--) {
00359 String rowLabel = Integer.toString(rowNo);
00360 int labelWidth = metrics.stringWidth(rowLabel);
00361 int xLabel = midX - 6*boxWidth - labelWidth -8;
00362
00363 int thisRow = rowNo;
00364 boolean rowUsed = (moduleCells[selection]!=null && moduleCells[selection][thisRow]!=null);
00365 g2.setColor(java.awt.Color.lightGray);
00366 for(int x=-6;x<=6;x++) {
00367 if(x==0) continue;
00368 g2.drawRect(midX+(boxWidth*x),yOffSet+(y*boxHeight), boxWidth, boxHeight);
00369 }
00370 java.awt.Color labelColor = rowUsed ? java.awt.Color.black : java.awt.Color.lightGray;
00371 g2.setColor(labelColor);
00372 g2.drawString(rowLabel,xLabel,yOffSet+y*boxHeight+boxHeight);
00373 rowNo++;
00374 }
00375 g2.setColor(java.awt.Color.lightGray);
00376 g2.drawRect(midX - 6*boxWidth, yOffSet, 13*boxWidth, 56*boxHeight);
00377 break;
00378 case EndCap1a:
00379 case EndCap1c:
00380 case EndCap7a:
00381 case EndCap7c:
00382 case EndCap8a:
00383 case EndCap8c:
00384 for(int quadrant=0;quadrant<4;quadrant++) {
00385 for(int number=13;number<23;number++) {
00386 calculatePolygon(selection,quadrant,number);
00387 if(polygon!=null) {
00388 g2.setColor(java.awt.Color.white);
00389 g2.fill(polygon);
00390 g2.setColor(java.awt.Color.lightGray);
00391 g2.draw(polygon);
00392 }
00393 }
00394 }
00395 for(int quadrant=0;quadrant<4;quadrant++) {
00396 for(int number=0;number<13;number++) {
00397 calculatePolygon(selection,quadrant,number);
00398 if(polygon!=null) {
00399 g2.setColor(java.awt.Color.white);
00400 g2.fill(polygon);
00401 g2.setColor(java.awt.Color.lightGray);
00402 g2.draw(polygon);
00403 }
00404 }
00405 }
00406 break;
00407 case EndCap2a:
00408 case EndCap2c:
00409 case EndCap3a:
00410 case EndCap3c:
00411 case EndCap4a:
00412 case EndCap4c:
00413 case EndCap5a:
00414 case EndCap5c:
00415 case EndCap6a:
00416 case EndCap6c:
00417 for(int quadrant=0;quadrant<4;quadrant++) {
00418 for(int number=13;number<23;number++) {
00419 calculatePolygon(selection,quadrant,number);
00420 if(polygon!=null) {
00421 g2.setColor(java.awt.Color.white);
00422 g2.fill(polygon);
00423 g2.setColor(java.awt.Color.lightGray);
00424 g2.draw(polygon);
00425 }
00426 }
00427 }
00428 for(int quadrant=0;quadrant<4;quadrant++) {
00429 for(int number=0;number<33;number++) {
00430 if(number>=13 && number<23) continue;
00431 calculatePolygon(selection,quadrant,number);
00432 if(polygon!=null) {
00433 g2.setColor(java.awt.Color.white);
00434 g2.fill(polygon);
00435 g2.setColor(java.awt.Color.lightGray);
00436 g2.draw(polygon);
00437 }
00438 }
00439 }
00440 break;
00441 case EndCap9a:
00442 case EndCap9c:
00443 for(int quadrant=0;quadrant<4;quadrant++) {
00444 for(int number=0;number<13;number++) {
00445 calculatePolygon(selection,quadrant,number);
00446 if(polygon!=null) g2.draw(polygon);
00447 }
00448 }
00449 break;
00450 default:
00451
00452 }
00453
00454
00455 }
00456
00457 private void calculatePolygon(int view, int quadrant, int number) {
00458 double thetaOffset=0.;
00459 double theta=0.;
00460 int rindex=0;
00461 int segment=0;
00462 int modules=0;
00463 if(number<13) {
00464 thetaOffset = Math.PI/52.;
00465 theta = Math.PI/26.;
00466 rindex=4;
00467 segment=number;
00468 modules=13;
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482 }
00483 else if(number>=13 && number<23) {
00484 thetaOffset=0.;
00485 theta = Math.PI/20.;
00486 rindex=2;
00487 segment=number-13;
00488 modules=10;
00489 }
00490 else {
00491 thetaOffset=Math.PI/40.;
00492 theta = Math.PI/20.;
00493 rindex=0;
00494 segment=number-23;
00495 modules=10;
00496 }
00497 segment += quadrant*modules;
00498
00499 xPoints[0] = midX+(int)(radius[rindex]*Math.sin(segment*theta+thetaOffset));
00500 xPoints[1] = midX+(int)(radius[rindex]*Math.sin((segment+1)*theta+thetaOffset));
00501 xPoints[2] = midX+(int)(radius[rindex+1]*Math.sin((segment+1)*theta+thetaOffset));
00502 xPoints[3] = midX+(int)(radius[rindex+1]*Math.sin(segment*theta+thetaOffset));
00503
00504 yPoints[0] = midY-(int)(radius[rindex]*Math.cos(segment*theta+thetaOffset));
00505 yPoints[1] = midY-(int)(radius[rindex]*Math.cos((segment+1)*theta+thetaOffset));
00506 yPoints[2] = midY-(int)(radius[rindex+1]*Math.cos((segment+1)*theta+thetaOffset));
00507 yPoints[3] = midY-(int)(radius[rindex+1]*Math.cos(segment*theta+thetaOffset));
00508 polygon = new java.awt.Polygon(xPoints,yPoints,4);
00509
00510 }
00511
00512 public void updateModuleGroups(List cellList) {
00513 for(int i=0;i<cellList.size();i++) {
00514 ModuleCell thisCell = (ModuleCell)cellList.get(i);
00515 try {
00516 h.setModuleGroup(thisCell.getSerialNo(),(short)thisCell.getModuleGroup());
00517 }catch(Exception e){System.err.println("Exception setting modulegroup for module "+thisCell.getSerialNo()+" : "+e.toString());}
00518 }
00519 }
00520 private void writeLookUpTable() {
00521 String directory = Preferences.PreferencesInterface.getInstance().getPreference(Preferences.PreferencesInterface.WEB_LOOKUP_DIR);
00522 if(directory.equals(Preferences.PreferencesInterface.unDefinedString)) return;
00523
00524
00525
00526 for(int i=0;i<SCTViewNames.length;i++) {
00527 Map lookupMap = new HashMap();
00528 Set moduleList = getSortedVector(i);
00529 for (java.util.Iterator m = moduleList.iterator(); m.hasNext(); ) {
00530 String thisSN=(String)m.next();
00531 ModuleCell thisCell = getModuleCell(i,thisSN);
00532 String desc = SCTViewNames[i]+", Row "+thisCell.getRow()+", Position "+thisCell.getPosition();
00533 lookupMap.put(thisSN,desc);
00534 }
00535 if(lookupMap.size()>0) saveLookUpMap(i,directory,lookupMap);
00536 }
00537
00538
00539 }
00540 private Map getLookUpMap(String directory) {
00541 Map thisMap = new HashMap();
00542 java.util.regex.Pattern p = java.util.regex.Pattern.compile("(\\d{14})=(.*)");
00543 java.io.File lookupFile = new java.io.File(directory,"moduleMap.txt");
00544 try {
00545 if(lookupFile.exists()) {
00546 java.io.BufferedReader in = new java.io.BufferedReader(new java.io.FileReader(lookupFile));
00547 String line;
00548 while((line=in.readLine())!=null) {
00549 java.util.regex.Matcher matcher = p.matcher(line);
00550 if(matcher.matches()) thisMap.put(line.substring(matcher.start(1),matcher.end(1)),line.substring(matcher.start(2),matcher.end(2)));
00551 }
00552 in.close();
00553 }
00554 }catch(Exception e){System.err.println("SctGUI::ConfigurationInterface - Exception reading in "+lookupFile.getName()+" - "+e.toString());}
00555 return thisMap;
00556 }
00557 private void saveLookUpMap(int view,String directory,Map thisMap) {
00558 java.io.File lookupFile = new java.io.File(directory,"moduleMap"+Integer.toString(view)+".txt");
00559 int lineCount=0;
00560 try {
00561 java.io.BufferedWriter out = new java.io.BufferedWriter(new java.io.FileWriter(lookupFile));
00562 String line;
00563 for (Iterator i=thisMap.entrySet().iterator(); i.hasNext(); ) {
00564 Map.Entry e = (Map.Entry) i.next();
00565 out.write((String)e.getKey()+"="+(String)e.getValue());
00566 out.newLine();
00567 lineCount++;
00568 }
00569 out.close();
00570
00571 }catch(Exception e){System.err.println("SctGUI::ConfigurationInterface - Exception writing to "+lookupFile.getName()+" - "+e.toString());}
00572 }
00573
00574
00575
00576
00577
00578
00579
00580
00581 }