Coverage Report - org.argouml.uml.reveng.Import
 
Classes in this File Line Coverage Branch Coverage Complexity
Import
0%
0/152
0%
0/80
2.944
Import$1
0%
0/5
0%
0/2
2.944
Import$2
0%
0/3
N/A
2.944
Import$ConfigPanelExtension
0%
0/66
0%
0/26
2.944
Import$ConfigPanelExtension$1
0%
0/3
N/A
2.944
Import$ConfigPanelExtension$2
0%
0/4
N/A
2.944
Import$ConfigPanelExtension$3
0%
0/3
N/A
2.944
Import$ImportFileChooser
0%
0/40
0%
0/8
2.944
Import$SelectedLanguageListener
0%
0/14
0%
0/2
2.944
 
 1  
 /* $Id: Import.java 17870 2010-01-12 20:49:32Z linus $
 2  
  *****************************************************************************
 3  
  * Copyright (c) 2009 Contributors - see below
 4  
  * All rights reserved. This program and the accompanying materials
 5  
  * are made available under the terms of the Eclipse Public License v1.0
 6  
  * which accompanies this distribution, and is available at
 7  
  * http://www.eclipse.org/legal/epl-v10.html
 8  
  *
 9  
  * Contributors:
 10  
  *    linus
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 1996-2008 The Regents of the University of California. All
 17  
 // Rights Reserved. Permission to use, copy, modify, and distribute this
 18  
 // software and its documentation without fee, and without a written
 19  
 // agreement is hereby granted, provided that the above copyright notice
 20  
 // and this paragraph appear in all copies.  This software program and
 21  
 // documentation are copyrighted by The Regents of the University of
 22  
 // California. The software program and documentation are supplied "AS
 23  
 // IS", without any accompanying services from The Regents. The Regents
 24  
 // does not warrant that the operation of the program will be
 25  
 // uninterrupted or error-free. The end-user understands that the program
 26  
 // was developed for research purposes and is advised not to rely
 27  
 // exclusively on the program for any reason.  IN NO EVENT SHALL THE
 28  
 // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 29  
 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 30  
 // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 31  
 // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 32  
 // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 33  
 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 34  
 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 35  
 // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 36  
 // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
 37  
 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 38  
 
 39  
 package org.argouml.uml.reveng;
 40  
 
 41  
 import java.awt.BorderLayout;
 42  
 import java.awt.Frame;
 43  
 import java.awt.GridBagConstraints;
 44  
 import java.awt.GridBagLayout;
 45  
 import java.awt.Insets;
 46  
 import java.awt.event.ActionEvent;
 47  
 import java.awt.event.ActionListener;
 48  
 import java.awt.event.FocusEvent;
 49  
 import java.awt.event.FocusListener;
 50  
 import java.io.File;
 51  
 import java.nio.charset.Charset;
 52  
 import java.util.List;
 53  
 import java.util.StringTokenizer;
 54  
 
 55  
 import javax.swing.ButtonGroup;
 56  
 import javax.swing.JCheckBox;
 57  
 import javax.swing.JComboBox;
 58  
 import javax.swing.JComponent;
 59  
 import javax.swing.JDialog;
 60  
 import javax.swing.JFileChooser;
 61  
 import javax.swing.JLabel;
 62  
 import javax.swing.JPanel;
 63  
 import javax.swing.JRadioButton;
 64  
 import javax.swing.JTabbedPane;
 65  
 import javax.swing.JTextField;
 66  
 import javax.swing.filechooser.FileSystemView;
 67  
 
 68  
 import org.argouml.application.api.Argo;
 69  
 import org.argouml.configuration.Configuration;
 70  
 import org.argouml.i18n.Translator;
 71  
 import org.argouml.moduleloader.ModuleInterface;
 72  
 import org.argouml.uml.reveng.SettingsTypes.BooleanSelection2;
 73  
 import org.argouml.uml.reveng.SettingsTypes.PathListSelection;
 74  
 import org.argouml.uml.reveng.SettingsTypes.PathSelection;
 75  
 import org.argouml.uml.reveng.SettingsTypes.Setting;
 76  
 import org.argouml.uml.reveng.SettingsTypes.UniqueSelection2;
 77  
 import org.argouml.uml.reveng.SettingsTypes.UserString2;
 78  
 import org.argouml.uml.reveng.ui.ImportClasspathDialog;
 79  
 import org.argouml.uml.reveng.ui.ImportStatusScreen;
 80  
 import org.argouml.util.SuffixFilter;
 81  
 import org.argouml.util.UIUtils;
 82  
 import org.tigris.gef.base.Globals;
 83  
 import org.tigris.swidgets.GridLayout2;
 84  
 
 85  
 
 86  
 
 87  
 /**
 88  
  * This is the main class for the Swing importer framework.  It extends 
 89  
  * ImportCommon which contains all the GUI independent pieces of the import
 90  
  * framework.  
 91  
  * <p>
 92  
  * The Service Providers Interface (SPI) to the individual language importers
 93  
  * is defined in such a way that they can be completely GUI independent as
 94  
  * well, receiving lists of source files and settings for the import and 
 95  
  * reporting progress via progress monitor API.
 96  
  * 
 97  
  * It provides JPanels for tailoring the import run in the FileChooser.
 98  
  * <p>
 99  
  * 
 100  
  * The Import run is started by calling doFile(Project, File)
 101  
  * <p>
 102  
  * 
 103  
  * Supports recursive search in folder for source files with matching 
 104  
  * extensions.
 105  
  * <p>
 106  
  * 
 107  
  * There are three levels of detail for import:
 108  
  * <p>
 109  
  * 
 110  
  * <ol>
 111  
  * <li> 0 - classifiers only
 112  
  * <li> 1 - classifiers plus feature specifications
 113  
  * <li> 2 - full import, feature detail (ie. operations with methods)
 114  
  * </ol>
 115  
  * 
 116  
  * @author Andreas Rueckert a_rueckert@gmx.net
 117  
  * @author Tom Morris <tfmorris@gmail.com>
 118  
  */
 119  0
 public class Import extends ImportCommon implements ImportSettings {
 120  
 
 121  
     private JComponent configPanel;
 122  
 
 123  
     private JCheckBox descend;
 124  
 
 125  
     private JCheckBox changedOnly;
 126  
 
 127  
     private JCheckBox createDiagrams;
 128  
 
 129  
     private JCheckBox minimiseFigs;
 130  
 
 131  
     private JCheckBox layoutDiagrams;
 132  
 
 133  
     // level 0 import detail
 134  
     private JRadioButton classOnly;
 135  
 
 136  
     // level 1 import detail
 137  
     private JRadioButton classAndFeatures;
 138  
 
 139  
     // level 2 import detail
 140  
     private JRadioButton fullImport;
 141  
 
 142  
     private JComboBox sourceEncoding;
 143  
     
 144  
     private JDialog dialog;
 145  
 
 146  
     private ImportStatusScreen iss;
 147  
 
 148  
     private Frame myFrame;
 149  
 
 150  
     /**
 151  
      * Creates dialog window with chooser and configuration panel.
 152  
      * 
 153  
      * @param frame the ui frame to display dialogs on
 154  
      */
 155  
     public Import(Frame frame) {
 156  0
         super();
 157  0
         myFrame = frame;
 158  
 
 159  
         // TODO: this needs to be improved
 160  
         // even for nongui calling, the config panel needs to be initialized:
 161  0
         getConfigPanel();
 162  
         
 163  0
         if (frame != null) {
 164  0
             JComponent chooser = getChooser();
 165  0
             dialog =
 166  
                 new JDialog(frame,
 167  
                         Translator.localize("action.import-sources"), true);
 168  
     
 169  0
             dialog.getContentPane().add(chooser, BorderLayout.CENTER);
 170  0
             dialog.getContentPane().add(getConfigPanel(), BorderLayout.EAST);
 171  0
             dialog.pack();
 172  0
             int x = (frame.getSize().width - dialog.getSize().width) / 2;
 173  0
             int y = (frame.getSize().height - dialog.getSize().height) / 2;
 174  0
             dialog.setLocation(x > 0 ? x : 0, y > 0 ? y : 0);
 175  
     
 176  0
             UIUtils.loadCommonKeyMap(dialog);
 177  
     
 178  0
             dialog.setVisible(true);
 179  
         }
 180  0
     }
 181  
 
 182  
     /*
 183  
      * @see org.argouml.uml.reveng.ImportSettings#getInputSourceEncoding()
 184  
      */
 185  
     public String getInputSourceEncoding() {
 186  0
         return (String) sourceEncoding.getSelectedItem();
 187  
     }
 188  
 
 189  
     /**
 190  
      * Close dialog window.
 191  
      */
 192  
     private void disposeDialog() {
 193  0
         StringBuffer flags = new StringBuffer(30);
 194  0
         flags.append(isDescendSelected()).append(",");
 195  0
         flags.append(isChangedOnlySelected()).append(",");
 196  0
         flags.append(isCreateDiagramsSelected()).append(",");
 197  0
         flags.append(isMinimizeFigsSelected()).append(",");
 198  0
         flags.append(isDiagramLayoutSelected());
 199  0
         Configuration.setString(Argo.KEY_IMPORT_GENERAL_SETTINGS_FLAGS, flags
 200  
                 .toString());
 201  0
         Configuration.setString(Argo.KEY_IMPORT_GENERAL_DETAIL_LEVEL, String
 202  
                 .valueOf(getImportLevel()));
 203  0
         Configuration.setString(Argo.KEY_INPUT_SOURCE_ENCODING,
 204  
                 getInputSourceEncoding());
 205  0
         dialog.setVisible(false);
 206  0
         dialog.dispose();
 207  0
     }
 208  
 
 209  
     /**
 210  
      * Get the panel that lets the user set reverse engineering parameters.
 211  
      * 
 212  
      * @param importInstance the instance of the import
 213  
      * @return the panel This is an internal method. Use the accessors in
 214  
      *         {@link ImportSettings} to determine the current settings.
 215  
      */
 216  
     private JComponent getConfigPanel() {
 217  
 
 218  0
         final JTabbedPane tab = new JTabbedPane();
 219  
 
 220  
         // build the configPanel:
 221  0
         if (configPanel == null) {
 222  0
             JPanel general = new JPanel();
 223  0
             general.setLayout(new GridLayout2(20, 1, 0, 0, GridLayout2.NONE));
 224  
 
 225  0
             general.add(new JLabel(Translator
 226  
                     .localize("action.import-select-lang")));
 227  
 
 228  0
             JComboBox selectedLanguage = new JComboBox(getModules().keySet()
 229  
                     .toArray());
 230  0
             selectedLanguage
 231  
                     .addActionListener(new SelectedLanguageListener(tab));
 232  0
             general.add(selectedLanguage);
 233  
 
 234  0
             addConfigCheckboxes(general);
 235  
 
 236  0
             addDetailLevelButtons(general);
 237  
 
 238  0
             addSourceEncoding(general);
 239  
 
 240  0
             tab.add(general, Translator.localize("action.import-general"));
 241  
 
 242  0
             ImportInterface current = getCurrentModule();
 243  0
             if (current != null) {
 244  0
                 tab.add(getConfigPanelExtension(),
 245  
                         current.getName());            
 246  
             }
 247  0
             configPanel = tab;
 248  
         }
 249  0
         return configPanel;
 250  
 
 251  
     }
 252  
 
 253  
 
 254  
     private void addConfigCheckboxes(JPanel panel) {
 255  0
         boolean desc = true;
 256  0
         boolean chan = true;
 257  0
         boolean crea = true;
 258  0
         boolean mini = true;
 259  0
         boolean layo = true;
 260  0
         String flags = Configuration
 261  
                 .getString(Argo.KEY_IMPORT_GENERAL_SETTINGS_FLAGS);
 262  0
         if (flags != null && flags.length() > 0) {
 263  0
             StringTokenizer st = new StringTokenizer(flags, ",");
 264  0
             if (st.hasMoreTokens() && st.nextToken().equals("false")) {
 265  0
                 desc = false;
 266  
             }
 267  0
             if (st.hasMoreTokens() && st.nextToken().equals("false")) {
 268  0
                 chan = false;
 269  
             }
 270  0
             if (st.hasMoreTokens() && st.nextToken().equals("false")) {
 271  0
                 crea = false;
 272  
             }
 273  0
             if (st.hasMoreTokens() && st.nextToken().equals("false")) {
 274  0
                 mini = false;
 275  
             }
 276  0
             if (st.hasMoreTokens() && st.nextToken().equals("false")) {
 277  0
                 layo = false;
 278  
             }
 279  
         }
 280  
 
 281  0
         descend = new JCheckBox(Translator
 282  
                 .localize("action.import-option-descend-dir-recur"), desc);
 283  0
         panel.add(descend);
 284  
 
 285  0
         changedOnly = new JCheckBox(Translator
 286  
                 .localize("action.import-option-changed_new"), chan);
 287  0
         panel.add(changedOnly);
 288  
 
 289  0
         createDiagrams = new JCheckBox(Translator
 290  
                 .localize("action.import-option-create-diagram"), crea);
 291  0
         panel.add(createDiagrams);
 292  
 
 293  0
         minimiseFigs = new JCheckBox(Translator
 294  
                 .localize("action.import-option-min-class-icon"), mini);
 295  0
         panel.add(minimiseFigs);
 296  
 
 297  0
         layoutDiagrams = new JCheckBox(Translator.localize(
 298  
                 "action.import-option-perform-auto-diagram-layout"),
 299  
                 layo);
 300  0
         panel.add(layoutDiagrams);
 301  
 
 302  
         // de-selects the fig minimising & layout
 303  
         // if we are not creating diagrams
 304  0
         createDiagrams.addActionListener(new ActionListener() {
 305  
             public void actionPerformed(ActionEvent actionEvent) {
 306  0
                 if (!createDiagrams.isSelected()) {
 307  0
                     minimiseFigs.setSelected(false);
 308  0
                     layoutDiagrams.setSelected(false);
 309  
                 }
 310  0
             }
 311  
         });
 312  0
     }
 313  
 
 314  
 
 315  
     private void addDetailLevelButtons(JPanel panel) {
 316  
         // select the level of import
 317  
         // 0 - classifiers only
 318  
         // 1 - classifiers plus feature specifications
 319  
         // 2 - full import, feature detail
 320  
 
 321  0
         JLabel importDetailLabel = new JLabel(Translator
 322  
                 .localize("action.import-level-of-import-detail"));
 323  0
         ButtonGroup detailButtonGroup = new ButtonGroup();
 324  
 
 325  0
         classOnly = new JRadioButton(Translator
 326  
                 .localize("action.import-option-classifiers"));
 327  0
         detailButtonGroup.add(classOnly);
 328  
 
 329  0
         classAndFeatures = new JRadioButton(Translator
 330  
                 .localize("action.import-option-classifiers-plus-specs"));
 331  0
         detailButtonGroup.add(classAndFeatures);
 332  
 
 333  0
         fullImport = new JRadioButton(Translator
 334  
                 .localize("action.import-option-full-import"));
 335  0
         String detaillevel = Configuration
 336  
                 .getString(Argo.KEY_IMPORT_GENERAL_DETAIL_LEVEL);
 337  0
         if ("0".equals(detaillevel)) {
 338  0
             classOnly.setSelected(true);
 339  0
         } else if ("1".equals(detaillevel)) {
 340  0
             classAndFeatures.setSelected(true);
 341  
         } else {
 342  0
             fullImport.setSelected(true);
 343  
         }
 344  0
         detailButtonGroup.add(fullImport);
 345  
 
 346  0
         panel.add(importDetailLabel);
 347  0
         panel.add(classOnly);
 348  0
         panel.add(classAndFeatures);
 349  0
         panel.add(fullImport);
 350  0
     }
 351  
     
 352  
     
 353  
     private void addSourceEncoding(JPanel panel) {
 354  0
         panel.add(new JLabel(
 355  
                 Translator.localize("action.import-file-encoding")));
 356  0
         String enc =
 357  
             Configuration.getString(Argo.KEY_INPUT_SOURCE_ENCODING);
 358  0
         if (enc == null || enc.trim().equals("")) {
 359  0
             enc = System.getProperty("file.encoding");
 360  
         }
 361  
         // cp1252 is often the default, but windows-1252 is the name listed
 362  
         // by Charset.availableCharsets
 363  0
         if (enc.startsWith("cp")) {
 364  0
             enc = "windows-" + enc.substring(2);
 365  
         }
 366  
 
 367  0
         sourceEncoding = new JComboBox(Charset
 368  
                 .availableCharsets().keySet().toArray());
 369  0
         sourceEncoding.setSelectedItem(enc);
 370  0
         panel.add(sourceEncoding);
 371  0
     }
 372  
     
 373  
     /*
 374  
      * Get the extension panel for the configuration settings.
 375  
      */
 376  
     private JComponent getConfigPanelExtension() {
 377  0
         List<Setting> settings = null;
 378  0
         ImportInterface current = getCurrentModule();
 379  0
         if (current != null) {
 380  0
             settings = current.getImportSettings();
 381  
         }
 382  0
         return  new ConfigPanelExtension(settings);
 383  
     }
 384  
 
 385  
     private class SelectedLanguageListener implements ActionListener {
 386  
 
 387  
         /**
 388  
          * The pane.
 389  
          */
 390  
         private JTabbedPane tab;
 391  
 
 392  
         /**
 393  
          * The constructor.
 394  
          * 
 395  
          * @param i The current import.
 396  
          * @param t The pane.
 397  
          */
 398  0
         SelectedLanguageListener(JTabbedPane t) {
 399  0
             tab = t;
 400  0
         }
 401  
 
 402  
         /*
 403  
          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
 404  
          */
 405  
         public void actionPerformed(ActionEvent e) {
 406  0
             JComboBox cb = (JComboBox) e.getSource();
 407  0
             String selected = (String) cb.getSelectedItem();
 408  0
             ImportInterface oldModule = getCurrentModule();
 409  0
             setCurrentModule(getModules().get(selected));
 410  0
             updateFilters((JFileChooser) dialog.getContentPane()
 411  
                     .getComponent(0), oldModule.getSuffixFilters(),
 412  
                     getCurrentModule().getSuffixFilters());
 413  0
             updateTabbedPane();
 414  0
         }
 415  
         
 416  
         private void updateTabbedPane() {
 417  0
             String name = ((ModuleInterface) getCurrentModule()).getName();
 418  0
             if (tab.indexOfTab(name) < 0) {
 419  0
                 tab.add(getConfigPanelExtension(), name);
 420  
             }
 421  0
         }
 422  
     }
 423  
     
 424  
 
 425  
 
 426  
     /**
 427  
      * Parse all selected files in a separate thread. It calls the actual
 428  
      * parser methods depending on the type of the file.
 429  
      */
 430  
     public void doFile() {
 431  0
         iss = new ImportStatusScreen(myFrame, "Importing", "Splash");
 432  0
         Thread t = new Thread(new Runnable() {
 433  
             public void run() {
 434  0
                 doImport(iss);
 435  
                 // ExplorerEventAdaptor.getInstance().structureChanged();
 436  
                 // ProjectBrowser.getInstance().getStatusBar().showProgress(0);
 437  0
             }
 438  
         }, "Import Thread");
 439  0
         t.start();
 440  0
     }
 441  
 
 442  
     /*
 443  
      * @see org.argouml.uml.reveng.ImportCommon#getImportLevel()
 444  
      */
 445  
     public int getImportLevel() {
 446  0
         if (classOnly != null && classOnly.isSelected()) {
 447  0
             return ImportSettings.DETAIL_CLASSIFIER;
 448  0
         } else if (classAndFeatures != null && classAndFeatures.isSelected()) {
 449  0
             return ImportSettings.DETAIL_CLASSIFIER_FEATURE;
 450  0
         } else if (fullImport != null && fullImport.isSelected()) {
 451  0
             return ImportSettings.DETAIL_FULL;
 452  
         } else {
 453  0
             return ImportSettings.DETAIL_CLASSIFIER;
 454  
         }
 455  
     }
 456  
 
 457  
     /*
 458  
      * @see org.argouml.uml.reveng.ImportCommon#isCreateDiagramsChecked()
 459  
      */
 460  
     public boolean isCreateDiagramsSelected() {
 461  0
         if (createDiagrams != null) {
 462  0
             return createDiagrams.isSelected();
 463  
         }
 464  0
         return true;
 465  
     }
 466  
 
 467  
     /*
 468  
      * @see org.argouml.uml.reveng.ImportCommon#isMinimiseFigsChecked()
 469  
      */
 470  
     public boolean isMinimizeFigsSelected() {
 471  0
         if (minimiseFigs != null) {
 472  0
             return minimiseFigs.isSelected();
 473  
         }
 474  0
         return false;
 475  
     }
 476  
 
 477  
     /*
 478  
      * @see org.argouml.uml.reveng.ImportCommon#isDiagramLayoutSelected()
 479  
      */
 480  
     public boolean isDiagramLayoutSelected() {
 481  0
         if (layoutDiagrams != null) {
 482  0
             return layoutDiagrams.isSelected();
 483  
         }
 484  0
         return false;
 485  
     }
 486  
 
 487  
     /*
 488  
      * @see org.argouml.uml.reveng.ImportCommon#isDescendSelected()
 489  
      */
 490  
     public boolean isDescendSelected() {
 491  0
         if (descend != null) {
 492  0
             return descend.isSelected();
 493  
         }
 494  0
         return true;
 495  
     }
 496  
 
 497  
     /*
 498  
      * @see org.argouml.uml.reveng.ImportCommon#isChangedOnlySelected()
 499  
      */
 500  
     public boolean isChangedOnlySelected() {
 501  0
         if (changedOnly != null) {
 502  0
             return changedOnly.isSelected();
 503  
         }
 504  0
         return false;
 505  
     }
 506  
 
 507  
     /*
 508  
      * Create chooser for objects we are to import. Old style modules get to
 509  
      * provide their own (although I don't believe any of them do), while new
 510  
      * style modules get the a chooser provided by us (which matches what the
 511  
      * abstract class FileImportSupport used to provide).
 512  
      */
 513  
     private JComponent getChooser() {
 514  0
         String directory = Globals.getLastDirectory();
 515  
 
 516  0
         final JFileChooser chooser = new ImportFileChooser(this, directory);
 517  
 
 518  0
         chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
 519  0
         ImportInterface current = getCurrentModule();
 520  
 
 521  0
         if (current != null) {
 522  0
             updateFilters(chooser, null, current.getSuffixFilters());
 523  
         }
 524  0
         return chooser;
 525  
     }
 526  
 
 527  
     private static void updateFilters(JFileChooser chooser,
 528  
             SuffixFilter[] oldFilters, SuffixFilter[] newFilters) {
 529  0
         if (oldFilters != null) {
 530  0
             for (int i = 0; i < oldFilters.length; i++) {
 531  0
                 chooser.removeChoosableFileFilter(oldFilters[i]);
 532  
             }
 533  
         }
 534  0
         if (newFilters != null) {
 535  0
             for (int i = 0; i < newFilters.length; i++) {
 536  0
                 chooser.addChoosableFileFilter(newFilters[i]);
 537  
             }
 538  0
             if (newFilters.length > 0) {
 539  0
                 chooser.setFileFilter(newFilters[0]);
 540  
             }
 541  
         }
 542  0
     }
 543  
 
 544  
     private static class ImportFileChooser extends JFileChooser {
 545  
 
 546  
         private Import theImport;
 547  
 
 548  
         /**
 549  
          * Constructs a new ImportFileChooser opened to the given directory.
 550  
          * 
 551  
          * @param imp the import manager
 552  
          * @param currentDirectoryPath the directory path
 553  
          * @see javax.swing.JFileChooser#JFileChooser(String)
 554  
          */
 555  
         public ImportFileChooser(Import imp, String currentDirectoryPath) {
 556  0
             super(currentDirectoryPath);
 557  0
             theImport = imp;
 558  0
             initChooser();
 559  0
         }
 560  
 
 561  
         /**
 562  
          * Constructs a JFileChooser using the given current directory path and
 563  
          * FileSystemView.
 564  
          * 
 565  
          * @param imp the import manager
 566  
          * @param currentDirectoryPath the directory path
 567  
          * @param fsv the file system view
 568  
          * @see javax.swing.JFileChooser#JFileChooser(String, FileSystemView)
 569  
          */
 570  
         public ImportFileChooser(Import imp, String currentDirectoryPath,
 571  
                 FileSystemView fsv) {
 572  0
             super(currentDirectoryPath, fsv);
 573  0
             theImport = imp;
 574  0
             initChooser();
 575  0
         }
 576  
 
 577  
         /**
 578  
          * Constructs a new default ImportFileChooser.
 579  
          * 
 580  
          * @param imp the import manager
 581  
          * @see javax.swing.JFileChooser#JFileChooser()
 582  
          */
 583  
         public ImportFileChooser(Import imp) {
 584  0
             super();
 585  0
             theImport = imp;
 586  0
             initChooser();
 587  0
         }
 588  
 
 589  
         /**
 590  
          * Constructs a JFileChooser using the given FileSystemView.
 591  
          * 
 592  
          * @param imp the import manager
 593  
          * @param fsv the file system view
 594  
          * @see javax.swing.JFileChooser#JFileChooser(FileSystemView)
 595  
          */
 596  
         public ImportFileChooser(Import imp, FileSystemView fsv) {
 597  0
             super(fsv);
 598  0
             theImport = imp;
 599  0
             initChooser();
 600  0
         }
 601  
 
 602  
         private void initChooser() {
 603  0
             setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
 604  0
             setMultiSelectionEnabled(true);
 605  0
             setSelectedFile(getCurrentDirectory());
 606  0
         }
 607  
 
 608  
         /*
 609  
          * @see javax.swing.JFileChooser#approveSelection()
 610  
          */
 611  
         @Override
 612  
         public void approveSelection() {
 613  0
             File[] files = getSelectedFiles();
 614  0
             File dir = getCurrentDirectory();
 615  0
             if (files.length == 0) {
 616  0
                 files = new File[] {dir};
 617  
             }
 618  0
             if (files.length == 1) {
 619  0
                 File file = files[0];
 620  0
                 if (file != null && file.isDirectory()) {
 621  0
                     dir = file;
 622  
                 } else {
 623  0
                     dir = file.getParentFile();
 624  
                 }
 625  
             }
 626  0
             theImport.setSelectedFiles(files);
 627  
             try {
 628  0
                 theImport.setSelectedSuffixFilter(
 629  
                         (SuffixFilter) getFileFilter());
 630  0
             } catch (Exception e) {
 631  
                 // this is because of the (senseless?) "All files" FileFilter
 632  0
                 theImport.setSelectedSuffixFilter(null);
 633  0
             }
 634  0
             Globals.setLastDirectory(dir.getPath());
 635  0
             theImport.disposeDialog();
 636  
 
 637  0
             theImport.doFile();
 638  0
         }
 639  
 
 640  
         /*
 641  
          * @see javax.swing.JFileChooser#cancelSelection()
 642  
          */
 643  
         @Override
 644  
         public void cancelSelection() {
 645  0
             theImport.disposeDialog();
 646  0
         }
 647  
 
 648  
     }
 649  
 
 650  
     /**
 651  
      * Extended configuration panel for file import.  Built based on settings
 652  
      * requested by the specific language importer.
 653  
      */
 654  
     class ConfigPanelExtension extends JPanel {
 655  
 
 656  
 
 657  
         /**
 658  
          * Construct the configuration extension panel.
 659  
          * @param settings A list of settings requested by the language importer
 660  
          */
 661  0
         public ConfigPanelExtension(final List<Setting> settings) {
 662  
 
 663  0
             setLayout(new GridBagLayout());
 664  
             
 665  0
             if (settings == null || settings.size() == 0) {
 666  0
                 JLabel label = new JLabel("No settings for this importer");
 667  0
                 add(label, createGridBagConstraints(true, false, false));
 668  0
                 add(new JPanel(), createGridBagConstraintsFinal()); 
 669  0
                 return;
 670  
             }
 671  
 
 672  0
             for (Setting setting : settings) {
 673  0
                 if (setting instanceof UniqueSelection2) {
 674  0
                     JLabel label = new JLabel(setting.getLabel());
 675  0
                     add(label, createGridBagConstraints(true, false, false));
 676  
 
 677  0
                     final UniqueSelection2 us = (UniqueSelection2) setting;
 678  0
                     ButtonGroup group = new ButtonGroup();
 679  0
                     int count = 0;
 680  0
                     for (String option : us.getOptions()) {
 681  0
                         JRadioButton button = new JRadioButton(option);
 682  0
                         final int index = count++;
 683  0
                         if (us.getDefaultSelection() == index) {
 684  0
                             button.setSelected(true);
 685  
                         }
 686  0
                         group.add(button);
 687  0
                         button.addActionListener(new ActionListener() {
 688  
                             public void actionPerformed(ActionEvent e) {
 689  0
                                 us.setSelection(index);
 690  0
                             }
 691  
                         });
 692  0
                         add(button, createGridBagConstraints(false, false,
 693  
                                 false));
 694  0
                     }
 695  0
                 } else if (setting instanceof UserString2) {
 696  0
                     JLabel label = new JLabel(setting.getLabel());
 697  0
                     add(label, createGridBagConstraints(true, false, false));
 698  0
                     final UserString2 us = (UserString2) setting;
 699  0
                     final JTextField text = 
 700  
                         new JTextField(us.getDefaultString());
 701  0
                     text.addFocusListener(new FocusListener() {
 702  0
                         public void focusGained(FocusEvent e) { } 
 703  
                         public void focusLost(FocusEvent e) {
 704  0
                             us.setUserString(text.getText());           
 705  0
                         }
 706  
                         
 707  
                     });
 708  0
                     add(text, createGridBagConstraints(true, false, false));
 709  0
                 } else if (setting instanceof BooleanSelection2) {
 710  0
                     final BooleanSelection2 bs = (BooleanSelection2) setting;
 711  0
                     final JCheckBox button = new JCheckBox(setting.getLabel());
 712  0
                     button.setEnabled(bs.isSelected());
 713  0
                     button.addActionListener(new ActionListener() {
 714  
                         public void actionPerformed(ActionEvent e) {
 715  0
                             bs.setSelected(button.isSelected());
 716  0
                         }
 717  
                     });
 718  0
                     add(button, createGridBagConstraints(true, false, false));
 719  0
                 } else if (setting instanceof PathSelection) {
 720  0
                     JLabel label = new JLabel(setting.getLabel());
 721  0
                     add(label, createGridBagConstraints(true, false, false));
 722  0
                     PathSelection ps = (PathSelection) setting;
 723  
                     // TODO: Need to add FileChooser 
 724  0
                     JTextField text = new JTextField(ps.getDefaultPath());
 725  0
                     add(text, createGridBagConstraints(true, false, false));
 726  
                     // TODO: Update setting
 727  0
                 } else if (setting instanceof PathListSelection) {
 728  0
                     PathListSelection pls = (PathListSelection) setting;
 729  0
                     add(new ImportClasspathDialog(pls),
 730  
                             createGridBagConstraints(true, false, false));
 731  0
                 } else {
 732  0
                     throw new RuntimeException("Unknown setting type requested "
 733  
                             + setting);
 734  
                 }
 735  
             }
 736  0
             add(new JPanel(), createGridBagConstraintsFinal()); 
 737  0
         }
 738  
 
 739  
 
 740  
         /**
 741  
          * Create a GridBagConstraints object to use with the layout.
 742  
          * 
 743  
          * @param topInset true to use a top inset 
 744  
          * @param bottomInset true to use a bottom inset
 745  
          * @param fill true to fill (horizontally)
 746  
          * @return the grid bag constraints
 747  
          */
 748  
         private GridBagConstraints createGridBagConstraints(boolean topInset,
 749  
                 boolean bottomInset, boolean fill) {
 750  0
             GridBagConstraints gbc = new GridBagConstraints();
 751  0
             gbc.gridx = GridBagConstraints.RELATIVE;
 752  0
             gbc.gridy = GridBagConstraints.RELATIVE;
 753  0
             gbc.gridwidth = GridBagConstraints.REMAINDER;
 754  0
             gbc.gridheight = 1;
 755  0
             gbc.weightx = 1.0;
 756  0
             gbc.weighty = 0.0;
 757  0
             gbc.anchor = GridBagConstraints.NORTHWEST;
 758  0
             gbc.fill = fill ? GridBagConstraints.HORIZONTAL
 759  
                     : GridBagConstraints.NONE;
 760  0
             gbc.insets = 
 761  
                 new Insets(
 762  
                         topInset ? 5 : 0, 
 763  
                                 5, 
 764  
                                 bottomInset ? 5 : 0, 
 765  
                                         5);
 766  0
             gbc.ipadx = 0;
 767  0
             gbc.ipady = 0;
 768  0
             return gbc;
 769  
         }
 770  
 
 771  
         /**
 772  
          * A GridBagConstraints for the last item to take up the rest of the
 773  
          * space.
 774  
          * 
 775  
          * @return the GridBagConstraints object
 776  
          */
 777  
         private GridBagConstraints createGridBagConstraintsFinal() {
 778  0
             GridBagConstraints gbc = createGridBagConstraints(false, true,
 779  
                     false);
 780  0
             gbc.gridheight = GridBagConstraints.REMAINDER;
 781  0
             gbc.weighty = 1.0;
 782  0
             return gbc;
 783  
         }
 784  
 
 785  
     }
 786  
 
 787  
 
 788  
 
 789  
 }