Coverage Report - org.argouml.uml.reveng.ImportCommon
 
Classes in this File Line Coverage Branch Coverage Complexity
ImportCommon
0%
0/193
0%
0/124
2.972
 
 1  
 /* $Id: ImportCommon.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  
  *    bobtarling
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 2006, 2009 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.io.File;
 42  
 import java.io.PrintWriter;
 43  
 import java.io.StringWriter;
 44  
 import java.util.ArrayList;
 45  
 import java.util.Arrays;
 46  
 import java.util.Collection;
 47  
 import java.util.Collections;
 48  
 import java.util.HashSet;
 49  
 import java.util.Hashtable;
 50  
 import java.util.List;
 51  
 import java.util.StringTokenizer;
 52  
 
 53  
 import org.argouml.application.api.Argo;
 54  
 import org.argouml.cognitive.Designer;
 55  
 import org.argouml.configuration.Configuration;
 56  
 import org.argouml.i18n.Translator;
 57  
 import org.argouml.kernel.Project;
 58  
 import org.argouml.kernel.ProjectManager;
 59  
 import org.argouml.model.Model;
 60  
 import org.argouml.taskmgmt.ProgressMonitor;
 61  
 import org.argouml.ui.explorer.ExplorerEventAdaptor;
 62  
 import org.argouml.ui.targetmanager.TargetManager;
 63  
 import org.argouml.uml.diagram.ArgoDiagram;
 64  
 import org.argouml.uml.diagram.static_structure.ClassDiagramGraphModel;
 65  
 import org.argouml.uml.diagram.static_structure.layout.ClassdiagramLayouter;
 66  
 import org.argouml.util.SuffixFilter;
 67  
 import org.tigris.gef.base.Globals;
 68  
 
 69  
 /**
 70  
  * Source language import class - GUI independent superclass.
 71  
  * <p>
 72  
  * Specific Swing and SWT/Eclipse importers will extend this class.
 73  
  * <p>
 74  
  * <em>NOTE:</em>Any change to the public API here must be tested in both Swing
 75  
  * (standalone ArgoUML) and Eclipse (ArgoEclipse) environments.
 76  
  * 
 77  
  * @author Tom Morris
 78  
  */
 79  
 public abstract class ImportCommon implements ImportSettingsInternal {
 80  
 
 81  
     /**
 82  
      * The % maximum progress required to preparing for import.
 83  
      */
 84  
     protected static final int MAX_PROGRESS_PREPARE = 1;
 85  
 
 86  
     /**
 87  
      * The % maximum progress required to import.
 88  
      */
 89  
     protected static final int MAX_PROGRESS_IMPORT = 99;
 90  
 
 91  
     protected static final int MAX_PROGRESS = MAX_PROGRESS_PREPARE
 92  
             + MAX_PROGRESS_IMPORT;
 93  
     /**
 94  
      * keys are module name, values are PluggableImport instance.
 95  
      */
 96  
     private Hashtable<String, ImportInterface> modules;
 97  
 
 98  
     /**
 99  
      * Current language module.
 100  
      */
 101  
     private ImportInterface currentModule;
 102  
 
 103  
 
 104  
     /**
 105  
      * Imported directory.
 106  
      */
 107  
     private String srcPath;
 108  
 
 109  
     /**
 110  
      * Create a interface to the current diagram.
 111  
      */
 112  
     private DiagramInterface diagramInterface;
 113  
 
 114  
     private File[] selectedFiles;
 115  
     
 116  
     private SuffixFilter selectedSuffixFilter;
 117  
 
 118  
     protected ImportCommon() {
 119  0
         super();
 120  0
         modules = new Hashtable<String, ImportInterface>();
 121  
 
 122  0
         for (ImportInterface importer : ImporterManager.getInstance()
 123  
                 .getImporters()) {
 124  0
             modules.put(importer.getName(), importer);
 125  
         }
 126  0
         if (modules.isEmpty()) {
 127  0
             throw new RuntimeException("Internal error. "
 128  
                     + "No importer modules found.");
 129  
         }
 130  
         // "Java" is the default module for historical reasons,
 131  
         // but it's not required to be there
 132  0
         currentModule = modules.get("Java");
 133  0
         if (currentModule == null) {
 134  0
             currentModule = modules.elements().nextElement();
 135  
         }
 136  0
     }
 137  
 
 138  
     /*
 139  
      * @see org.argouml.uml.reveng.ImportSettings#getImportLevel()
 140  
      */
 141  
     public abstract int getImportLevel();
 142  
 
 143  
 
 144  
     /**
 145  
      * Compute and cache the current diagram interface.
 146  
      */
 147  
     protected void initCurrentDiagram() {
 148  0
         diagramInterface = getCurrentDiagram();
 149  0
     }
 150  
 
 151  
     /**
 152  
      * Set target diagram.<p>
 153  
      *
 154  
      * @return selected diagram, if it is class diagram,
 155  
      * else return null.
 156  
      */
 157  
     private DiagramInterface getCurrentDiagram() {
 158  0
         DiagramInterface result = null;
 159  0
         if (Globals.curEditor().getGraphModel()
 160  
                 instanceof ClassDiagramGraphModel) {
 161  0
             result =  new DiagramInterface(Globals.curEditor());
 162  
         } else {
 163  0
             Project p =  ProjectManager.getManager().getCurrentProject();
 164  0
             TargetManager.getInstance().setTarget(p.getInitialTarget());
 165  
             // the previous line helps, but we better check again:
 166  0
             if (Globals.curEditor().getGraphModel()
 167  
                     instanceof ClassDiagramGraphModel) {
 168  0
                 result =  new DiagramInterface(Globals.curEditor());
 169  
             }
 170  
         }
 171  0
         return result;
 172  
     }
 173  
 
 174  
     /*
 175  
      * @see org.argouml.uml.reveng.ImportSettings#getInputSourceEncoding()
 176  
      */
 177  
     public abstract String getInputSourceEncoding();
 178  
 
 179  
 
 180  
     /**
 181  
      * Get the files.  We generate it based on their specified
 182  
      * file suffixes.
 183  
      * @param monitor progress monitor which can be used to cancel long running 
 184  
      * request
 185  
      * @return the list of files to be imported
 186  
      */
 187  
     protected List<File> getFileList(ProgressMonitor monitor) {
 188  0
         List<File> files = Arrays.asList(getSelectedFiles());
 189  0
         if (files.size() == 1) {
 190  0
             File file = files.get(0);
 191  0
             SuffixFilter suffixFilters[] = {selectedSuffixFilter};
 192  0
             if (suffixFilters[0] == null) {
 193  
                 // not a SuffixFilter selected, so we take all
 194  0
                 suffixFilters = currentModule.getSuffixFilters();
 195  
             }
 196  0
             files =
 197  
                 FileImportUtils.getList(
 198  
                         file, isDescendSelected(),
 199  
                         suffixFilters, monitor);
 200  0
             if (file.isDirectory()) {
 201  0
                 setSrcPath(file.getAbsolutePath());
 202  
             } else {
 203  0
                 setSrcPath(null);
 204  
             }
 205  
         }
 206  
 
 207  
 
 208  0
         if (isChangedOnlySelected()) {
 209  
             // filter out all unchanged files
 210  0
             Object model =
 211  
                 ProjectManager.getManager().getCurrentProject().getModel();
 212  0
             for (int i = files.size() - 1; i >= 0; i--) {
 213  0
                 File f = files.get(i);
 214  0
                 String fn = f.getAbsolutePath();
 215  0
                 String lm = String.valueOf(f.lastModified());
 216  
                 
 217  0
                 if (Model.getFacade().getUmlVersion().charAt(0) == '1') {
 218  
                     // TODO: Not yet working for UML2
 219  0
                     if (lm.equals(
 220  
                             Model.getFacade().getTaggedValueValue(model, fn))) {
 221  0
                         files.remove(i);
 222  
                     }
 223  
                 }
 224  
             }
 225  
         }
 226  
 
 227  0
         return files;
 228  
     }
 229  
 
 230  
     /**
 231  
      * Set path for processed directory.
 232  
      *
 233  
      * @param path the given path
 234  
      */
 235  
     public void setSrcPath(String path) {
 236  0
         srcPath = path;
 237  0
     }
 238  
 
 239  
     /**
 240  
      * @return path for processed directory.
 241  
      */
 242  
     public String getSrcPath() {
 243  0
         return srcPath;
 244  
     }
 245  
 
 246  
     /*
 247  
      * Create a TaggedValue with a tag/type matching our source module
 248  
      * filename and a value of the file's last modified timestamp.
 249  
      *
 250  
      * TODO: This functionality needs to be moved someplace useful if
 251  
      * it's needed, otherwise it can be deleted. - tfm - 20070217
 252  
      */
 253  
     private void setLastModified(Project project, File file) {
 254  
         // set the lastModified value
 255  0
         String fn = file.getAbsolutePath();
 256  0
         String lm = String.valueOf(file.lastModified());
 257  0
         if (lm != null) {
 258  0
             Model.getCoreHelper()
 259  
                 .setTaggedValue(project.getModel(), fn, lm);
 260  
         }
 261  0
     }
 262  
 
 263  
     /*
 264  
      * @see org.argouml.uml.reveng.ImportSettings#isCreateDiagramsSelected()
 265  
      */
 266  
     public abstract boolean isCreateDiagramsSelected();
 267  
 
 268  
     /*
 269  
      * @see org.argouml.uml.reveng.ImportSettings#isMinimiseFigsSelected()
 270  
      */
 271  
     public abstract boolean isMinimizeFigsSelected();
 272  
 
 273  
     /*
 274  
      * @see org.argouml.uml.reveng.ImportSettingsInternal#isDiagramLayoutSelected()
 275  
      */
 276  
     public abstract boolean isDiagramLayoutSelected();
 277  
 
 278  
     /*
 279  
      * @see org.argouml.uml.reveng.ImportSettingsInternal#isDescendSelected()
 280  
      */
 281  
     public abstract boolean isDescendSelected();
 282  
 
 283  
     /*
 284  
      * @see org.argouml.uml.reveng.ImportSettingsInternal#isChangedOnlySelected()
 285  
      */
 286  
     public abstract boolean isChangedOnlySelected();
 287  
 
 288  
     /**
 289  
      * Gets the specified import module.
 290  
      * 
 291  
      * @param importerName The import module's name
 292  
      * @return The found import module, otherwise null
 293  
      */
 294  
     public ImportInterface getImporter(String importerName) {
 295  0
         return getModules().get(importerName);
 296  
     }
 297  
 
 298  
     /**
 299  
      * Sets the files that will be imported.
 300  
      * 
 301  
      * @param files The array of files.
 302  
      */
 303  
     public void setFiles(File[] files) {
 304  0
         if (files != null) {
 305  0
             setSelectedFiles(files);
 306  
         }
 307  0
     }
 308  
 
 309  
     protected Hashtable<String, ImportInterface> getModules() {
 310  0
         return modules;
 311  
     }
 312  
 
 313  
     protected void setSelectedFiles(final File[] files) {
 314  0
         selectedFiles = files;
 315  0
     }
 316  
 
 317  
     /**
 318  
      * Set the selected (file) suffix filter.
 319  
      * 
 320  
      * @param suffixFilter the (file) suffix filter
 321  
      */
 322  
     protected void setSelectedSuffixFilter(final SuffixFilter suffixFilter) {
 323  0
         selectedSuffixFilter = suffixFilter;
 324  0
     }
 325  
 
 326  
     protected File[] getSelectedFiles() {
 327  0
         File[] copy = new File[selectedFiles.length];
 328  0
         for (int i = 0; i < selectedFiles.length; i++) {
 329  0
             copy[i] = selectedFiles[i];
 330  
         }
 331  0
         return copy;
 332  
         //return Arrays.copyOf(selectedFiles, selectedFiles.length);
 333  
     }
 334  
     
 335  
     /**
 336  
      * Sets the current import module.
 337  
      * 
 338  
      * @param module
 339  
      */
 340  
     public void setCurrentModule(ImportInterface module) {
 341  0
         currentModule = module;
 342  0
     }
 343  
 
 344  
     protected ImportInterface getCurrentModule() {
 345  0
         return currentModule;
 346  
     }
 347  
 
 348  
     /**
 349  
      * Returns the possible languages in which the user can import the sources.
 350  
      * @return a list of Strings with the names of the languages available
 351  
      */
 352  
     public List<String> getLanguages() {
 353  0
         return Collections.unmodifiableList(
 354  
                 new ArrayList<String>(modules.keySet()));
 355  
     }
 356  
 
 357  
     /**
 358  
      * The flag for: descend directories recursively.
 359  
      * This should be asked by the GUI for initialization.
 360  
      * @return the flag stored in KEY_IMPORT_GENERAL_SETTINGS_FLAGS key or
 361  
      * true if this is null.
 362  
      */
 363  
     public boolean isDescend() {
 364  0
         String flags =
 365  
                 Configuration.getString(
 366  
                         Argo.KEY_IMPORT_GENERAL_SETTINGS_FLAGS);
 367  0
         if (flags != null && flags.length() > 0) {
 368  0
             StringTokenizer st = new StringTokenizer(flags, ",");
 369  0
             if (st.hasMoreTokens() && st.nextToken().equals("false")) {
 370  0
                 return false;
 371  
             }
 372  
         }
 373  0
         return true;
 374  
     }
 375  
 
 376  
     /**
 377  
      * The flag for: changed/new files only.
 378  
      * This should be asked by the GUI for initialization.
 379  
      * @return the flag stored in KEY_IMPORT_GENERAL_SETTINGS_FLAGS key or
 380  
      * true if this is null.
 381  
      */
 382  
     public boolean isChangedOnly() {
 383  0
         String flags =
 384  
                 Configuration.getString(Argo.KEY_IMPORT_GENERAL_SETTINGS_FLAGS);
 385  0
         if (flags != null && flags.length() > 0) {
 386  0
             StringTokenizer st = new StringTokenizer(flags, ",");
 387  0
             skipTokens(st, 1);
 388  0
             if (st.hasMoreTokens() && st.nextToken().equals("false")) {
 389  0
                 return false;
 390  
             }
 391  
         }
 392  0
         return true;
 393  
     }
 394  
 
 395  
     /**
 396  
      * The flag for: create diagrams from imported code.
 397  
      * This should be asked by the GUI for initialization.
 398  
      * @return the flag stored in KEY_IMPORT_GENERAL_SETTINGS_FLAGS key or
 399  
      * true if this is null.
 400  
      */
 401  
     public boolean isCreateDiagrams() {
 402  0
         String flags =
 403  
                 Configuration.getString(
 404  
                         Argo.KEY_IMPORT_GENERAL_SETTINGS_FLAGS);
 405  0
         if (flags != null && flags.length() > 0) {
 406  0
             StringTokenizer st = new StringTokenizer(flags, ",");
 407  0
             skipTokens(st, 2);
 408  0
             if (st.hasMoreTokens() && st.nextToken().equals("false")) {
 409  0
                 return false;
 410  
             }
 411  
         }
 412  0
         return true;
 413  
     }
 414  
 
 415  
     /**
 416  
      * The flag for: minimise class icons in diagrams.
 417  
      * This should be asked by the GUI for initialization.
 418  
      * @return the flag stored in KEY_IMPORT_GENERAL_SETTINGS_FLAGS key or
 419  
      * true if this is null.
 420  
      */
 421  
     public boolean isMinimizeFigs() {
 422  0
         String flags =
 423  
                 Configuration.getString(
 424  
                         Argo.KEY_IMPORT_GENERAL_SETTINGS_FLAGS);
 425  0
         if (flags != null && flags.length() > 0) {
 426  0
             StringTokenizer st = new StringTokenizer(flags, ",");
 427  0
             skipTokens(st, 3);
 428  0
             if (st.hasMoreTokens() && st.nextToken().equals("false")) {
 429  0
                 return false;
 430  
             }
 431  
         }
 432  0
         return true;
 433  
     }
 434  
 
 435  
     private void skipTokens(StringTokenizer st, int count) {
 436  0
         for (int i = 0; i < count; i++) {
 437  0
             if (st.hasMoreTokens()) {
 438  0
                 st.nextToken();
 439  
             }
 440  
         }
 441  0
     }
 442  
 
 443  
     /**
 444  
      * The flag for: perform automatic diagram layout.
 445  
      * This should be asked by the GUI for initialization.
 446  
      * @return the flag stored in KEY_IMPORT_GENERAL_SETTINGS_FLAGS key or
 447  
      * true if this is null.
 448  
      */
 449  
     public boolean isDiagramLayout() {
 450  0
         String flags =
 451  
                 Configuration.getString(
 452  
                         Argo.KEY_IMPORT_GENERAL_SETTINGS_FLAGS);
 453  0
         if (flags != null && flags.length() > 0) {
 454  0
             StringTokenizer st = new StringTokenizer(flags, ",");
 455  0
             skipTokens(st, 4);
 456  0
             if (st.hasMoreTokens() && st.nextToken().equals("false")) {
 457  0
                 return false;
 458  
             }
 459  
         }
 460  0
         return true;
 461  
     }
 462  
 
 463  
     /**
 464  
      * The default encoding. This should be asked by the GUI for
 465  
      * initialization.
 466  
      * @return the encoding stored in Argo.KEY_INPUT_SOURCE_ENCODING key or if
 467  
      * this is null the default system encoding
 468  
      */
 469  
     public String getEncoding() {
 470  0
         String enc = Configuration.getString(Argo.KEY_INPUT_SOURCE_ENCODING);
 471  0
         if (enc == null || enc.trim().equals("")) { //$NON-NLS-1$
 472  0
             enc = System.getProperty("file.encoding"); //$NON-NLS-1$
 473  
         }
 474  
 
 475  0
         return enc;
 476  
     }
 477  
 
 478  
 
 479  
     /**
 480  
      * Layouts the diagrams.
 481  
      *
 482  
      * @param monitor
 483  
      *            the progress meter.  Null if not progress updates desired.
 484  
      * @param startingProgress
 485  
      *            the actual progress until now
 486  
      */
 487  
     public void layoutDiagrams(ProgressMonitor monitor, int startingProgress) {
 488  
 
 489  0
         if (diagramInterface == null) {
 490  0
             return;
 491  
         }
 492  
 //        if (monitor != null) {
 493  
 //            monitor.updateSubTask(ImportsMessages.layoutingAction);
 494  
 //        }
 495  0
         List<ArgoDiagram> diagrams = diagramInterface.getModifiedDiagramList();
 496  0
         int total = startingProgress + diagrams.size()
 497  
                 / 10;
 498  0
         for (int i = 0; i < diagrams.size(); i++) {
 499  0
             ArgoDiagram diagram = diagrams.get(i);
 500  0
             ClassdiagramLayouter layouter = new ClassdiagramLayouter(diagram);
 501  0
             layouter.layout();
 502  0
             int act = startingProgress + (i + 1) / 10;
 503  0
             int progress = MAX_PROGRESS_PREPARE
 504  
                     + MAX_PROGRESS_IMPORT * act / total;
 505  0
             if (monitor != null) {
 506  0
                 monitor.updateProgress(progress);
 507  
             }
 508  
 //          iss.setValue(countFiles + (i + 1) / 10);
 509  
         }
 510  
 
 511  0
     }
 512  
 
 513  
 
 514  
     /**
 515  
      * Import the selected source files. It calls the actual
 516  
      * parser methods depending on the type of the file.
 517  
      *
 518  
      * @param monitor
 519  
      *            a ProgressMonitor to both receive progress updates and to be
 520  
      *            polled for user requests to cancel.
 521  
      */
 522  
     public void doImport(ProgressMonitor monitor) {
 523  
         // Roughly equivalent to and derived from old Import.doFile()
 524  0
         monitor.setMaximumProgress(MAX_PROGRESS);
 525  0
         int progress = 0;
 526  0
         monitor.updateSubTask(Translator.localize("dialog.import.preImport"));
 527  0
         List<File> files = getFileList(monitor);
 528  0
         progress += MAX_PROGRESS_PREPARE;
 529  0
         monitor.updateProgress(progress);
 530  0
         if (files.size() == 0) {
 531  0
             monitor.notifyNullAction();
 532  0
             return;
 533  
         }
 534  0
         Model.getPump().stopPumpingEvents();
 535  0
         boolean criticThreadWasOn = Designer.theDesigner().getAutoCritique();
 536  0
         if (criticThreadWasOn) {
 537  0
             Designer.theDesigner().setAutoCritique(false);
 538  
         }
 539  
         try {
 540  0
             doImportInternal(files, monitor, progress);
 541  
         } finally {
 542  0
             if (criticThreadWasOn) {
 543  0
                 Designer.theDesigner().setAutoCritique(true);
 544  
             }
 545  
             // TODO: Send an event instead of calling Explorer directly
 546  0
             ExplorerEventAdaptor.getInstance().structureChanged();
 547  0
             Model.getPump().startPumpingEvents();
 548  0
         }
 549  0
     }
 550  
 
 551  
 
 552  
     /**
 553  
      * Do the import.
 554  
      * @param filesLeft the files to parse
 555  
      * @param monitor the progress meter
 556  
      * @param progress the actual progress until now
 557  
      */
 558  
     private void doImportInternal(List<File> filesLeft,
 559  
             final ProgressMonitor monitor, int progress) {
 560  0
         Project project =  ProjectManager.getManager().getCurrentProject();
 561  0
         initCurrentDiagram();
 562  0
         final StringBuffer problems = new StringBuffer();
 563  0
         Collection newElements = new HashSet();
 564  
         
 565  
         try {
 566  0
             newElements.addAll(currentModule.parseFiles(
 567  
                     project, filesLeft, this, monitor));
 568  0
         } catch (Exception e) {
 569  0
             problems.append(printToBuffer(e));
 570  0
         }
 571  
         // New style importers don't create diagrams, so we'll do it
 572  
         // based on the list of newElements that they created
 573  0
         if (isCreateDiagramsSelected() && diagramInterface != null) {
 574  0
             addFiguresToDiagrams(newElements);
 575  
         }
 576  
 
 577  
         // Do layout even if problems occurred during import
 578  0
         if (isDiagramLayoutSelected()) {
 579  
             // TODO: Monitor is getting dismissed before layout is complete
 580  0
             monitor.updateMainTask(
 581  
                     Translator.localize("dialog.import.postImport"));
 582  0
             monitor.updateSubTask(
 583  
                     Translator.localize("dialog.import.layoutAction"));
 584  0
             layoutDiagrams(monitor, progress + filesLeft.size());
 585  
         }
 586  
         
 587  
         // Add messages from caught exceptions
 588  0
         if (problems != null && problems.length() > 0) {
 589  0
             monitor.notifyMessage(
 590  
                     Translator.localize(
 591  
                             "dialog.title.import-problems"), //$NON-NLS-1$
 592  
                             Translator.localize(
 593  
                             "label.import-problems"),        //$NON-NLS-1$
 594  
                             problems.toString());
 595  
         }
 596  
         
 597  0
         monitor.updateMainTask(Translator.localize("dialog.import.done"));
 598  0
         monitor.updateSubTask(""); //$NON-NLS-1$
 599  0
         monitor.updateProgress(MAX_PROGRESS);
 600  
 
 601  0
     }
 602  
 
 603  
 
 604  
     /**
 605  
      * Create diagram figures for a collection of model elements.
 606  
      *
 607  
      * @param newElements
 608  
      *            the collection of elements for which figures should be
 609  
      *            created.
 610  
      */
 611  
     private void addFiguresToDiagrams(Collection newElements) {
 612  0
         for (Object element : newElements) {
 613  0
             if (Model.getFacade().isAClassifier(element)
 614  
                     || Model.getFacade().isAPackage(element)) {
 615  
 
 616  0
                 Object ns = Model.getFacade().getNamespace(element);
 617  0
                 if (ns == null) {
 618  0
                     diagramInterface.createRootClassDiagram();
 619  
                 } else {
 620  0
                     String packageName = getQualifiedName(ns);
 621  
                     // Select the correct diagram (implicitly creates it)
 622  0
                     if (packageName != null
 623  
                             && !packageName.equals("")) {
 624  0
                         diagramInterface.selectClassDiagram(ns,
 625  
                                 packageName);
 626  
                     } else {
 627  0
                         diagramInterface.createRootClassDiagram();
 628  
                     }
 629  
                     // Add the element to the diagram
 630  0
                     if (Model.getFacade().isAInterface(element)) {
 631  0
                         diagramInterface.addInterface(element,
 632  
                                 isMinimizeFigsSelected());
 633  0
                     } else if (Model.getFacade().isAClass(element)) {
 634  0
                         diagramInterface.addClass(element,
 635  
                                 isMinimizeFigsSelected());
 636  0
                     } else if (Model.getFacade().isAPackage(element)) {
 637  0
                         diagramInterface.addPackage(element);
 638  
                     }
 639  
                 }
 640  0
             }
 641  
         }
 642  0
     }
 643  
 
 644  
     /**
 645  
      * Return the fully qualified name of a model element in Java (dot
 646  
      * separated) format.
 647  
      * <p>
 648  
      * TODO: We really need a language independent format here. Perhaps the list
 649  
      * of names that form the hierarchy? - tfm
 650  
      */
 651  
     private String getQualifiedName(Object element) {
 652  0
         StringBuffer sb = new StringBuffer();
 653  
         
 654  0
         Object ns = element;
 655  0
         while (ns != null) {
 656  0
             String name = Model.getFacade().getName(ns);
 657  0
             if (name == null) {
 658  0
                 name = "";
 659  
             }
 660  0
             sb.insert(0, name);
 661  0
             ns = Model.getFacade().getNamespace(ns);
 662  0
             if (ns != null) {
 663  0
                 sb.insert(0, ".");
 664  
             }
 665  0
         }
 666  0
         return sb.toString();
 667  
     }
 668  
 
 669  
     /*
 670  
      * Print an exception trace to a string buffer
 671  
      */
 672  
     private StringBuffer printToBuffer(Exception e) {
 673  0
         StringWriter sw = new StringWriter();
 674  0
         PrintWriter pw = new java.io.PrintWriter(sw);
 675  0
         e.printStackTrace(pw);
 676  0
         return sw.getBuffer();
 677  
     }
 678  
 
 679  
 }