Moderate code cleanup

This commit is contained in:
Vhati 2018-01-05 13:07:19 -05:00
parent 7528ec271a
commit ad6d87a4bf
4 changed files with 81 additions and 79 deletions

View file

@ -112,10 +112,10 @@ public class FTLModManager {
props.setProperty( SlipstreamConfig.STEAM_EXE_PATH, "" ); // Prompt. props.setProperty( SlipstreamConfig.STEAM_EXE_PATH, "" ); // Prompt.
props.setProperty( SlipstreamConfig.RUN_STEAM_FTL, "" ); // Prompt. props.setProperty( SlipstreamConfig.RUN_STEAM_FTL, "" ); // Prompt.
props.setProperty( SlipstreamConfig.NEVER_RUN_FTL, "false" ); props.setProperty( SlipstreamConfig.NEVER_RUN_FTL, "false" );
props.setProperty( SlipstreamConfig.UPDATE_CATALOG, "" ); // Prompt.
props.setProperty( SlipstreamConfig.UPDATE_APP, "" ); // Prompt.
props.setProperty( SlipstreamConfig.USE_DEFAULT_UI, "false" ); props.setProperty( SlipstreamConfig.USE_DEFAULT_UI, "false" );
props.setProperty( SlipstreamConfig.REMEMBER_GEOMETRY, "true" ); props.setProperty( SlipstreamConfig.REMEMBER_GEOMETRY, "true" );
// "update_catalog" doesn't have a default.
// "update_app" doesn't have a default.
// "manager_geometry" doesn't have a default. // "manager_geometry" doesn't have a default.
// Read the config file. // Read the config file.

View file

@ -95,7 +95,7 @@ public class SlipstreamConfig {
List<String> allFieldsList = new ArrayList<String>( userFieldsMap.size() + appFieldsMap.size() ); List<String> allFieldsList = new ArrayList<String>( userFieldsMap.size() + appFieldsMap.size() );
allFieldsList.addAll( userFieldsMap.keySet() ); allFieldsList.addAll( userFieldsMap.keySet() );
allFieldsList.addAll( userFieldsMap.keySet() ); allFieldsList.addAll( appFieldsMap.keySet() );
int fieldWidth = 0; int fieldWidth = 0;
for ( String fieldName : allFieldsList ) { for ( String fieldName : allFieldsList ) {
fieldWidth = Math.max( fieldName.length(), fieldWidth ); fieldWidth = Math.max( fieldName.length(), fieldWidth );

View file

@ -405,11 +405,11 @@ public class ManagerFrame extends JFrame implements ActionListener, ModsScanObse
/** /**
* Extra initialization that must be called after the constructor. * Extra initialization that must be called after the constructor.
* This must be called on the Swing event thread (use invokeLater()).
*/ */
public void init() { public void init() {
ManagerInitThread initThread = new ManagerInitThread( this, ManagerInitThread initThread = new ManagerInitThread(
this,
new SlipstreamConfig( appConfig ), new SlipstreamConfig( appConfig ),
modsDir, modsDir,
modsTableStateFile, modsTableStateFile,
@ -551,7 +551,7 @@ public class ManagerFrame extends JFrame implements ActionListener, ModsScanObse
// Links. // Links.
infoArea.appendRegularText( String.format( "Version %s: ", appUpdateInfo.getLatestVersion().toString() ) ); infoArea.appendRegularText( String.format( "Version %s: ", appUpdateInfo.getLatestVersion().toString() ) );
boolean first = true; boolean first = true;
for ( Map.Entry<String,String> entry : appUpdateInfo.getLatestURLs().entrySet() ) { for ( Map.Entry<String, String> entry : appUpdateInfo.getLatestURLs().entrySet() ) {
if ( !first ) infoArea.appendRegularText( " " ); if ( !first ) infoArea.appendRegularText( " " );
infoArea.appendRegularText( "[" ); infoArea.appendRegularText( "[" );
infoArea.appendLinkText( entry.getValue(), entry.getKey() ); infoArea.appendLinkText( entry.getValue(), entry.getKey() );
@ -569,7 +569,7 @@ public class ManagerFrame extends JFrame implements ActionListener, ModsScanObse
} }
// Changelog. // Changelog.
for ( Map.Entry<ComparableVersion,List<String>> entry : appUpdateInfo.getChangelog().entrySet() ) { for ( Map.Entry<ComparableVersion, List<String>> entry : appUpdateInfo.getChangelog().entrySet() ) {
if ( appVersion.compareTo( entry.getKey() ) >= 0 ) break; if ( appVersion.compareTo( entry.getKey() ) >= 0 ) break;
if ( buf.length() > 0 ) buf.append( "\n" ); if ( buf.length() > 0 ) buf.append( "\n" );
@ -1032,33 +1032,21 @@ public class ManagerFrame extends JFrame implements ActionListener, ModsScanObse
} }
/** /**
* Sets the ModDB for the catalog. (thread-safe) * Sets the ModDB for the catalog.
*/ */
public void setCatalogModDB( final ModDB newDB ) { public void setCatalogModDB( ModDB newDB ) {
Runnable r = new Runnable() { catalogModDB = newDB;
@Override
public void run() { catalogModDB = newDB; }
};
if ( SwingUtilities.isEventDispatchThread() ) r.run();
else SwingUtilities.invokeLater( r );
} }
/** /**
* Sets info about available app updates. (thread-safe) * Sets info about available app updates.
*/ */
public void setAppUpdateInfo( final AutoUpdateInfo aui ) { public void setAppUpdateInfo( AutoUpdateInfo aui ) {
Runnable r = new Runnable() {
@Override
public void run() {
appUpdateInfo = aui; appUpdateInfo = aui;
boolean isUpdateAvailable = ( appVersion.compareTo(appUpdateInfo.getLatestVersion()) < 0 ); boolean isUpdateAvailable = ( appVersion.compareTo(appUpdateInfo.getLatestVersion()) < 0 );
updateBtn.setForeground( isUpdateAvailable ? updateBtnEnabledColor : updateBtnDisabledColor ); updateBtn.setForeground( isUpdateAvailable ? updateBtnEnabledColor : updateBtnDisabledColor );
updateBtn.setEnabled( isUpdateAvailable ); updateBtn.setEnabled( isUpdateAvailable );
} }
};
if ( SwingUtilities.isEventDispatchThread() ) r.run();
else SwingUtilities.invokeLater( r );
}
@Override @Override

View file

@ -10,7 +10,6 @@ import java.io.IOException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
@ -53,6 +52,7 @@ public class ManagerInitThread extends Thread {
public ManagerInitThread( ManagerFrame frame, SlipstreamConfig appConfig, File modsDir, File modsTableStateFile, File metadataFile, File catalogFile, File catalogETagFile, File appUpdateFile, File appUpdateETagFile ) { public ManagerInitThread( ManagerFrame frame, SlipstreamConfig appConfig, File modsDir, File modsTableStateFile, File metadataFile, File catalogFile, File catalogETagFile, File appUpdateFile, File appUpdateETagFile ) {
super( "init" );
this.frame = frame; this.frame = frame;
this.appConfig = appConfig; this.appConfig = appConfig;
this.modsDir = modsDir; this.modsDir = modsDir;
@ -77,6 +77,7 @@ public class ManagerInitThread extends Thread {
private void init() throws InterruptedException { private void init() throws InterruptedException {
if ( metadataFile.exists() ) { if ( metadataFile.exists() ) {
// Load cached metadata first, before scanning for new info. // Load cached metadata first, before scanning for new info.
ModDB cachedDB = JacksonCatalogReader.parse( metadataFile ); ModDB cachedDB = JacksonCatalogReader.parse( metadataFile );
@ -105,27 +106,24 @@ public class ManagerInitThread extends Thread {
int catalogUpdateInterval = appConfig.getPropertyAsInt( "update_catalog", 0 ); int catalogUpdateInterval = appConfig.getPropertyAsInt( "update_catalog", 0 );
boolean needNewCatalog = false; boolean needNewCatalog = false;
if ( catalogFile.exists() ) { // Load the catalog first, before downloading.
// Load the catalog first, before updating. if ( catalogFile.exists() ) reloadCatalog();
reloadCatalog();
if ( catalogUpdateInterval > 0 ) { if ( catalogUpdateInterval > 0 ) {
if ( catalogFile.exists() ) {
// Check if the downloaded catalog is stale. // Check if the downloaded catalog is stale.
if ( isFileStale( catalogFile, catalogUpdateInterval ) ) { if ( isFileStale( catalogFile, catalogUpdateInterval ) ) {
log.debug( String.format( "Catalog is older than %d days.", catalogUpdateInterval ) ); log.debug( String.format( "Catalog is older than %d days", catalogUpdateInterval ) );
needNewCatalog = true; needNewCatalog = true;
} else { } else {
log.debug( "Catalog isn't stale yet." ); log.debug( "Catalog isn't stale yet" );
}
} }
} }
else { else {
// Catalog file doesn't exist. // Catalog file doesn't exist.
needNewCatalog = true; needNewCatalog = true;
} }
}
// Don't update if the user doesn't want to.
if ( catalogUpdateInterval <= 0 ) needNewCatalog = false;
if ( needNewCatalog ) { if ( needNewCatalog ) {
boolean fetched = URLFetcher.refetchURL( ManagerFrame.CATALOG_URL, catalogFile, catalogETagFile ); boolean fetched = URLFetcher.refetchURL( ManagerFrame.CATALOG_URL, catalogFile, catalogETagFile );
@ -134,30 +132,27 @@ public class ManagerInitThread extends Thread {
} }
} }
int appUpdateInterval = appConfig.getPropertyAsInt( "update_app", 0 ); // Load the cached info first, before downloading.
if ( appUpdateFile.exists() ) reloadAppUpdateInfo();
int appUpdateInterval = appConfig.getPropertyAsInt( SlipstreamConfig.UPDATE_APP, 0 );
boolean needAppUpdate = false; boolean needAppUpdate = false;
if ( appUpdateFile.exists() ) {
// Load the info first, before downloading.
reloadAppUpdateInfo();
if ( appUpdateInterval > 0 ) { if ( appUpdateInterval > 0 ) {
if ( appUpdateFile.exists() ) {
// Check if the app update info is stale. // Check if the app update info is stale.
if ( isFileStale( appUpdateFile, appUpdateInterval ) ) { if ( isFileStale( appUpdateFile, appUpdateInterval ) ) {
log.debug( String.format( "App update info is older than %d days.", appUpdateInterval ) ); log.debug( String.format( "App update info is older than %d days", appUpdateInterval ) );
needAppUpdate = true; needAppUpdate = true;
} else { } else {
log.debug( "App update info isn't stale yet." ); log.debug( "App update info isn't stale yet" );
}
} }
} }
else { else {
// App update file doesn't exist. // App update file doesn't exist.
needAppUpdate = true; needAppUpdate = true;
} }
}
// Don't update if the user doesn't want to.
if ( appUpdateInterval <= 0 ) needAppUpdate = false;
if ( needAppUpdate ) { if ( needAppUpdate ) {
boolean fetched = URLFetcher.refetchURL( ManagerFrame.APP_UPDATE_URL, appUpdateFile, appUpdateETagFile ); boolean fetched = URLFetcher.refetchURL( ManagerFrame.APP_UPDATE_URL, appUpdateFile, appUpdateETagFile );
@ -186,7 +181,7 @@ public class ManagerInitThread extends Thread {
catch ( FileNotFoundException e ) { catch ( FileNotFoundException e ) {
} }
catch ( IOException e ) { catch ( IOException e ) {
log.error( String.format( "Error reading \"%s\".", modsTableStateFile.getName() ), e ); log.error( String.format( "Error reading \"%s\"", modsTableStateFile.getName() ), e );
fileNames.clear(); fileNames.clear();
} }
finally { finally {
@ -207,13 +202,27 @@ public class ManagerInitThread extends Thread {
private void reloadCatalog() { private void reloadCatalog() {
ModDB currentDB = JacksonCatalogReader.parse( catalogFile ); final ModDB currentDB = JacksonCatalogReader.parse( catalogFile );
if ( currentDB != null ) frame.setCatalogModDB( currentDB ); if ( currentDB != null ) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
frame.setCatalogModDB( currentDB );
}
});
}
} }
private void reloadAppUpdateInfo() { private void reloadAppUpdateInfo() {
AutoUpdateInfo aui = JacksonAutoUpdateReader.parse( appUpdateFile ); final AutoUpdateInfo aui = JacksonAutoUpdateReader.parse( appUpdateFile );
if ( aui != null ) frame.setAppUpdateInfo( aui ); if ( aui != null ) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
frame.setAppUpdateInfo( aui );
}
});
}
} }
@ -221,9 +230,14 @@ public class ManagerInitThread extends Thread {
* Returns true if a file is older than N days. * Returns true if a file is older than N days.
*/ */
private boolean isFileStale( File f, int maxDays ) { private boolean isFileStale( File f, int maxDays ) {
Date modifiedDate = new Date( f.lastModified() ); Calendar fileCal = Calendar.getInstance();
Calendar cal = Calendar.getInstance(); fileCal.setTimeInMillis( f.lastModified() );
cal.add( Calendar.DATE, maxDays * -1 ); fileCal.getTimeInMillis(); // Re-calculate calendar fields.
return modifiedDate.before( cal.getTime() );
Calendar freshCal = Calendar.getInstance();
freshCal.add( Calendar.DATE, maxDays * -1 );
freshCal.getTimeInMillis(); // Re-calculate calendar fields.
return (fileCal.compareTo( freshCal ) < 0);
} }
} }