1 29 38 39 package org.planetamessenger.mos.engine; 40 41 import org.planetamessenger.io.*; 42 43 44 public class JConfiguration { 45 46 public static final int SHARE_CONFIG = 0; 47 public static final int NOSHARE_CONFIG = 1; 48 49 private static final String SYSTEM_VERSION = "Pentarou 0.3"; 50 private static final String PROPERTY_FILE_TITLE = "PlanetaMessenger configuration file"; 51 52 private final int PROP_MAIN_WINDOW = 0; 53 private final int PROP_PROFILE_WINDOW = 1; 54 private final int PROP_PREFERENCES_WINDOW = 2; 55 private final int PM_DEFAULT_WIDTH = 195; 56 private final int PM_DEFAULT_HEIGHT = 365; 57 private final int PROF_DEFAULT_WIDTH = 500; 58 private final int PROF_DEFAULT_HEIGHT = 411; 59 private final int PREF_DEFAULT_WIDTH = 698; 60 private final int PREF_DEFAULT_HEIGHT = 571; 61 private final int PREF_DEFAULT_SPLIT_POS = 167; 62 private final int MAX_REQUIRED_DIR = 7; 63 64 private final String PM_DEFAULT_STATUS = "visible"; 65 private final String PROPERTY_DIR = ".planetamessenger"; 66 private final String PROPERTY_FILE = ".properties"; 67 private final String PM_HOME = "planetamessenger.home"; 68 private final String PM_VERSION = "planetamessenger.version"; 69 private final String MAIN_WINDOW_X = "mainwindow.x"; 70 private final String MAIN_WINDOW_Y = "mainwindow.y"; 71 private final String MAIN_WINDOW_W = "mainwindow.width"; 72 private final String MAIN_WINDOW_H = "mainwindow.height"; 73 private final String MAIN_WINDOW_S = "mainwindow.status"; 74 private final String PROFILE_WINDOW_X = "profilewindow.x"; 75 private final String PROFILE_WINDOW_Y = "profilewindow.y"; 76 private final String PROFILE_WINDOW_W = "profilewindow.width"; 77 private final String PROFILE_WINDOW_H = "profilewindow.height"; 78 private final String PREFERENCES_WINDOW_X = "preferenceswindow.x"; 79 private final String PREFERENCES_WINDOW_Y = "preferenceswindow.y"; 80 private final String PREFERENCES_WINDOW_W = "preferenceswindow.width"; 81 private final String PREFERENCES_WINDOW_H = "preferenceswindow.height"; 82 private final String PREFERENCES_WINDOW_SPLIT_POS = "preferenceswindow.split.position"; 83 84 85 static int nMode; 86 String strPath; 87 String strMainWindowStatus; 88 java.awt.Dimension dmMainWindow; 89 java.awt.Point ptMainWindow; 90 java.awt.Dimension dmProfileWindow; 91 java.awt.Point ptProfileWindow; 92 java.awt.Dimension dmPreferencesWindow; 93 java.awt.Point ptPreferencesWindow; 94 int nPreferencesSplitPosition; 95 96 97 100 static { 101 nMode = SHARE_CONFIG; 102 } 103 104 105 109 public JConfiguration() { 110 111 java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); 112 java.util.Properties properties = new java.util.Properties (); 113 String strHomeDir = ( nMode == SHARE_CONFIG ? JClassPath.getCurrentDirectory() + JClassPath.getFileSeparator() + ".." : JClassPath.getUserHomeDirectory() ); 114 String strCurrentDir = strHomeDir + JClassPath.getFileSeparator(); 115 String strPMDir = strCurrentDir + PROPERTY_DIR; 116 java.io.File dir = new java.io.File ( strPMDir ); 117 java.io.File propertyFile = new java.io.File ( strPMDir + JClassPath.getFileSeparator() + PROPERTY_FILE ); 118 boolean bAutoConfig = true; 119 120 121 dmMainWindow = new java.awt.Dimension ( PM_DEFAULT_WIDTH, PM_DEFAULT_HEIGHT ); 122 ptMainWindow = new java.awt.Point ( 0, 0 ); 123 dmProfileWindow = new java.awt.Dimension ( PROF_DEFAULT_WIDTH, PROF_DEFAULT_HEIGHT ); 124 ptProfileWindow = new java.awt.Point ( ( ( screenSize.width - PROF_DEFAULT_WIDTH ) / 2 ), ( ( screenSize.height - PROF_DEFAULT_HEIGHT ) / 2 ) ); 125 dmPreferencesWindow = new java.awt.Dimension ( PREF_DEFAULT_WIDTH, PREF_DEFAULT_HEIGHT ); 126 ptPreferencesWindow = new java.awt.Point ( ( ( screenSize.width - PREF_DEFAULT_WIDTH ) / 2 ), ( ( screenSize.height - PREF_DEFAULT_HEIGHT ) / 2 ) ); 127 128 while( true ) { 129 if( !dir.exists() || !propertyFile.exists() || !bAutoConfig ) { 130 int nDlgResult; 131 boolean bHomeIsOk = false; 132 133 134 do { 135 136 if( !bAutoConfig ) { 138 javax.swing.JFileChooser dlg = new javax.swing.JFileChooser (); 139 140 141 dlg.setDialogTitle( "PlanetaMessenger home directory" ); 142 dlg.setMultiSelectionEnabled( false ); 143 dlg.setFileSelectionMode( javax.swing.JFileChooser.DIRECTORIES_ONLY ); 144 145 javax.swing.JOptionPane.showMessageDialog( null, "Please to continue you must select the home directory where PlanetaMessenger was installed", "Information", javax.swing.JOptionPane.INFORMATION_MESSAGE ); 146 nDlgResult = dlg.showOpenDialog( null ); 147 148 if( dlg.getSelectedFile() != null ) 149 strCurrentDir = dlg.getSelectedFile().getPath() + JClassPath.getFileSeparator(); 150 else 151 strCurrentDir = null; 152 } 153 else 154 nDlgResult = javax.swing.JFileChooser.APPROVE_OPTION; 155 156 if( strCurrentDir != null ) { 157 java.lang.String [] strDirs; 159 java.io.File dirList = new java.io.File ( strCurrentDir ); 160 161 162 strDirs = dirList.list(); 163 164 if( strDirs != null ) { 165 if( strDirs.length >= MAX_REQUIRED_DIR ) { 166 167 int nDirFound = 0; 168 169 for( int nCount = 0; nCount < strDirs.length; nCount++ ) { 170 174 if( ( strDirs[nCount].compareToIgnoreCase( "lib" ) == 0 ) || 175 ( strDirs[nCount].compareToIgnoreCase( "bin" ) == 0 ) || 176 ( strDirs[nCount].compareToIgnoreCase( "plugins" ) == 0 ) || 177 ( strDirs[nCount].compareToIgnoreCase( "resources" ) == 0 ) || 178 ( strDirs[nCount].compareToIgnoreCase( "plaf" ) == 0 ) || 179 ( strDirs[nCount].compareToIgnoreCase( "languages" ) == 0 ) || 180 ( strDirs[nCount].compareToIgnoreCase( "db" ) == 0 ) ) 181 nDirFound++; 182 } 183 184 if( nDirFound == MAX_REQUIRED_DIR ) { 185 dir.mkdir(); 186 strPath = strCurrentDir; 187 properties.setProperty( PM_HOME, strPath ); 189 properties.setProperty( PM_VERSION, SYSTEM_VERSION ); 190 191 properties.setProperty( MAIN_WINDOW_X, "0" ); 193 properties.setProperty( MAIN_WINDOW_Y, "0" ); 194 properties.setProperty( MAIN_WINDOW_W, Integer.toString( PM_DEFAULT_WIDTH ) ); 195 properties.setProperty( MAIN_WINDOW_H, Integer.toString( PM_DEFAULT_HEIGHT ) ); 196 properties.setProperty( MAIN_WINDOW_S, PM_DEFAULT_STATUS ); 197 strMainWindowStatus = PM_DEFAULT_STATUS; 198 199 properties.setProperty( PROFILE_WINDOW_X, Integer.toString( ptProfileWindow.x ) ); 201 properties.setProperty( PROFILE_WINDOW_Y, Integer.toString( ptProfileWindow.y ) ); 202 properties.setProperty( PROFILE_WINDOW_W, Integer.toString( PROF_DEFAULT_WIDTH ) ); 203 properties.setProperty( PROFILE_WINDOW_H, Integer.toString( PROF_DEFAULT_HEIGHT ) ); 204 205 nPreferencesSplitPosition = PREF_DEFAULT_SPLIT_POS; 207 properties.setProperty( PREFERENCES_WINDOW_X, Integer.toString( ptPreferencesWindow.x ) ); 208 properties.setProperty( PREFERENCES_WINDOW_Y, Integer.toString( ptPreferencesWindow.y ) ); 209 properties.setProperty( PREFERENCES_WINDOW_W, Integer.toString( PREF_DEFAULT_WIDTH ) ); 210 properties.setProperty( PREFERENCES_WINDOW_H, Integer.toString( PREF_DEFAULT_HEIGHT ) ); 211 properties.setProperty( PREFERENCES_WINDOW_SPLIT_POS, Integer.toString( nPreferencesSplitPosition ) ); 212 213 try { 214 java.io.FileOutputStream out = new java.io.FileOutputStream ( dir.getPath() + JClassPath.getFileSeparator() + PROPERTY_FILE ); 215 216 properties.store( out, PROPERTY_FILE_TITLE ); 217 nDlgResult = javax.swing.JFileChooser.APPROVE_OPTION; 218 bHomeIsOk = true; 219 } catch( java.io.IOException e ) { 220 System.err.println( "JConfiguration.JConfiguration() - Exception " + e ); 221 } 222 }else{ 223 System.err.println("JConfiguration.JConfiguration() - Expected number of directories ("+MAX_REQUIRED_DIR+") was not found."); 224 } 225 } 226 } 227 } 228 229 if( !bHomeIsOk && nDlgResult == javax.swing.JFileChooser.APPROVE_OPTION ) { 230 javax.swing.JOptionPane.showMessageDialog( null, "Unable to continue ! You MUST select the home directory that PlanetaMessenger is installed", "Error", javax.swing.JOptionPane.ERROR_MESSAGE ); 231 nDlgResult = javax.swing.JFileChooser.CANCEL_OPTION; 232 } 233 else 234 if( !bHomeIsOk ) { 235 javax.swing.JOptionPane.showMessageDialog( null, "The PlanetaMessenger Home directory could not be found. Shutting down.", "Error", javax.swing.JOptionPane.ERROR_MESSAGE ); 236 System.err.println( "JConfiguration.JConfiguration() - Fatal, the PM Home directory cannot be configured" ); 237 System.exit( 0 ); 238 } 239 240 bAutoConfig = false; 241 242 } while( nDlgResult != javax.swing.JFileChooser.APPROVE_OPTION ); 243 244 return; 245 } 246 else { 247 try { 248 java.io.FileInputStream in = new java.io.FileInputStream ( dir.getPath() + JClassPath.getFileSeparator() + PROPERTY_FILE ); 249 String strVersion; 250 251 properties.load( in ); 252 strPath = properties.getProperty( PM_HOME ); 253 254 try { 255 ptMainWindow.x = Integer.parseInt( properties.getProperty( MAIN_WINDOW_X, "0" ) ); 257 ptMainWindow.y = Integer.parseInt( properties.getProperty( MAIN_WINDOW_Y, "0" ) ); 258 dmMainWindow.width = Integer.parseInt( properties.getProperty( MAIN_WINDOW_W, Integer.toString( PM_DEFAULT_WIDTH ) ) ); 259 dmMainWindow.height = Integer.parseInt( properties.getProperty( MAIN_WINDOW_H, Integer.toString( PM_DEFAULT_HEIGHT ) ) ); 260 261 screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); 263 264 ptProfileWindow.x = Integer.parseInt( properties.getProperty( PROFILE_WINDOW_X, Integer.toString( ( screenSize.width - PROF_DEFAULT_WIDTH ) / 2 ) ) ); 265 ptProfileWindow.y = Integer.parseInt( properties.getProperty( PROFILE_WINDOW_Y, Integer.toString( ( screenSize.height - PROF_DEFAULT_HEIGHT ) / 2 ) ) ); 266 dmProfileWindow.width = Integer.parseInt( properties.getProperty( PROFILE_WINDOW_W ) ); 267 dmProfileWindow.height = Integer.parseInt( properties.getProperty( PROFILE_WINDOW_H ) ); 268 269 ptPreferencesWindow.x = Integer.parseInt( properties.getProperty( PREFERENCES_WINDOW_X, Integer.toString( ( screenSize.width - PREF_DEFAULT_WIDTH ) / 2 ) ) ); 271 ptPreferencesWindow.y = Integer.parseInt( properties.getProperty( PREFERENCES_WINDOW_Y, Integer.toString( ( screenSize.height - PREF_DEFAULT_HEIGHT ) / 2 ) ) ); 272 dmPreferencesWindow.width = Integer.parseInt( properties.getProperty( PREFERENCES_WINDOW_W ) ); 273 dmPreferencesWindow.height = Integer.parseInt( properties.getProperty( PREFERENCES_WINDOW_H ) ); 274 nPreferencesSplitPosition = Integer.parseInt( properties.getProperty( PREFERENCES_WINDOW_SPLIT_POS ) ); 275 276 } catch( NumberFormatException e ) { 277 System.err.println( "JConfiguration.JConfiguration() - " + e ); 278 } 279 280 strMainWindowStatus = properties.getProperty( MAIN_WINDOW_S, PM_DEFAULT_STATUS ); 281 strVersion = properties.getProperty( PM_VERSION ); 282 283 287 if( strVersion.compareTo( SYSTEM_VERSION ) != 0 ) { 288 propertyFile.delete(); 289 propertyFile = null; 290 dir = null; 291 dir = new java.io.File ( strPMDir ); 292 propertyFile = new java.io.File ( strPMDir + JClassPath.getFileSeparator() + PROPERTY_FILE ); 293 bAutoConfig = false; 294 properties.clear(); 295 296 continue; 297 } 298 } catch( java.io.IOException e ) { 299 System.err.println( "JConfiguration.JConfiguration() - Exception " + e ); 300 propertyFile = null; 301 dir = null; 302 dir = new java.io.File ( strPMDir ); 303 propertyFile = new java.io.File ( strPMDir + JClassPath.getFileSeparator() + PROPERTY_FILE ); 304 bAutoConfig = false; 305 properties.clear(); 306 } 307 308 break; 309 } 310 } 311 } 312 313 324 static public void setMode( int nMode ) { 325 326 JConfiguration.nMode = nMode; 327 } 328 329 332 public java.lang.String getPluginsPath() { 333 334 return strPath + "plugins" + JClassPath.getFileSeparator(); 335 } 336 337 340 public java.lang.String getLibPath() { 341 342 return strPath + "lib" + JClassPath.getFileSeparator(); 343 } 344 345 348 public java.lang.String getDepLibPath() { 349 350 return getPluginsPath() + "deplib" + JClassPath.getFileSeparator(); 351 } 352 353 356 public java.lang.String getResourcesPath() { 357 358 return strPath + "resources" + JClassPath.getFileSeparator(); 359 } 360 361 364 public java.lang.String getLookAndFeelPath() { 365 366 return strPath + "plaf" + JClassPath.getFileSeparator(); 367 } 368 369 372 public java.lang.String getLanguagesPath() { 373 374 return strPath + "languages" + JClassPath.getFileSeparator(); 375 } 376 377 380 public java.lang.String getBinPath() { 381 382 return strPath + "bin" + JClassPath.getFileSeparator(); 383 } 384 385 388 public java.lang.String getDatabaseName() { 389 390 return strPath + "db" + JClassPath.getFileSeparator() + "planetamessenger"; 391 } 392 393 396 public java.awt.Point getMainWindowLocation() { 397 398 return ptMainWindow; 399 } 400 401 404 public java.awt.Dimension getMainWindowSize() { 405 406 return dmMainWindow; 407 } 408 409 412 public boolean getMainWindowStatus() { 413 414 if( strMainWindowStatus.compareTo( PM_DEFAULT_STATUS ) == 0 ) 415 return true; 416 else 417 return false; 418 } 419 420 430 public void setMainWindowProperties( java.awt.Point ptWindow, java.awt.Dimension dmWindow, boolean bWindowStatus ) { 431 432 setWindowProperties( ptWindow, dmWindow, bWindowStatus, PROP_MAIN_WINDOW ); 433 } 434 435 438 public java.awt.Point getProfileWindowLocation() { 439 440 return ptProfileWindow; 441 } 442 443 446 public java.awt.Dimension getProfileWindowSize() { 447 448 return dmProfileWindow; 449 } 450 451 458 public void setProfileWindowProperties( java.awt.Point ptWindow, java.awt.Dimension dmWindow ) { 459 460 setWindowProperties( ptWindow, dmWindow, true, PROP_PROFILE_WINDOW ); 461 } 462 463 466 public java.awt.Point getPreferencesWindowLocation() { 467 468 return ptPreferencesWindow; 469 } 470 471 474 public java.awt.Dimension getPreferencesWindowSize() { 475 476 return dmPreferencesWindow; 477 } 478 479 482 public int getPreferencesSplitPosition() { 483 484 return nPreferencesSplitPosition; 485 } 486 487 494 public void setPreferencesWindowProperties( java.awt.Point ptWindow, java.awt.Dimension dmWindow ) { 495 496 setWindowProperties( ptWindow, dmWindow, true, PROP_PREFERENCES_WINDOW ); 497 } 498 499 503 public void setPreferencesSplitPosition( int nPreferencesSplitPosition ) { 504 505 this.nPreferencesSplitPosition = nPreferencesSplitPosition; 506 } 507 508 520 private void setWindowProperties( java.awt.Point ptWindow, java.awt.Dimension dmWindow, boolean bWindowStatus, int nWindowType ) { 521 522 if( JSharedObjects.getProfileManager().getDefaultProfile() == -1 ) 524 return; 525 526 try { 527 String strPropNameX; 528 String strPropNameY; 529 String strPropNameW; 530 String strPropNameH; 531 int nDefPropW; 532 int nDefPropH; 533 java.util.Properties properties = new java.util.Properties (); 534 String strHomeDir = ( nMode == SHARE_CONFIG ? JClassPath.getCurrentDirectory() + JClassPath.getFileSeparator() + ".." : JClassPath.getUserHomeDirectory() ); 535 String strPropFile = strHomeDir + JClassPath.getFileSeparator() + PROPERTY_DIR + JClassPath.getFileSeparator() + PROPERTY_FILE; 536 java.io.FileInputStream in = new java.io.FileInputStream ( strPropFile ); 537 java.io.FileOutputStream out; 538 539 properties.load( in ); 540 in.close(); 541 542 switch( nWindowType ) { 543 544 case PROP_MAIN_WINDOW : { 545 strPropNameX = MAIN_WINDOW_X; 546 strPropNameY = MAIN_WINDOW_Y; 547 strPropNameW = MAIN_WINDOW_W; 548 strPropNameH = MAIN_WINDOW_H; 549 nDefPropW = PM_DEFAULT_WIDTH; 550 nDefPropH = PM_DEFAULT_WIDTH; 551 ptMainWindow = ptWindow; 552 dmMainWindow = dmWindow; 553 554 if( bWindowStatus ) 555 strMainWindowStatus = PM_DEFAULT_STATUS; 556 else 557 strMainWindowStatus = "hidden"; 558 559 properties.setProperty( MAIN_WINDOW_S, ( ( dmWindow.width == 0 || dmWindow.height == 0 ) ? PM_DEFAULT_STATUS : strMainWindowStatus ) ); 560 } break; 561 562 case PROP_PROFILE_WINDOW : { 563 strPropNameX = PROFILE_WINDOW_X; 564 strPropNameY = PROFILE_WINDOW_Y; 565 strPropNameW = PROFILE_WINDOW_W; 566 strPropNameH = PROFILE_WINDOW_H; 567 nDefPropW = PROF_DEFAULT_WIDTH; 568 nDefPropH = PROF_DEFAULT_WIDTH; 569 ptProfileWindow = ptWindow; 570 dmProfileWindow = dmWindow; 571 } break; 572 573 case PROP_PREFERENCES_WINDOW : { 574 strPropNameX = PREFERENCES_WINDOW_X; 575 strPropNameY = PREFERENCES_WINDOW_Y; 576 strPropNameW = PREFERENCES_WINDOW_W; 577 strPropNameH = PREFERENCES_WINDOW_H; 578 nDefPropW = PREF_DEFAULT_WIDTH; 579 nDefPropH = PREF_DEFAULT_WIDTH; 580 ptPreferencesWindow = ptWindow; 581 dmPreferencesWindow = dmWindow; 582 properties.setProperty( PREFERENCES_WINDOW_SPLIT_POS, Integer.toString( nPreferencesSplitPosition ) ); 583 } break; 584 585 default : return; 586 } 587 588 out = new java.io.FileOutputStream ( strPropFile ); 589 properties.setProperty( strPropNameX, Integer.toString( ptWindow.x ) ); 590 properties.setProperty( strPropNameY, Integer.toString( ptWindow.y ) ); 591 properties.setProperty( strPropNameW, Integer.toString( ( dmWindow.width == 0 ? nDefPropW : dmWindow.width ) ) ); 592 properties.setProperty( strPropNameH, Integer.toString( ( dmWindow.height == 0 ? nDefPropH : dmWindow.height ) ) ); 593 594 properties.store( out, "PlanetaMessenger configuration file" ); 595 out.close(); 596 597 } catch( java.io.IOException e ) { 598 System.err.println( "JConfiguration.setWindowProperties() - " + e ); 599 } 600 } 601 } 602 | Popular Tags |