View Javadoc

1   package org.devaki.nextobjects.ui.main;
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.io.File;
17  import java.io.IOException;
18  import java.awt.Dimension;
19  import java.awt.GridBagConstraints;
20  import java.awt.GridBagLayout;
21  import java.awt.Insets;
22  import javax.swing.JDialog;
23  import javax.swing.JLabel;
24  import javax.swing.JProgressBar;
25  import javax.swing.WindowConstants;
26  import org.devaki.nextobjects.NextObjects;
27  import org.devaki.nextobjects.util.NOPreferences;
28  import org.devaki.nextobjects.util.NOFileUtil;
29  import org.devaki.nextobjects.util.SwingWorker;
30  import java.io.InputStream;
31  import java.io.FileOutputStream;
32  /***
33   *  This class is responsible for setting up the torque template at first use of
34   *  this project It will create the projects repository and unzip templates from
35   *  the torque-gen archive
36   *
37   * @author     <a href="mailto:eflorent@devaki.org">Emmanuel Florent</a>
38   */
39  public class InitHome extends JDialog
40  {
41      /***  The setup template progress bar */
42      private JProgressBar jProgressBar = new JProgressBar();
43      /***  Main label */
44      private JLabel jLabel = new JLabel("Completing install");
45      /***  Create a new 'InitHome' object */
46      public InitHome()
47      {
48          super(NextObjects.getReference(), "Preparing First Execution...");
49          // Define the size of the window
50          this.setSize(new Dimension(350, 70));
51          // Center the window
52          this.setLocationRelativeTo(null);
53          // Specify that the window is modal
54          this.setModal(true);
55          // Specify that the window is not resizable
56          this.setResizable(false);
57          // Define what to do when closing this window using
58          this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
59          // PANELS
60          // Define a grid bag layout for the main panel
61          this.getContentPane().setLayout(new GridBagLayout());
62          // PROGRESS BARS
63          // Define the progress bar to paint a
64          // String representing the progression
65          this.jProgressBar.setStringPainted(true);
66          // Content Panel
67          this.getContentPane().add(
68              this.jLabel,
69              new GridBagConstraints(
70                  0,
71                  0,
72                  1,
73                  1,
74                  0.0,
75                  0.0,
76                  GridBagConstraints.CENTER,
77                  GridBagConstraints.NONE,
78                  new Insets(3, 3, 0, 3),
79                  0,
80                  0));
81          this.getContentPane().add(
82              this.jProgressBar,
83              new GridBagConstraints(
84                  0,
85                  1,
86                  1,
87                  1,
88                  1.0,
89                  1.0,
90                  GridBagConstraints.CENTER,
91                  GridBagConstraints.BOTH,
92                  new Insets(3, 3, 3, 3),
93                  0,
94                  0));
95      }
96      // End of 'InitHome()'
97      /***  Check if a Torque directory exists */
98      public final void checkForTorque()
99      {
100         // Verify the Torque version used in the nextObjects properties
101         // else decompress torque in the user home directory
102         if (!new File(NOPreferences.getProperty("nextobjects.io.torque.home"))
103             .exists())
104         {
105             // Construct and launch the uncompressing task in a new thread
106             SwingWorker worker = new SwingWorker()
107             {
108                 public final Object construct()
109                 {
110                     unZipTorque();
111                     // Task is over, make the progress bar disappear
112                     dispose();
113                     // Return something ...
114                     return null;
115                 }
116             };
117             worker.start();
118             // Make the progress bar appears
119             this.setVisible(true);
120         }
121     }
122     /***
123      *  Unzip the Torque-TORQUE_VERSION.zip file in the nextObjects directory in
124      *  (user.home)
125      */
126     public final void unZipTorque()
127     {
128         // Place to find the Torque Development Kit zipped
129         String tdk =
130             new StringBuffer(System.getProperty("user.dir"))
131                 .append(File.separator)
132                 .append("lib")
133                 .append(File.separator)
134                 .append("torque-gen-")
135                 .append(NOPreferences.getProperty("torque.version"))
136                 .append(".zip")
137                 .toString();
138         if (new File(tdk).exists())
139         {
140             // the logger is not yet here ...
141             System.out.println("Using torque from :" + tdk);
142             // UnZip
143             try
144             {
145                 NOFileUtil.unzip(
146                     tdk,
147                     NOPreferences.APP_HOME,
148                     this.jProgressBar);
149                 NOPreferences.setProperty(
150                     "nextobjects.io.torque.home",
151                     NOPreferences.APP_HOME
152                     + File.separator
153                         + "torque-gen-"
154                         + NOPreferences.getProperty("torque.version"));
155             }
156             catch (IOException exc)
157             {
158                 System.out.println(exc.toString());
159                 System.out.println("I give up.");
160                 System.exit(1);
161             }
162         }
163         else
164         {
165             System.out.println("Can't find Torque distribution at : " + tdk);
166         }
167     }
168     /***
169      * Copy defaultbuild.properties at first use
170      *
171      */
172     public static void copyDefaultBuildProperties()
173     {
174         String pDefault = "org/devaki/nextobjects/defaultbuild.properties";
175         try
176         {
177             File fp =
178                 new File(
179                     NOPreferences.APP_HOME
180                         + File.separator
181                         + "defaultbuild.properties");
182             // Get current classloader
183             ClassLoader cl =
184                 NextObjects.getReference().getClass().getClassLoader();
185             InputStream in = cl.getResourceAsStream(pDefault);
186             FileOutputStream out = new FileOutputStream(fp);
187             int c;
188             while ((c = in.read()) != -1)
189             {
190                 out.write(c);
191             }
192             out.close();
193             System.out.println("wrote " + fp.toString());
194         }
195         catch (Throwable t)
196         {
197             System.out.println(t.getMessage());
198             System.out.println("Unable to initialise " + pDefault);
199         }
200     }
201     /***
202      * Copy default.properties at first use
203      *
204      */
205     public static void copyDefaultProperties()
206     {
207         String pDefault = "org/devaki/nextobjects/default.properties";
208         try
209         {
210             File fp =
211                 new File(
212                     NOPreferences.APP_HOME
213                         + File.separator
214                         + "devaki.properties");
215             // Get current classloader
216             ClassLoader cl =
217                 NextObjects.getReference().getClass().getClassLoader();
218             InputStream in = cl.getResourceAsStream(pDefault);
219             FileOutputStream out = new FileOutputStream(fp);
220             int c;
221             while ((c = in.read()) != -1)
222             {
223                 out.write(c);
224             }
225             out.close();
226             System.out.println("wrote " + fp.toString());
227         }
228         catch (Throwable t)
229         {
230             System.out.println(t.getMessage());
231             System.out.println("Unable to initialise " + pDefault);
232         }
233     }
234 }