Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Settings.java

Go to the documentation of this file.
00001 /*
00002  * Settings.java
00003  *
00004  * Copyright (C) 2005 Project SQUID, http://www.cs.helsinki.fi/group/squid/
00005  *
00006  * This file is part of Ikayaki.
00007  *
00008  * Ikayaki is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * Ikayaki is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with Ikayaki; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00021  */
00022 
00023 package ikayaki;
00024 
00025 import ikayaki.gui.SequenceColumn;
00026 import ikayaki.gui.StyledWrapper;
00027 import ikayaki.util.DocumentUtilities;
00028 import ikayaki.util.LastExecutor;
00029 import org.w3c.dom.Document;
00030 import org.w3c.dom.Element;
00031 import org.w3c.dom.NodeList;
00032 import org.xml.sax.SAXException;
00033 
00034 import javax.xml.parsers.DocumentBuilder;
00035 import javax.xml.parsers.DocumentBuilderFactory;
00036 import javax.xml.parsers.ParserConfigurationException;
00037 import java.awt.*;
00038 import java.io.*;
00039 import java.util.*;
00040 import java.util.List;
00041 
00047 public class Settings {
00048 
00049     private static final int DIRECTORY_HISTORY_SIZE = 30;
00050     private static final int PROJECT_HISTORY_SIZE = 10;
00051 
00052     private static final StyledWrapper defaultWrapper = new StyledWrapper();
00053     private static final StyledWrapper measuringWrapper = new StyledWrapper();
00054     private static final StyledWrapper doneRecentlyWrapper = new StyledWrapper();
00055 
00059     private static Properties properties = new Properties();
00060 
00064     private static File propertiesFile = Ikayaki.PROPERTIES_FILE;
00065 
00069     private static boolean propertiesModified = false;
00070 
00074     private static List<MeasurementSequence> sequences = new ArrayList<MeasurementSequence>();
00075 
00079     private static File sequencesFile = Ikayaki.SEQUENCES_FILE;
00080 
00084     private static boolean sequencesModified = false;
00085 
00089     private static List<File> directoryHistory = new LinkedList<File>();
00090 
00094     private static List<File> projectHistory = new LinkedList<File>();
00095 
00099     private static LastExecutor autosaveQueue = new LastExecutor(500, true);
00100 
00104     private static Runnable autosaveRunnable = new Runnable() {
00105         public void run() {
00106             saveNow();
00107         }
00108     };
00109 
00110     static {
00111 
00112         // ensure that the configuration files and directories exist
00113         if (!Ikayaki.CALIBRATION_PROJECT_DIR.exists()) {
00114             if (!Ikayaki.CALIBRATION_PROJECT_DIR.mkdir()) {
00115                 System.err.println("Unable to create directory: " + Ikayaki.CALIBRATION_PROJECT_DIR);
00116             }
00117         }
00118         if (!Ikayaki.CALIBRATION_PROJECT_DIR.isDirectory()) {
00119             System.err.println("No such directory: " + Ikayaki.CALIBRATION_PROJECT_DIR);
00120         }
00121 
00122         // load saved properties
00123         if (propertiesFile.exists()) {
00124             try {
00125                 InputStream in = new BufferedInputStream(new FileInputStream(propertiesFile));
00126                 properties.loadFromXML(in);
00127                 in.close();
00128             } catch (FileNotFoundException e) {
00129                 e.printStackTrace();
00130             } catch (InvalidPropertiesFormatException e) {
00131                 e.printStackTrace();
00132             } catch (IOException e) {
00133                 e.printStackTrace();
00134             }
00135         }
00136 
00137         // load saved sequences
00138         if (sequencesFile.exists()) {
00139             Document document = null;
00140             try {
00141                 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
00142                 document = builder.parse(sequencesFile);
00143             } catch (SAXException e) {
00144                 e.printStackTrace();
00145             } catch (ParserConfigurationException e) {
00146                 e.printStackTrace();
00147             } catch (IOException e) {
00148                 e.printStackTrace();
00149             }
00150             Element root = document.getDocumentElement();
00151             NodeList sequenceList = root.getElementsByTagName("sequence");
00152             for (int i = 0; i < sequenceList.getLength(); i++) {
00153                 sequences.add(new MeasurementSequence((Element) sequenceList.item(i)));
00154             }
00155         }
00156 
00157         // load custom properties
00158         loadDirectoryHistory();
00159         loadProjectHistory();
00160 
00161         // set background colors for the styled wrappers
00162         defaultWrapper.opaque = true;
00163         defaultWrapper.background = new Color(0xFFFFFF);
00164         defaultWrapper.selectedBackground = new Color(0xC3D4E8);
00165         defaultWrapper.focusBackground = new Color(0xFFFFFF);
00166         defaultWrapper.selectedFocusBackground = new Color(0xC3D4E8);
00167 
00168         measuringWrapper.opaque = true;
00169         measuringWrapper.background = new Color(0xEEBAEE);
00170         measuringWrapper.selectedBackground = new Color(0xFFCCFF);
00171         measuringWrapper.focusBackground = new Color(0xEEBAEE);
00172         measuringWrapper.selectedFocusBackground = new Color(0xFFCCFF);
00173 
00174         doneRecentlyWrapper.opaque = true;
00175 //        doneRecentlyWrapper.background = new Color(0xBAEEBA);
00176 //        doneRecentlyWrapper.selectedBackground = new Color(0xCCFFCC);
00177 //        doneRecentlyWrapper.focusBackground = new Color(0xBAEEBA);
00178 //        doneRecentlyWrapper.selectedFocusBackground = new Color(0xCCFFCC);
00179         doneRecentlyWrapper.background = new Color(0xE9FFE9);
00180         doneRecentlyWrapper.selectedBackground = new Color(0xBAEEBA);
00181         doneRecentlyWrapper.focusBackground = new Color(0xE9FFE9);
00182         doneRecentlyWrapper.selectedFocusBackground = new Color(0xBAEEBA);
00183     }
00184 
00188     public static synchronized void firePropertiesModified() {
00189         propertiesModified = true;
00190         save();
00191     }
00192 
00196     public static synchronized void fireSequencesModified() {
00197         sequencesModified = true;
00198         save();
00199     }
00200 
00205     public static synchronized void save() {
00206         autosaveQueue.execute(autosaveRunnable);
00207     }
00208 
00214     public static synchronized boolean saveNow() {
00215         boolean ok = true;
00216 
00217         // save properties to file
00218         if (propertiesModified) {
00219             try {
00220                 OutputStream out = new FileOutputStream(propertiesFile);
00221                 properties.storeToXML(out, null);
00222                 out.close();
00223                 propertiesModified = false;
00224             } catch (FileNotFoundException e) {
00225                 e.printStackTrace();
00226                 ok = false;
00227             } catch (IOException e) {
00228                 e.printStackTrace();
00229                 ok = false;
00230             }
00231         }
00232 
00233         // save sequences to file
00234         if (sequencesModified) {
00235             try {
00236                 Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
00237                 Element root = document.createElement("sequences");
00238                 for (MeasurementSequence sequence : sequences) {
00239                     root.appendChild(sequence.getElement(document));
00240                 }
00241                 document.appendChild(root);
00242 
00243                 if (DocumentUtilities.storeToXML(sequencesFile, document)) {
00244                     sequencesModified = false;
00245                 } else {
00246                     ok = false;
00247                 }
00248             } catch (ParserConfigurationException e) {
00249                 e.printStackTrace();
00250                 ok = false;
00251             }
00252         }
00253         return ok;
00254     }
00255 
00262     private static synchronized String getProperty(String key) {
00263         return properties.getProperty(key);
00264     }
00265 
00273     private static synchronized String getProperty(String key, String defaultValue) {
00274         return properties.getProperty(key, defaultValue);
00275     }
00276 
00284     private static synchronized void setProperty(String key, String value) {
00285         if (value == null) {
00286             properties.remove(key);
00287         } else {
00288             properties.setProperty(key, value);
00289         }
00290         firePropertiesModified();
00291     }
00292 
00298     public static synchronized Object getXXX() {
00299         return null;
00300     }
00301 
00307     public static synchronized boolean setXXX(Object value) {
00308         if (value == null) ; // just to avoid "value is never used" warnings ;)
00309         return false;
00310     }
00311 
00312     /* Serial ports */
00313 
00314     public static synchronized String getMagnetometerPort() {
00315         return getProperty("squid.magnetometer.port", "");
00316     }
00317 
00318     public static synchronized boolean setMagnetometerPort(String value) {
00319         setProperty("squid.magnetometer.port", value);
00320         return true;
00321     }
00322 
00323     public static synchronized String getHandlerPort() {
00324         return getProperty("squid.handler.port", "");
00325     }
00326 
00327     public static synchronized boolean setHandlerPort(String value) {
00328         setProperty("squid.handler.port", value);
00329         return true;
00330     }
00331 
00332     public static synchronized String getDegausserPort() {
00333         return getProperty("squid.degausser.port", "");
00334     }
00335 
00336     public static synchronized boolean setDegausserPort(String value) {
00337         setProperty("squid.degausser.port", value);
00338         return true;
00339     }
00340 
00341     /* Magnetometer */
00342 
00343     public static synchronized double getMagnetometerXAxisCalibration() {
00344         return Double.parseDouble(getProperty("squid.magnetometer.xaxiscalibration", "0.0"));
00345     }
00346 
00347     public static synchronized boolean setMagnetometerXAxisCalibration(double value) {
00348         setProperty("squid.magnetometer.xaxiscalibration", Double.toString(value));
00349         return true;
00350     }
00351 
00352     public static synchronized double getMagnetometerYAxisCalibration() {
00353         return Double.parseDouble(getProperty("squid.magnetometer.yaxiscalibration", "0.0"));
00354     }
00355 
00356     public static synchronized boolean setMagnetometerYAxisCalibration(double value) {
00357         setProperty("squid.magnetometer.yaxiscalibration", Double.toString(value));
00358         return true;
00359     }
00360 
00361     public static synchronized double getMagnetometerZAxisCalibration() {
00362         return Double.parseDouble(getProperty("squid.magnetometer.zaxiscalibration", "0.0"));
00363     }
00364 
00365     public static synchronized boolean setMagnetometerZAxisCalibration(double value) {
00366         setProperty("squid.magnetometer.zaxiscalibration", Double.toString(value));
00367         return true;
00368     }
00369 
00370     /* Degausser */
00371 
00372     public static synchronized int getDegausserRamp() {
00373         return Integer.parseInt(getProperty("squid.degausser.ramp", "0"));
00374     }
00375 
00376     public static synchronized boolean setDegausserRamp(int value) {
00377         if (value == 3 || value == 5 || value == 7 || value == 9) {
00378             setProperty("squid.degausser.ramp", Integer.toString(value));
00379             return true;
00380         } else {
00381             return false;
00382         }
00383     }
00384 
00385     public static synchronized int getDegausserDelay() {
00386         return Integer.parseInt(getProperty("squid.degausser.delay", "0"));
00387     }
00388 
00389     public static synchronized boolean setDegausserDelay(int value) {
00390         if (value > 0 && value < 10) {
00391             setProperty("squid.degausser.delay", Integer.toString(value));
00392             return true;
00393         } else {
00394             return false;
00395         }
00396     }
00397 
00398     public static synchronized boolean setDegausserMaximumField(double value) {
00399         if (value >= 0.0 && value <= 300.0) {
00400             setProperty("squid.degausser.maximumfield", Double.toString(value));
00401             return true;
00402         } else {
00403             return false;
00404         }
00405     }
00406 
00407     public static synchronized double getDegausserMaximumField() {
00408         return Double.parseDouble(getProperty("squid.degausser.maximumfield", "0.0"));
00409     }
00410 
00411     public static synchronized double getDegausserMinimumField() {
00412         return 1.1;
00413     }
00414 
00415     public static synchronized double getDegausserMinimumFieldIncrement() {
00416         return 0.1;
00417     }
00418 
00419     /* Sample handler */
00420 
00421     public static synchronized int getHandlerAcceleration() {
00422         return Integer.parseInt(getProperty("squid.handler.acceleration", "0"));
00423     }
00424 
00425     public static synchronized boolean setHandlerAcceleration(int value) {
00426         if (value >= 0 && value <= 127) {
00427             setProperty("squid.handler.acceleration", Integer.toString(value));
00428             return true;
00429         } else {
00430             return false;
00431         }
00432     }
00433 
00434     public static synchronized int getHandlerDeceleration() {
00435         return Integer.parseInt(getProperty("squid.handler.deceleration", "0"));
00436     }
00437 
00438     public static synchronized boolean setHandlerDeceleration(int value) {
00439         if (value >= 0 && value <= 127) {
00440             setProperty("squid.handler.deceleration", Integer.toString(value));
00441             return true;
00442         } else {
00443             return false;
00444         }
00445     }
00446 
00447     public static synchronized int getHandlerVelocity() {
00448         return Integer.parseInt(getProperty("squid.handler.velocity", "0"));
00449     }
00450 
00451     public static synchronized boolean setHandlerVelocity(int value) {
00452         if (value >= 50 && value <= 8500) {
00453             setProperty("squid.handler.velocity", Integer.toString(value));
00454             return true;
00455         } else {
00456             return false;
00457         }
00458     }
00459 
00460     public static synchronized int getHandlerMeasurementVelocity() {
00461         return Integer.parseInt(getProperty("squid.handler.measurementvelocity", "0"));
00462     }
00463 
00464     public static synchronized boolean setHandlerMeasurementVelocity(int value) {
00465         if (value >= 50 && value <= 8500) {
00466             setProperty("squid.handler.measurementvelocity", Integer.toString(value));
00467             return true;
00468         } else {
00469             return false;
00470         }
00471     }
00472 
00473     public static synchronized int getHandlerRotationVelocity() {
00474         return Integer.parseInt(getProperty("squid.handler.rotationvelocity", "50"));
00475     }
00476 
00477     public static synchronized boolean setHandlerRotationVelocity(int value) {
00478         if (value >= 50 && value <= 8500) {
00479             setProperty("squid.handler.rotationvelocity", Integer.toString(value));
00480             return true;
00481         } else {
00482             return false;
00483         }
00484     }
00485 
00486     public static synchronized int getHandlerRotationAcceleration() {
00487         return Integer.parseInt(getProperty("squid.handler.rotationacceleration", "0"));
00488     }
00489 
00490     public static synchronized boolean setHandlerRotationAcceleration(int value) {
00491         if (value >= 0 && value <= 127) {
00492             setProperty("squid.handler.rotationacceleration", Integer.toString(value));
00493             return true;
00494         } else {
00495             return false;
00496         }
00497     }
00498 
00499     public static synchronized int getHandlerRotationDeceleration() {
00500         return Integer.parseInt(getProperty("squid.handler.rotationdeceleration", "0"));
00501     }
00502 
00503     public static synchronized boolean setHandlerRotationDeceleration(int value) {
00504         if (value >= 50 && value <= 2000) {
00505             setProperty("squid.handler.rotationdeceleration", Integer.toString(value));
00506             return true;
00507         } else {
00508             return false;
00509         }
00510     }
00511 
00512     public static synchronized int getHandlerTransverseYAFPosition() {
00513         return Integer.parseInt(getProperty("squid.handler.pos.transverseyaf", "0"));
00514     }
00515 
00516     public static synchronized boolean setHandlerTransverseYAFPosition(int value) {
00517         if (value >= 1 && value <= 16777215) {
00518             setProperty("squid.handler.pos.transverseyaf", Integer.toString(value));
00519             return true;
00520         } else {
00521             return false;
00522         }
00523     }
00524 
00525     public static synchronized int getHandlerAxialAFPosition() {
00526         return Integer.parseInt(getProperty("squid.handler.pos.axialaf", "0"));
00527     }
00528 
00529     public static synchronized boolean setHandlerAxialAFPosition(int value) {
00530         if (value >= 1 && value <= 16777215) {
00531             setProperty("squid.handler.pos.axialaf", Integer.toString(value));
00532             return true;
00533         } else {
00534             return false;
00535         }
00536     }
00537 
00538     public static synchronized int getHandlerSampleLoadPosition() {
00539         return Integer.parseInt(getProperty("squid.handler.pos.sampleload", "0"));
00540     }
00541 
00542     public static synchronized boolean setHandlerSampleLoadPosition(int value) {
00543         if (value >= 0 && value <= 16777215) {
00544             setProperty("squid.handler.pos.sampleload", Integer.toString(value));
00545             return true;
00546         } else {
00547             return false;
00548         }
00549     }
00550 
00551     public static synchronized int getHandlerBackgroundPosition() {
00552         return Integer.parseInt(getProperty("squid.handler.pos.background", "0"));
00553     }
00554 
00555     public static synchronized boolean setHandlerBackgroundPosition(int value) {
00556         if (value >= 1 && value <= 16777215) {
00557             setProperty("squid.handler.pos.background", Integer.toString(value));
00558             return true;
00559         } else {
00560             return false;
00561         }
00562     }
00563 
00564     public static synchronized int getHandlerMeasurementPosition() {
00565         return Integer.parseInt(getProperty("squid.handler.pos.measurement", "0"));
00566     }
00567 
00568     public static synchronized boolean setHandlerMeasurementPosition(int value) {
00569         if (value >= 1 && value <= 16777215) {
00570             setProperty("squid.handler.pos.measurement", Integer.toString(value));
00571             return true;
00572         } else {
00573             return false;
00574         }
00575     }
00576 
00577     public static synchronized int getHandlerRightLimit() {
00578         return Integer.parseInt(getProperty("squid.handler.pos.rightlimit", "0"));
00579     }
00580 
00581     public static synchronized boolean setHandlerRightLimit(int value) {
00582         if (value == 0 || value == 1) {
00583             setProperty("squid.handler.pos.rightlimit", Integer.toString(value));
00584             return true;
00585         } else {
00586             return false;
00587         }
00588     }
00589 
00590     public static synchronized int getHandlerRotation() {
00591         return Integer.parseInt(getProperty("squid.handler.rotation", "0"));
00592     }
00593 
00594     public static synchronized boolean setHandlerRotation(int value) {
00595         setProperty("squid.handler.rotation", Integer.toString(value));
00596         return true;
00597     }
00598 
00599     /* Other settings for measurements */
00600 
00604     public static synchronized int getMeasurementRotations() {
00605         return Integer.parseInt(getProperty("measurement.rotations", "1"));
00606     }
00607 
00608     public static synchronized boolean setMeasurementRotations(int value) {
00609         if (value >= 0) {
00610             setProperty("measurement.rotations", Integer.toString(value));
00611             return true;
00612         } else {
00613             return false;
00614         }
00615     }
00616 
00617     /* Program window */
00618 
00619     public static synchronized int getWindowWidth() {
00620         int i = Integer.parseInt(getProperty("gui.window.width", "1000"));
00621         Rectangle maxBounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
00622         if (i < 400) {
00623             i = 400;
00624         } else if (i > maxBounds.width) {
00625             i = maxBounds.width;
00626         }
00627         return i;
00628     }
00629 
00630     public static synchronized boolean setWindowWidth(int value) {
00631         setProperty("gui.window.width", Integer.toString(value));
00632         return true;
00633     }
00634 
00635     public static synchronized int getWindowHeight() {
00636         int i = Integer.parseInt(getProperty("gui.window.height", "700"));
00637         Rectangle maxBounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
00638         if (i < 300) {
00639             i = 300;
00640         } else if (i > maxBounds.height) {
00641             i = maxBounds.height;
00642         }
00643         return i;
00644     }
00645 
00646     public static synchronized boolean setWindowHeight(int value) {
00647         setProperty("gui.window.height", Integer.toString(value));
00648         return true;
00649     }
00650 
00651     public static synchronized boolean getWindowMaximized() {
00652         return Boolean.parseBoolean(getProperty("gui.window.maximized", "false"));
00653     }
00654 
00655     public static synchronized boolean setWindowMaximized(boolean value) {
00656         setProperty("gui.window.maximized", Boolean.toString(value));
00657         return true;
00658     }
00659 
00660     /* Directory history */
00661 
00662     public static synchronized File getLastDirectory() {
00663         File[] dirs = getDirectoryHistory();
00664         if (dirs.length > 0) {
00665             return dirs[0];
00666         } else {
00667             return new File("").getAbsoluteFile();
00668         }
00669     }
00670 
00671     public static synchronized File[] getDirectoryHistory() {
00672         return directoryHistory.toArray(new File[directoryHistory.size()]);
00673     }
00674 
00675     public static synchronized boolean updateDirectoryHistory(File visited) {
00676         if (!visited.isAbsolute()) {
00677             visited = visited.getAbsoluteFile();
00678         }
00679 
00680         // update history list
00681         while (directoryHistory.remove(visited)) ;
00682         directoryHistory.add(0, visited);
00683         while (directoryHistory.size() > DIRECTORY_HISTORY_SIZE) {
00684             directoryHistory.remove(directoryHistory.size() - 1);
00685         }
00686 
00687         // save as properties
00688         for (int i = 0; i < Math.max(directoryHistory.size(), DIRECTORY_HISTORY_SIZE); i++) {
00689             if (i < directoryHistory.size()) {
00690                 setProperty("history.dir." + i, directoryHistory.get(i).getAbsolutePath());
00691             } else {
00692                 setProperty("history.dir." + i, null);
00693             }
00694         }
00695         return true;
00696     }
00697 
00698     private static synchronized void loadDirectoryHistory() {
00699         // reset history list
00700         directoryHistory.clear();
00701 
00702         // load from properties
00703         int i = 0;
00704         while (true) {
00705             String s = getProperty("history.dir." + i);
00706             if (s == null) {
00707                 break;
00708             } else {
00709                 File file = new File(s);
00710                 if (file.isDirectory()) {
00711                     directoryHistory.add(file);
00712                 }
00713             }
00714             i++;
00715         }
00716     }
00717 
00718     /* Project history */
00719 
00720     public static synchronized File[] getProjectHistory() {
00721         return projectHistory.toArray(new File[projectHistory.size()]);
00722     }
00723 
00724     public static synchronized boolean updateProjectHistory(File visited) {
00725         if (visited == null) {
00726             return false;
00727         }
00728         if (!visited.isAbsolute()) {
00729             visited = visited.getAbsoluteFile();
00730         }
00731 
00732         // update history list
00733         while (projectHistory.remove(visited)) ;
00734         projectHistory.add(0, visited);
00735         while (projectHistory.size() > PROJECT_HISTORY_SIZE) {
00736             projectHistory.remove(projectHistory.size() - 1);
00737         }
00738 
00739         // save as properties
00740         for (int i = 0; i < Math.max(projectHistory.size(), PROJECT_HISTORY_SIZE); i++) {
00741             if (i < projectHistory.size()) {
00742                 setProperty("history.project." + i, projectHistory.get(i).getAbsolutePath());
00743             } else {
00744                 setProperty("history.project." + i, null);
00745             }
00746         }
00747         return true;
00748     }
00749 
00750     private static synchronized void loadProjectHistory() {
00751         // reset history list
00752         projectHistory.clear();
00753 
00754         // load from properties
00755         int i = 0;
00756         while (true) {
00757             String s = getProperty("history.project." + i);
00758             if (s == null) {
00759                 break;
00760             } else {
00761                 File file = new File(s);
00762                 if (file.isFile()) {
00763                     projectHistory.add(file);
00764                 }
00765             }
00766             i++;
00767         }
00768     }
00769 
00770     /* Saves sequences */
00771 
00775     public static synchronized MeasurementSequence[] getSequences() {
00776         MeasurementSequence[] s = sequences.toArray(new MeasurementSequence[sequences.size()]);
00777         Arrays.sort(s);
00778         return s;
00779     }
00780 
00784     public static synchronized void addSequence(MeasurementSequence sequence) {
00785         if (sequence != null && !sequences.contains(sequence)) {
00786             sequences.add(sequence);
00787             fireSequencesModified();
00788         }
00789     }
00790 
00794     public static synchronized void removeSequence(MeasurementSequence sequence) {
00795         if (sequence != null) {
00796             sequences.remove(sequence);
00797             fireSequencesModified();
00798         }
00799     }
00800 
00801     /* Sequence table settings */
00802 
00803     public static synchronized List<SequenceColumn> getDefaultColumns() {
00804         String s = getProperty("sequence.defaultcolumns",
00805                 "COUNT,STEP,DECLINATION,INCLINATION,RELATIVE_MAGNETIZATION,THETA63," +
00806                 "SAMPLE_X_NORMALIZED,SAMPLE_Y_NORMALIZED,SAMPLE_Z_NORMALIZED");
00807         String[] columnNames = s.split(",");
00808         List<SequenceColumn> columns = new ArrayList<SequenceColumn>(columnNames.length);
00809         for (String name : columnNames) {
00810             try {
00811                 columns.add(SequenceColumn.valueOf(name));
00812             } catch (IllegalArgumentException e) {
00813                 e.printStackTrace();
00814             }
00815         }
00816         return columns;
00817     }
00818 
00819     public static synchronized void setDefaultColumn(SequenceColumn column, boolean enabled) {
00820         if (column == null) {
00821             throw new NullPointerException();
00822         }
00823         List<SequenceColumn> columns = getDefaultColumns();
00824         columns.remove(column);
00825         if (enabled) {
00826             columns.add(column);
00827         }
00828 
00829         String s = "";
00830         for (SequenceColumn c : columns) {
00831             if (s.length() > 0) {
00832                 s += ",";
00833             }
00834             s += c.name();
00835         }
00836         setProperty("sequence.defaultcolumns", s);
00837     }
00838 
00839     /* Calibration projects */
00840 
00841     public static synchronized File[] getCalibrationProjectFiles() {
00842         File[] files = Ikayaki.CALIBRATION_PROJECT_DIR.listFiles(new FileFilter() {
00843             public boolean accept(File file) {
00844                 return file.isFile()
00845                         && file.getName().endsWith(Ikayaki.FILE_TYPE)
00846                         && Project.getType(file) == Project.Type.CALIBRATION;
00847             }
00848         });
00849         if (files == null) {
00850             return new File[0];
00851         } else {
00852             Arrays.sort(files);
00853             return files;
00854         }
00855     }
00856 
00857     public static synchronized File getHolderCalibrationFile() {
00858         String s = getProperty("calibration.holder");
00859         if (s == null) {
00860             return null;
00861         }
00862         return new File(s).getAbsoluteFile();
00863     }
00864 
00865     public static synchronized void setHolderCalibrationFile(File file) {
00866         if (file == null) {
00867             setProperty("calibration.holder", null);
00868             return;
00869         }
00870         if (!file.exists()) {
00871             throw new IllegalArgumentException("Does not exist: " + file);
00872         }
00873         File[] files = getCalibrationProjectFiles();
00874         for (File f : files) {
00875             if (f.equals(file)) {
00876                 setProperty("calibration.holder", file.getAbsolutePath());
00877                 return;
00878             }
00879         }
00880         throw new IllegalArgumentException("Not a calibration project: " + file);
00881     }
00882 
00883     public static MeasurementResult getHolderCalibration() {
00884         /* NOTE:
00885          * Making this method synchronized causes sometimes deadlock
00886          * when this method tries to aquire a lock on Project class,
00887          * while another tread is loading a project file and tries to
00888          * aquire a lock on Settings class in Project.isHolderCalibration().
00889          */
00890 
00891         File file = getHolderCalibrationFile();
00892         if (file == null) {
00893             return null;
00894         }
00895         Project project = Project.loadProject(file);
00896         if (project == null || !project.isHolderCalibration()) {
00897             return null;
00898         }
00899 
00900         int index = project.getCompletedSteps() - 1;
00901         if (index < 0) {
00902             return null;
00903         }
00904         Double x = project.getValue(index, MeasurementValue.SAMPLE_X);
00905         Double y = project.getValue(index, MeasurementValue.SAMPLE_Y);
00906         Double z = project.getValue(index, MeasurementValue.SAMPLE_Z);
00907         if (x == null || y == null || z == null) {
00908             return null;
00909         }
00910         return new MeasurementResult(MeasurementResult.Type.HOLDER, 0, x, y, z);
00911     }
00912 
00913     /* JTable styles */
00914 
00918     public static StyledWrapper getDefaultWrapperInstance() {
00919         return (StyledWrapper) defaultWrapper.clone();
00920     }
00921 
00925     public static StyledWrapper getMeasuringWrapperInstance() {
00926         return (StyledWrapper) measuringWrapper.clone();
00927     }
00928 
00932     public static StyledWrapper getDoneRecentlyWrapperInstance() {
00933         return (StyledWrapper) doneRecentlyWrapper.clone();
00934     }
00935 }

Generated on Fri May 6 16:00:33 2005 for Squid by  doxygen 1.4.1