1 package org.devaki.nextobjects.ui.main;
2
3
4
5
6
7
8
9
10
11
12
13
14
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
50 this.setSize(new Dimension(350, 70));
51
52 this.setLocationRelativeTo(null);
53
54 this.setModal(true);
55
56 this.setResizable(false);
57
58 this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
59
60
61 this.getContentPane().setLayout(new GridBagLayout());
62
63
64
65 this.jProgressBar.setStringPainted(true);
66
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
97 /*** Check if a Torque directory exists */
98 public final void checkForTorque()
99 {
100
101
102 if (!new File(NOPreferences.getProperty("nextobjects.io.torque.home"))
103 .exists())
104 {
105
106 SwingWorker worker = new SwingWorker()
107 {
108 public final Object construct()
109 {
110 unZipTorque();
111
112 dispose();
113
114 return null;
115 }
116 };
117 worker.start();
118
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
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
141 System.out.println("Using torque from :" + tdk);
142
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
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
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 }