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