Mercurial > repos > yufei-luo > s_mart
comparison SMART/Java/Sav/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.io.*; | |
| 37 import javax.swing.*; | |
| 38 import javax.swing.filechooser.*; | |
| 39 import javax.swing.border.*; | |
| 40 import javax.swing.SwingUtilities; | |
| 41 | |
| 42 | |
| 43 public class Smart extends JPanel implements ActionListener { | |
| 44 | |
| 45 String version = "1.0.2"; | |
| 46 | |
| 47 JFrame mainFrame; | |
| 48 JButton openButton; | |
| 49 JButton comparisonGoButton; | |
| 50 | |
| 51 JComboBox formatTypes; | |
| 52 JComboBox fileFormats; | |
| 53 String[] emptyFormats = {"Choose a type first..."}; | |
| 54 | |
| 55 JFrame askFrame; | |
| 56 JButton pythonButton; | |
| 57 JButton mySqlButton; | |
| 58 JButton rButton; | |
| 59 | |
| 60 HashMap <JButton, Program> callingProgram; | |
| 61 | |
| 62 // comparison | |
| 63 JList comparisonFile1List; | |
| 64 JList comparisonFile2List; | |
| 65 JTextField comparisonOutputTextField; | |
| 66 JTextField comparisonFiveQueryExtensionTextField; | |
| 67 JCheckBox comparisonColinearCheckBox; | |
| 68 JCheckBox comparisonAntisenseCheckBox; | |
| 69 JCheckBox comparisonInvertCheckBox; | |
| 70 | |
| 71 JList fileList; | |
| 72 JTextArea logArea; | |
| 73 | |
| 74 // progress bar | |
| 75 JLabel messageField; | |
| 76 JProgressBar progressBar; | |
| 77 JLabel etaField; | |
| 78 | |
| 79 // process | |
| 80 Program currentProgram; | |
| 81 Process process; | |
| 82 javax.swing.Timer processTimer; | |
| 83 | |
| 84 | |
| 85 int previousStatus; | |
| 86 | |
| 87 public Smart() { | |
| 88 super(new BorderLayout()); | |
| 89 | |
| 90 callingProgram = new HashMap <JButton, Program> (); | |
| 91 | |
| 92 previousStatus = -1; | |
| 93 | |
| 94 processTimer = new javax.swing.Timer(1000, this); | |
| 95 processTimer.stop(); | |
| 96 | |
| 97 // Ask frame buttons | |
| 98 pythonButton = new JButton("find..."); | |
| 99 mySqlButton = new JButton("find..."); | |
| 100 rButton = new JButton("find..."); | |
| 101 | |
| 102 // Get available formats | |
| 103 FormatsReader formatReader = new FormatsReader(Global.smartFormatsFileName); | |
| 104 if (! formatReader.read()) { | |
| 105 System.out.println("Something was wrong while reading file format..."); | |
| 106 } | |
| 107 | |
| 108 // Get screen size | |
| 109 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); | |
| 110 | |
| 111 // Log | |
| 112 logArea = new JTextArea(512, Global.logAreaSize); | |
| 113 logArea.setPreferredSize(new Dimension(screenSize.width, (int) (screenSize.height * 0.22))); | |
| 114 logArea.setFont(new Font("Monospaced", logArea.getFont().getStyle(), logArea.getFont().getSize())); | |
| 115 JScrollPane logScroll = new JScrollPane(logArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); | |
| 116 TitledBorder logBorder = BorderFactory.createTitledBorder("Log"); | |
| 117 logScroll.setBorder(logBorder); | |
| 118 logArea.append("Using S-MART " + version + "\n"); | |
| 119 | |
| 120 GridLayout horizontalLayout = new GridLayout(1, 0); | |
| 121 | |
| 122 // check configuration | |
| 123 this.readConfigurationFile(); | |
| 124 this.checkConfiguration(); | |
| 125 | |
| 126 // Tabs | |
| 127 JTabbedPane tabbedPane = new JTabbedPane(); | |
| 128 tabbedPane.setPreferredSize(new Dimension(screenSize.width, (int) (screenSize.height * 0.75))); | |
| 129 | |
| 130 // File panel | |
| 131 JPanel filePanel = new JPanel(false); | |
| 132 filePanel.setLayout(new FlowLayout()); | |
| 133 tabbedPane.add("Files", filePanel); | |
| 134 | |
| 135 // Format sub-panel | |
| 136 JPanel formatComboPanel = new JPanel(false); | |
| 137 JPanel formatPanel = new JPanel(false); | |
| 138 Vector<String> formatTypesString = Global.formats.getFormatTypes(); | |
| 139 formatPanel.setLayout(horizontalLayout); | |
| 140 formatTypesString.insertElementAt("Choose the format type", 0); | |
| 141 JLabel formatLabel = new JLabel("Format"); | |
| 142 formatTypes = new JComboBox(formatTypesString); | |
| 143 fileFormats = new JComboBox(emptyFormats); | |
| 144 formatLabel.setLabelFor(fileFormats); | |
| 145 formatTypes.addActionListener(this); | |
| 146 formatComboPanel.add(formatTypes); | |
| 147 formatComboPanel.add(fileFormats); | |
| 148 | |
| 149 formatPanel.add(formatLabel); | |
| 150 formatPanel.add(formatComboPanel); | |
| 151 | |
| 152 // File chooser sub-panel | |
| 153 JPanel fileChooserPanel = new JPanel(false); | |
| 154 fileChooserPanel.setLayout(horizontalLayout); | |
| 155 JLabel fileLabel = new JLabel("File"); | |
| 156 openButton = new JButton("Open a File..."); | |
| 157 openButton.addActionListener(this); | |
| 158 | |
| 159 fileChooserPanel.add(fileLabel); | |
| 160 fileChooserPanel.add(openButton); | |
| 161 | |
| 162 // File list sub-panel | |
| 163 JPanel existingFilesPanel = new JPanel(false); | |
| 164 existingFilesPanel.setLayout(horizontalLayout); | |
| 165 existingFilesPanel.setMinimumSize(new Dimension(10000, 10000)); | |
| 166 JLabel existingFilesLabel = new JLabel("Existing files"); | |
| 167 Box fileListBox = Box.createHorizontalBox(); | |
| 168 fileListBox.add(Box.createRigidArea(new Dimension(0, 100))); | |
| 169 JList fileList = new JList(Global.fileNames); | |
| 170 fileList.setLayoutOrientation(JList.HORIZONTAL_WRAP); | |
| 171 fileList.setVisibleRowCount(4); | |
| 172 JScrollPane listScroller = new JScrollPane(fileList); | |
| 173 fileListBox.add(listScroller); | |
| 174 | |
| 175 existingFilesPanel.add(existingFilesLabel); | |
| 176 existingFilesPanel.add(fileListBox); | |
| 177 | |
| 178 // File panel layout | |
| 179 Box box = Box.createVerticalBox(); | |
| 180 box.add(formatPanel); | |
| 181 box.add(fileChooserPanel); | |
| 182 box.add(existingFilesPanel); | |
| 183 filePanel.add(box); | |
| 184 | |
| 185 // Program panels | |
| 186 TreeMap < String, JTabbedPane > panels = new TreeMap < String, JTabbedPane >(); | |
| 187 PythonProgramFinder programFinder = new PythonProgramFinder("Python"); | |
| 188 String comments = programFinder.findPrograms(); | |
| 189 if (comments != null) { | |
| 190 logArea.append(comments); | |
| 191 } | |
| 192 for (int i = 0; i < programFinder.getPrograms().size(); i++) { | |
| 193 Program program = programFinder.getPrograms().get(i); | |
| 194 JPanel programPanel = program.getPanel(); | |
| 195 String section = program.getSection(); | |
| 196 JTabbedPane sectionPane = null; | |
| 197 if (panels.containsKey(section)) { | |
| 198 sectionPane = panels.get(section); | |
| 199 } | |
| 200 else { | |
| 201 sectionPane = new JTabbedPane(); | |
| 202 tabbedPane.addTab(section, sectionPane); | |
| 203 panels.put(section, sectionPane); | |
| 204 } | |
| 205 | |
| 206 JScrollPane programScroll = new JScrollPane(programPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); | |
| 207 sectionPane.addTab(program.getName(), programScroll); | |
| 208 | |
| 209 JButton button = program.getButton(); | |
| 210 button.addActionListener(this); | |
| 211 callingProgram.put(button, program); | |
| 212 } | |
| 213 | |
| 214 // Progress bar | |
| 215 JPanel progressPanel = new JPanel(new GridLayout(1, 0), false); | |
| 216 progressPanel.setPreferredSize(new Dimension(screenSize.width, (int) (screenSize.height * 0.02))); | |
| 217 messageField = new JLabel(); | |
| 218 progressBar = new JProgressBar(0, 100); | |
| 219 etaField = new JLabel(); | |
| 220 messageField.setHorizontalAlignment(JLabel.LEFT); | |
| 221 progressBar.setValue(0); | |
| 222 etaField.setHorizontalAlignment(JLabel.RIGHT); | |
| 223 progressBar.setStringPainted(true); | |
| 224 progressPanel.add(messageField); | |
| 225 progressPanel.add(progressBar); | |
| 226 progressPanel.add(etaField); | |
| 227 | |
| 228 add(tabbedPane, BorderLayout.PAGE_START); | |
| 229 add(logScroll, BorderLayout.CENTER); | |
| 230 add(progressPanel, BorderLayout.PAGE_END); | |
| 231 } | |
| 232 | |
| 233 | |
| 234 public boolean checkConfiguration() { | |
| 235 int status = this.testConfiguration(); | |
| 236 | |
| 237 if (status == previousStatus) { | |
| 238 logArea.append("S-MART does not seem to work properly... Tried to manage it by myself, unsuccessfully... Check documentation for further explanation...\n"); | |
| 239 return false; | |
| 240 } | |
| 241 | |
| 242 switch (status) { | |
| 243 case 0: | |
| 244 return true; | |
| 245 case 1: | |
| 246 logArea.append("S-MART does not seem to work properly... Check documentation for further explanation...\n"); | |
| 247 break; | |
| 248 case 3: | |
| 249 this.askWhereIsProgram("python"); | |
| 250 break; | |
| 251 case 4: | |
| 252 break; | |
| 253 case 5: | |
| 254 this.askWhereIsProgram("mySQL"); | |
| 255 break; | |
| 256 case 6: | |
| 257 this.askWhereIsProgram("R"); | |
| 258 break; | |
| 259 case 7: | |
| 260 logArea.append("Please install 'ColorBrewer' R package...\n"); | |
| 261 break; | |
| 262 default: | |
| 263 logArea.append("Weird configuration test status: " + status + "...\n"); | |
| 264 } | |
| 265 previousStatus = status; | |
| 266 return true; | |
| 267 } | |
| 268 | |
| 269 | |
| 270 public int testConfiguration() { | |
| 271 String[] command = {Global.pythonCommand, "Python" + java.io.File.separator + "testInstall.py"}; | |
| 272 ProgramLauncher launcher = new ProgramLauncher(command, logArea, messageField, progressBar, etaField); | |
| 273 String line; | |
| 274 launcher.execute(); | |
| 275 (new Exception("hello")).printStackTrace(); | |
| 276 return launcher.getExitValue(); | |
| 277 } | |
| 278 | |
| 279 | |
| 280 public void readConfigurationFile() { | |
| 281 java.io.File file = new java.io.File(Global.smartConfFileName); | |
| 282 String line = null; | |
| 283 | |
| 284 if (! file.exists()) { | |
| 285 return; | |
| 286 } | |
| 287 | |
| 288 try { | |
| 289 BufferedReader reader = new BufferedReader(new FileReader(file)); | |
| 290 | |
| 291 while ((line = reader.readLine()) != null) { | |
| 292 line = line.trim(); | |
| 293 if (line.startsWith("python:")) Global.pythonCommand = line.substring("python:".length()).trim(); | |
| 294 else if (line.startsWith("mysql:")) Global.mysqlCommand = line.substring("mysql:".length()).trim(); | |
| 295 else if (line.startsWith("r:")) Global.rCommand = line.substring("r:".length()).trim(); | |
| 296 } | |
| 297 reader.close(); | |
| 298 } | |
| 299 catch (FileNotFoundException e) { | |
| 300 logArea.append("Configuration file is empty: " + e.getMessage() + "!\n"); | |
| 301 return; | |
| 302 } | |
| 303 catch (IOException e) { | |
| 304 logArea.append("Weird with configuration file: " + e.getMessage() + "!\n"); | |
| 305 return; | |
| 306 } | |
| 307 } | |
| 308 | |
| 309 | |
| 310 public void askWhereIsProgram(String program) { | |
| 311 askFrame = new JFrame("Where is " + program + "?"); | |
| 312 askFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
| 313 JLabel label = new JLabel("Where is your " + program + " (or " + program + ".exe) file?"); | |
| 314 JButton button = null; | |
| 315 if ("python".equals(program)) { | |
| 316 button = pythonButton; | |
| 317 } | |
| 318 else if ("mySQL".equals(program)) { | |
| 319 button = mySqlButton; | |
| 320 } | |
| 321 else if ("R".equals(program)) { | |
| 322 button = rButton; | |
| 323 } | |
| 324 else { | |
| 325 logArea.append("Problem with the button!\n"); | |
| 326 } | |
| 327 askFrame.getContentPane().add(label, BorderLayout.WEST); | |
| 328 askFrame.getContentPane().add(button, BorderLayout.EAST); | |
| 329 button.addActionListener(this); | |
| 330 askFrame.pack(); | |
| 331 askFrame.setVisible(true); | |
| 332 askFrame.setAlwaysOnTop(true); | |
| 333 } | |
| 334 | |
| 335 | |
| 336 public void actionPerformed(ActionEvent e) { | |
| 337 | |
| 338 // Python command chooser | |
| 339 if (e.getSource() == pythonButton) { | |
| 340 JFileChooser chooser = new JFileChooser(); | |
| 341 if (chooser.showOpenDialog(mainFrame) == JFileChooser.APPROVE_OPTION) { | |
| 342 Global.pythonCommand = chooser.getSelectedFile().getPath(); | |
| 343 askFrame.setVisible(false); | |
| 344 askFrame.dispose(); | |
| 345 try { | |
| 346 BufferedWriter out = new BufferedWriter(new FileWriter(Global.smartConfFileName, true)); | |
| 347 out.write("python: " + Global.pythonCommand + "\n"); | |
| 348 out.close(); | |
| 349 } | |
| 350 catch (IOException exception) { | |
| 351 logArea.append("Cannot write configuration file!\n"); | |
| 352 } | |
| 353 } | |
| 354 this.checkConfiguration(); | |
| 355 } | |
| 356 // MySQL command chooser | |
| 357 else if (e.getSource() == mySqlButton) { | |
| 358 JFileChooser chooser = new JFileChooser(); | |
| 359 if (chooser.showOpenDialog(mainFrame) == JFileChooser.APPROVE_OPTION) { | |
| 360 Global.mysqlCommand = chooser.getSelectedFile().getPath(); | |
| 361 askFrame.setVisible(false); | |
| 362 askFrame.dispose(); | |
| 363 try { | |
| 364 BufferedWriter out = new BufferedWriter(new FileWriter(Global.smartConfFileName, true)); | |
| 365 out.write("mysql: " + Global.mysqlCommand + "\n"); | |
| 366 out.close(); | |
| 367 } | |
| 368 catch (IOException exception) { | |
| 369 logArea.append("Cannot write configuration file!\n"); | |
| 370 } | |
| 371 } | |
| 372 this.checkConfiguration(); | |
| 373 } | |
| 374 // R command chooser | |
| 375 else if (e.getSource() == rButton) { | |
| 376 JFileChooser chooser = new JFileChooser(); | |
| 377 if (chooser.showOpenDialog(mainFrame) == JFileChooser.APPROVE_OPTION) { | |
| 378 Global.rCommand = chooser.getSelectedFile().getPath(); | |
| 379 askFrame.setVisible(false); | |
| 380 askFrame.dispose(); | |
| 381 try { | |
| 382 BufferedWriter out = new BufferedWriter(new FileWriter(Global.smartConfFileName, true)); | |
| 383 out.write("r: " + Global.rCommand + "\n"); | |
| 384 out.close(); | |
| 385 } | |
| 386 catch (IOException exception) { | |
| 387 logArea.append("Cannot write configuration file!\n"); | |
| 388 } | |
| 389 } | |
| 390 this.checkConfiguration(); | |
| 391 } | |
| 392 // Format type | |
| 393 else if (e.getSource() == formatTypes) { | |
| 394 fileFormats.removeAllItems(); | |
| 395 Vector < String > selectedFormats = Global.formats.getFormats((String) formatTypes.getSelectedItem()).getFormats(); | |
| 396 for (int i = 0; i < selectedFormats.size(); i++) { | |
| 397 fileFormats.addItem(selectedFormats.get(i)); | |
| 398 } | |
| 399 } | |
| 400 // Main file chooser | |
| 401 else if (e.getSource() == openButton) { | |
| 402 JFileChooser chooser = new JFileChooser(); | |
| 403 if (chooser.showOpenDialog(mainFrame) == JFileChooser.APPROVE_OPTION) { | |
| 404 String fileName = chooser.getSelectedFile().getPath(); | |
| 405 Global.fileNames.addElement(fileName); | |
| 406 Global.files.addFile(fileName, (String) formatTypes.getSelectedItem(), (String) fileFormats.getSelectedItem()); | |
| 407 } | |
| 408 } | |
| 409 // Other file choosers | |
| 410 else if (Global.otherFilesChooser.containsKey(e.getSource())) { | |
| 411 JTextField textField = Global.otherFilesChooser.get(e.getSource()); | |
| 412 JFileChooser chooser = new JFileChooser(); | |
| 413 if (chooser.showOpenDialog(mainFrame) == JFileChooser.APPROVE_OPTION) { | |
| 414 textField.setText(chooser.getSelectedFile().getPath()); | |
| 415 } | |
| 416 } | |
| 417 // Other directories choosers | |
| 418 else if (Global.otherDirectoriesChooser.containsKey(e.getSource())) { | |
| 419 JTextField textField = Global.otherDirectoriesChooser.get(e.getSource()); | |
| 420 JFileChooser chooser = new JFileChooser(); | |
| 421 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); | |
| 422 if (chooser.showOpenDialog(mainFrame) == JFileChooser.APPROVE_OPTION) { | |
| 423 textField.setText(chooser.getSelectedFile().getPath()); | |
| 424 } | |
| 425 } | |
| 426 else if (Global.otherFileConcatenationChooser.containsKey(e.getSource())) { | |
| 427 JTextField textField = Global.otherDirectoriesChooser.get(e.getSource()); | |
| 428 JFileChooser chooser = new JFileChooser(); | |
| 429 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); | |
| 430 if (chooser.showOpenDialog(mainFrame) == JFileChooser.APPROVE_OPTION) { | |
| 431 String text = textField.getText(); | |
| 432 if ((text == null) || ("".equals(text))) { | |
| 433 textField.setText(chooser.getSelectedFile().getPath()); | |
| 434 } | |
| 435 else { | |
| 436 textField.setText(text + "," + chooser.getSelectedFile().getPath()); | |
| 437 } | |
| 438 } | |
| 439 } | |
| 440 // Programs | |
| 441 else { | |
| 442 currentProgram = callingProgram.get(e.getSource()); | |
| 443 String comment = currentProgram.checkValues(); | |
| 444 if (comment != null) { | |
| 445 logArea.append(comment); | |
| 446 return; | |
| 447 } | |
| 448 LinkedList <String> command = currentProgram.getCommand(); | |
| 449 ProgramLauncher launcher = new ProgramLauncher(command, logArea, messageField, progressBar, etaField); | |
| 450 launcher.execute(); | |
| 451 Vector < File > outputFiles = currentProgram.getOutputFiles(); | |
| 452 for (int i = 0; i < outputFiles.size(); i++) { | |
| 453 File file = outputFiles.get(i); | |
| 454 if (file.getFormatType().compareToIgnoreCase("other") != 0) { | |
| 455 Global.fileNames.addElement(file.getName()); | |
| 456 Global.files.addFile(file); | |
| 457 } | |
| 458 } | |
| 459 currentProgram = null; | |
| 460 } | |
| 461 } | |
| 462 | |
| 463 | |
| 464 private static void createAndShowGUI() { | |
| 465 // Create and set up the window. | |
| 466 JFrame mainFrame = new JFrame("S-Mart"); | |
| 467 mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
| 468 | |
| 469 //Create and set up the content pane. | |
| 470 JComponent newContentPane = new Smart(); | |
| 471 newContentPane.setOpaque(true); | |
| 472 mainFrame.setContentPane(newContentPane); | |
| 473 | |
| 474 // Display the window. | |
| 475 mainFrame.pack(); | |
| 476 mainFrame.setVisible(true); | |
| 477 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); | |
| 478 mainFrame.setBounds(0, 0, screenSize.width, screenSize.height); | |
| 479 } | |
| 480 | |
| 481 | |
| 482 public static void main(String[] args) { | |
| 483 javax.swing.SwingUtilities.invokeLater(new Runnable() { | |
| 484 public void run() { | |
| 485 createAndShowGUI(); | |
| 486 } | |
| 487 }); | |
| 488 } | |
| 489 } |
