View Javadoc

1   package org.devaki.nextobjects.util;
2   /*
3    *  nextobjects Copyright (C) 2001-2005 Emmanuel Florent
4    *  This program is free software; you can redistribute it and/or modify
5    *  it under the terms of the GNU General Public License as published by the
6    *  Free Software Foundation; either version 2 of the License, or (at your
7    *  option) any later version.
8    *  This program is distributed in the hope that it will
9    *  be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
10   *  of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   *  PURPOSE. See the GNU General Public License for more details.
12   *  You should have received a copy of the GNU General Public License along
13   *  with this program; if not, write to the Free Software Foundation, Inc., 59
14   *  Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15   */
16  import java.awt.Point;
17  import java.io.File;
18  import java.util.Iterator;
19  import java.util.Vector;
20  import org.w3c.dom.Document;
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.devaki.nextobjects.workspace.models.PhysicalModel;
24  import org.devaki.nextobjects.workspace.models.graphics.ClassView;
25  import org.devaki.nextobjects.workspace.models.objects.Table;
26  /***
27   *  This class is responsible for starting tghe right torque task against a
28   *  database,then parse and load the obtained project-schema.xml
29   *
30   * @author     <a href="mailto:eflorent@devaki.org">Emmanuel Florent</a>
31   */
32  public final class DatabaseLoader
33  {
34      /***  The logger */
35      private static Log logger =
36          LogFactory.getLog(DatabaseLoader.class.getName());
37      /***  Empty constructor */
38      private DatabaseLoader()
39      {
40      }
41      /***
42       *  Load the database
43       *
44       * @param  pdm  the model to load
45       */
46      public static void loadDB(final PhysicalModel pdm)
47      {
48          final SwingWorker worker = new SwingWorker()
49          {
50              /***
51               *  Swingworker thread init.
52               *
53               * @return    Description of the Return Value
54               */
55              public final Object construct()
56              {
57                  File in =
58                      new File(
59                          NOPreferences.getProperty("nextobjects.io.torque.home")
60                              + "/schema/schema.xml");
61                  if (in.exists())
62                  {
63                      in.delete();
64                  }
65                  //start torque import feature
66  				TorqueWrapper tw= new TorqueWrapper();
67                  tw.jdbc2xml(pdm);
68                  tw=null;
69                  //load torque output
70                  loadProjectSchema(in, pdm);
71                  // we have nothing to return (swingworker)
72                  return null;
73              }
74          };
75          // end of worker
76          worker.start();
77      }
78      /***
79       *  Load a physical data model from a ${project}-schema document
80       *
81       * @param  in   the file to load
82       * @param  pdm  the physical model
83       */
84      public static void loadProjectSchema(
85          final File in,
86          final PhysicalModel pdm)
87      {
88          Document doc = NOXMLFactory.loadModel(in);
89          NOXMLFactory.loadPdm(doc, pdm);
90          fixIt(pdm);
91      }
92      /***
93       *  Load a physical data model from a ${project}-schema document
94       *
95       * @param  doc  the DOM document
96       * @param  pdm  the physical model
97       */
98      public static void loadProjectSchema(
99          final Document doc,
100         final PhysicalModel pdm)
101     {
102         NOXMLFactory.loadPdm(doc, pdm);
103         fixIt(pdm);
104     }
105     /***
106      *  Move the component so they are all visibles
107      *
108      * @param  pdm  the Physical Model
109      */
110     public static void fixIt(final PhysicalModel pdm)
111     {
112         Vector vctTables = pdm.getTables();
113         Iterator itTables = vctTables.iterator();
114         int x = 0;
115         int y = 0;
116         int dx = 40;
117         int dy = 200;
118         int maxY = 0;
119         double val = 0;
120         int rows = (int) Math.sqrt(vctTables.size()) + 1;
121         //fix some default values
122         pdm.setId(pdm.getName());
123         //evaluate a sequential location
124         while (itTables.hasNext())
125         {
126             Table table = (Table) itTables.next();
127             ((ClassView) table.getObjectView()).autoResize(true);
128             //evaluate a sequential location
129             val = ++x % rows;
130             table.getObjectView().setLocation(new Point(dx + 5000, dy + 2500));
131             // set next time espacement.
132             dx = dx + (int) table.getObjectView().getSize().getWidth() + 20;
133             if (maxY < table.getObjectView().getSize().getHeight())
134             {
135                 maxY = (int) table.getObjectView().getSize().getHeight();
136             }
137             if (val == 0)
138             {
139                 dy = maxY + dy + 20;
140                 x = 0;
141                 dx = 0;
142             }
143             //x++;
144         }
145         ModelMan.resizeModelObjects(pdm);
146         pdm.getModelView().center(true);
147     }
148 }