001package jmri.jmrix.rps;
002
003import java.io.File;
004import java.util.ArrayList;
005import java.util.List;
006
007/**
008 * Model holds RPS representation of layout geometry and logical structure.
009 * <p>
010 * The layout geometry is expressed as a set of (perhaps overlapping) Regions.
011 *
012 * @since 2.1.7
013 * @author Bob Jacobsen Copyright (C) 2008
014 */
015public class Model {
016
017    public Model() {
018    }
019
020    void loadValues() {
021        // load dummy contents
022        setInitialModel();
023    }
024
025    public void dispose() {
026    }
027
028    ArrayList<Region> regions = new ArrayList<Region>();
029
030    /**
031     * Include a region in the model.
032     * @param r region to add.
033     */
034    public void addRegion(Region r) {
035        regions.add(r);
036    }
037
038    /**
039     * Include a region in the model.
040     * @param r region to remove.
041     */
042    public void removeRegion(Region r) {
043        regions.remove(r);
044    }
045
046    /**
047     * Get the list of active regions.
048     * <p>
049     * This list should be immutable, hence copied, but for now it's not.
050     * @return list of regions.
051     */
052    public List<Region> getRegions() {
053        return regions;
054    }
055
056    // Store model info
057    public void storeModel(File file) { //throws org.jdom2.JDOMException, IOException {
058/*         PositionFile pf = new PositionFile(); */
059        /*         pf.prepare(); */
060        /*         pf.setConstants(getVSound(), getOffset()); */
061        /*          */
062        /*         for (int i = 1; i<=getReceiverCount(); i++) { */
063        /*             if (getReceiver(i) == null) continue; */
064        /*             pf.setReceiver(i, getReceiver(i)); */
065        /*         } */
066        /*         pf.store(file); */
067    }
068
069    public void loadModel(File file) { // throws org.jdom2.JDOMException, IOException {
070/*         // start by getting the file */
071        /*         PositionFile pf = new PositionFile(); */
072        /*         pf.loadFile(file); */
073        /*          */
074        /*         // get VSound */
075        /*         setVSound(pf.getVSound()); */
076        /*          */
077        /*         // get offset */
078        /*         setOffset(pf.getOffset()); */
079        /*          */
080        /*         // get receivers */
081        /*         setReceiverCount(pf.maxReceiver());  // count from 1 */
082        /*         Point3d p; */
083        /*         for (int i = 1; i<=getReceiverCount(); i++) {     */
084        /*             p = pf.getReceiverPosition(i); */
085        /*             if (p == null) continue; */
086        /*             log.debug("load "+i+" with "+p); */
087        /*             setReceiver(i, new Receiver(p)); */
088        /*         } */
089    }
090
091    void setInitialModel() {
092        /*         File defaultFile = new File(PositionFile.defaultFilename()); */
093        /*         try { */
094        /*             loadAlignment(defaultFile); */
095        /*         } catch (Exception e) { */
096        /*             log.debug("load exception"+e); */
097        /*             // load dummy values */
098        /*             setReceiverCount(2); */
099        /*             setReceiver(0, new Receiver(new Point3d(0.0,0.0,72.0))); */
100        /*             setReceiver(1, new Receiver(new Point3d(72.0,0.0,72.0))); */
101        /*         }                 */
102    }
103
104    // for now, we only allow one Model
105    static Model _instance = null;
106
107    static public Model instance() {
108        if (_instance == null) {
109            Model i = new Model();
110            i.loadValues();
111            // don't expose until initialized
112            _instance = i;
113        }
114        return _instance;
115    }
116
117}