Coverage Report - org.pdfsam.guiclient.gui.frames.JMainFrame
 
Classes in this File Line Coverage Branch Coverage Complexity
JMainFrame
83%
99/119
45%
11/24
1.65
JMainFrame$1
100%
3/3
N/A
1.65
 
 1  
 /*
 2  
  * Created on 09-Nov-2007
 3  
  * Copyright (C) 2006 by Andrea Vacondio.
 4  
  *
 5  
  * This program is free software; you can redistribute it and/or modify it under the terms of the 
 6  
  * GNU General Public License as published by the Free Software Foundation; 
 7  
  * either version 2 of the License.
 8  
  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 9  
  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 10  
  * See the GNU General Public License for more details.
 11  
  * You should have received a copy of the GNU General Public License along with this program; 
 12  
  * if not, write to the Free Software Foundation, Inc., 
 13  
  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 14  
  */
 15  
 package org.pdfsam.guiclient.gui.frames;
 16  
 
 17  
 
 18  
 
 19  
 import java.awt.BorderLayout;
 20  
 import java.awt.CardLayout;
 21  
 import java.awt.Dimension;
 22  
 import java.io.File;
 23  
 import java.net.URL;
 24  
 import java.util.Map;
 25  
 
 26  
 import javax.swing.ImageIcon;
 27  
 import javax.swing.JFrame;
 28  
 import javax.swing.JPanel;
 29  
 import javax.swing.JScrollPane;
 30  
 import javax.swing.JSplitPane;
 31  
 import javax.swing.SwingUtilities;
 32  
 import javax.swing.ToolTipManager;
 33  
 import javax.swing.tree.DefaultMutableTreeNode;
 34  
 
 35  
 import org.apache.commons.lang.time.DurationFormatUtils;
 36  
 import org.apache.commons.lang.time.StopWatch;
 37  
 import org.apache.log4j.Logger;
 38  
 import org.pdfsam.console.business.dto.WorkDoneDataModel;
 39  
 import org.pdfsam.guiclient.GuiClient;
 40  
 import org.pdfsam.guiclient.business.ApplicationCloser;
 41  
 import org.pdfsam.guiclient.business.Environment;
 42  
 import org.pdfsam.guiclient.business.actions.LoadEnvironmentAction;
 43  
 import org.pdfsam.guiclient.business.actions.SaveEnvironmentAction;
 44  
 import org.pdfsam.guiclient.business.listeners.LogActionListener;
 45  
 import org.pdfsam.guiclient.business.listeners.MainWindowListener;
 46  
 import org.pdfsam.guiclient.business.listeners.mediators.ApplicationExitMediator;
 47  
 import org.pdfsam.guiclient.business.listeners.mediators.TreeMediator;
 48  
 import org.pdfsam.guiclient.business.listeners.mediators.UpdateCheckerMediator;
 49  
 import org.pdfsam.guiclient.configuration.Configuration;
 50  
 import org.pdfsam.guiclient.gui.components.JMainMenuBar;
 51  
 import org.pdfsam.guiclient.gui.panels.JButtonsPanel;
 52  
 import org.pdfsam.guiclient.gui.panels.JInfoPanel;
 53  
 import org.pdfsam.guiclient.gui.panels.JLogPanel;
 54  
 import org.pdfsam.guiclient.gui.panels.JSettingsPanel;
 55  
 import org.pdfsam.guiclient.gui.panels.JStatusPanel;
 56  
 import org.pdfsam.guiclient.gui.panels.JTreePanel;
 57  
 import org.pdfsam.guiclient.plugins.PlugInsLoader;
 58  
 import org.pdfsam.guiclient.plugins.interfaces.AbstractPlugablePanel;
 59  
 import org.pdfsam.guiclient.plugins.models.PluginDataModel;
 60  
 import org.pdfsam.i18n.GettextResource;
 61  
 
 62  
 /**
 63  
  * Main frame
 64  
  * @author Andrea Vacondio
 65  
  *
 66  
  */
 67  1043553
 public class JMainFrame extends JFrame {
 68  
 
 69  
         private static final long serialVersionUID = -3858244069059677829L;
 70  
 
 71  1043553
         private static final Logger log = Logger.getLogger(JMainFrame.class.getPackage().getName());
 72  1043553
         private String DEFAULT_ICON = "/images/pdf.png";
 73  1043553
         private String DEFAULT_ICON_16 = "/images/pdf16.png";
 74  
         
 75  
         private JSplashScreen screen;
 76  
         private Map<PluginDataModel, AbstractPlugablePanel> pluginsMap;
 77  
         private ApplicationExitMediator exitMediator;
 78  
         private UpdateCheckerMediator updateMediator;
 79  
         private JStatusPanel statusPanel;
 80  
         private JTreePanel treePanel;
 81  
         private JButtonsPanel buttonsPanel;
 82  1043553
         private JPanel mainPanel = new JPanel(new CardLayout());
 83  
         private JScrollPane mainScrollPanel;
 84  
         private JSplitPane verticalSplitPane;
 85  
         private JSplitPane horizSplitPane;
 86  
         private JLogPanel logPanel;
 87  
         
 88  1043553
         public JMainFrame(){
 89  1043553
                 StopWatch stopWatch = new StopWatch();
 90  1043553
                 stopWatch.start();
 91  1043553
                 log.info("Starting "+GuiClient.getApplicationName()+" Ver. "+GuiClient.getVersion());
 92  1043553
                 runSplash();
 93  1043553
                 ToolTipManager.sharedInstance().setDismissDelay (300000);
 94  1043553
                 initialize();
 95  1043553
                 closeSplash();
 96  1043553
                 stopWatch.stop();
 97  1043553
                 log.info(GuiClient.getApplicationName()+" Ver. "+GuiClient.getVersion()+" "+GettextResource.gettext(Configuration.getInstance().getI18nResourceBundle(),"started in ")+DurationFormatUtils.formatDurationWords(stopWatch.getTime(), true, true));
 98  1043553
         }
 99  
         /**
 100  
          * initialization
 101  
          */
 102  
         private void initialize() {
 103  
                 try{                        
 104  
                         //center(this,800,600);
 105  1043553
                         URL iconUrl = this.getClass().getResource("/images/pdf_"+GuiClient.getVersionType()+".png");
 106  1043553
                         URL iconUrl16 = this.getClass().getResource("/images/pdf_"+GuiClient.getVersionType()+"16.png");
 107  1043553
                         if(iconUrl == null){
 108  0
                                 iconUrl = this.getClass().getResource(DEFAULT_ICON);
 109  
                         }
 110  1043553
                         if(iconUrl16 == null){
 111  0
                                 iconUrl16 = this.getClass().getResource(DEFAULT_ICON_16);
 112  
                         }
 113  1043553
                         setIconImage(new ImageIcon(iconUrl).getImage());
 114  1043553
                 setTitle(GuiClient.getApplicationName()+" Ver. "+GuiClient.getVersion());
 115  
                 
 116  
                 //load plugins
 117  1043553
                 setSplashStep(GettextResource.gettext(Configuration.getInstance().getI18nResourceBundle(),"Loading plugins.."));
 118  1043553
                 PlugInsLoader pluginsLoader = new PlugInsLoader(Configuration.getInstance().getPluginAbsolutePath());
 119  1043553
                 pluginsMap = pluginsLoader.loadPlugins();
 120  
                         
 121  
                         //Info panel
 122  1043553
                         JInfoPanel infoPanel = new JInfoPanel(pluginsMap);
 123  1043553
                         PluginDataModel infoDataModel = new PluginDataModel(infoPanel.getPluginName(), infoPanel.getVersion(), infoPanel.getPluginAuthor(), infoPanel.getClass().getName());
 124  1043553
                         mainPanel.add(infoPanel,infoPanel.getPluginName());
 125  
                         
 126  
                 //Settings panel
 127  1043553
                 JSettingsPanel settingsPanel = new JSettingsPanel();
 128  1043553
                         PluginDataModel settingsDataModel = new PluginDataModel(settingsPanel.getPluginName(), settingsPanel.getVersion(), settingsPanel.getPluginAuthor(), settingsPanel.getClass().getName());
 129  1043553
                         mainPanel.add(settingsPanel,settingsPanel.getPluginName());
 130  
                         
 131  
                 //sets main panel
 132  1043553
                 mainPanel.setPreferredSize(new Dimension(670,500));
 133  1043553
                 for(AbstractPlugablePanel instance: pluginsMap.values()){
 134  6261318
                         mainPanel.add(instance,instance.getPluginName());
 135  
                 }
 136  
                 
 137  
                 //status panel
 138  1043553
                 setSplashStep(GettextResource.gettext(Configuration.getInstance().getI18nResourceBundle(),"Building status bar.."));
 139  1043553
                 statusPanel = new JStatusPanel(new ImageIcon(iconUrl16),GuiClient.getApplicationName(),WorkDoneDataModel.MAX_PERGENTAGE);
 140  1043553
                 getContentPane().add(statusPanel,BorderLayout.PAGE_END); 
 141  1043553
                 Configuration.getInstance().getConsoleServicesFacade().addExecutionObserver(statusPanel);
 142  
                         
 143  
                 //tree panel
 144  1043553
                 setSplashStep(GettextResource.gettext(Configuration.getInstance().getI18nResourceBundle(),"Building tree.."));
 145  1043553
                 treePanel = new JTreePanel(new DefaultMutableTreeNode(GuiClient.UNIXNAME+" "+GuiClient.getVersion()));
 146  1043553
                     for (PluginDataModel item : pluginsMap.keySet()) {
 147  6261318
                             treePanel.addToPlugsNode(item);
 148  
                 }
 149  1043553
                 treePanel.addToRootNode(settingsDataModel);
 150  1043553
                 treePanel.addToRootNode(infoDataModel);
 151  1043553
                 treePanel.getTree().addTreeSelectionListener(new TreeMediator(this));
 152  1043553
                 treePanel.expand();
 153  
                 
 154  
                 //add info and settings to plugins map
 155  1043553
                 pluginsMap.put(settingsDataModel, settingsPanel);
 156  1043553
                 pluginsMap.put(infoDataModel, infoPanel);                              
 157  
                 
 158  
                 //menu
 159  1043553
                 setSplashStep(GettextResource.gettext(Configuration.getInstance().getI18nResourceBundle(),"Building menus.."));        
 160  
 
 161  1043553
                 Environment environment = new Environment(pluginsMap, treePanel);
 162  1043553
                         SaveEnvironmentAction saveAction = new SaveEnvironmentAction(environment, this);
 163  1043553
                         LoadEnvironmentAction loadAction = new LoadEnvironmentAction(environment, this);
 164  1043553
                 exitMediator = new ApplicationExitMediator(new ApplicationCloser(this));
 165  1043553
                 getRootPane().setJMenuBar(new JMainMenuBar(saveAction, loadAction, exitMediator));
 166  
 
 167  
 
 168  
                 //buttons bar
 169  1043553
                 setSplashStep(GettextResource.gettext(Configuration.getInstance().getI18nResourceBundle(),"Building buttons bar.."));
 170  1043553
                 buttonsPanel = new JButtonsPanel(saveAction, loadAction, exitMediator, new LogActionListener());
 171  1043553
                 getContentPane().add(buttonsPanel,BorderLayout.PAGE_START);  
 172  
                 
 173  
                 //set up check for updates mediator
 174  1043553
                 updateMediator = new UpdateCheckerMediator(statusPanel);
 175  1043553
                 if(Configuration.getInstance().isCheckForUpdates()){
 176  1043553
                         updateMediator.checkForUpdates(5000, false);
 177  
                 }
 178  1043553
                 settingsPanel.setCheckUpdateMediator(updateMediator);
 179  
                 
 180  
                 //final set up
 181  1043553
                 mainScrollPanel = new JScrollPane(mainPanel);
 182  1043553
                 mainScrollPanel.setMinimumSize(new Dimension(100, 400));
 183  
                 
 184  1043553
                 logPanel = new JLogPanel();
 185  
                 
 186  1043553
                 horizSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treePanel, mainScrollPanel);
 187  1043553
                 horizSplitPane.setOneTouchExpandable(true);
 188  1043553
                 horizSplitPane.setDividerLocation(155);
 189  
                 
 190  1043553
                 verticalSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, horizSplitPane,logPanel);
 191  1043553
                 verticalSplitPane.setOneTouchExpandable(true);
 192  1043553
                 verticalSplitPane.setResizeWeight(1.0);
 193  1043553
                 verticalSplitPane.setDividerLocation(0.75);
 194  
                 
 195  
                 //load the default env if set
 196  1043553
                 String defaultEnvString = Configuration.getInstance().getDefaultEnvironment();
 197  1043553
                 if(defaultEnvString!=null && defaultEnvString.length()>0){
 198  0
                         File defaultEnv = new File(defaultEnvString);
 199  0
                         if(defaultEnv != null && defaultEnv.exists() && defaultEnv.isFile()){
 200  0
                                 log.info(GettextResource.gettext(Configuration.getInstance().getI18nResourceBundle(),"Loading default environment."));
 201  0
                                 environment.loadJobs(defaultEnv);
 202  
                         }
 203  
                 }
 204  1043553
                 getContentPane().add(verticalSplitPane,BorderLayout.CENTER);
 205  
                 
 206  1043553
                 setSize(640, 480);
 207  1043553
                         setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
 208  1043553
                         addWindowListener(new MainWindowListener(this));
 209  
 
 210  0
                 }catch(Exception e){
 211  0
                         log.fatal(GettextResource.gettext(Configuration.getInstance().getI18nResourceBundle(),"Error starting pdfsam."),e);
 212  1043553
                 }
 213  
                 
 214  1043553
         }        
 215  
     
 216  
          /**
 217  
      * Run a splash screen
 218  
      */
 219  
     private void runSplash(){
 220  1043553
                     screen = new JSplashScreen("pdfsam loader", "Initialization..");
 221  1043553
                         screen.setMaximumBarValue(5);
 222  1043553
                     Runnable runner = new Runnable() {
 223  
                             public void run() {                                    
 224  1043553
                                     screen.setVisible(true);
 225  1043553
                             }
 226  
                     };
 227  1043553
                     SwingUtilities.invokeLater(runner);
 228  1043553
     }
 229  
     
 230  
     /**
 231  
      * close the splash screen
 232  
      */
 233  
     private void closeSplash(){
 234  1043553
         if(screen != null){
 235  1043553
                     screen.setVisible(false);                           
 236  1043553
                     screen.dispose();
 237  
             }
 238  1043553
     }
 239  
     
 240  
     /**
 241  
      * Sets the splash text and increment the progress bar
 242  
      * @param message
 243  
      */
 244  
     private void setSplashStep(String message){
 245  5217765
             if(screen != null){
 246  5217765
                     screen.setText(message);
 247  5217765
                     screen.addBarValue();
 248  
             }
 249  5217765
     }
 250  
         /**
 251  
          * @return the statusPanel
 252  
          */
 253  
         public JStatusPanel getStatusPanel() {
 254  0
                 return statusPanel;
 255  
         }
 256  
         /**
 257  
          * @return the treePanel
 258  
          */
 259  
         public JTreePanel getTreePanel() {
 260  70308
                 return treePanel;
 261  
         }
 262  
         /**
 263  
          * @return the buttonsPanel
 264  
          */
 265  
         public JButtonsPanel getButtonsPanel() {
 266  0
                 return buttonsPanel;
 267  
         }
 268  
         /**
 269  
          * @return the pluginsMap
 270  
          */
 271  
         public Map<PluginDataModel, AbstractPlugablePanel> getPluginsMap() {
 272  0
                 return pluginsMap;
 273  
         }
 274  
         /**
 275  
          * @return the mainPanel
 276  
          */
 277  
         public JPanel getMainPanel() {
 278  0
                 return mainPanel;
 279  
         }    
 280  
     
 281  
         /**
 282  
          * sets the minimum size of the scroll pane
 283  
          * @param d
 284  
          */
 285  
         public void setMainPanelPreferredSize(Dimension d){
 286  0
                 mainPanel.setPreferredSize(d);
 287  0
         }
 288  
         
 289  
         /**
 290  
          * @return
 291  
          * @see javax.swing.JSplitPane#getDividerLocation()
 292  
          */
 293  
         public int getVerticalDividerLocation() {
 294  70308
                 return verticalSplitPane.getDividerLocation();
 295  
         }
 296  
         
 297  
         /**
 298  
          * @param location
 299  
          * @see javax.swing.JSplitPane#setDividerLocation(int)
 300  
          */
 301  
         public void setVerticalDividerLocation(int location) {
 302  0
                 verticalSplitPane.setDividerLocation(location);
 303  0
         }
 304  
         
 305  
         /**
 306  
          * @return
 307  
          * @see javax.swing.JSplitPane#getDividerLocation()
 308  
          */
 309  
         public int getHorizontalDividerLocation() {
 310  70308
                 return horizSplitPane.getDividerLocation();
 311  
         }
 312  
         
 313  
         /**
 314  
          * @param location
 315  
          * @see javax.swing.JSplitPane#setDividerLocation(int)
 316  
          */
 317  
         public void setHorizontalDividerLocation(int location) {
 318  1043553
                 horizSplitPane.setDividerLocation(location);
 319  1043553
         }
 320  
         /**
 321  
          * @return
 322  
          * @see java.awt.Component#getSize()
 323  
          */
 324  
         public Dimension getHorizontalDividerDimension() {
 325  70308
                 return horizSplitPane.getSize();
 326  
         }
 327  
         /**
 328  
          * @param d
 329  
          * @see java.awt.Component#setSize(java.awt.Dimension)
 330  
          */
 331  
         public void setHorizontalDividerDimension(Dimension d) {
 332  0
                 horizSplitPane.setSize(d);
 333  0
         }
 334  
         /**
 335  
          * @return
 336  
          * @see java.awt.Component#getSize()
 337  
          */
 338  
         public Dimension getVerticalDividerDimension() {
 339  70308
                 return verticalSplitPane.getSize();
 340  
         }
 341  
         /**
 342  
          * @param d
 343  
          * @see java.awt.Component#setSize(java.awt.Dimension)
 344  
          */
 345  
         public void setVerticalDividerDimension(Dimension d) {
 346  0
                 verticalSplitPane.setSize(d);
 347  0
         }
 348  
         
 349  
         
 350  
         
 351  
 }