001/*
002 * Copyright (c) 2011-2012 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2012-05-09 11:05:40 -0700 (Wed, 09 May 2012) $' 
007 * '$Revision: 29823 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.gui;
031
032import java.util.ArrayList;
033import java.util.Collections;
034import java.util.HashMap;
035import java.util.List;
036import java.util.Map;
037
038import org.apache.commons.logging.Log;
039import org.apache.commons.logging.LogFactory;
040
041import ptolemy.kernel.util.NamedObj;
042
043/**
044 * This singleton class holds a map of all KeplerGraphFrame objects and their
045 * associated NamedObj models.
046 * 
047 * @author Aaron Schultz
048 */
049public class ModelToFrameManager {
050
051    // FIXME: This seems to be a duplicate of the Configuration? -cxh
052
053        private static final Log log = LogFactory.getLog(ModelToFrameManager.class
054                        .getName());
055        private static final boolean isDebugging = log.isDebugEnabled();
056
057        private Map<NamedObj, KeplerGraphFrame> modelToFrameMap;
058
059        /**
060         * Empty constructor
061         */
062        public ModelToFrameManager() {
063                HashMap<NamedObj, KeplerGraphFrame> hashMap = new HashMap<NamedObj, KeplerGraphFrame>();
064                modelToFrameMap = Collections.synchronizedMap(hashMap);
065                if (isDebugging) {
066                        log.debug("Constructing ModelToFrameManager");
067                }
068        }
069        
070        /** Get the frame for a model. */
071        public KeplerGraphFrame getFrame(NamedObj model) {
072            return modelToFrameMap.get(model);
073        }
074
075        /**
076         * Map a NamedObj to a KeplerGraphFrame.
077         * 
078         * @param obj
079         * @param frame
080         */
081        public void add(NamedObj obj, KeplerGraphFrame frame) {
082                if (obj != null && frame != null) {
083                        modelToFrameMap.put(obj, frame);
084                        if (isDebugging) {
085                                log.debug("mapping NamedObj: " + obj.getName()
086                                                + " to KeplerGraphFrame: " + frame.getTitle());
087                        }
088                }
089        }
090
091        /**
092         * Remove all mappings to the given KeplerGraphFrame.
093         * 
094         * @param frame
095         */
096        public void remove(KeplerGraphFrame frame) {
097
098                List<NamedObj> removeList = new ArrayList<NamedObj>();
099
100                for (NamedObj obj : modelToFrameMap.keySet()) {
101                        KeplerGraphFrame kgf = modelToFrameMap.get(obj);
102                        if (kgf == frame) {
103                                removeList.add(obj);
104                        }
105                }
106                for (NamedObj removeObj : removeList) {
107                        modelToFrameMap.remove(removeObj);
108                        if (isDebugging) {
109                                log.debug("removing KeplerGraphFrame mapping of obj:"
110                                                + removeObj.getName());
111                        }
112                }
113        }
114        
115        /** Remove a model from the mapping. */
116        public void removeModel(NamedObj model) {
117            modelToFrameMap.remove(model);
118        }
119
120        /** Return the number of open frames. 
121         *  XXX this is incorrect since multiple models may be
122         *  contained in the same frame.
123         */
124        public int numberOfOpenFrames() {
125            return modelToFrameMap.size();
126        }
127        
128        /**
129         * Print the map for debugging purposes.
130         */
131        public void printDebugInfo() {
132                System.out.println("ModelToFrameManager.printDebugInfo()");
133                for (NamedObj key : modelToFrameMap.keySet()) {
134                        KeplerGraphFrame kgf = modelToFrameMap.get(key);
135                        System.out.println(key.getName() + " : " + kgf.getTitle());
136                }
137        }
138
139        /**
140         * Method for getting an instance of this singleton class.
141         */
142        public static ModelToFrameManager getInstance() {
143                return ModelToFrameManagerHolder.INSTANCE;
144        }
145
146        private static class ModelToFrameManagerHolder {
147                private static final ModelToFrameManager INSTANCE = new ModelToFrameManager();
148        }
149}