1 package main;
2
3 import intellego.Intellego;
4 import util.*;
5 import interfaces.*;
6 import real.*;
7 import simworldobjects.*;
8 import main.*;
9 import NetBeansResources.*;
10
11 import java.awt.*;
12 import java.lang.*;
13 import java.awt.event.*;
14 import javax.swing.*;
15 import java.io.*;
16 import java.util.*;
17
18
19
22 public class SaveWorldDialog extends JDialog implements ActionListener
23 {
24 private JLabel fileL, folderL, typeL;
25 private JButton OK, cancel;
26 private JTextField fileT, folderT;
27
28 private SimGround simg;
29 private GridDisplay grid;
30 private int gridSize;
31 private int pixelSize=4;
32 private boolean noGrid=true;
33
34 private BasicSimWorld world;
35 private String path;
36 private String currentWorldFile;
37 private String fileName = "";
38 private JFileChooser chooser;
39
40
41
46
47 public void createSaveWorldPopUp(String path, SimWorld world, String currentWorldFile)
48 {
49 this.path = path;
51 this.world = (BasicSimWorld)world;
52 this.currentWorldFile=currentWorldFile.substring(0,currentWorldFile.length()-6);
53
54 setTitle("Save Instance of this SimWorld:");
56 setSize(275,155);
57 setLocation(400,400);
58
60 this.setDefaultLookAndFeelDecorated(false);
61 this.setUndecorated(true);
62 this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
63
64 setVisible(true);
65
66 Container mainPanel=getContentPane();
67 mainPanel.setLayout(new BorderLayout(1,1));
68
69 Container Panell=new Container();
70 Panell.setLayout(new AbsoluteLayout());
71
72 Container Panel5=new Container();
73 Panel5.setLayout(new FlowLayout());
74
75 this.fileL = new JLabel("File Name: ");
76 fileL.setForeground(Color.lightGray);
77 this.folderL = new JLabel("Folder: ");
78 folderL.setForeground(Color.lightGray);
79 this.typeL = new JLabel(". JAVA");
80 typeL.setForeground(Color.lightGray);
81
82 this.fileT = new JTextField(11);
83 this.folderT = new JTextField();
84 this.folderT.setEditable(false);
85 this.folderT.setText(this.path);
86 this.folderT.setFocusable(false);
87
88 fileT.setBackground(Color.yellow);
90 fileT.setForeground(Color.darkGray);
91 folderT.setBackground(Color.yellow);
92 folderT.setForeground(Color.darkGray);
93
94 (OK=new JButton("OK")).addActionListener(this);
95 (cancel=new JButton("Cancel")).addActionListener(this);
96
97 OK.setBackground(Color.lightGray);
99 OK.setForeground(Color.darkGray);
100 cancel.setBackground(Color.lightGray);
101 cancel.setForeground(Color.darkGray);
102
103 Panell.add(this.fileL,new AbsoluteConstraints(10,20));
104 Panell.add(this.fileT,new AbsoluteConstraints(80,17));
105 Panell.add(this.typeL,new AbsoluteConstraints(210,20));
106 Panell.add(this.folderL,new AbsoluteConstraints(30,57));
107 Panell.add(this.folderT,new AbsoluteConstraints(80,54));
108
109 Panel5.add("South",OK);
110 Panel5.add("South",cancel);
111
112 mainPanel.setBackground(Color.darkGray);
113 mainPanel.add("North",Panell);
114 mainPanel.add("South",Panel5);
115
116 this.show();
117 }
118
119
120 private void createInstanceWorld(){
121
122 File newWorldJava = new File(path+fileName+".java");
123 FileWriter out = null;
124 Calendar rightNow = Calendar.getInstance();
125
126 try{
127 out = new FileWriter(newWorldJava);
128 try{
129 out.write("\n");
131 out.write("\n //Date created: "+rightNow.getTime().toString());
132 out.write("// This file was generated at you request by Intelleg.");
133 out.write("\n");
134 out.write("\n import simworldobjects.*;");
135 out.write("\n import java.awt.*;");
136 out.write("\n");
137 out.write("\n public class "+fileName+" extends BasicSimWorld {");
138 out.write("\n");
139 out.write("\n /** Creates a new instance of"+fileName+" */");
140 out.write("\n public "+fileName+"() {");
141 out.write("\n super("+world.getWorldDimensions()[0] + ","+world.getWorldDimensions()[1]+","+world.getWorldDimensions()[2]+ ", new Color("+world.getWorldColor().getRGB()+"));");
142 out.write("\n");
143 LinkedList objectList = world.getObjectList();
145
146 for(int a=0;a<objectList.size();a++){
147
148 SimObject sim = (SimObject)(objectList.get(a));
149
150 if(sim instanceof SimGround){
151 SimGround ground = (SimGround)sim;
152 out.write("\n SimGround "+(ground.getType()+a)+"= new SimGround("+ground.getXCoord()+","+ground.getYCoord()+","+ground.getZCoord()+","+ground.getActualBearingXZ()+","+ground.getWidth()+","+ground.getLength()+", new Color("+ground.getColor().getRGB()+"));");
153 out.write("\n addObject("+(ground.getType()+a)+");");
154 out.write("\n");
155 }
156 else if(sim instanceof SimWall){
157 SimWall wall = (SimWall)sim;
158 out.write("\n SimWall "+(wall.getType()+a)+"= new SimWall("+wall.getXCoord()+","+wall.getYCoord()+","+wall.getZCoord()+","+wall.getActualBearingXZ()+","+wall.getWidth()+","+wall.getLength()+");");
159 out.write("\n addObject("+(wall.getType()+a)+");");
160 out.write("\n");
161
162 }
163 else if(sim instanceof SimRoad){
164 SimRoad road = (SimRoad)sim;
165 out.write("\n SimRoad "+(road.getType()+a)+"= new SimRoad("+road.getXCoord()+","+road.getYCoord()+","+road.getZCoord()+","+road.getActualBearingXZ()+","+road.getWidth()+","+road.getLength()+");");
166 out.write("\n addObject("+(road.getType()+a)+");");
167 out.write("\n");
168
169 }
170 }
171 out.write("\n }");
172 out.write("\n }");
173
174 out.close();
176
177 externalCommand("javac "+newWorldJava.getPath());
179 }
180 catch(Exception ea){
181 MainInterface.displayMessage("Cannot write to the desired filename.");
182 Intellego.addToLog("SaveWorldDialog.createInstanceWorld(): Writing to file failed: "+ea);
183 }
184 }
185 catch(Exception e){
186 MainInterface.displayMessage("Cannot recognize the entered file name");
187 Intellego.addToLog("SaveWorldDialog.createInstanceWorld(): Creating file failed: "+e);
188 }
189 }
190
191
195 private void externalCommand(String cmd)
196 {
197 int len;
198 byte buffer[] = new byte[1000];
199 Intellego.addToLog("SaveWorldDialog.externalCommand(): Processing External Command: "+cmd);
200 try
201 {
202 Process external=Runtime.getRuntime().exec(cmd);
203 InputStream ees = external.getErrorStream();
204 try
205 {
206 ExternalMessager output=MainInterface.createExternalMessagerFrame(0);
207 while ((len = ees.read(buffer)) != -1)
208 {
209 String eo = new String (buffer, 0, len);
210 output.append(eo);
211 }
212 external.waitFor();
213
214 if(output.successfullCompile()){
215
216 output.append("\n"+"\n"+" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
217 output.append("\n"+"\n"+" | COMPILING SUCCESSFUL: NO ERRORS FOUND |");
218 output.append("\n"+"\n"+" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
219 }
220
221 else{
222
223 output.append("\n"+"\n"+" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
224 output.append("\n"+"\n"+" | COMPILING FAILED: ERRORS FOUND |");
225 output.append("\n"+"\n"+" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
226
227 }
228
229
230 }
231 catch (Exception e)
232 {
233 Intellego.addToLog("SaveWorldDialog.externalCommand(): error: "+e.getMessage());
234 }
235 }
236 catch (Exception e)
237 {
238 Intellego.addToLog("SaveWorldDialog.externalCommand(): error: "+e.getMessage());
239 }
240 }
241
242
245 public void actionPerformed(ActionEvent e)
246 { boolean flag = false;
247 if (e.getSource()==OK)
248 {
249 fileName = (this.fileT.getText()).trim();
250
251 for(int a=0;a<fileName.length();a++){
253 char blank = ' ';
254 if(fileName.charAt(a) == blank){
255 flag=true;
256 break;
257 }
258 }
259 chooser=new JFileChooser(new File(System.getProperties().getProperty("user.dir"),"simworlds"));
260 chooser.setVisible(false);
261
262 File[] arrayF = (chooser.getCurrentDirectory().listFiles());
263 String lowerCaseFile = fileName.toLowerCase();
264
265 for(int i=0;i<arrayF.length;i++){
266 try{
267 String lowerCaseCurr = (arrayF[i].getName()).substring(0,(arrayF[i].getName()).indexOf('.')).toLowerCase();
268 if(lowerCaseFile.equals(lowerCaseCurr)){
269 flag = true;
270 System.out.println("Flag is TRUE!!");
271 break;
272 }
273 }
274 catch(Exception e2){ }
276 }
277
278 if(flag || fileName.equals("")){
279 MainInterface.displayMessage("ERROR: invalid file name");
280 this.fileT.setText("");
281 }
282 else{
283 createInstanceWorld();
284 this.dispose();
285 }
286 }
287 else{this.dispose();}
288 }
289 }