/* This file provides the Class SettingsManager,
* which (surprise, surprise) helps you to organize/manage your Settings
*
* Short HOWTO:
* void setSetting( String name, type value )
* this Function set a setting, type kann be String/boolean or int
*
* int getIntSetting( String name );
* boolean getBooleanSetting( String name );
* String getStringSetting( String name );
* these Functions return the value of the Setting
*/
package org.javwer;
import java.io.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.events.*;
public class SettingsManager {
private int[] int_values;
private boolean[] boolean_values;
private boolean writable, fileErrorMsgSent;
private StringBuffer[] int_names, boolean_names, string_names,
string_values;
private String filename;
SettingsManager() {
filename = "";
writable = false;
fileErrorMsgSent = false;
int_values = new int[ 0 ];
boolean_values = new boolean[ 0 ];
string_values = new StringBuffer[ 0 ];
boolean_names = new StringBuffer[ 0 ];
string_names = new StringBuffer[ 0 ];
int_names = new StringBuffer[ 0 ];
}
SettingsManager(String f_filename ) {
filename = f_filename;
writable = true;
int_values = new int[ 0 ];
boolean_values = new boolean[ 0 ];
string_values = new StringBuffer[ 0 ];
boolean_names = new StringBuffer[ 0 ];
string_names = new StringBuffer[ 0 ];
int_names = new StringBuffer[ 0 ];
readFromFile( filename );
}
SettingsManager( String f_filename, String f_globalFilename ) {
filename = f_filename;
writable = true;
int_values = new int[ 0 ];
boolean_values = new boolean[ 0 ];
string_values = new StringBuffer[ 0 ];
boolean_names = new StringBuffer[ 0 ];
string_names = new StringBuffer[ 0 ];
int_names = new StringBuffer[ 0 ];
readFromFile( f_globalFilename );
readFromFile( filename );
}
/* only for testing
public static void main( String[] args ) {
SettingsManager test = new SettingsManager( "/setting_manager.txt" );
test.setSetting( "test_int", 11 );
test.setSetting( "server", "dojklm" );
test.setSetting( "name", "martjklin" );
test.setSetting( "Server", "asdfasdfrtin" );
test.setSetting( "Server", "domlkj" );
test.setSetting( "2autologin", false );
test.setSetting( "3autologin", true );
test.setSetting( "autologin", true );
System.out.println( test.getBooleanSetting( "3autologin" ) );
System.out.println( test.getStringSetting( "Server" ) );
test.setSetting( "3autologin", false );
test.setSetting( "autologin", false );
}
*/
private void addSetting( String name, String value ) {
StringBuffer[] temp_names = new StringBuffer[ string_names.length ];
StringBuffer[] temp_values = new StringBuffer[ string_values.length ];
System.arraycopy( string_names, 0, temp_names, 0, string_names.length );
System.arraycopy( string_values, 0, temp_values, 0, string_values.length );
string_names = new StringBuffer[ string_names.length + 1 ];
string_values = new StringBuffer[ string_values.length + 1 ];
System.arraycopy( temp_names, 0, string_names, 0, temp_names.length );
System.arraycopy( temp_values, 0, string_values, 0, temp_values.length );
string_names[ temp_names.length ] = new StringBuffer( name );
string_values[ temp_values.length ] = new StringBuffer( value );
writeToFile();
}
private void addSetting( String name, int value ) {
StringBuffer[] temp_names = new StringBuffer[ int_names.length ];
int[] temp_values = new int[ int_values.length ];
System.arraycopy( int_names, 0, temp_names, 0, int_names.length );
System.arraycopy( int_values, 0, temp_values, 0, int_values.length );
int_names = new StringBuffer[ int_names.length + 1 ];
int_values = new int[ int_values.length + 1 ];
System.arraycopy( temp_names, 0, int_names, 0, temp_names.length );
System.arraycopy( temp_values, 0, int_values, 0, temp_values.length );
int_names[ temp_names.length ] = new StringBuffer( name );
int_values[ temp_values.length ] = value;
writeToFile();
}
private void addSetting( String name, boolean value ) {
StringBuffer[] temp_names = new StringBuffer[ boolean_names.length ];
boolean[] temp_values = new boolean[ boolean_values.length ];
System.arraycopy( boolean_names, 0, temp_names, 0, boolean_names.length );
System.arraycopy( boolean_values, 0, temp_values, 0, boolean_values.length );
boolean_names = new StringBuffer[ boolean_names.length + 1 ];
boolean_values = new boolean[ boolean_values.length + 1 ];
System.arraycopy( temp_names, 0, boolean_names, 0, temp_names.length );
System.arraycopy( temp_values, 0, boolean_values, 0, temp_values.length );
boolean_names[ temp_names.length ] = new StringBuffer( name );
boolean_values[ temp_values.length ] = value;
writeToFile();
}
public void showSettingsDialog( final JRoster caller ) {
final Shell shell = new Shell( SWT.DIALOG_TRIM );
shell.setText( "Settings - Javwer" );
shell.setLayout( new GridLayout( 2, false ) );
CTabFolder tabFolder = new CTabFolder( shell, SWT.TOP | SWT.BORDER | SWT.FLAT);
tabFolder.setLayoutData( new GridData( SWT.LEFT, SWT.CENTER, true, true, 2, 1 ) );
//Startup
CTabItem item = new CTabItem( tabFolder, SWT.NONE );
item.setText( "Startup" );
Composite composite = new Composite( tabFolder, SWT.NONE );
RowLayout layout = new RowLayout( SWT.VERTICAL );
composite.setLayout( layout );
item.setControl( composite );
final Button autoLogin = new Button( composite, SWT.CHECK );
autoLogin.setSelection( getBooleanSetting( "autologin" ) );
autoLogin.setText( "Autologin" );
//Roster
CTabItem item2 = new CTabItem( tabFolder, SWT.NONE );
item2.setText( "Roster" );
composite = new Composite( tabFolder, SWT.NONE );
composite.setLayout( new RowLayout( SWT.VERTICAL ) );
item2.setControl( composite );
Label sorting = new Label( composite, SWT.None );
sorting.setText( "Sort contacts by:" );
final Button[] sortRadio = new Button[ 2 ];
for ( int i = 0; i < sortRadio.length; i++ ) {
sortRadio[ i ] = new Button( composite, SWT.RADIO );
if ( i == getIntSetting( "sort" ) )
sortRadio[ i ].setSelection( true );
else
sortRadio[ i ].setSelection( false );
}
sortRadio[ 0 ].setText( "Alphabet" );
sortRadio[ 1 ].setText( "Status" );
final Button trayIconButton = new Button( composite, SWT.CHECK );
trayIconButton.setText( "display Tray-Icon" );
trayIconButton.setSelection( getBooleanSetting( "tray_icon" ) );
//Status
CTabItem item3 = new CTabItem( tabFolder, SWT.NONE );
item3.setText( "Status" );
composite = new Composite( tabFolder, SWT.NONE );
composite.setLayout( new GridLayout( 2, false ) );
item3.setControl( composite );
Label status = new Label( composite, SWT.NONE );
status.setText( "Online priority: " );
status.setLayoutData( new GridData( SWT.LEFT, SWT.CENTER, true, true ) );
final Spinner availablePriority = new Spinner( composite, SWT.None );
availablePriority.setLayoutData( new GridData( SWT.RIGHT, SWT.CENTER, true, true ) );
availablePriority.setSelection( getIntSetting( "status_available_priority" ) );
status = new Label( composite, SWT.NONE );
status.setText( "Away priority: " );
status.setLayoutData( new GridData( SWT.LEFT, SWT.CENTER, true, true ) );
final Spinner awayPriority = new Spinner( composite, SWT.None );
awayPriority.setLayoutData( new GridData( SWT.RIGHT, SWT.CENTER, true, true ) );
awayPriority.setSelection( getIntSetting( "status_away_priority" ) );
status = new Label( composite, SWT.NONE );
status.setText( "Extended away priority: " );
status.setLayoutData( new GridData( SWT.LEFT, SWT.CENTER, true, true ) );
final Spinner xaPriority = new Spinner( composite, SWT.None );
xaPriority.setLayoutData( new GridData( SWT.RIGHT, SWT.CENTER, true, true ) );
xaPriority.setSelection( getIntSetting( "status_xa_priority" ) );
status = new Label( composite, SWT.NONE );
status.setText( "Do not disturb priority: " );
status.setLayoutData( new GridData( SWT.LEFT, SWT.CENTER, true, true ) );
final Spinner dndPriority = new Spinner( composite, SWT.None );
dndPriority.setLayoutData( new GridData( SWT.RIGHT, SWT.CENTER, true, true ) );
dndPriority.setSelection( getIntSetting( "status_dnd_priority" ) );
status = new Label( composite, SWT.NONE );
status.setText( "Free to chat priority: " );
status.setLayoutData( new GridData( SWT.LEFT, SWT.CENTER, true, true ) );
final Spinner chatPriority = new Spinner( composite, SWT.None );
chatPriority.setLayoutData( new GridData( SWT.RIGHT, SWT.CENTER, true, true ) );
chatPriority.setSelection( getIntSetting( "status_chat_priority" ) );
status = new Label( composite, SWT.NONE );
status.setText( "Invisible priority: " );
status.setLayoutData( new GridData( SWT.LEFT, SWT.CENTER, true, true ) );
final Spinner invisiblePriority = new Spinner( composite, SWT.None );
invisiblePriority.setLayoutData( new GridData( SWT.RIGHT, SWT.CENTER, true, true ) );
invisiblePriority.setSelection( getIntSetting( "status_invisible_priority" ) );
// Chat-Window
CTabItem item4 = new CTabItem( tabFolder, SWT.NONE );
item4.setText( "Chat-Window" );
composite = new Composite( tabFolder, SWT.NONE );
composite.setLayout( new GridLayout( 2, false ) );
item4.setControl( composite );
final Button fileButton = new Button( composite, SWT.PUSH );
fileButton.setToolTipText( getStringSetting( "style" ) );
fileButton.setText( getStringSetting( "style" ).substring( getStringSetting( "style" ).lastIndexOf( '/' ) + 1 ) );
fileButton.addSelectionListener( new SelectionAdapter() {
public void widgetSelected( SelectionEvent e ) {
FileDialog dialog = new FileDialog( shell, SWT.OPEN );
dialog.setText( "Select Style-File" );
dialog.setFileName( getStringSetting( "style" ).substring( getStringSetting( "style" ).lastIndexOf( '/' ) ) );
dialog.setFilterPath( getStringSetting( "style" ).substring( 0, getStringSetting( "style" ).lastIndexOf( '/' ) ) );
String tmp = dialog.open();
if( tmp != null ) {
fileButton.setText( tmp.substring( tmp.lastIndexOf( "/" ) + 1 ) );
fileButton.setToolTipText( tmp );
}
}
});
GridData data = new GridData();
data.widthHint = 100;
fileButton.setLayoutData( data );
Label styleFile = new Label( composite, SWT.NONE );
styleFile.setText( "CSS-File" );
styleFile.setLayoutData( new GridData( GridData.HORIZONTAL_ALIGN_END ) );
final Button displayStatusButton[] = new Button[ 3 ];
displayStatusButton[ 1 ] = new Button( composite, SWT.RADIO );
displayStatusButton[ 1 ].setText( "Always" );
Label displayStatusLabel = new Label( composite, SWT.WRAP );
displayStatusLabel.setText( "Display Status-Messages" );
displayStatusLabel.setLayoutData( new GridData( GridData.BEGINNING, GridData.CENTER, true, false, 1, 3 ) );
displayStatusButton[ 2 ] = new Button( composite, SWT.RADIO );
displayStatusButton[ 2 ].setText( "on mode-change" );
displayStatusButton[ 2 ].setToolTipText( "If you choose this option, you will only be informed when the user changes his mode (available/away/.../offline), but not when he changes his message.\nThis is usefull when people having clients which inform you every minutes how minutes their user is already away" );
displayStatusButton[ 0 ] = new Button( composite, SWT.RADIO );
displayStatusButton[ 0 ].setText( "Never" );
displayStatusButton[ getIntSetting( "display_status" ) ].setSelection( true );
final Button timestampButton = new Button( composite, SWT.CHECK | SWT.RIGHT );
timestampButton.setSelection( getBooleanSetting( "show_timestamp" ) );
timestampButton.setText( "Timestamps" );
timestampButton.setLayoutData( new GridData( GridData.FILL, GridData.FILL, true, false, 2, 1 ) );
final Button smilieButton = new Button( composite, SWT.CHECK | SWT.RIGHT );
smilieButton.setSelection( getBooleanSetting( "image_smilies" ) );
smilieButton.setText( "Image-Smilies" );
smilieButton.setLayoutData( new GridData( GridData.FILL, GridData.FILL, true, false, 2, 1 ) );
//Buttons
Button saveButton = new Button( shell, SWT.PUSH );
saveButton.setLayoutData( new GridData( SWT.RIGHT, SWT.CENTER, true, true ) );
saveButton.setText( "&Save" );
saveButton.addSelectionListener( new SelectionAdapter() {
public void widgetSelected( SelectionEvent e ) {
//Startup
setSetting( "autologin", autoLogin.getSelection() );
//Roster
for ( int i = 0; i < sortRadio.length; i++ ) {
if ( sortRadio[ i ].getSelection() )
setSetting( "sort", i );
}
setSetting( "tray_icon", trayIconButton.getSelection() );
if( caller.getTrayItem() != null )
caller.getTrayItem().setVisible( trayIconButton.getSelection() );
//Status
setSetting( "status_available_priority", availablePriority.getSelection() );
setSetting( "status_available_priority", awayPriority.getSelection() );
setSetting( "status_available_priority", xaPriority.getSelection() );
setSetting( "status_available_priority", dndPriority.getSelection() );
setSetting( "status_available_priority", chatPriority.getSelection() );
setSetting( "status_available_priority", invisiblePriority.getSelection() );
//Chat-Window
setSetting( "style", fileButton.getToolTipText() );
setSetting( "image_smilies", smilieButton.getSelection() );
setSetting( "show_timestamp", timestampButton.getSelection() );
for ( int i = 0; i < displayStatusButton.length; i++ ) {
if ( displayStatusButton[ i ].getSelection() )
setSetting( "display_status", i );
}
shell.dispose();
caller.redrawGroups();
}
});
Button cancelButton = new Button( shell, SWT.PUSH );
cancelButton.setLayoutData( new GridData( SWT.LEFT, SWT.CENTER, true, true ) );
cancelButton.setText( "&Cancel" );
cancelButton.addSelectionListener( new SelectionAdapter() {
public void widgetSelected( SelectionEvent e ) {
shell.dispose();
}
});
shell.pack();
shell.open();
}
public int getIntSetting( String name ) {
for ( int i = 0; i < int_names.length; i++ )
if ( int_names[ i ].toString().compareTo( name ) == 0 )
return int_values[ i ];
throw new IllegalArgumentException( "Setting not found: '" + name + "'" );
}
public boolean getBooleanSetting( String name ) {
for ( int i = 0; i < boolean_names.length; i++ )
if ( boolean_names[ i ].toString().compareTo( name ) == 0 )
return boolean_values[ i ];
throw new IllegalArgumentException( "Setting not found: '" + name + "'" );
}
public String getStringSetting( String name ) {
for ( int i = 0; i < string_names.length; i++ )
if ( string_names[ i ].toString().compareTo( name ) == 0 )
return string_values[ i ].toString();
throw new IllegalArgumentException( "Setting not found: '" + name + "'" );
}
public void setSetting( String name, int value ) {
for ( int i = 0; i < int_names.length; i++ )
if ( int_names[ i ].toString().compareTo( name ) == 0 ) {
int_values[ i ] = value;
writeToFile();
return;
}
addSetting( name, value );
}
public void setSetting( String name, boolean value ) {
for ( int i = 0; i < boolean_names.length; i++ )
if ( boolean_names[ i ].toString().compareTo( name ) == 0 ) {
boolean_values[ i ] = value;
writeToFile();
return;
}
addSetting( name, value );
}
public void setSetting( String name, String value ) {
for ( int i = 0; i < string_names.length; i++ )
if ( string_names[ i ].toString().compareTo( name ) == 0 ) {
string_values[ i ] = new StringBuffer( value );
writeToFile();
return;
}
addSetting( name, value );
}
private void writeToFile() {
if( writable ) {
try {
PrintStream file = new PrintStream( filename );
for ( int i = 0; i < int_names.length; i++ )
file.println( "i: " + int_names[ i ] + ": " + int_values[ i ] );
for ( int i = 0; i < boolean_names.length; i++ )
file.println( "b: " + boolean_names[ i ] + ": "
+ boolean_values[ i ] );
for ( int i = 0; i < string_names.length; i++ )
file.println( "s: " + string_names[ i ] + ": "
+ string_values[ i ] );
file.close();
} catch ( Exception e ) {
if ( fileErrorMsgSent == false ) {
fileErrorMsgSent = true;
System.out.println( "SettingsManager.writeToFile(): Could not write to file: '" + filename + "'\n" );
/* GUI-Mode (unfinished)
Display display = new Display ();
Shell shell = new Shell ( display, SWT.DIALOG_TRIM + SWT.WRAP);
shell.setSize( 600, 250 );
// shell.setLayout(new FillLayout() );
shell.setText("Error: could not write to file " + filename );
Label label = new Label( shell, SWT.WRAP );
label.setBounds( 120, 20, 600-40-100, 250-40 );
label.setText( "There was a problem while writing to " + filename + "\n"
+ "The reason of this problem is probably that you don't have the wirte-access to this file, please change that - otherwise some of your settings will not be saved.\n"
+ "This message-box only appears once per file an runtime, which doesn't mean that Javwer will not try to access this file again." );
Button button = new Button( shell, SWT.PUSH + SWT.CENTER );
button.setSize( 100, 30 );
button.addSelectionListener( new SelectionAdapter () {
public void widgetSelected( SelectionEvent e ) {
shell.setText("");
}
}
);
//new MessageDialogWithToggle.openError( shell, "title", "message", "togglemsg", true, new IPreferenceStore(), "key" );
shell.open();
while( !shell.isDisposed() )
if( !display.readAndDispatch() )
display.sleep();
*/
}
}
}
}
private void readFromFile( String f_filename ) {
try {
RandomAccessFile file = new RandomAccessFile( f_filename, "r" );
int int_value, firstDel, secondDel;
boolean boolean_value, old_writable;
old_writable = writable;
writable = false;
String type, name, string_value, line;
line = file.readLine();
while( line != null ) {
// System.out.println( line);
firstDel = line.indexOf( ": " );
secondDel = line.lastIndexOf( ": " );
type = line.substring( 0, firstDel );
name = line.substring( firstDel+2, secondDel );
//scanner.useDelimiter("\n");
if ( type.compareTo( "i" ) == 0 ) {
int_value = Integer.parseInt( line.substring( secondDel+2 ) );
// System.out.println("'"+ type + "' '"+name+"' '"+int_value+"'\n");
setSetting( name, int_value );
}
else if ( type.compareTo( "b" ) == 0 ) {
boolean_value = Boolean.parseBoolean( line.substring( secondDel+2 ) );
// System.out.println("'"+ type + "' '"+name+"' '"+boolean_value+"'\n");
setSetting( name, boolean_value );
}
else if ( type.compareTo( "s" ) == 0 ) {
string_value = line.substring( secondDel+2 );
// System.out.println("'"+ type + "' '"+name+"' '"+string_value+"'\n");
setSetting( name, string_value );
}
else
System.err.println( "Error: '" + type + "' '" + name + "' '" + line.substring( secondDel + 2 ) + "'\n" );
line = file.readLine();
}
writable = old_writable;
}
catch ( Exception e ) {
}
}
}