Coverage Report - org.argouml.uml.reveng.ui.ImportStatusScreen
 
Classes in this File Line Coverage Branch Coverage Complexity
ImportStatusScreen
0%
0/77
0%
0/4
1.167
ImportStatusScreen$1
0%
0/5
0%
0/2
1.167
ImportStatusScreen$2
0%
0/4
N/A
1.167
ImportStatusScreen$3
0%
0/7
0%
0/4
1.167
ImportStatusScreen$4
0%
0/4
N/A
1.167
ImportStatusScreen$5
0%
0/3
N/A
1.167
ImportStatusScreen$6
0%
0/3
N/A
1.167
 
 1  
 /* $Id: ImportStatusScreen.java 17871 2010-01-12 20:49:55Z 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) 2008, 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.ui;
 40  
 
 41  
 import java.awt.BorderLayout;
 42  
 import java.awt.Container;
 43  
 import java.awt.Dimension;
 44  
 import java.awt.Frame;
 45  
 import java.awt.GridBagConstraints;
 46  
 import java.awt.GridBagLayout;
 47  
 import java.awt.Toolkit;
 48  
 import java.awt.event.ActionEvent;
 49  
 import java.awt.event.ActionListener;
 50  
 import java.awt.event.WindowEvent;
 51  
 import java.awt.event.WindowListener;
 52  
 
 53  
 import javax.swing.JButton;
 54  
 import javax.swing.JDialog;
 55  
 import javax.swing.JLabel;
 56  
 import javax.swing.JPanel;
 57  
 import javax.swing.JProgressBar;
 58  
 import javax.swing.JScrollPane;
 59  
 import javax.swing.JTextArea;
 60  
 import javax.swing.SwingConstants;
 61  
 import javax.swing.SwingUtilities;
 62  
 
 63  
 import org.argouml.i18n.Translator;
 64  
 import org.argouml.taskmgmt.ProgressEvent;
 65  
 import org.argouml.taskmgmt.ProgressMonitor;
 66  
 
 67  
 /**
 68  
  * A window that shows the progress bar and a cancel button. As a convenience to
 69  
  * callers which may be executing on a thread other than the Swing event thread,
 70  
  * all methods use SwingUtilities.invokeLater() or
 71  
  * SwingUtilities.invokeAndWait() to make sure that Swing calls happen on the
 72  
  * appropriate thread.
 73  
  *<p>
 74  
  * TODO: React on the close button as if the Cancel button was pressed.
 75  
  */
 76  0
 public class ImportStatusScreen extends JDialog 
 77  
     implements ProgressMonitor, WindowListener {
 78  
     
 79  
     private JButton cancelButton;
 80  
     private JLabel progressLabel;
 81  
     private JProgressBar progress;
 82  
     private JTextArea messageArea;
 83  0
     private boolean hasMessages = false;
 84  0
     private boolean canceled = false;
 85  
 
 86  
     /**
 87  
      * The constructor.
 88  
      *
 89  
      * @param title
 90  
      * @param iconName
 91  
      */
 92  
     public ImportStatusScreen(Frame frame, String title, String iconName) {
 93  0
         super(frame, true);
 94  0
         if (title != null) {
 95  0
             setTitle(title);
 96  
         }
 97  0
         Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
 98  0
         getContentPane().setLayout(new BorderLayout(4, 4));
 99  0
         Container panel = new JPanel(new GridBagLayout());
 100  
 
 101  
         // Parsing file x of z.
 102  0
         progressLabel = new JLabel();
 103  0
         progressLabel.setHorizontalAlignment(SwingConstants.RIGHT);
 104  
 
 105  0
         GridBagConstraints gbc = new GridBagConstraints();
 106  0
         gbc.anchor = GridBagConstraints.NORTH;
 107  0
         gbc.fill = GridBagConstraints.HORIZONTAL;
 108  0
         gbc.gridwidth = GridBagConstraints.REMAINDER;
 109  0
         gbc.gridheight = 1;
 110  0
         gbc.gridx = 0;
 111  0
         gbc.gridy = 0;
 112  0
         gbc.weightx = 0.1;
 113  
         
 114  0
         panel.add(progressLabel, gbc);
 115  0
         gbc.gridy++;
 116  
         
 117  
         // progress bar
 118  0
         progress = new JProgressBar();
 119  0
         gbc.anchor = GridBagConstraints.CENTER;
 120  0
         panel.add(progress, gbc);
 121  0
         gbc.gridy++;
 122  
         
 123  0
         panel.add(
 124  
                 new JLabel(Translator.localize("label.import-messages")), gbc);
 125  0
         gbc.gridy++;
 126  
         
 127  
         // Error/warning messageArea
 128  0
         messageArea = new JTextArea(10, 50);
 129  0
         gbc.weighty = 0.8;
 130  
 //        gbc.gridheight = 10;
 131  0
         gbc.fill = GridBagConstraints.BOTH;
 132  0
         panel.add(new JScrollPane(messageArea), gbc);
 133  0
         gbc.gridy++;
 134  
 
 135  
         // cancel/close button
 136  0
         cancelButton = new JButton(Translator.localize("button.cancel"));
 137  
 
 138  0
         gbc.fill = GridBagConstraints.NONE;
 139  0
         gbc.anchor = GridBagConstraints.SOUTH;
 140  0
         gbc.weighty = 0.1;
 141  0
         gbc.gridheight = GridBagConstraints.REMAINDER;
 142  0
         panel.add(cancelButton, gbc);
 143  0
         gbc.gridy++;
 144  
         
 145  0
         cancelButton.addActionListener(new ActionListener() {
 146  
 
 147  
             public void actionPerformed(ActionEvent e) {
 148  0
                 if (isComplete()) {
 149  0
                     close();
 150  
                 }
 151  0
                 canceled = true;
 152  0
             }
 153  
 
 154  
         });
 155  
         
 156  0
         getContentPane().add(panel);
 157  0
         pack();
 158  0
         Dimension contentPaneSize = getContentPane().getPreferredSize();
 159  0
         setLocation(scrSize.width / 2 - contentPaneSize.width / 2,
 160  
                 scrSize.height / 2 - contentPaneSize.height / 2);
 161  0
         setResizable(true);
 162  0
         setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
 163  0
         addWindowListener(this);
 164  0
     }
 165  
 
 166  
     public void setMaximumProgress(final int i) {
 167  0
         SwingUtilities.invokeLater(new Runnable () {
 168  
             public void run() {
 169  0
                 progress.setMaximum(i);
 170  0
                 setVisible(true);
 171  0
             }
 172  
         });
 173  0
     }
 174  
 
 175  
     public void updateProgress(final int i) {
 176  0
         SwingUtilities.invokeLater(new Runnable () {
 177  
             public void run() {
 178  0
                 progress.setValue(i);
 179  0
                 if (isComplete()) {
 180  0
                     if (hasMessages) {
 181  0
                         cancelButton.setText(
 182  
                                 Translator.localize("button.close"));
 183  
                     } else {
 184  0
                         close();
 185  
                     }
 186  
                 }
 187  0
             }
 188  
         });
 189  0
     }
 190  
     
 191  
     private boolean isComplete() {
 192  0
         return progress.getValue() == progress.getMaximum();
 193  
     }
 194  
 
 195  
     /**
 196  
      * The UID.
 197  
      */
 198  
     private static final long serialVersionUID = -1336242911879462274L;
 199  
 
 200  
     /*
 201  
      * @see org.argouml.application.api.ProgressMonitor#close()
 202  
      */
 203  
     public void close() {
 204  0
         SwingUtilities.invokeLater(new Runnable () {
 205  
             public void run() {
 206  0
                 setVisible(false);
 207  0
                 dispose();
 208  0
             }
 209  
         });
 210  0
     }
 211  
 
 212  
     /*
 213  
      * @see org.argouml.application.api.ProgressMonitor#isCanceled()
 214  
      */
 215  
     public boolean isCanceled() {
 216  0
         return canceled;
 217  
     }
 218  
 
 219  
     /*
 220  
      * @see org.argouml.application.api.ProgressMonitor#notifyMessage(java.lang.String, java.lang.String, java.lang.String)
 221  
      */
 222  
     public void notifyMessage(final String title, final String introduction,
 223  
             final String message) {
 224  0
         hasMessages = true;
 225  
         // TODO: Add filename ?
 226  0
         messageArea.setText(messageArea.getText() + title + "\n" + introduction
 227  
                 + "\n" + message + "\n\n");
 228  0
         messageArea.setCaretPosition(messageArea.getText().length());
 229  0
     }
 230  
 
 231  
     /*
 232  
      * @see org.argouml.application.api.ProgressMonitor#notifyNullAction()
 233  
      */
 234  
     public void notifyNullAction() {
 235  0
         String msg = Translator.localize("label.import.empty");
 236  0
         notifyMessage(msg, msg, msg);
 237  0
     }
 238  
 
 239  
     /*
 240  
      * @see org.argouml.application.api.ProgressMonitor#updateMainTask(java.lang.String)
 241  
      */
 242  
     public void updateMainTask(final String name) {
 243  0
         SwingUtilities.invokeLater(new Runnable () {
 244  
             public void run() {
 245  0
                 setTitle(name);
 246  0
             }
 247  
         });
 248  0
     }
 249  
 
 250  
     /*
 251  
      * @see org.argouml.application.api.ProgressMonitor#updateSubTask(java.lang.String)
 252  
      */
 253  
     public void updateSubTask(final String action) {
 254  0
         SwingUtilities.invokeLater(new Runnable () {
 255  
             public void run() {
 256  0
                 progressLabel.setText(action);
 257  0
             }
 258  
         });
 259  0
     }
 260  
 
 261  
     /*
 262  
      * @see org.argouml.persistence.ProgressListener#progress(org.argouml.persistence.ProgressEvent)
 263  
      */
 264  
     public void progress(ProgressEvent event) throws InterruptedException {
 265  
         // ignored
 266  0
     }
 267  
 
 268  
     public void windowClosing(WindowEvent e) {
 269  
         // User closing the progress window is interpreted as cancel request
 270  0
         canceled = true;
 271  0
         close();
 272  0
     }
 273  
     
 274  0
     public void windowActivated(WindowEvent e) { }
 275  0
     public void windowClosed(WindowEvent e) { }
 276  0
     public void windowDeactivated(WindowEvent e) { }
 277  0
     public void windowDeiconified(WindowEvent e) { }
 278  0
     public void windowIconified(WindowEvent e) { }
 279  0
     public void windowOpened(WindowEvent e) { }
 280  
 
 281  
 }