Coverage Report - de.glossmaker.gloss.datastructure.gui.Config
 
Classes in this File Line Coverage Branch Coverage Complexity
Config
0%
0/186
0%
0/24
1,8
 
 1  
 package de.glossmaker.gloss.datastructure.gui;
 2  
 
 3  
 import jancilla.util.Properties;
 4  
 import jancilla.util.PropertiesFactory;
 5  
 
 6  
 import java.io.File;
 7  
 import java.io.FileInputStream;
 8  
 import java.io.FileNotFoundException;
 9  
 import java.io.FileOutputStream;
 10  
 import java.util.HashMap;
 11  
 import java.util.HashSet;
 12  
 import java.util.NoSuchElementException;
 13  
 
 14  
 import org.apache.log4j.Logger;
 15  
 
 16  
 /**
 17  
  * This class stores the configuration parameter in a text file.
 18  
  * 
 19  
  * @author Markus Flingelli
 20  
  *
 21  
  */
 22  
 public class Config {
 23  0
         private final static Logger mLogger = Logger.getLogger(Config.class);
 24  
         public final static String CONFIG_FILE = "glossmaker.properties";
 25  
         
 26  0
         private static Config mConfig = null;
 27  0
         private Properties mProps = null;
 28  0
         private HashSet<String> mLastOpenedFiles = null;
 29  0
         private HashMap<String, GuiTable> mGuiTableMap = null;
 30  
         
 31  0
         Config(String fileName) {
 32  0
                 mProps = PropertiesFactory.createSortedProperties();
 33  0
                 mLastOpenedFiles = new HashSet<String>();
 34  0
                 mGuiTableMap = new HashMap<String, GuiTable>();
 35  
                 try {
 36  0
                         mProps.load(new FileInputStream(fileName));
 37  0
                         readLastOpenedFiles();
 38  0
                         readGuiTables();
 39  0
                 } catch (FileNotFoundException e) {
 40  0
                         mLogger.error(e);
 41  0
                 }
 42  0
         }
 43  
         
 44  
         public void setPrintBlanksAndTabs(boolean value) {
 45  0
                 mProps.setProperty("printBlanksAndTabs", value);
 46  0
         }
 47  
         
 48  
         public boolean isPrintBlankAndTabs() {
 49  0
                 boolean result = false;
 50  
                 try {
 51  0
                         result = mProps.getBoolean("printBlanksAndTabs");
 52  0
                 } catch (NoSuchElementException e) {
 53  0
                         result = false;
 54  0
                 }
 55  
                 
 56  0
                 return result;
 57  
         }
 58  
         
 59  
         private void readLastOpenedFiles() {
 60  
                 int result;
 61  
                 try {
 62  0
                         result = Integer.parseInt(mProps.getProperty("lastOpenedFileCount"));
 63  0
                 } catch(Exception e) {
 64  0
                         result = 0;
 65  0
                 }
 66  0
                 for(int i = 0; i < result; i++) {
 67  0
                         String fileName = mProps.getProperty("lastOpenedFile." + i);
 68  0
                         File f = new File(fileName);
 69  0
                         if (f.exists()) {
 70  0
                                 mLastOpenedFiles.add(fileName);
 71  
                         }
 72  
                 }
 73  0
         }
 74  
         
 75  
         public static Config getInstance() {
 76  0
                 if (mConfig == null) {
 77  0
                         mConfig = new Config(CONFIG_FILE);
 78  
                 }
 79  
                 
 80  0
                 return mConfig;
 81  
         }
 82  
         
 83  
         public void setWindowPosition(String name, int x, int y, int widht, int height) {        
 84  0
                 mProps.setProperty(name + ".x", x);
 85  0
                 mProps.setProperty(name + ".y", y);
 86  0
                 mProps.setProperty(name + ".width", widht);
 87  0
                 mProps.setProperty(name + ".height", height);
 88  0
         }
 89  
         
 90  
         public int getWindowPositionX(String name, int defaultValue) {
 91  0
                 int result = defaultValue;
 92  
                 try {
 93  0
                         result = mProps.getInt(name + ".x");
 94  0
                 } catch(Exception e) {
 95  0
                         result = defaultValue;
 96  0
                 }
 97  0
                 return result;
 98  
         }
 99  
         
 100  
         public int getWindowPositionY(String name, int defaultValue) {
 101  0
                 int result = defaultValue;
 102  
                 try {
 103  0
                         result = mProps.getInt(name + ".y");
 104  0
                 } catch(Exception e) {
 105  0
                         result = defaultValue;
 106  0
                 }
 107  0
                 return result;
 108  
         }
 109  
 
 110  
         public int getWindowPositionHeight(String name, int defaultValue) {
 111  0
                 int result = defaultValue;
 112  
                 try {
 113  0
                         result = mProps.getInt(name + ".height");
 114  0
                 } catch(Exception e) {
 115  0
                         result = defaultValue;
 116  0
                 }
 117  0
                 return result;
 118  
         }
 119  
 
 120  
         public int getWindowPositionWidth(String name, int defaultValue) {
 121  0
                 int result = defaultValue;
 122  
                 try {
 123  0
                         result = mProps.getInt(name + ".width");
 124  0
                 } catch(Exception e) {
 125  0
                         result = defaultValue;
 126  0
                 }
 127  0
                 return result;
 128  
         }
 129  
         
 130  
         public String getLanguage() {
 131  0
                 String result = null;
 132  
                 try {
 133  0
                         result =mProps.getProperty("language");
 134  0
                 } catch (NoSuchElementException e) {
 135  0
                         mLogger.error("No such element availabe.");
 136  0
                 }                
 137  0
                 return result;
 138  
         }
 139  
         
 140  
         public void setLanguage(String lang) {
 141  0
                 mProps.setProperty("language", lang);
 142  0
         }
 143  
         
 144  
         public String getLastSavedPath() {
 145  0
                 String result = null;
 146  
                 try {
 147  0
                         result = mProps.getProperty("lastSavedPath");
 148  0
                 } catch (NoSuchElementException e) {
 149  0
                         mLogger.error("No such element availabe.");
 150  0
                 }                
 151  0
                 return result;
 152  
         }
 153  
         
 154  
         public void setLastSavedPath(String path) {
 155  0
                 mProps.setProperty("lastSavedPath", path);
 156  0
         }
 157  
         
 158  
         public String getLastOpenedPath() {
 159  0
                 String lastOpenedPath = null;
 160  
                 try {
 161  0
                         lastOpenedPath = mProps.getProperty("lastOpenedPath");
 162  0
                 } catch (NoSuchElementException e) {
 163  0
                         mLogger.error("No such element availabe.");
 164  0
                 }
 165  0
                 return lastOpenedPath;
 166  
         }
 167  
         
 168  
         public void setLastOpenedPath(String path) {
 169  0
                 mProps.setProperty("lastOpenedPath", path);
 170  0
         }
 171  
         
 172  
         public String getLastImporteddPath() {
 173  0
                 String result = null;
 174  
                 try {
 175  0
                         result = mProps.getProperty("lastImportedPath");
 176  0
                 } catch (NoSuchElementException e) {
 177  0
                         mLogger.error("No such element availabe.");
 178  0
                 }                
 179  0
                 return result;
 180  
         }
 181  
         
 182  
         public void setLastImportedPath(String path) {
 183  0
                 mProps.setProperty("lastImportedPath", path);
 184  0
         }
 185  
         
 186  
         public int getLastOpenedFilesCount() {
 187  0
                 int result = 0;
 188  
                 try {
 189  0
                         result =mLastOpenedFiles.size();
 190  0
                 } catch (NoSuchElementException e) {
 191  0
                         mLogger.error("No such element availabe.");
 192  0
                 }                
 193  0
                 return result;
 194  
         }
 195  
         
 196  
         public void setBibTeXFile(String filename) {
 197  0
                 mProps.setProperty("bibtex-file", filename);
 198  0
         }
 199  
         
 200  
         public boolean isAutoUpdateEnabled() {
 201  0
                 boolean result = false;
 202  
                 try {
 203  0
                         result = mProps.getBoolean("isAutoUpdateEnabled");
 204  0
                 } catch (NoSuchElementException e) {
 205  0
                         mLogger.error("No such element availabe.");
 206  0
                 }                
 207  0
                 return result;
 208  
         }
 209  
         
 210  
         public void setAutoUpdateEnabled(boolean isEnabled) {
 211  0
                 mProps.setProperty("isAutoUpdateEnabled", isEnabled);
 212  0
         }
 213  
         
 214  
         public String getBibTeXFile() {
 215  0
                 String bibTeXfile = null;
 216  
                 try {
 217  0
                         bibTeXfile = mProps.getProperty("bibtex-file");
 218  0
                 } catch (NoSuchElementException e) {
 219  0
                         mLogger.error("No such element availabe.");
 220  0
                 }
 221  
                 
 222  0
                 return bibTeXfile; 
 223  
         }
 224  
         
 225  
         public String getLastOpenedFile(int index) {
 226  0
                 String result = null;
 227  0
                 int i = 0;
 228  0
                 for(String fileName : mLastOpenedFiles) {
 229  0
                         if (i == index) {
 230  0
                                 result = fileName;
 231  0
                                 break;
 232  
                         }
 233  0
                         i++;
 234  
                 }
 235  
                 
 236  0
                 return result;
 237  
         }
 238  
         
 239  
         public void addLastOpenedFile(String fileName) {
 240  0
                 mLastOpenedFiles.add(fileName);
 241  0
         }
 242  
         
 243  
         public boolean removeLastOpenedFile(String fileName) {
 244  0
                 return mLastOpenedFiles.remove(fileName);
 245  
         }
 246  
         
 247  
         public void saveFile(String fileName) {
 248  
                 try {
 249  0
                         saveLastOpenedFiles();
 250  0
                         saveGuiTables();
 251  0
                         mProps.store(new FileOutputStream(fileName));
 252  0
                 } catch (FileNotFoundException e) {
 253  0
                         mLogger.error(e);
 254  0
                 }        
 255  0
         }
 256  
         
 257  
         private void saveLastOpenedFiles() {
 258  0
                 mProps.setProperty("lastOpenedFileCount", "" + mLastOpenedFiles.size());
 259  0
                 int i = 0;
 260  0
                 for(String fileName : mLastOpenedFiles) {
 261  0
                         mProps.setProperty("lastOpenedFile." + i, fileName);
 262  0
                         i++;
 263  
                 }
 264  0
         }
 265  
         
 266  
         private void saveGuiTables() {
 267  0
                 mProps.setProperty("tableCount", mGuiTableMap.size());
 268  0
                 int i = 0;
 269  0
                 for(GuiTable table : mGuiTableMap.values()) {
 270  0
                         mProps.setProperty("table-name." + i, table.getTableName());
 271  0
                         mProps.setProperty("table-size." + i, table.size());
 272  0
                         for(int j = 0; j < table.size(); j++) {
 273  0
                                 mProps.setProperty("table-width." + i + "." + j, table.getColumnWidth(j));
 274  0
                                 mProps.setProperty("table-position." + i + "." + j, table.getColumnPosition(j));
 275  
                         }
 276  0
                         i++;
 277  
                 }
 278  0
         }
 279  
         
 280  
         private void readGuiTables() {
 281  
                 try {
 282  0
                         int count = mProps.getInt("tableCount");
 283  0
                         for(int i = 0; i < count; i++) {                        
 284  0
                                 String tableName = mProps.getProperty("table-name." + i);
 285  0
                                 int size = mProps.getInt("table-size." + i);
 286  0
                                 GuiTable table = new GuiTable(tableName, size);
 287  0
                                 for(int j = 0; j < size; j++) {
 288  0
                                         table.setColumnWidth(j, mProps.getInt("table-width." + i + "." + j));
 289  0
                                         table.setColumnPosition(j, mProps.getInt("table-position." + i + "." + j));
 290  
                                 }
 291  0
                                 mGuiTableMap.put(tableName, table);
 292  
                         }
 293  0
                 } catch (Exception e) {
 294  0
                         mLogger.error(e);
 295  0
                 }
 296  0
         }
 297  
         
 298  
         public void addTable(String name, int size) {
 299  0
                 GuiTable table = new GuiTable(name, size);
 300  0
                 mGuiTableMap.put(name, table);
 301  0
         }
 302  
         
 303  
         public void setTableWidth(String name, int col, int width) {
 304  0
                 GuiTable table = mGuiTableMap.get(name);
 305  0
                 table.setColumnWidth(col, width);
 306  0
         }
 307  
         
 308  
         public void moveColumn(String name, int fromColumn, int toColumn) {
 309  0
                 GuiTable table = mGuiTableMap.get(name);
 310  0
                 table.moveColumn(fromColumn, toColumn);
 311  0
         }
 312  
         
 313  
         public int getColumnWidth(String name, int index, int defaultValue) {
 314  0
                 int result = defaultValue;
 315  0
                 GuiTable table = mGuiTableMap.get(name);
 316  0
                 if (table != null) {
 317  0
                         result = table.getColumnWidth(index);
 318  
                 }
 319  0
                 return result;
 320  
         }
 321  
         
 322  
         public int getColumnPosition(String name, int index) {
 323  0
                 int result = index;
 324  0
                 GuiTable table = mGuiTableMap.get(name);
 325  0
                 if (table != null) {
 326  0
                         result = table.getColumnPosition(index);
 327  
                 }
 328  0
                 return result;
 329  
         }
 330  
 }