001 /**
002 * Created by IntelliJ IDEA.
003 * User: Wei Wang
004 * Date: Jan 19, 2003
005 * Time: 7:47:48 PM
006 */
007
008 package EVolve.util;
009
010 import EVolve.Scene;
011 import EVolve.data.ElementDefinition;
012 import EVolve.visualization.Visualization;
013 import javax.swing.*;
014 import java.io.*;
015 import java.awt.event.*;
016 import java.awt.*;
017 import java.util.*;
018
019 public class SceneSetting {
020 private static SceneSetting instance = null;
021 private JDialog settingDialog, pathDialog;
022 private JTextField[] path;
023 private JTextField textClassPath, textSourcePath, textVizConfigPath, textVizResultPath, textDefaultDataPath ;
024 private JButton buttonSave, buttonCancel;
025 private String iniFilename;
026 private final String optionFilename = "EVolve.ini";
027 private ArrayList additionClassPath, sourcePath;
028 private String[] tags = {"[Default Data Path]","[Default Viz result path]","[Default Viz Config path]",
029 "[Additional class path]","[Source file path]"};
030 private JButton[] buttonDir;
031
032
033 private SceneSetting() {
034 iniFilename = "." + File.separatorChar + optionFilename;
035 path = new JTextField[5];
036 path[0] = textDefaultDataPath = new JTextField(12);
037 path[1] = textVizResultPath = new JTextField(12);
038 path[2] = textVizConfigPath = new JTextField(12);
039 path[3] = textClassPath = new JTextField(12);
040 path[4] = textSourcePath = new JTextField(12);
041 buttonDir = new JButton[path.length];
042 for (int i=0; i<buttonDir.length; i++) {
043 buttonDir[i] = new JButton("...");
044 buttonDir[i].setName(String.valueOf(i));
045 buttonDir[i].addActionListener(new ActionListener() {
046 public void actionPerformed(ActionEvent e) {
047 addPath();
048 }
049 });
050 }
051 additionClassPath = new ArrayList();
052 sourcePath = new ArrayList();
053 }
054
055 public static SceneSetting v() {
056 if (instance == null)
057 instance = new SceneSetting();
058 return instance;
059 }
060
061 public void initialScene() {
062 String line;
063 RandomAccessFile iniFile;
064 File file = new File(iniFilename);
065 if (!file.exists()) return;
066
067 try {
068 iniFile = new RandomAccessFile(file,"r");
069 line = iniFile.readLine();
070 /** ini file is organized as:
071 * [Default Data Path]
072 * [Viz result path]
073 * [Viz Config path]
074 * [Additional class path]
075 * [Source file path]
076 */
077 while (line!=null) {
078 line = iniFile.readLine();
079 if (line.length() != 0) {
080 textDefaultDataPath.setText(line);
081 }
082
083 line = iniFile.readLine(); //skip one tag
084 line = iniFile.readLine();
085 if (line.length() != 0) {
086 textVizResultPath.setText(line);
087 }
088
089 line = iniFile.readLine(); //skip one tag
090 line = iniFile.readLine();
091 if (line.length() != 0) {
092 textVizConfigPath.setText(line);
093 }
094
095 line = iniFile.readLine(); //skip one tag
096 line = iniFile.readLine();
097 if (line.length() != 0) {
098 additionClassPath.clear();
099 textClassPath.setText(line);
100 StringTokenizer token = new StringTokenizer(line+File.pathSeparator,File.pathSeparator);
101 while (token.hasMoreTokens()) {
102 additionClassPath.add(token.nextToken());
103 }
104 }
105
106 line = iniFile.readLine(); //skip one tag
107 line = iniFile.readLine();
108 if (line.length() != 0) {
109 sourcePath.clear();
110 textSourcePath.setText(line);
111 StringTokenizer token = new StringTokenizer(line+File.pathSeparator,File.pathSeparator);
112 while (token.hasMoreTokens()) {
113 sourcePath.add(token.nextToken());
114 }
115 }
116 line = iniFile.readLine();
117 }
118 } catch (IOException e) {
119 Scene.showErrorMessage("Unable to read EVolve setting file \""+optionFilename+"\"");
120 return;
121 }
122 Scene.getUIManager().setLastConfigDir(textVizConfigPath.getText());
123 Scene.getUIManager().setLastDataDir(textDefaultDataPath.getText());
124 Scene.getUIManager().setLastResultDir(textVizResultPath.getText());
125 }
126
127 public void showPathDialog() {
128 Box boxMain = new Box(BoxLayout.Y_AXIS);
129 pathDialog = new JDialog(Scene.getFrame(), "Set path...", true);
130 pathDialog.setSize(600,300);
131
132 for (int i=0; i<tags.length; i++) {
133 String prompt = tags[i].substring(1,tags[i].length()-1) + ":";
134 boxMain.add(createPathItem(prompt, path[i],buttonDir[i]));
135 boxMain.add(Box.createVerticalStrut(20));
136 }
137
138 Box boxSaveCancel = new Box(BoxLayout.X_AXIS);
139 buttonSave = new JButton("Save");
140 buttonSave.addActionListener(new ActionListener() {
141 public void actionPerformed(ActionEvent e) {
142 save();
143 }
144 });
145
146 buttonCancel = new JButton("Cancel");
147 buttonCancel.addActionListener(new ActionListener() {
148 public void actionPerformed(ActionEvent e) {
149 pathDialog.setVisible(false);
150 }
151 });
152 boxSaveCancel.add(Box.createHorizontalStrut(40));
153 boxSaveCancel.add(buttonSave);
154 boxSaveCancel.add(Box.createHorizontalStrut(25));
155 boxSaveCancel.add(buttonCancel);
156
157 boxMain.add(boxSaveCancel);
158 pathDialog.getContentPane().add(boxMain);
159 //pathDialog.pack();
160 Scene.getUIManager().showDialog(pathDialog, pathDialog.getWidth(), pathDialog.getHeight());
161 }
162
163 private Box createPathItem(String prompt, JTextField item, JButton button) {
164 Box itemBox = new Box(BoxLayout.X_AXIS);
165
166 itemBox.add(new JLabel(prompt));
167 itemBox.add(Box.createHorizontalStrut(15));
168 itemBox.add(item);
169 itemBox.add(Box.createHorizontalStrut(10));
170 itemBox.add(button);
171 return itemBox;
172 }
173
174 private void save() {
175 try {
176 FileWriter writer = new FileWriter(iniFilename);
177 for (int i=0; i<tags.length; i++) {
178 writer.write(tags[i]+"\n");
179 writer.write(path[i].getText()+"\n");
180
181 }
182 writer.flush();
183 writer.close();
184 } catch (IOException e) {
185 Scene.showErrorMessage("Unable to write changes to file EVolve.ini");
186 }
187 pathDialog.setVisible(false);
188 initialScene();
189 }
190
191 public void showCurrentSetting() {
192 createSettingDialog();
193 }
194
195 private void createSettingDialog() {
196 Box boxMain = new Box(BoxLayout.Y_AXIS);
197 String setting;
198 int index;
199
200 settingDialog = new JDialog(Scene.getFrame(), "Current Setting", true);
201 settingDialog.setSize(500,500);
202
203 // show data source and its path
204 setting = Scene.getCurrentDataFilename();
205 index = setting.lastIndexOf(File.separatorChar);
206 if (index == -1) { // have not loaded date yet
207 boxMain.add(createInfoItem("Data source name:", "N/A"));
208 boxMain.add(Box.createVerticalStrut(20));
209 boxMain.add(createInfoItem("Data source path:", "N/A"));
210 } else {
211 boxMain.add(createInfoItem("Data source name:", setting.substring(index+1)));
212 boxMain.add(Box.createVerticalStrut(20));
213 boxMain.add(createInfoItem("Data source path:", setting.substring(0,index)));
214 }
215 boxMain.add(Box.createVerticalStrut(20));
216
217 // show path info
218 for (int i=0; i<tags.length; i++) {
219 String prompt = tags[i].substring(1,tags[i].length()-1) + ":";
220 boxMain.add(createInfoItem(prompt, path[i].getText()));
221 boxMain.add(Box.createVerticalStrut(20));
222 }
223
224 // show current visualization info
225 HashMap config = readVizSetting();
226 if (config != null) {
227 boxMain.add(createInfoItem("Visualization: ", (String)config.get("Name")));
228 boxMain.add(createInfoItem("Subject: ",((ElementDefinition)config.get("Subject")).getName()));
229 EVolve.visualization.Dimension[] dims = (EVolve.visualization.Dimension[])config.get("Dimension");
230 for (int i=0; i<dims.length; i++)
231 boxMain.add(createInfoItem("Dimension "+(char)(88+i)+" ",dims[i].getName()));
232 boxMain.add(createInfoItem("Interval",config.get("Interval").toString()));
233 } else {
234 boxMain.add(createInfoItem("Visualization: ", "N/A"));
235 boxMain.add(createInfoItem("Subject: ","N/A"));
236 for (int i=0; i<3; i++)
237 boxMain.add(createInfoItem("Dimension "+(char)(88+i)+" ","N/A"));
238 boxMain.add(createInfoItem("Interval","N/A"));
239 }
240
241
242 // create button box
243 JButton buttonOK = new JButton("OK");
244 buttonOK.addActionListener(new ActionListener() {
245 public void actionPerformed(ActionEvent e) {
246 settingDialog.setVisible(false);
247 }
248 });
249 JPanel panelButton = new JPanel(new BorderLayout());
250 //paneButton.add(Box.createHorizontalStrut(settingDialog.getWidth()/2 - 10));
251 panelButton.add(buttonOK,BorderLayout.CENTER);
252 boxMain.add(panelButton);
253
254 settingDialog.getContentPane().add(boxMain);
255 //settingDialog.pack();
256 Scene.getUIManager().showDialog(settingDialog, settingDialog.getWidth(), settingDialog.getHeight());
257 }
258
259 private Box createInfoItem(String prompt, String info) {
260 Box item = new Box(BoxLayout.X_AXIS);
261 item.add(new JLabel(prompt));
262 item.add(Box.createHorizontalStrut(15));
263 item.add(new JTextField(info));
264 return item;
265 }
266
267 private void addPath() {
268 int activeButtonId = 0;
269 for (int i=0; i<buttonDir.length; i++) {
270 if (buttonDir[i].isFocusOwner()) {
271 activeButtonId = i;
272 break;
273 }
274 }
275 JFileChooser fc = new JFileChooser(Scene.getUIManager().getLastDataDir());
276 String newPath;
277
278 fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
279
280 if(fc.showOpenDialog(Scene.getFrame()) == JFileChooser.APPROVE_OPTION) {
281 newPath = fc.getSelectedFile().getAbsolutePath();
282 switch (activeButtonId) {
283 case 3:
284 case 4:
285 String oldPath = path[activeButtonId].getText();
286 if (oldPath.length() != 0)
287 newPath = oldPath + File.pathSeparator + newPath;
288 case 0:
289 case 1:
290 case 2:
291 path[activeButtonId].setText(newPath);
292 break;
293 }
294 }
295
296 }
297
298 public ArrayList getAdditionalClassPath() {
299 return additionClassPath;
300 }
301
302 public ArrayList getSourcePath() {
303 return sourcePath;
304 }
305
306 private HashMap readVizSetting() {
307 ArrayList list = Scene.getVisualizationManager().getVisualizationList();
308
309 Visualization visual = null;
310 for (int i=0; i<list.size();i++) {
311 visual = (Visualization)list.get(i);
312 if (visual.getWindow().isSelected()) break;
313 }
314
315 if (visual != null)
316 return visual.getCurrentConfigure();
317 else
318 return null;
319 }
320
321 }