001: /**
002: * This class loads the data sources for use by the editor.
003: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
004: * the GNU Public License (GPL), please see license.txt for more details. If
005: * you make commercial use of this software you must purchase a commercial
006: * license from Xoetrope.</p>
007: * <p> $Revision: 1.7 $</p>
008: */package com.xoetrope.data;
009:
010: import java.util.Hashtable;
011: import net.xoetrope.data.XDataSource;
012: import net.xoetrope.debug.DebugLogger;
013: import net.xoetrope.editor.project.ProjectListener;
014: import net.xoetrope.editor.project.XEditorProject;
015: import net.xoetrope.optional.data.XDataModelLoader;
016: import net.xoetrope.optional.data.XOptionalDataSource;
017: import net.xoetrope.xui.XProject;
018: import net.xoetrope.xui.helper.SwingWorker;
019: import net.xoetrope.xml.XmlElement;
020: import net.xoetrope.xui.build.BuildProperties;
021: import net.xoetrope.xui.data.XModel;
022: import net.xoetrope.xui.helper.ReflectionHelper;
023:
024: public class DataSourceLoader implements ProjectListener {
025: // key: run time datasource class
026: // value: design time datasource class
027: protected Hashtable dataSources;
028:
029: /**
030: * Creates a new instance of DataSourceLoader
031: */
032: public DataSourceLoader() {
033: initDataSources();
034: }
035:
036: /**
037: * Fills the datasources hashtable
038: */
039: @SuppressWarnings("all")
040: private void initDataSources() {
041: dataSources = new Hashtable();
042:
043: dataSources.put(
044: "net.xoetrope.optional.data.pojo.XPojoDataSource",
045: "com.xoetrope.data.pojo.XPojoDataSourceEx");
046:
047: dataSources
048: .put(
049: "net.xoetrope.optional.data.pojo.XPersistentPojoDataSource",
050: "com.xoetrope.data.pojo.XPersistentPojoDataSourceEx");
051:
052: dataSources
053: .put(
054: "net.xoetrope.optional.data.pojo.XHibernatePojoDataSource",
055: "com.xoetrope.data.pojo.XHibernatePojoDataSourceEx");
056: }
057:
058: /**
059: * Notification of project initialized.
060: * @param project the editor project just initialized
061: */
062: public void projectInitialized(XEditorProject project) {
063: }
064:
065: /**
066: * Builds data sources of the project for use
067: * by the editor.
068: * @param project the project whose data sources will
069: * be loaded
070: */
071: public void projectLoaded(XEditorProject project) {
072: loadDatasets(project);
073: }
074:
075: /**
076: * Loads the data sources for use by the editor (i.e. data visualizer)
077: * @param project the project whose data sources are to be loaded
078: */
079: private void loadDatasets(XEditorProject project) {
080: XOptionalDataSource modelDataSource = new XOptionalDataSource(
081: project);
082: modelDataSource.setModelLoader(new DataLoader(project));
083: try {
084: String fileName = project.getStartupParam("ModelData");
085: if (fileName != null) {
086: try {
087: modelDataSource.read(project.getBufferedReader(
088: fileName, null));
089: } catch (Exception ex3) {
090: if (BuildProperties.DEBUG)
091: DebugLogger.logError("Could not access file:"
092: + fileName);
093: }
094: }
095: } catch (Exception ex) {
096: if (BuildProperties.DEBUG)
097: DebugLogger.logError("Exception in loadDatasets");
098: }
099: }
100:
101: /**
102: * The data source loader.
103: */
104: private class DataLoader implements XDataModelLoader {
105: private XEditorProject currentProject;
106:
107: /**
108: * Creates a new instance of DataLoader
109: * @param project the current project
110: */
111: public DataLoader(XEditorProject project) {
112: currentProject = project;
113: }
114:
115: /**
116: * Load a custom data model node by reflectively constructing a data model
117: * loader and invoking its load method(s).
118: * @param cn the class to instantiate, a class implementing XDataModelLoader
119: * @param src the XML source being loaded
120: * @param model the model to load
121: */
122: public void loadModel(String cn, final XmlElement src,
123: final XModel model) {
124: String className = (String) dataSources.get(cn);
125: // if there's no design time datasource version specified, use the runtime one
126: if (className == null)
127: className = cn;
128:
129: // create the data source object for use in the editor
130: final XDataSource dataSource = (XDataSource) ReflectionHelper
131: .constructViaReflection(
132: getClass().getClassLoader(), className,
133: XProject.class, currentProject);
134:
135: // create the "worker" tasked with loading the data to the model
136: SwingWorker swingWorker = (new SwingWorker() {
137:
138: public Object construct() {
139: dataSource.loadTable(src, model);
140: return model;
141: }
142:
143: public void finished() {
144: }
145: });
146:
147: // load the data in a background thread
148: swingWorker.start();
149: }
150:
151: /*
152: public void loadModel( String className, final XmlElement src, final XModel model )
153: {
154: if ( "net.xoetrope.optional.data.pojo.XPersistentPojoDataSource".equals( className )) {
155: final XPojoDataSourceEx dataSource = new XPersistentPojoDataSourceEx( currentProject );
156: SwingWorker swingWorker = new SwingWorker() {
157:
158: public Object construct()
159: {
160: dataSource.loadTable( src, model );
161: return model;
162: }
163:
164: public void finished()
165: {
166: }
167: };
168:
169: swingWorker.start();
170: }
171: else if ( "net.xoetrope.optional.data.pojo.XPojoDataSource".equals( className )) {
172: final XPojoDataSourceEx dataSource = new XPojoDataSourceEx( currentProject );
173:
174: SwingWorker swingWorker = new SwingWorker() {
175: public Object construct()
176: {
177: dataSource.loadTable( src, model );
178: return model;
179: }
180:
181: public void finished()
182: {
183: }
184: };
185:
186: swingWorker.start();
187: }
188: else {
189: Object[] values = new Object[]{ currentProject };
190: XDataSource dataSource = (XDataSource)ReflectionHelper.constructViaReflection( className, values );
191: if ( dataSource != null )
192: dataSource.loadTable( src, model );
193: }
194: }
195: */
196:
197: }
198:
199: public void projectUpdated(XEditorProject project) {
200: }
201:
202: public void saveProject(XEditorProject project) {
203: }
204:
205: public void checkProject(XEditorProject project) {
206: }
207:
208: public void resetProject(String moduleName, XEditorProject project) {
209: }
210: }
|