root/trunk/src/swing/org/pathvisio/gex/GexManager.java @ 3128

Revision 3128, 5.6 KB (checked in by martijn, 6 months ago)

Updated to new org.bridgedb.rdb.construct pacakge

  • Property svn:eol-style set to native
Line 
1// PathVisio,
2// a tool for data visualization and analysis using Biological Pathways
3// Copyright 2006-2009 BiGCaT Bioinformatics
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
17package org.pathvisio.gex;
18
19import java.util.EventObject;
20import java.util.HashSet;
21import java.util.Set;
22
23import org.bridgedb.IDMapperException;
24import org.bridgedb.rdb.construct.DBConnector;
25import org.pathvisio.debug.Logger;
26import org.pathvisio.preferences.GlobalPreference;
27import org.pathvisio.preferences.PreferenceManager;
28
29/**
30 * Manage the centralized SimpleGex
31 *
32 * Use one of the two setCurrentGex methods
33 * to connect to a new Gex database.
34 *
35 * Register for GexManagerEvent if you want to be notified
36 * when a Gex is connected or closed.
37 *
38 */
39public class GexManager
40{
41        private static GexManager gexManager = new GexManager();
42
43        @Deprecated
44        public static GexManager getCurrent()
45        {
46                return gexManager;
47        }
48
49        private SimpleGex currentGex = null;
50        public SimpleGex getCurrentGex() { return currentGex; }
51
52        private CachedData cachedData = null;
53        public CachedData getCachedData() { return cachedData; }
54
55        /**
56         * Returns true if the current gex is initialized
57         * (non-null), and if it is connected.
58         * If it returns true it is safe to work with getCurrentGex()
59         */
60        public boolean isConnected()
61        {
62                return
63                        currentGex != null &&
64                        currentGex.isConnected();
65        }
66
67        /**
68         * Set a premade Gex instance as the current Gex.
69         * Mostly used for testing, when you want to specify
70         * the DBConnector used. The other setCurrentGex() is preferred
71         * for most use cases.
72         *
73         * @param gex a premade Gex instance
74         */
75        public void setCurrentGex (SimpleGex gex)
76        {
77                close(); // close old gex.
78                currentGex = gex;
79                cachedData = new CachedData(gex);
80                fireExpressionDataEvent(new GexManagerEvent(gex, GexManagerEvent.CONNECTION_OPENED));
81        }
82
83        /**
84         * Create or connect to a new Gex based on the dbName.
85         * Uses a DBConnector obtained from the preferences.
86         *
87         * @param dbName name of the database (usually file or directory name)
88         * @param create true if you want to create / overwrite a database
89         */
90        public void setCurrentGex (String dbName, boolean create) throws IDMapperException
91        {
92                DBConnector connector;
93                try
94                {
95                        connector = getDBConnector();
96                }
97                catch (IllegalAccessException e)
98                {
99                        throw new IDMapperException (e);
100                }
101                catch (InstantiationException e)
102                {
103                        throw new IDMapperException (e);
104                }
105                catch (ClassNotFoundException e)
106                {
107                        throw new IDMapperException (e);
108                }
109                SimpleGex gex = new SimpleGex (dbName, create, connector);
110                setCurrentGex (gex);
111        }
112
113        public DBConnector getDBConnector() throws
114                ClassNotFoundException,
115                InstantiationException,
116                IllegalAccessException
117        {
118                DBConnector connector = null;
119
120                String className = null;
121                className = PreferenceManager.getCurrent().get(GlobalPreference.DB_ENGINE_GEX);
122
123                if(className == null) return null;
124
125                Class<?> dbc = Class.forName(className);
126                Object o = dbc.newInstance();
127                if(o instanceof DBConnector)
128                {
129                        connector = (DBConnector)dbc.newInstance();
130                        connector.setDbType(DBConnector.TYPE_GEX);
131                }
132
133                return connector;
134        }
135
136        /**
137         * Close the current Gex, if it wasn't already closed.
138         * Sends a GexManagerEvent around.
139         */
140        public void close()
141        {
142                if (currentGex == null) return; // was already closed.
143                fireExpressionDataEvent(new GexManagerEvent(currentGex, GexManagerEvent.CONNECTION_CLOSED));
144                try
145                {
146                        currentGex.close();
147                }
148                catch (IDMapperException e)
149                {
150                        Logger.log.error ("Problem while closing previous gex", e);
151                }
152                currentGex = null; // garbage collection
153                cachedData.dispose();
154                cachedData = null;
155        }
156
157        /**
158         * Fire a {@link GexManagerEvent} to notify all {@link GexManagerListener}s registered
159         * to this class
160         * @param e
161         */
162        private void fireExpressionDataEvent(GexManagerEvent e)
163        {
164                for(GexManagerListener l : listeners) l.gexManagerEvent(e);
165        }
166
167        /**
168         * Implement this interface if you want to receive an event when
169         * an expression dataset is opened / closed
170         */
171        public interface GexManagerListener
172        {
173                public void gexManagerEvent(GexManagerEvent e);
174        }
175
176        Set<GexManagerListener> listeners = new HashSet<GexManagerListener>();
177
178        /**
179         * Add a {@link GexManagerListener}, that will be notified if an
180         * event related to expression data occurs
181         * @param l The {@link GexManagerListener} to add
182         */
183        public void addListener(GexManagerListener l)
184        {
185                listeners.add(l);
186        }
187
188        public void removeListener(GexManagerListener l)
189        {
190                listeners.remove(l);
191        }
192
193        /**
194         * Events in the centralized Gex.
195         */
196        public static class GexManagerEvent extends EventObject
197        {
198
199                /** Event passed just after a new Gex is opened */
200                public static final int CONNECTION_OPENED = 0;
201                /** Event passed just before the current Gex is closed */
202                public static final int CONNECTION_CLOSED = 1;
203
204                private int type;
205                /** The type, one of CONNECTION_OPENED or CONNECTION_CLOSED */
206                public int getType() { return type; }
207
208                /**
209                 * @param source the current SimpleGex
210                 * @param type one of CONNECTION_OPENED or CONNECTION_CLOSED */
211                public GexManagerEvent(Object source, int type)
212                {
213                        super(source);
214                        this.source = source;
215                        this.type = type;
216                }
217        }
218
219}
Note: See TracBrowser for help on using the browser.