Mercurial > repos > yufei-luo > s_mart
comparison SMART/Java/Smart.java @ 6:769e306b7933
Change the repository level.
| author | yufei-luo |
|---|---|
| date | Fri, 18 Jan 2013 04:54:14 -0500 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 5:ea3082881bf8 | 6:769e306b7933 |
|---|---|
| 1 /** | |
| 2 * | |
| 3 * Copyright INRA-URGI 2009-2010 | |
| 4 * | |
| 5 * This software is governed by the CeCILL license under French law and | |
| 6 * abiding by the rules of distribution of free software. You can use, | |
| 7 * modify and/ or redistribute the software under the terms of the CeCILL | |
| 8 * license as circulated by CEA, CNRS and INRIA at the following URL | |
| 9 * "http://www.cecill.info". | |
| 10 * | |
| 11 * As a counterpart to the access to the source code and rights to copy, | |
| 12 * modify and redistribute granted by the license, users are provided only | |
| 13 * with a limited warranty and the software's author, the holder of the | |
| 14 * economic rights, and the successive licensors have only limited | |
| 15 * liability. | |
| 16 * | |
| 17 * In this respect, the user's attention is drawn to the risks associated | |
| 18 * with loading, using, modifying and/or developing or reproducing the | |
| 19 * software by the user in light of its specific status of free software, | |
| 20 * that may mean that it is complicated to manipulate, and that also | |
| 21 * therefore means that it is reserved for developers and experienced | |
| 22 * professionals having in-depth computer knowledge. Users are therefore | |
| 23 * encouraged to load and test the software's suitability as regards their | |
| 24 * requirements in conditions enabling the security of their systems and/or | |
| 25 * data to be ensured and, more generally, to use and operate it in the | |
| 26 * same conditions as regards security. | |
| 27 * | |
| 28 * The fact that you are presently reading this means that you have had | |
| 29 * knowledge of the CeCILL license and that you accept its terms. | |
| 30 * | |
| 31 */ | |
| 32 import java.util.*; | |
| 33 import java.awt.*; | |
| 34 import java.awt.event.ActionEvent; | |
| 35 import java.awt.event.ActionListener; | |
| 36 import java.awt.event.WindowEvent; | |
| 37 import java.awt.event.WindowAdapter; | |
| 38 import java.io.*; | |
| 39 import javax.swing.*; | |
| 40 import javax.swing.filechooser.*; | |
| 41 import javax.swing.border.*; | |
| 42 import javax.swing.SwingUtilities; | |
| 43 import java.util.prefs.BackingStoreException; | |
| 44 | |
| 45 | |
| 46 public class Smart extends JPanel implements ActionListener { | |
| 47 | |
| 48 String version = "1.1.0"; | |
| 49 | |
| 50 JFrame mainFrame; | |
| 51 JButton openButton; | |
| 52 JButton resetFileButton; | |
| 53 | |
| 54 JComboBox formatTypes; | |
| 55 JComboBox fileFormats; | |
| 56 String[] emptyFormats = {"Choose a type first..."}; | |
| 57 | |
| 58 JFrame askFrame; | |
| 59 JButton pythonButton; | |
| 60 JButton mySqlButton; | |
| 61 JButton rButton; | |
| 62 | |
| 63 HashMap <JButton, Program> callingProgram; | |
| 64 | |
| 65 static JList fileList; | |
| 66 static JTextArea logArea; | |
| 67 | |
| 68 // progress bar | |
| 69 static JLabel messageField; | |
| 70 static JProgressBar progressBar; | |
| 71 static JLabel etaField; | |
| 72 | |
| 73 // process | |
| 74 Program currentProgram; | |
| 75 Process process; | |
| 76 javax.swing.Timer processTimer; | |
| 77 | |
| 78 | |
| 79 int previousStatus; | |
| 80 | |
| 81 public Smart() { | |
| 82 super(new BorderLayout()); | |
| 83 | |
| 84 callingProgram = new HashMap <JButton, Program> (); | |
| 85 | |
| 86 previousStatus = -1; | |
| 87 | |
| 88 processTimer = new javax.swing.Timer(1000, this); | |
| 89 processTimer.stop(); | |
| 90 | |
| 91 // Ask frame buttons | |
| 92 pythonButton = new JButton("find..."); | |
| 93 mySqlButton = new JButton("find..."); | |
| 94 rButton = new JButton("find..."); | |
| 95 | |
| 96 // Get available formats | |
| 97 FormatsReader formatReader = new FormatsReader(Global.smartFormatsFileName); | |
| 98 if (! formatReader.read()) { | |
| 99 System.out.println("Something was wrong while reading file format..."); | |
| 100 } | |
| 101 | |
| 102 // Get screen size | |
| 103 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); | |
| 104 | |
| 105 // Log | |
| 106 logArea = new JTextArea(512, Global.logAreaSize); | |
| 107 logArea.setPreferredSize(new Dimension(screenSize.width, (int) (screenSize.height * 0.22))); | |
| 108 logArea.setFont(new Font("Monospaced", logArea.getFont().getStyle(), logArea.getFont().getSize())); | |
| 109 JScrollPane logScroll = new JScrollPane(logArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); | |
| 110 TitledBorder logBorder = BorderFactory.createTitledBorder("Log"); | |
| 111 logScroll.setBorder(logBorder); | |
| 112 logArea.append("Using S-MART " + version + "\n"); | |
| 113 | |
| 114 GridLayout horizontalLayout = new GridLayout(1, 0); | |
| 115 | |
| 116 // check configuration | |
| 117 this.readConfigurationFile(); | |
| 118 if (System.getProperty("os.name").matches("(?i).*Windows.*")) { | |
| 119 if (! this.checkDefaultDir()) { | |
| 120 this.checkRegistries(); | |
| 121 } | |
| 122 } | |
| 123 this.checkConfiguration(); | |
| 124 | |
| 125 // Tabs | |
| 126 JTabbedPane tabbedPane = new JTabbedPane(); | |
| 127 tabbedPane.setPreferredSize(new Dimension(screenSize.width, (int) (screenSize.height * 0.75))); | |
| 128 | |
| 129 // File panel | |
| 130 JPanel filePanel = new JPanel(false); | |
| 131 filePanel.setLayout(new FlowLayout()); | |
| 132 tabbedPane.add("Files", filePanel); | |
| 133 | |
| 134 // Format sub-panel | |
| 135 JPanel formatComboPanel = new JPanel(false); | |
| 136 JPanel formatPanel = new JPanel(false); | |
| 137 Vector<String> formatTypesString = Global.formats.getFormatTypes(); | |
| 138 formatPanel.setLayout(horizontalLayout); | |
| 139 formatTypesString.insertElementAt("Choose the format type", 0); | |
| 140 JLabel formatLabel = new JLabel("Format"); | |
| 141 formatTypes = new JComboBox(formatTypesString); | |
| 142 fileFormats = new JComboBox(emptyFormats); | |
| 143 formatLabel.setLabelFor(fileFormats); | |
| 144 formatTypes.addActionListener(this); | |
| 145 formatComboPanel.add(formatTypes); | |
| 146 formatComboPanel.add(fileFormats); | |
| 147 | |
| 148 formatPanel.add(formatLabel); | |
| 149 formatPanel.add(formatComboPanel); | |
| 150 | |
| 151 // File chooser sub-panel | |
| 152 JPanel fileChooserPanel = new JPanel(false); | |
| 153 fileChooserPanel.setLayout(horizontalLayout); | |
| 154 JLabel fileLabel = new JLabel("File"); | |
| 155 openButton = new JButton("Open a File..."); | |
| 156 openButton.addActionListener(this); | |
| 157 | |
| 158 fileChooserPanel.add(fileLabel); | |
| 159 fileChooserPanel.add(openButton); | |
| 160 | |
| 161 // File list sub-panel | |
| 162 JPanel existingFilesPanel = new JPanel(false); | |
| 163 existingFilesPanel.setLayout(horizontalLayout); | |
| 164 existingFilesPanel.setMinimumSize(new Dimension(10000, 10000)); | |
| 165 JLabel existingFilesLabel = new JLabel("Existing files"); | |
| 166 Box fileListBox = Box.createHorizontalBox(); | |
| 167 fileListBox.add(Box.createRigidArea(new Dimension(0, 100))); | |
| 168 fileList = new JList(new DefaultListModel()); | |
| 169 fileList.setLayoutOrientation(JList.HORIZONTAL_WRAP); | |
| 170 fileList.setVisibleRowCount(4); | |
| 171 JScrollPane listScroller = new JScrollPane(fileList); | |
| 172 fileListBox.add(listScroller); | |
| 173 | |
| 174 existingFilesPanel.add(existingFilesLabel); | |
| 175 existingFilesPanel.add(fileListBox); | |
| 176 | |
| 177 // Reset sub-panel | |
| 178 JPanel resetFilePanel = new JPanel(false); | |
| 179 resetFileButton = new JButton("Reset"); | |
| 180 resetFileButton.addActionListener(this); | |
| 181 | |
| 182 // File panel layout | |
| 183 Box box = Box.createVerticalBox(); | |
| 184 box.add(formatPanel); | |
| 185 box.add(fileChooserPanel); | |
| 186 box.add(existingFilesPanel); | |
| 187 box.add(resetFileButton); | |
| 188 filePanel.add(box); | |
| 189 | |
| 190 // Program panels | |
| 191 TreeMap < String, JTabbedPane > panels = new TreeMap < String, JTabbedPane >(); | |
| 192 PythonProgramFinder programFinder = new PythonProgramFinder("Python"); | |
| 193 String comments = programFinder.findPrograms(); | |
| 194 if (comments != null) { | |
| 195 logArea.append(comments); | |
| 196 } | |
| 197 for (int i = 0; i < programFinder.getPrograms().size(); i++) { | |
| 198 Program program = programFinder.getPrograms().get(i); | |
| 199 JPanel programPanel = program.getPanel(); | |
| 200 String section = program.getSection(); | |
| 201 JTabbedPane sectionPane = null; | |
| 202 if (panels.containsKey(section)) { | |
| 203 sectionPane = panels.get(section); | |
| 204 } | |
| 205 else { | |
| 206 sectionPane = new JTabbedPane(); | |
| 207 tabbedPane.addTab(section, sectionPane); | |
| 208 panels.put(section, sectionPane); | |
| 209 } | |
| 210 | |
| 211 JScrollPane programScroll = new JScrollPane(programPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); | |
| 212 sectionPane.addTab(program.getName(), programScroll); | |
| 213 | |
| 214 JButton button = program.getButton(); | |
| 215 button.addActionListener(this); | |
| 216 callingProgram.put(button, program); | |
| 217 } | |
| 218 | |
| 219 // Progress bar | |
| 220 JPanel progressPanel = new JPanel(new GridLayout(1, 0), false); | |
| 221 progressPanel.setPreferredSize(new Dimension(screenSize.width, (int) (screenSize.height * 0.02))); | |
| 222 messageField = new JLabel(); | |
| 223 progressBar = new JProgressBar(0, 100); | |
| 224 etaField = new JLabel(); | |
| 225 messageField.setHorizontalAlignment(JLabel.LEFT); | |
| 226 progressBar.setValue(0); | |
| 227 etaField.setHorizontalAlignment(JLabel.RIGHT); | |
| 228 progressBar.setStringPainted(true); | |
| 229 progressPanel.add(messageField); | |
| 230 progressPanel.add(progressBar); | |
| 231 progressPanel.add(etaField); | |
| 232 | |
| 233 add(tabbedPane, BorderLayout.PAGE_START); | |
| 234 add(logScroll, BorderLayout.CENTER); | |
| 235 add(progressPanel, BorderLayout.PAGE_END); | |
| 236 } | |
| 237 | |
| 238 | |
| 239 public String checkSubKey(int hkey, String key, String subKey, String trace) { | |
| 240 try { | |
| 241 for (String currentSubKey: WindowsRegistry.readStringSubKeys(hkey, key)) { | |
| 242 trace += "Looking at sub-key " + currentSubKey; | |
| 243 if (currentSubKey.matches(subKey)) { | |
| 244 trace += " OK"; | |
| 245 return subKey; | |
| 246 } | |
| 247 trace += "\n"; | |
| 248 } | |
| 249 } | |
| 250 catch (Exception e) { | |
| 251 final Writer writer = new StringWriter(); | |
| 252 final PrintWriter printWriter = new PrintWriter(writer); | |
| 253 e.printStackTrace(printWriter); | |
| 254 trace += writer.toString(); | |
| 255 } | |
| 256 return null; | |
| 257 } | |
| 258 | |
| 259 public void debugRegistry(int hkey, String keys[], String valueName, String trace) { | |
| 260 String concatenatedKeys = ""; | |
| 261 String selectedKey = null; | |
| 262 for (String key: keys) { | |
| 263 selectedKey = checkSubKey(hkey, concatenatedKeys, key, trace); | |
| 264 if (selectedKey != null) { | |
| 265 concatenatedKeys += "\\" + selectedKey; | |
| 266 } | |
| 267 else { | |
| 268 return; | |
| 269 } | |
| 270 } | |
| 271 } | |
| 272 | |
| 273 | |
| 274 public String checkRegistry(int hkey, String key, String valueName, String trace) { | |
| 275 String result = null; | |
| 276 try { | |
| 277 result = WindowsRegistry.readString(hkey, key, valueName); | |
| 278 } | |
| 279 catch (Exception e) { | |
| 280 final Writer writer = new StringWriter(); | |
| 281 final PrintWriter printWriter = new PrintWriter(writer); | |
| 282 e.printStackTrace(printWriter); | |
| 283 trace += result.toString(); | |
| 284 } | |
| 285 return result; | |
| 286 } | |
| 287 | |
| 288 | |
| 289 public boolean checkDefaultDir() { | |
| 290 String defaultPythonPath = System.getProperty("user.dir") + "\\Apps\\Python\\python.exe"; | |
| 291 java.io.File defaultPythonFile = new java.io.File(defaultPythonPath); | |
| 292 String defaultRPath = System.getProperty("user.dir") + "\\Apps\\R\\bin\\R.exe"; | |
| 293 java.io.File defaultRFile = new java.io.File(defaultRPath); | |
| 294 if (defaultPythonFile.exists()) { | |
| 295 Global.pythonCommand = defaultPythonPath; | |
| 296 logArea.append("Python found in default directory: " + defaultPythonPath + "\n"); | |
| 297 } | |
| 298 else { | |
| 299 logArea.append("Python not found in default directory: " + defaultPythonPath + "\n"); | |
| 300 return false; | |
| 301 } | |
| 302 if (defaultRFile.exists()) { | |
| 303 logArea.append("R found in default directory: " + defaultRPath + "\n"); | |
| 304 Global.rCommand = defaultRPath; | |
| 305 return true; | |
| 306 } | |
| 307 logArea.append("Python not found in default directory: " + defaultPythonPath + "\n"); | |
| 308 return false; | |
| 309 } | |
| 310 | |
| 311 | |
| 312 public boolean checkRegistries() { | |
| 313 String pythonDir = null; | |
| 314 String validValue = null; | |
| 315 String rDir; | |
| 316 String[] pythonFlavors = {"2.5", "2.6", "2.7"}; | |
| 317 String[] pythonDirectories = {"Python25", "Python26", "Python27"}; | |
| 318 String[] rDirectories = {"R-2.11.0", "R-2.11.0-x64"}; | |
| 319 String trace = ""; | |
| 320 for (String pythonFlavor: pythonFlavors) { | |
| 321 pythonDir = checkRegistry(WindowsRegistry.HKEY_LOCAL_MACHINE, "SOFTWARE\\Python\\PythonCore\\" + pythonFlavor + "\\InstallPath", "", trace); | |
| 322 if (pythonDir != null) { | |
| 323 break; | |
| 324 } | |
| 325 } | |
| 326 if (pythonDir == null) { | |
| 327 try { | |
| 328 logArea.append("Using OS: " + WindowsRegistry.readString(WindowsRegistry.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "ProductName") + "\n"); | |
| 329 } | |
| 330 catch (Exception e) { | |
| 331 logArea.append("Cannot do simple registry test. Strange...\n"); | |
| 332 } | |
| 333 String keys[] = {"SOFTWARE", "Python", "PythonCore", "2.[567]", "InstallPath"}; | |
| 334 debugRegistry(WindowsRegistry.HKEY_LOCAL_MACHINE, keys, "", trace); | |
| 335 logArea.append("S-MART cannot find Python installation directory using registry. Trying desperate move...\n"); | |
| 336 for (String currentDirectory: pythonDirectories) { | |
| 337 String fileName = "C:\\" + currentDirectory; | |
| 338 java.io.File file = new java.io.File(fileName); | |
| 339 if (file.exists()) { | |
| 340 pythonDir = fileName; | |
| 341 break; | |
| 342 } | |
| 343 } | |
| 344 if (pythonDir == null) { | |
| 345 logArea.append("S-MART cannot find Python installation directory despite all my efforts...\n" + trace); | |
| 346 return false; | |
| 347 } | |
| 348 } | |
| 349 rDir = checkRegistry(WindowsRegistry.HKEY_LOCAL_MACHINE, "SOFTWARE\\R-core\\R", "InstallPath", trace); | |
| 350 if (rDir == null) { | |
| 351 logArea.append("S-MART cannot find R installation directory using registry. Trying desperate move...\n"); | |
| 352 for (String currentDirectory: rDirectories) { | |
| 353 String fileName = "C:\\Program Files\\R\\" + currentDirectory; | |
| 354 java.io.File file = new java.io.File(fileName); | |
| 355 if (file.exists()) { | |
| 356 rDir = fileName; | |
| 357 break; | |
| 358 } | |
| 359 } | |
| 360 if (rDir == null) { | |
| 361 logArea.append("S-MART cannot find R installation directory despite all my efforts...\n" + trace); | |
| 362 return false; | |
| 363 } | |
| 364 } | |
| 365 Global.pythonCommand = pythonDir + "\\" + "python.exe"; | |
| 366 Global.rCommand = rDir + "\\" + "bin\\R.exe"; | |
| 367 return true; | |
| 368 } | |
| 369 | |
| 370 | |
| 371 public boolean checkConfiguration() { | |
| 372 int status = this.testConfiguration(); | |
| 373 | |
| 374 if (status == previousStatus) { | |
| 375 logArea.append("S-MART does not seem to work properly... Tried to manage it by myself, unsuccessfully... Check documentation for further explanation...\n"); | |
| 376 return false; | |
| 377 } | |
| 378 | |
| 379 switch (status) { | |
| 380 case 0: | |
| 381 return true; | |
| 382 case 1: | |
| 383 logArea.append("S-MART does not seem to work properly... Check documentation for further explanation...\n"); | |
| 384 break; | |
| 385 case 3: | |
| 386 this.askWhereIsProgram("python"); | |
| 387 break; | |
| 388 case 4: | |
| 389 break; | |
| 390 case 5: | |
| 391 this.askWhereIsProgram("mySQL"); | |
| 392 break; | |
| 393 case 6: | |
| 394 this.askWhereIsProgram("R"); | |
| 395 break; | |
| 396 case 7: | |
| 397 logArea.append("Please install 'ColorBrewer' R package...\n"); | |
| 398 break; | |
| 399 default: | |
| 400 logArea.append("Weird configuration test status: " + status + "...\n"); | |
| 401 } | |
| 402 previousStatus = status; | |
| 403 return true; | |
| 404 } | |
| 405 | |
| 406 | |
| 407 public int testConfiguration() { | |
| 408 String[] command = {Global.pythonCommand, "Python" + java.io.File.separator + "testInstall.py"}; | |
| 409 ProgramLauncher launcher = new ProgramLauncher(command, logArea, messageField, progressBar, etaField); | |
| 410 String line; | |
| 411 launcher.execute(); | |
| 412 return launcher.getExitValue(); | |
| 413 } | |
| 414 | |
| 415 | |
| 416 public void readConfigurationFile() { | |
| 417 java.io.File file = new java.io.File(Global.smartConfFileName); | |
| 418 String line = null; | |
| 419 | |
| 420 if (! file.exists()) { | |
| 421 return; | |
| 422 } | |
| 423 | |
| 424 try { | |
| 425 BufferedReader reader = new BufferedReader(new FileReader(file)); | |
| 426 | |
| 427 while ((line = reader.readLine()) != null) { | |
| 428 line = line.trim(); | |
| 429 if (line.startsWith("python:")) Global.pythonCommand = line.substring("python:".length()).trim(); | |
| 430 else if (line.startsWith("mysql:")) Global.mysqlCommand = line.substring("mysql:".length()).trim(); | |
| 431 else if (line.startsWith("r:")) Global.rCommand = line.substring("r:".length()).trim(); | |
| 432 } | |
| 433 reader.close(); | |
| 434 } | |
| 435 catch (FileNotFoundException e) { | |
| 436 logArea.append("Configuration file is empty: " + e.getMessage() + "!\n"); | |
| 437 return; | |
| 438 } | |
| 439 catch (IOException e) { | |
| 440 logArea.append("Weird with configuration file: " + e.getMessage() + "!\n"); | |
| 441 return; | |
| 442 } | |
| 443 } | |
| 444 | |
| 445 | |
| 446 public void askWhereIsProgram(String program) { | |
| 447 askFrame = new JFrame("Where is " + program + "?"); | |
| 448 askFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
| 449 JLabel label = new JLabel("Where is your " + program + " (or " + program + ".exe) file?"); | |
| 450 JButton button = null; | |
| 451 if ("python".equals(program)) { | |
| 452 button = pythonButton; | |
| 453 } | |
| 454 else if ("mySQL".equals(program)) { | |
| 455 button = mySqlButton; | |
| 456 } | |
| 457 else if ("R".equals(program)) { | |
| 458 button = rButton; | |
| 459 } | |
| 460 else { | |
| 461 logArea.append("Problem with the button!\n"); | |
| 462 } | |
| 463 askFrame.getContentPane().add(label, BorderLayout.WEST); | |
| 464 askFrame.getContentPane().add(button, BorderLayout.EAST); | |
| 465 button.addActionListener(this); | |
| 466 askFrame.pack(); | |
| 467 askFrame.setVisible(true); | |
| 468 askFrame.setAlwaysOnTop(true); | |
| 469 } | |
| 470 | |
| 471 | |
| 472 public void actionPerformed(ActionEvent e) { | |
| 473 | |
| 474 // Python command chooser | |
| 475 if (e.getSource() == pythonButton) { | |
| 476 JFileChooser chooser = new JFileChooser(); | |
| 477 if (chooser.showOpenDialog(mainFrame) == JFileChooser.APPROVE_OPTION) { | |
| 478 Global.pythonCommand = chooser.getSelectedFile().getPath(); | |
| 479 askFrame.setVisible(false); | |
| 480 askFrame.dispose(); | |
| 481 try { | |
| 482 BufferedWriter out = new BufferedWriter(new FileWriter(Global.smartConfFileName, true)); | |
| 483 out.write("python: " + Global.pythonCommand + "\n"); | |
| 484 out.close(); | |
| 485 } | |
| 486 catch (IOException exception) { | |
| 487 logArea.append("Cannot write configuration file!\n"); | |
| 488 } | |
| 489 } | |
| 490 this.checkConfiguration(); | |
| 491 } | |
| 492 // MySQL command chooser | |
| 493 else if (e.getSource() == mySqlButton) { | |
| 494 JFileChooser chooser = new JFileChooser(); | |
| 495 if (chooser.showOpenDialog(mainFrame) == JFileChooser.APPROVE_OPTION) { | |
| 496 Global.mysqlCommand = chooser.getSelectedFile().getPath(); | |
| 497 askFrame.setVisible(false); | |
| 498 askFrame.dispose(); | |
| 499 try { | |
| 500 BufferedWriter out = new BufferedWriter(new FileWriter(Global.smartConfFileName, true)); | |
| 501 out.write("mysql: " + Global.mysqlCommand + "\n"); | |
| 502 out.close(); | |
| 503 } | |
| 504 catch (IOException exception) { | |
| 505 logArea.append("Cannot write configuration file!\n"); | |
| 506 } | |
| 507 } | |
| 508 this.checkConfiguration(); | |
| 509 } | |
| 510 // R command chooser | |
| 511 else if (e.getSource() == rButton) { | |
| 512 JFileChooser chooser = new JFileChooser(); | |
| 513 if (chooser.showOpenDialog(mainFrame) == JFileChooser.APPROVE_OPTION) { | |
| 514 Global.rCommand = chooser.getSelectedFile().getPath(); | |
| 515 askFrame.setVisible(false); | |
| 516 askFrame.dispose(); | |
| 517 try { | |
| 518 BufferedWriter out = new BufferedWriter(new FileWriter(Global.smartConfFileName, true)); | |
| 519 out.write("r: " + Global.rCommand + "\n"); | |
| 520 out.close(); | |
| 521 } | |
| 522 catch (IOException exception) { | |
| 523 logArea.append("Cannot write configuration file!\n"); | |
| 524 } | |
| 525 } | |
| 526 this.checkConfiguration(); | |
| 527 } | |
| 528 // Format type | |
| 529 else if (e.getSource() == formatTypes) { | |
| 530 if (((String) formatTypes.getSelectedItem()).startsWith("Choose")) { | |
| 531 return; | |
| 532 } | |
| 533 fileFormats.removeAllItems(); | |
| 534 Vector < String > selectedFormats = Global.formats.getFormats((String) formatTypes.getSelectedItem()).getFormats(); | |
| 535 for (int i = 0; i < selectedFormats.size(); i++) { | |
| 536 fileFormats.addItem(selectedFormats.get(i)); | |
| 537 } | |
| 538 } | |
| 539 // Main file chooser | |
| 540 else if (e.getSource() == openButton) { | |
| 541 if (((String) formatTypes.getSelectedItem()).startsWith("Choose")) { | |
| 542 logArea.append("Please choose a type and format before selecting a file!\n"); | |
| 543 return; | |
| 544 } | |
| 545 JFileChooser chooser = new JFileChooser(); | |
| 546 if (chooser.showOpenDialog(mainFrame) == JFileChooser.APPROVE_OPTION) { | |
| 547 String fileName = chooser.getSelectedFile().getPath(); | |
| 548 Global.fileNames.addElement(fileName); | |
| 549 Global.files.addFile(fileName, (String) formatTypes.getSelectedItem(), (String) fileFormats.getSelectedItem()); | |
| 550 DefaultListModel defaultListModel = (DefaultListModel) fileList.getModel(); | |
| 551 defaultListModel.addElement(fileName); | |
| 552 } | |
| 553 } | |
| 554 // Reset file chooser | |
| 555 else if (e.getSource() == resetFileButton) { | |
| 556 Global.files.clear(); | |
| 557 Global.fileNames.clear(); | |
| 558 DefaultListModel defaultListModel = (DefaultListModel) fileList.getModel(); | |
| 559 defaultListModel.clear(); | |
| 560 } | |
| 561 // Other file choosers | |
| 562 else if (Global.otherFilesChooser.containsKey(e.getSource())) { | |
| 563 JTextField textField = Global.otherFilesChooser.get(e.getSource()); | |
| 564 JFileChooser chooser = new JFileChooser(); | |
| 565 if (chooser.showOpenDialog(mainFrame) == JFileChooser.APPROVE_OPTION) { | |
| 566 textField.setText(chooser.getSelectedFile().getPath()); | |
| 567 } | |
| 568 } | |
| 569 // Other directories choosers | |
| 570 else if (Global.otherDirectoriesChooser.containsKey(e.getSource())) { | |
| 571 JTextField textField = Global.otherDirectoriesChooser.get(e.getSource()); | |
| 572 JFileChooser chooser = new JFileChooser(); | |
| 573 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); | |
| 574 if (chooser.showOpenDialog(mainFrame) == JFileChooser.APPROVE_OPTION) { | |
| 575 textField.setText(chooser.getSelectedFile().getPath()); | |
| 576 } | |
| 577 } | |
| 578 else if (Global.otherFileConcatenationChooser.containsKey(e.getSource())) { | |
| 579 JTextField textField = Global.otherDirectoriesChooser.get(e.getSource()); | |
| 580 JFileChooser chooser = new JFileChooser(); | |
| 581 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); | |
| 582 if (chooser.showOpenDialog(mainFrame) == JFileChooser.APPROVE_OPTION) { | |
| 583 String text = textField.getText(); | |
| 584 if ((text == null) || ("".equals(text))) { | |
| 585 textField.setText(chooser.getSelectedFile().getPath()); | |
| 586 } | |
| 587 else { | |
| 588 textField.setText(text + "," + chooser.getSelectedFile().getPath()); | |
| 589 } | |
| 590 } | |
| 591 } | |
| 592 // Programs | |
| 593 else { | |
| 594 currentProgram = callingProgram.get(e.getSource()); | |
| 595 String comment = currentProgram.checkValues(); | |
| 596 if (comment != null) { | |
| 597 logArea.append(comment); | |
| 598 return; | |
| 599 } | |
| 600 LinkedList <String> command = currentProgram.getCommand(); | |
| 601 ProgramLauncher launcher = new ProgramLauncher(command, logArea, messageField, progressBar, etaField); | |
| 602 launcher.execute(); | |
| 603 Vector < File > outputFiles = currentProgram.getOutputFiles(); | |
| 604 for (int i = 0; i < outputFiles.size(); i++) { | |
| 605 File file = outputFiles.get(i); | |
| 606 if (file.getFormatType().compareToIgnoreCase("other") != 0) { | |
| 607 Global.fileNames.addElement(file.getName()); | |
| 608 Global.files.addFile(file); | |
| 609 } | |
| 610 } | |
| 611 currentProgram = null; | |
| 612 } | |
| 613 } | |
| 614 | |
| 615 | |
| 616 private static void removeTmpFiles() { | |
| 617 logArea.append("You want to quit already?\nRemoving temporary files..."); | |
| 618 String[] command = {Global.pythonCommand, "Python" + java.io.File.separator + "removeAllTmpTables.py"}; | |
| 619 ProgramLauncher launcher = new ProgramLauncher(command, logArea, messageField, progressBar, etaField); | |
| 620 launcher.execute(); | |
| 621 logArea.append(" done.\nNow quitting.\nBye!"); | |
| 622 } | |
| 623 | |
| 624 | |
| 625 private static void printJavaVersions() { | |
| 626 String[] pro = {"java.version", "java.vm.version", "java.runtime.version"}; | |
| 627 | |
| 628 Properties properties = System.getProperties(); | |
| 629 for (int i = 0; i < pro.length; i++) { | |
| 630 logArea.append(pro[i] + ": " + properties.getProperty(pro[i]) + "\n"); | |
| 631 } | |
| 632 } | |
| 633 | |
| 634 private static void createAndShowGUI() { | |
| 635 // Create and set up the window. | |
| 636 JFrame mainFrame = new JFrame("S-Mart"); | |
| 637 mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
| 638 | |
| 639 //Create and set up the content pane. | |
| 640 JComponent newContentPane = new Smart(); | |
| 641 newContentPane.setOpaque(true); | |
| 642 mainFrame.setContentPane(newContentPane); | |
| 643 | |
| 644 // Display the window. | |
| 645 mainFrame.pack(); | |
| 646 mainFrame.setVisible(true); | |
| 647 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); | |
| 648 mainFrame.setBounds(0, 0, screenSize.width, screenSize.height); | |
| 649 printJavaVersions(); | |
| 650 | |
| 651 // Remove tmp file while quitting. | |
| 652 mainFrame.addWindowListener(new WindowAdapter() { | |
| 653 @Override | |
| 654 public void windowClosing(WindowEvent e) { | |
| 655 removeTmpFiles(); | |
| 656 } | |
| 657 }); | |
| 658 } | |
| 659 | |
| 660 | |
| 661 public static void main(String[] args) { | |
| 662 javax.swing.SwingUtilities.invokeLater(new Runnable() { | |
| 663 public void run() { | |
| 664 createAndShowGUI(); | |
| 665 } | |
| 666 }); | |
| 667 } | |
| 668 } |
