Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

DaveUtils.java

00001 package guiUtilities;
00002 import  java.io.*;
00003 import java.util.*;
00004 import java.util.regex.*;
00005 
00006 public class DaveUtils {
00007        
00008 
00009         public static String padString(String theString, int theLength) {
00010         int i, numberOfPads, reqLength;
00011         reqLength = theLength;
00012         if(reqLength < 0) reqLength *= -1;
00013         if(theString.length() >= reqLength) return theString;
00014         StringBuffer newString = new StringBuffer(theString);
00015         numberOfPads = reqLength - theString.length();
00016         if(theLength > 0) {
00017                 for(i=0;i<numberOfPads;i++) newString.insert(0," ");
00018                 }
00019         else {
00020                 for(i=0;i<numberOfPads;i++) newString.append(" ");
00021                 }
00022         return newString.toString();
00023         }
00024         public static String padString(String theString, int theLength, char c) {
00025         int i, numberOfPads, reqLength;
00026         reqLength = theLength;
00027         if(reqLength < 0) reqLength *= -1;
00028         if(theString.length() >= reqLength) return theString;
00029         StringBuffer newString = new StringBuffer(theString);
00030         numberOfPads = reqLength - theString.length();
00031         if(theLength > 0) {
00032                 for(i=0;i<numberOfPads;i++) newString.insert(0,c);
00033                 }
00034         else {
00035                 for(i=0;i<numberOfPads;i++) newString.append(c);
00036                 }
00037         return newString.toString();
00038         }
00039 
00040         public static void copyFile(String fileName, String targetFileName) throws IOException {
00041                 String lineString;
00042 
00043                         FileReader fr = new FileReader(fileName);
00044                         FileWriter fw = new FileWriter(targetFileName);
00045                         BufferedReader in = new BufferedReader(fr);
00046                         BufferedWriter out = new BufferedWriter(fw);
00047                         while( (lineString = in.readLine()) != null) {
00048                                 out.write(lineString,0,lineString.length());
00049                                 out.newLine();
00050                                 }
00051                         in.close();
00052                         out.close();
00053                 }
00054     public static String extractDate(String dbDate) {
00055        String[] monthnames = {"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"};
00056        int theMonth=0;
00057        if(dbDate.length() < 12) return dbDate;
00058        String day = dbDate.substring(8,10);
00059        String month = dbDate.substring(5,7);
00060        try {
00061          theMonth = Integer.parseInt(month);
00062         }
00063        catch (Exception e) {theMonth=1;System.out.println("Cant extract monthname!!");}
00064        String year = dbDate.substring(2,4);
00065        month = (theMonth>0 && theMonth<13) ? monthnames[theMonth-1] : "???"; 
00066        return day+"-"+month+"-"+year;
00067        }
00068 
00069     public static String extractSCTDAQDate(String dbDate) {
00070        String[] monthnames = {"01","02","03","04","05","06","07","08","09","10","11","12"};
00071        int theMonth=0;
00072        if(dbDate.length() < 12) return dbDate;
00073        String day = dbDate.substring(8,10);
00074        String month = dbDate.substring(5,7);
00075        try {
00076          theMonth = Integer.parseInt(month);
00077         }
00078        catch (Exception e) {theMonth=0;System.out.println("Cant extract monthname!!");}
00079        String year = dbDate.substring(0,4);
00080        month = (theMonth>0 && theMonth<13) ? monthnames[theMonth-1] : "???"; 
00081        return day+"/"+month+"/"+year;
00082        }
00083 
00084 
00085 public static void writeTXT(File file,javax.swing.table.AbstractTableModel db) {
00086 // write a spreadhseet file as text with semicolon as a delimiter
00087      try {
00088           FileWriter fw = new FileWriter(file);
00089           BufferedWriter out = new BufferedWriter(fw);
00090 
00091           int numRows = db.getRowCount();
00092           int numCols = db.getColumnCount();
00093           String line="";
00094           for(int i=0;i<numCols;i++) {
00095                              String title = db.getColumnName(i);
00096                              line += (i==0) ? db.getColumnName(i) : "\t"+db.getColumnName(i);
00097                              }
00098           out.write(line,0,line.length());
00099           out.newLine();
00100                          
00101           for (int i=0; i < numRows; i++) {
00102               line="";
00103               for (int j=0; j < numCols; j++) {
00104               
00105 //                             Vector theRow = (Vector)db.rows.elementAt(i);
00106                      String content = (String)db.getValueAt(i,j);
00107                      line += (j==0) ? content : "\t"+content;                              
00108                      }
00109                out.write(line,0,line.length());
00110                out.newLine();
00111                }
00112           out.close();
00113           System.out.println("Saved file tab-delimited text file "+file.getAbsolutePath());
00114       } catch(IOException e) {System.out.println("Error creating file "+file.getAbsolutePath());}
00115   }
00116    public static double getCorrectedCurrent(double uncorrectedCurrent, double temperature) {
00117 
00118             double tk = temperature+273.;
00119 // get correction factor for conversion to 20C
00120             double theExponent = -7019.*(temperature-20.0)/293./tk;
00121             double corrFactor = Math.exp(theExponent)*(293./tk)*(293./tk);
00122             return uncorrectedCurrent*corrFactor;
00123             }
00124 
00125 public static String getStyleSheet2() {
00126  StringBuffer sheet = new StringBuffer();
00127  sheet.append("<STYLE TYPE=\"text/css\">\n");
00128  sheet.append("<!--\n");
00129  sheet.append("p {font-family: Geneva,Arial,sans-serif; font-size: 10pt }\n");
00130  sheet.append("ul {font-family: Geneva,Arial,sans-serif; font-size: 10pt }\n");
00131  sheet.append("li {font-family: Geneva,Arial,sans-serif; font-size: 10pt }\n");
00132  sheet.append("dd {font-family: Geneva,Arial,sans-serif; font-size: 10pt }\n");
00133  sheet.append("table {font-family: Geneva,Arial,sans-serif; font-size: 10pt }\n");
00134  sheet.append("td {font-family: Geneva,Arial,sans-serif; font-size: 10pt }\n");
00135  sheet.append("td.bold {font-family: Geneva,Arial,sans-serif; font-size: 10pt; font-weight: bold }\n");
00136  sheet.append("h1 {font-family: Arial, sans-serif; font-size: 16pt ; color: navy }\n");
00137  sheet.append("h2 {font-family: Arial, sans-serif; font-size: 14pt ; color: navy }\n");
00138  sheet.append("h3 {font-family: Arial, sans-serif; font-size: 12pt ; color: navy }\n");
00139  sheet.append("b {font-family: Arial, sans-serif; font-size: 10pt; font-weight: bold}\n");
00140  sheet.append(".bold {font-weight: bold}\n");
00141  sheet.append(".boldred {font-weight: bold; color: red }\n");
00142  sheet.append(".boldgreen {font-weight: bold; color: green}\n");
00143  sheet.append(".boldpurple {font-weight: bold; color: purple }\n");
00144  sheet.append("-->\n");
00145  sheet.append("</STYLE>\n");
00146  return sheet.toString();
00147 }
00148 public static String getStyleSheet() {
00149  StringBuffer sheet = new StringBuffer();
00150  sheet.append("<STYLE TYPE=\"text/css\">\n");
00151  sheet.append("<!--\n");
00152  sheet.append("FONT             {FONT-FAMILY: Tahoma, Verdana, Helvetica; FONT-SIZE: 10pt}\n");
00153  sheet.append("TD               {FONT-FAMILY: Tahoma, Verdana, Helvetica; FONT-SIZE: 10pt}\n");
00154  sheet.append("BODY             {FONT-FAMILY: Tahoma, Verdana, Helvetica; FONT-SIZE: 10pt}\n");
00155  sheet.append("P                {FONT-FAMILY: Tahoma, Verdana, Helvetica; FONT-SIZE: 10pt}\n");
00156  sheet.append("DIV              {FONT-FAMILY: Tahoma, VerdanaHelvetica; FONT-SIZE: 10pt}\n");
00157  sheet.append("INPUT            {FONT-FAMILY: Tahoma, Verdana, Helvetica; FONT-SIZE: 10pt}\n");
00158  sheet.append("TEXTAREA {FONT-FAMILY: Tahoma, Verdana, Helvetica; FONT-SIZE: 10pt}\n");
00159  sheet.append("FORM             {FONT-FAMILY: Tahoma, Verdana, Helvetica; FONT-SIZE: 10pt}\n");
00160  sheet.append("SELECT           {FONT-FAMILY: Tahoma, Verdana, Helvetica; FONT-SIZE: 10pt}\n");
00161  sheet.append("A:link          {BACKGROUND: none; COLOR: #215DC6; FONT-SIZE: 10pt; FONT-FAMILY: Tahoma, Verdana, Helvetica; TEXT-DECORATION: none}\n");
00162  sheet.append("A:active        {BACKGROUND: none; COLOR: #215DC6; FONT-SIZE: 10pt; FONT-FAMILY: Tahoma, Verdana, Helvetica; TEXT-DECORATION: underline}\n");
00163  sheet.append("A:visited       {BACKGROUND: none; COLOR: #215DC6; FONT-SIZE: 10pt; FONT-FAMILY: Tahoma, Verdana, Helvetica; TEXT-DECORATION: none}\n");
00164  sheet.append("A:hover         {BACKGROUND: #215DC6; COLOR: #FFFFFF; FONT-SIZE: 10pt; FONT-FAMILY: Tahoma, Verdana, Helvetica; TEXT-DECORATION: none}\n");
00165  sheet.append("-->\n");
00166  sheet.append("</STYLE>\n");
00167  return sheet.toString();
00168 }
00169  public static String getHTMLHeader(String title) {
00170      StringBuffer html = new StringBuffer("<html><head>");
00171      html.append(guiUtilities.DaveUtils.getStyleSheet());
00172      html.append("<title>title</title>");
00173      html.append("</head><body>\n");
00174      html.append("<h2>"+title+"</h2>\n");
00175      return html.toString();
00176      }
00177  public static String getHTMLFooter() {
00178      return "</body></html>";
00179      }
00180 
00181   public static String getTodaysDate() {
00182         int number;
00183         Date today = new Date();
00184         java.text.DateFormat formatter = java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT,new Locale("en","GB"));
00185         String dateString = formatter.format(today);
00186         Pattern datePattern = Pattern.compile("(\\d+/\\d+/)(\\d+)");
00187         Matcher matcher = datePattern.matcher(dateString);
00188         if(matcher.matches()) dateString = dateString.substring(matcher.start(1),matcher.end(1))+"20"+dateString.substring(matcher.start(2),matcher.end(2));
00189         System.out.println("todays date is "+dateString);
00190         return dateString;
00191         }
00192 
00193   public static String getCurrentTime() {
00194         try {
00195           Date today = new Date();
00196           java.text.DateFormat formatter = java.text.DateFormat.getTimeInstance(java.text.DateFormat.DEFAULT,new Locale("en","GB"));
00197           return formatter.format(today);
00198           }catch(Exception e){}
00199         return "Unknown";
00200         }
00201  
00202 
00203 }
00204 

Generated on Thu Jul 15 09:55:41 2004 for SCT DAQ/DCS Software - Java by doxygen 1.3.5