Skip to content

Commit

Permalink
Merge pull request #4 from KlasJoenssson/Bug3069
Browse files Browse the repository at this point in the history
Bug3069
  • Loading branch information
goglepox committed Sep 4, 2012
2 parents 2d222ae + d7a5785 commit e908824
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 27 deletions.
Expand Up @@ -20,10 +20,12 @@
import net.bioclipse.usermanager.business.IUserManager;

import org.apache.log4j.Logger;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.core.runtime.preferences.ConfigurationScope;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.opentox.aa.opensso.OpenSSOToken;
import org.osgi.framework.BundleContext;
import org.osgi.service.prefs.BackingStoreException;
import org.osgi.service.prefs.Preferences;
import org.osgi.util.tracker.ServiceTracker;

/**
Expand Down Expand Up @@ -98,31 +100,63 @@ public void start(BundleContext context) throws Exception {
}
}

Preferences preferences =
ConfigurationScope.INSTANCE
.getNode( OpenToxConstants.PLUGIN_ID );
List<String[]> toPrefs = ServicesPreferencePage.convertPreferenceStringToArraylist( preferences.get( OpenToxConstants.SERVICES, "n/a" ) );

//Save the, possibly changed, list of services to prefs
//This way we have the list openToxServices synced with the prefs
List<String[]> toPrefs=new ArrayList<String[]>();
// List<String[]> toPrefs = new ArrayList<String[]>();
for (OpenToxService eps : epservices){
String[] entry = new String[3];
entry[0]=eps.getName();
entry[1]=eps.getService();
entry[2]=eps.getServiceSPARQL();

toPrefs.add(entry);
String[] entry = new String[3];
entry[0]=eps.getName();
entry[1]=eps.getService();
entry[2]=eps.getServiceSPARQL();

if (!listContains( toPrefs, entry ))
toPrefs.add(entry);
}

String toPrefsString = ServicesPreferencePage.convertToPreferenceString(toPrefs);

//Save the serialized services to preferences
IPreferenceStore prefsStore=Activator.getDefault().getPreferenceStore();
prefsStore.setValue(OpenToxConstants.SERVICES, toPrefsString);
preferences.put( OpenToxConstants.SERVICES, toPrefsString );
try {
preferences.flush();
} catch ( BackingStoreException e ) {
logger.error( e.getMessage() );
e.printStackTrace();
}

logger.debug("Saved the serialized services prefs string: " + toPrefsString);

logger.debug("OpenTox initialization ended");

}



/* This method is written 'cos using "toPrefs.contains( entry )" in the if-
* statement on line 117 above didn't worked. */
private boolean listContains(List<String[]> list, String[] item) {
boolean found = false, itemEquals;
for (String[] listItem : list) {
itemEquals = false;
for (int i = 0; i < listItem.length; i++) {
if (item[i] == null || item[i].isEmpty()) {
if (listItem[i].equals( "NA" ) || listItem[i].isEmpty() )
itemEquals = true;
} else if (listItem[i].equals( item[i] ) )
itemEquals = true;
else
itemEquals = false;
}
if (itemEquals)
found = true;
}

return found;
}

public void stop(BundleContext context) throws Exception {
plugin = null;
super.stop(context);
Expand Down
Expand Up @@ -5,5 +5,6 @@ public class OpenToxConstants {
public static final String PREFS_SEPERATOR = "__SEP__";
public static final String SERVICES = "opentox.services";
public static final String PREFERENCES_OBJECT_DELIMITER = null;
public static final String PLUGIN_ID="net.bioclipse.opentox";

}
Expand Up @@ -19,6 +19,7 @@
import net.bioclipse.ui.prefs.IPreferenceConstants;

import org.apache.log4j.Logger;
import org.eclipse.core.runtime.preferences.ConfigurationScope;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferencePage;
Expand All @@ -45,6 +46,8 @@
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.osgi.service.prefs.BackingStoreException;
import org.osgi.service.prefs.Preferences;

/**
*
Expand All @@ -54,10 +57,12 @@
public class ServicesPreferencePage extends PreferencePage implements
IWorkbenchPreferencePage {

private static final Logger logger = Logger.getLogger(ServicesPreferencePage.class.toString());

private static IPreferenceStore prefsStore=Activator.getDefault().getPreferenceStore();
private static final Logger logger =
Logger.getLogger(ServicesPreferencePage.class.toString());

private static Preferences preferences =
ConfigurationScope.INSTANCE.getNode( OpenToxConstants.PLUGIN_ID );

private List<String[]> appList;
private TableViewer checkboxTableViewer;

Expand Down Expand Up @@ -205,7 +210,16 @@ public void mouseUp(MouseEvent e) {
}

String[] chosen = (String[]) obj;

/* TODO Fix this in a better way. Without the if-statement below
* we'll get an unexpected ArrayIndexOutOfBoundsException in
* some cases, 'cos the last element is missing in the selection */
if (chosen.length < 3) {
String[] temp = {"NA", "NA", "NA"};
for (int i = 0; i < chosen.length; i++)
temp[i] = chosen[i];
chosen = temp;
}

ServicesEditDialog dlg=new ServicesEditDialog(getShell(), chosen[0], chosen[1], chosen[2]);

dlg.open();
Expand Down Expand Up @@ -280,12 +294,15 @@ public boolean performOk() {

String value=convertToPreferenceString(appList);
logger.debug("Update sites prefs to store: " + value);
prefsStore.setValue(OpenToxConstants.SERVICES,value);



//Save prefs as this must be done explicitly
Activator.getDefault().savePluginPreferences();

preferences.put( OpenToxConstants.SERVICES, value );
try {
preferences.flush();
} catch ( BackingStoreException e ) {
logger.error( e.getMessage() );
e.printStackTrace();
}
return true;
}

Expand All @@ -295,9 +312,8 @@ public boolean performOk() {
*/
public static List<String[]> getPreferencesFromStore() {


String entireString=prefsStore.getString(OpenToxConstants.SERVICES);

String entireString=preferences.get( OpenToxConstants.SERVICES, "n/a" );

return convertPreferenceStringToArraylist(entireString);
}

Expand All @@ -307,8 +323,7 @@ public static List<String[]> getPreferencesFromStore() {
*
*/
public static List<String[]> getDefaultPreferencesFromStore() {
String entireString=prefsStore.getDefaultString(OpenToxConstants.SERVICES);

String entireString=preferences.get( OpenToxConstants.SERVICES, "n/a" );
return convertPreferenceStringToArraylist(entireString);
}

Expand Down

0 comments on commit e908824

Please sign in to comment.