Coverage Report - de.glossmaker.gloss.GuiMain
 
Classes in this File Line Coverage Branch Coverage Complexity
GuiMain
0%
0/37
0%
0/4
3,25
GuiMain$1
0%
0/20
0%
0/14
3,25
 
 1  
 package de.glossmaker.gloss;
 2  
 
 3  
 import java.awt.Dimension;
 4  
 import java.awt.event.WindowAdapter;
 5  
 import java.awt.event.WindowEvent;
 6  
 import java.io.File;
 7  
 import java.util.Locale;
 8  
 
 9  
 import javax.swing.JFileChooser;
 10  
 import javax.swing.JFrame;
 11  
 import javax.swing.JOptionPane;
 12  
 
 13  
 import org.apache.log4j.ConsoleAppender;
 14  
 import org.apache.log4j.Logger;
 15  
 import org.apache.log4j.PatternLayout;
 16  
 
 17  
 import de.glossmaker.gloss.datastructure.GlossItems;
 18  
 import de.glossmaker.gloss.datastructure.gui.Config;
 19  
 import de.glossmaker.gloss.language.Translation;
 20  
 import de.glossmaker.gloss.observer.GlossItemsChangePublisher;
 21  
 import de.glossmaker.gui.gloss.GlossFileChooser;
 22  
 import de.glossmaker.gui.gloss.bars.MenuBar;
 23  
 import de.glossmaker.gui.gloss.help.HelpSystem;
 24  
 import de.glossmaker.gui.gloss.main.MainInputPanel;
 25  
 
 26  
 /**
 27  
  * Main class for the application GlossMaker.
 28  
  * 
 29  
  * @author Markus Flingelli
 30  
  */
 31  0
 public class GuiMain {
 32  0
         private static final Logger mLogger = Logger.getRootLogger();
 33  0
         private static GlossItems mGlossItems = null;
 34  0
         private static GlossItemsChangePublisher mGlossItemsChangePublisher = null;
 35  0
         private static Translation mTranslation = null;
 36  
         private static JFrame mFrame;
 37  
         
 38  
         private static void initLogging() {
 39  0
                 PatternLayout layout = new PatternLayout("[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M(%F:%L) - %m%n"); //"%-4r %-5p [%t] %37c %3x - %m%n");
 40  0
                 ConsoleAppender consoleAppender = new ConsoleAppender(layout);
 41  0
                 mLogger.addAppender(consoleAppender);
 42  0
         }
 43  
         
 44  
         private static void saveConfiguration() {
 45  0
                 Config.getInstance().setWindowPosition("main", mFrame.getX(), mFrame.getY(), mFrame.getWidth(), mFrame.getHeight());
 46  0
                 Config.getInstance().setLanguage(Locale.getDefault().getLanguage());
 47  0
                 Config.getInstance().saveFile(Config.CONFIG_FILE);
 48  0
         }
 49  
         
 50  
         public static void main(String[] args) {
 51  0
                 String locale = Config.getInstance().getLanguage();
 52  0
                 if (locale != null) {
 53  0
                         Locale.setDefault(new Locale(locale));
 54  
                 } else {
 55  0
                         Locale.setDefault(Locale.ENGLISH);
 56  
                 }
 57  0
                 initLogging();
 58  
                 
 59  0
                 mGlossItems = new GlossItems();
 60  0
                 if (args.length > 0) {
 61  0
                         mGlossItems.readFromFile(args[0]);
 62  
                 }
 63  0
                 mGlossItemsChangePublisher = GlossItemsChangePublisher.getInstance(mGlossItems);
 64  0
                 mTranslation = Translation.getInstance();
 65  
                 
 66  
                 
 67  0
                 MainInputPanel panel = new MainInputPanel(mGlossItems);
 68  0
                 mFrame = new JFrame("GlossMaker");
 69  0
                 HelpSystem.getInstance().enableHelpKey(mFrame.getRootPane(), "chapter.introduction", null);
 70  0
                 HelpSystem.getInstance().setRootPane(mFrame.getRootPane());
 71  0
                 mFrame.setJMenuBar(new MenuBar(mGlossItems));
 72  0
                 mFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 73  0
                 mFrame.add(panel);
 74  0
                 mFrame.setLocation(Config.getInstance().getWindowPositionX("main", 0), 
 75  
                                 Config.getInstance().getWindowPositionY("main", 0));
 76  0
                 mFrame.setPreferredSize(
 77  
                                 new Dimension(Config.getInstance().getWindowPositionWidth("main", 800), 
 78  
                                 Config.getInstance().getWindowPositionHeight("main", 600)));
 79  0
                 mFrame.setSize(Config.getInstance().getWindowPositionWidth("main", 800), 
 80  
                                 Config.getInstance().getWindowPositionHeight("main", 600));
 81  0
                 mFrame.pack();
 82  
                 
 83  0
                 mFrame.addWindowListener(new WindowAdapter() {
 84  
 
 85  
                         @Override
 86  
                         public void windowClosing(WindowEvent event) {
 87  0
                                 if (mGlossItemsChangePublisher.isFileChanged() == true) {
 88  0
                                         int result = JOptionPane.showConfirmDialog(null,
 89  
                                                         mTranslation.getValue("confirmation.save_changes.text"),
 90  
                                                         mTranslation.getValue("confirmation.save_changes.title"),
 91  
                                                         JOptionPane.YES_NO_OPTION);
 92  0
                                         if (result == JOptionPane.YES_OPTION) {
 93  0
                                                 if (mGlossItemsChangePublisher.isFileOpen() == true) {
 94  0
                                                         mGlossItemsChangePublisher.saveFile();
 95  
                                                 } else {
 96  0
                                                         result = JOptionPane.YES_OPTION;
 97  0
                                                         GlossFileChooser fc = new GlossFileChooser(
 98  
                                                                         mTranslation.getValue("filechooser.save_as.text"),
 99  
                                                                         mTranslation.getValue("filechooser.gloss.description.text"),
 100  
                                                                         "bib");
 101  0
                                                         if (fc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
 102  0
                                                                 String fileName = fc.getSelectedFile().getAbsolutePath();
 103  0
                                                                 if (fileName.contains(".") == false) {
 104  0
                                                                         fileName += ".bib";
 105  
                                                                 }
 106  0
                                                                 File f = new File (fileName);
 107  0
                                                                 if (f.exists()) {
 108  0
                                                                         result = JOptionPane.showConfirmDialog(null, mTranslation.getValue("filechooser.override_file.text", fileName));                                
 109  
                                                                 }
 110  0
                                                                 if (result == JOptionPane.YES_OPTION) {
 111  0
                                                                         mGlossItemsChangePublisher.saveFileAs(fileName);
 112  
                                                                 }
 113  
                                                         }
 114  
                                                 }
 115  
                                         } 
 116  
                                 }
 117  0
                                 saveConfiguration();
 118  0
                                 System.exit(0);        
 119  0
                         }                
 120  
                 });
 121  
                 
 122  0
                 mFrame.setVisible(true);
 123  0
         }
 124  
 
 125  
 }