Coverage Report - org.argouml.util.ArgoFrame
 
Classes in this File Line Coverage Branch Coverage Complexity
ArgoFrame
34%
9/26
21%
3/14
2.6
 
 1  
 /* $Id: ArgoFrame.java 17887 2010-01-12 21:17:18Z 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  
  *    tfmorris
 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.util;
 40  
 
 41  
 import java.awt.Frame;
 42  
 
 43  
 import javax.swing.JFrame;
 44  
 import javax.swing.JOptionPane;
 45  
 
 46  
 import org.apache.log4j.Logger;
 47  
 
 48  
 /**
 49  
  * Helper class to store/find a top level application frame.
 50  
  * 
 51  
  * This has been factored out of ProjectBrowser solely to allow easy
 52  
  * identification of things that want a ProjectBrowser because it is the top
 53  
  * level JFrame and those things that want to actually call a ProjectBrowser
 54  
  * method for some other purpose.
 55  
  * 
 56  
  * @author Tom Morris
 57  
  * 
 58  
  */
 59  
 public class ArgoFrame {
 60  
 
 61  900
     private static final Logger LOG = Logger.getLogger(ArgoFrame.class);
 62  
 
 63  
     private static Frame topFrame;
 64  
     
 65  0
     private ArgoFrame() {
 66  
         // prohibit instantiation
 67  0
     }
 68  
 
 69  
     /**
 70  
      * Get a top level JFrame which can be used as the parent for creating new
 71  
      * dialogs. The method name and return type were the same as the old
 72  
      * ProjectBrowser.getInstance() usage for compatibility, but getFrame should
 73  
      * be used for new uses.
 74  
      * 
 75  
      * @return a top level JFrame to use as parent for new dialogs
 76  
      * @deprecated for 0.29.1 by tfmorris. Use {@link #getFrame()}.
 77  
      */
 78  
     public static JFrame getInstance() {
 79  0
         Frame frame = getFrame();
 80  0
         if (frame instanceof JFrame) {
 81  0
             return (JFrame) frame;
 82  
         }
 83  0
         return null;
 84  
     }
 85  
     /**
 86  
      * Get a top level frame which can be used as the parent for creating new
 87  
      * dialogs. 
 88  
      * 
 89  
      * @return a top level JFrame to use as parent for new dialogs
 90  
      */
 91  
     public static Frame getFrame() {
 92  2022
         if (topFrame == null) {
 93  900
             Frame rootFrame = JOptionPane.getRootFrame();
 94  900
             if ( rootFrame instanceof JFrame) {
 95  900
                 setFrame(rootFrame);
 96  
             } else {
 97  0
                 Frame[] frames = Frame.getFrames();
 98  0
                 for (int i = 0; i < frames.length; i++) {
 99  0
                     if (frames[i] instanceof JFrame) {
 100  0
                         if (topFrame != null) {
 101  0
                             LOG.warn("Found multiple JFrames");
 102  
                         } else {
 103  0
                             setFrame(frames[i]);
 104  
                         }
 105  
                     }
 106  
                 }
 107  0
                 if (topFrame == null) {
 108  0
                     LOG.warn("Failed to find JFrame - using rootFrame");
 109  0
                     setFrame(JOptionPane.getRootFrame());
 110  
                 }
 111  
             }
 112  
         }
 113  
 
 114  2022
         return topFrame;
 115  
     }
 116  
 
 117  
     /**
 118  
      * Set the given JFrame to use as the main application frame.
 119  
      * 
 120  
      * @param frame the main application frame.
 121  
      * @deprecated for 0.29.1 by tfmorris.  Use {@link #setFrame(Frame)}.
 122  
      */
 123  
     @Deprecated
 124  
     public static void setInstance(JFrame frame) {
 125  0
         setFrame(frame);
 126  0
     }
 127  
     
 128  
     /**
 129  
      * Set the given Frame to use as the main application frame.
 130  
      * 
 131  
      * @param frame the main application frame.
 132  
      */
 133  
     public static void setFrame(Frame frame) {
 134  900
         topFrame = frame;
 135  900
         ArgoDialog.setFrame(topFrame);
 136  900
     }
 137  
 }