summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorPatrick Gottschaemmer2012-11-07 09:53:53 (EST)
committer Patrick Gottschaemmer2012-11-08 09:46:45 (EST)
commitc933d4ae9b1c814ad8a5f41e2fc160e4c16eadab (patch)
treee7060b067ee4e419ba1c7b11925d88722964f141
parentfadde38f72e074b88752d5ea9fb0ac7dadf56e08 (diff)
downloadorg.eclipse.recommenders-c933d4ae9b1c814ad8a5f41e2fc160e4c16eadab.zip
org.eclipse.recommenders-c933d4ae9b1c814ad8a5f41e2fc160e4c16eadab.tar.gz
org.eclipse.recommenders-c933d4ae9b1c814ad8a5f41e2fc160e4c16eadab.tar.bz2
[extdoc] store DnD providerRanking in ExtdocView via ExtdocPreferencesrefs/changes/67/8567/4
-rw-r--r--plugins/org.eclipse.recommenders.extdoc.rcp/src/org/eclipse/recommenders/extdoc/rcp/providers/ExtdocProvider.java6
-rw-r--r--plugins/org.eclipse.recommenders.extdoc.rcp/src/org/eclipse/recommenders/internal/extdoc/rcp/ui/ExtdocPreferences.java93
-rw-r--r--plugins/org.eclipse.recommenders.extdoc.rcp/src/org/eclipse/recommenders/internal/extdoc/rcp/ui/ExtdocView.java35
-rw-r--r--tests/org.eclipse.recommenders.tests.extdoc.rcp/src/org/eclipse/recommenders/tests/extdoc/ExtdocDnDTest.java18
-rw-r--r--tests/org.eclipse.recommenders.tests.extdoc.rcp/src/org/eclipse/recommenders/tests/extdoc/ExtdocProviderStoreTest.java105
5 files changed, 219 insertions, 38 deletions
diff --git a/plugins/org.eclipse.recommenders.extdoc.rcp/src/org/eclipse/recommenders/extdoc/rcp/providers/ExtdocProvider.java b/plugins/org.eclipse.recommenders.extdoc.rcp/src/org/eclipse/recommenders/extdoc/rcp/providers/ExtdocProvider.java
index 19a4ad2..7dc50b4 100644
--- a/plugins/org.eclipse.recommenders.extdoc.rcp/src/org/eclipse/recommenders/extdoc/rcp/providers/ExtdocProvider.java
+++ b/plugins/org.eclipse.recommenders.extdoc.rcp/src/org/eclipse/recommenders/extdoc/rcp/providers/ExtdocProvider.java
@@ -7,6 +7,7 @@
*
* Contributors:
* Sebastian Proksch - initial API and implementation
+ * Patrick Gottschaemmer, Olav Lenz - added getId() method
*/
package org.eclipse.recommenders.extdoc.rcp.providers;
@@ -34,6 +35,7 @@ import org.eclipse.swt.widgets.Display;
* operation in the UI thread. Providers may use {@link #runSyncInUiThread(Runnable)} for that purpose as convenient
* shortcut.
* </p>
+ *
* @see org.eclipse.recommenders.extdoc.rcp.provider extension point for details on how to register a new provider
*/
public abstract class ExtdocProvider {
@@ -99,4 +101,8 @@ public abstract class ExtdocProvider {
}
}
}
+
+ public String getId() {
+ return getClass().getName();
+ }
} \ No newline at end of file
diff --git a/plugins/org.eclipse.recommenders.extdoc.rcp/src/org/eclipse/recommenders/internal/extdoc/rcp/ui/ExtdocPreferences.java b/plugins/org.eclipse.recommenders.extdoc.rcp/src/org/eclipse/recommenders/internal/extdoc/rcp/ui/ExtdocPreferences.java
index 2214fe8..1cbe2ce 100644
--- a/plugins/org.eclipse.recommenders.extdoc.rcp/src/org/eclipse/recommenders/internal/extdoc/rcp/ui/ExtdocPreferences.java
+++ b/plugins/org.eclipse.recommenders.extdoc.rcp/src/org/eclipse/recommenders/internal/extdoc/rcp/ui/ExtdocPreferences.java
@@ -1,5 +1,5 @@
/**
- * Copyright (c) 2010, 2011 Darmstadt University of Technology.
+ * Copyright (c) 2010, 2012 Darmstadt University of Technology.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -13,42 +13,73 @@ package org.eclipse.recommenders.internal.extdoc.rcp.ui;
import static java.lang.Integer.parseInt;
import java.util.ArrayList;
+import java.util.LinkedList;
import java.util.List;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.recommenders.extdoc.rcp.providers.ExtdocProvider;
import org.eclipse.recommenders.internal.extdoc.rcp.wiring.ExtdocPlugin;
-import org.eclipse.recommenders.utils.Throws;
+import org.eclipse.recommenders.rcp.RecommendersPlugin;
import org.osgi.service.prefs.BackingStoreException;
+import org.osgi.service.prefs.Preferences;
public class ExtdocPreferences {
- private final IEclipsePreferences preferences;
-
- private static final String PROVIDER_NAMES = "providerNames";
+ public static final String PROVIDER_RANKING = "providerRanking";
private static final String DISABLED_PROVIDERS = "disabledProviders";
private static final String SASH_WEIGHTS = "sashWeights";
+ private final IEclipsePreferences pluginPreferences;
+ private final Preferences providerRankingPreferences;
+
public ExtdocPreferences() {
- preferences = InstanceScope.INSTANCE.getNode(ExtdocPlugin.PLUGIN_ID);
+ pluginPreferences = InstanceScope.INSTANCE.getNode(ExtdocPlugin.PLUGIN_ID);
+ providerRankingPreferences = pluginPreferences.node(PROVIDER_RANKING);
}
- public void storeOrderedProviders(final List<ExtdocProvider> providers) {
- final String[] providerNames = getProviderNames(providers);
- final String toSave = createString(providerNames);
- preferences.put(PROVIDER_NAMES, toSave);
+ public void storeProviderRanking(final List<ExtdocProvider> providerRanking) {
+ clearPreferences(providerRankingPreferences);
+
+ for (int i = 0; i < providerRanking.size(); i++) {
+ providerRankingPreferences.put(Integer.toString(i), providerRanking.get(i).getId());
+ }
flush();
}
- private static String[] getProviderNames(final List<ExtdocProvider> providers) {
- final String[] names = new String[providers.size()];
- int i = 0;
- for (final ExtdocProvider p : providers) {
- names[i] = p.getDescription().getName();
- i++;
+ public List<String> loadOrderedProviderIds() {
+ String[] keys = fetchKeys(providerRankingPreferences);
+ List<String> providerIds = new LinkedList<String>();
+
+ for (int i = 0; i < keys.length; i++) {
+ String tmp = providerRankingPreferences.get(Integer.toString(i), null);
+ if (tmp != null) {
+ providerIds.add(tmp);
+ } else {
+ RecommendersPlugin.logWarning("Loading entry for key " + i + " failed. No such entry.");
+ }
+ }
+ return providerIds;
+ }
+
+ private String[] fetchKeys(Preferences node) {
+ String[] keys = new String[0];
+ try {
+ keys = node.keys();
+ } catch (BackingStoreException e) {
+ RecommendersPlugin.logError(e, "Exception during loading the keys of: " + node.absolutePath());
+ }
+ return keys;
+ }
+
+ private boolean clearPreferences(Preferences prefs) {
+ try {
+ prefs.clear();
+ return true;
+ } catch (BackingStoreException e) {
+ RecommendersPlugin.logError(e, "Caught exception while clearing the preferences : " + prefs.absolutePath());
+ return false;
}
- return names;
}
private String createString(final String[] names) {
@@ -63,14 +94,8 @@ public class ExtdocPreferences {
}
}
- public String[] loadOrderedProviderNames() {
- final String orderedNameString = preferences.get(PROVIDER_NAMES, "");
- final String[] orderedNames = orderedNameString.split(",");
- return orderedNames;
- }
-
public boolean isProviderEnabled(final ExtdocProvider p) {
- final String arrayString = preferences.get(DISABLED_PROVIDERS, "");
+ final String arrayString = pluginPreferences.get(DISABLED_PROVIDERS, "");
final String[] deactivatedProviders = arrayString.split(",");
final String providerName = p.getDescription().getName();
for (final String deactivatedName : deactivatedProviders) {
@@ -84,7 +109,7 @@ public class ExtdocPreferences {
public void storeProviderEnablement(final List<ExtdocProvider> providers) {
final String[] disabledProviderNames = getDisabledProviderNames(providers);
final String toSave = createString(disabledProviderNames);
- preferences.put(DISABLED_PROVIDERS, toSave);
+ pluginPreferences.put(DISABLED_PROVIDERS, toSave);
flush();
}
@@ -99,22 +124,28 @@ public class ExtdocPreferences {
}
public int[] loadSashWeights() {
- final String weightString = preferences.get(SASH_WEIGHTS, "1,3");
+ final String weightString = pluginPreferences.get(SASH_WEIGHTS, "1,3");
final String[] weights = weightString.split(",");
return new int[] { parseInt(weights[0]), parseInt(weights[1]) };
}
public void storeSashWeights(final int[] weights) {
final String toSave = weights[0] + "," + weights[1];
- preferences.put(SASH_WEIGHTS, toSave);
+ pluginPreferences.put(SASH_WEIGHTS, toSave);
flush();
}
- private void flush() {
+ private boolean flush() {
try {
- preferences.flush();
- } catch (final BackingStoreException e) {
- Throws.throwUnhandledException(e);
+ pluginPreferences.flush();
+ return true;
+ } catch (BackingStoreException e) {
+ RecommendersPlugin.logError(e, "Caught exception while saving the order of ExtdocProviders");
+ return false;
}
}
+
+ public boolean clearProviderRankingPreferences() {
+ return clearPreferences(providerRankingPreferences);
+ }
} \ No newline at end of file
diff --git a/plugins/org.eclipse.recommenders.extdoc.rcp/src/org/eclipse/recommenders/internal/extdoc/rcp/ui/ExtdocView.java b/plugins/org.eclipse.recommenders.extdoc.rcp/src/org/eclipse/recommenders/internal/extdoc/rcp/ui/ExtdocView.java
index bec7731..fd4c81c 100644
--- a/plugins/org.eclipse.recommenders.extdoc.rcp/src/org/eclipse/recommenders/internal/extdoc/rcp/ui/ExtdocView.java
+++ b/plugins/org.eclipse.recommenders.extdoc.rcp/src/org/eclipse/recommenders/internal/extdoc/rcp/ui/ExtdocView.java
@@ -17,8 +17,10 @@ import static org.eclipse.recommenders.internal.extdoc.rcp.ui.ExtdocUtils.setInf
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
+import java.util.Map;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
@@ -107,7 +109,37 @@ public class ExtdocView extends ViewPart {
this.subscriptionManager = subscriptionManager;
this.providers = providers;
this.preferences = preferences;
- this.providerRanking = new LinkedList<ExtdocProvider>(providers);
+ this.providerRanking = loadProviderRanking();
+ }
+
+ private List<ExtdocProvider> loadProviderRanking() {
+ List<ExtdocProvider> providerRanking = new LinkedList<ExtdocProvider>();
+ Map<String, ExtdocProvider> providerMap = fillProviderMap();
+ List<String> providerIds = preferences.loadOrderedProviderIds();
+ LinkedList<ExtdocProvider> remainingProviders = new LinkedList<ExtdocProvider>(providers);
+
+ for (String providerName : providerIds) {
+ ExtdocProvider tmpProvider = providerMap.get(providerName);
+ if (tmpProvider != null) {
+ providerRanking.add(tmpProvider);
+ remainingProviders.remove(tmpProvider);
+ }
+ }
+ providerRanking.addAll(remainingProviders);
+ return providerRanking;
+ }
+
+ private HashMap<String, ExtdocProvider> fillProviderMap() {
+ HashMap<String, ExtdocProvider> providerMap = new HashMap<String, ExtdocProvider>();
+ for (ExtdocProvider provider : providers) {
+ providerMap.put(provider.getId(), provider);
+ }
+ return providerMap;
+ }
+
+ @VisibleForTesting
+ public void storeProviderRanking() {
+ preferences.storeProviderRanking(providerRanking);
}
@Override
@@ -246,6 +278,7 @@ public class ExtdocView extends ViewPart {
} else {
moveBefore(oldIndex, newIndex);
}
+ storeProviderRanking();
viewer.refresh();
return true;
}
diff --git a/tests/org.eclipse.recommenders.tests.extdoc.rcp/src/org/eclipse/recommenders/tests/extdoc/ExtdocDnDTest.java b/tests/org.eclipse.recommenders.tests.extdoc.rcp/src/org/eclipse/recommenders/tests/extdoc/ExtdocDnDTest.java
index 68a3cb0..22454cc 100644
--- a/tests/org.eclipse.recommenders.tests.extdoc.rcp/src/org/eclipse/recommenders/tests/extdoc/ExtdocDnDTest.java
+++ b/tests/org.eclipse.recommenders.tests.extdoc.rcp/src/org/eclipse/recommenders/tests/extdoc/ExtdocDnDTest.java
@@ -12,6 +12,7 @@ package org.eclipse.recommenders.tests.extdoc;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
import java.util.LinkedList;
import java.util.List;
@@ -41,20 +42,25 @@ public class ExtdocDnDTest {
EventBus bus = mock(EventBus.class);
SubscriptionManager subManger = mock(SubscriptionManager.class);
- provider0 = mock(ExtdocProvider.class);
- provider1 = mock(ExtdocProvider.class);
- provider2 = mock(ExtdocProvider.class);
- provider3 = mock(ExtdocProvider.class);
- provider4 = mock(ExtdocProvider.class);
+ provider0 = createProviderMock("provider0");
+ provider1 = createProviderMock("provider1");
+ provider2 = createProviderMock("provider2");
+ provider3 = createProviderMock("provider3");
+ provider4 = createProviderMock("provider4");
List<ExtdocProvider> providers = Lists.newArrayList(provider0, provider1, provider2, provider3, provider4);
-
defaultOrder = new LinkedList<ExtdocProvider>(providers);
ExtdocPreferences preferences = mock(ExtdocPreferences.class);
view = new ExtdocView(bus, subManger, providers, preferences);
}
+ private ExtdocProvider createProviderMock(String name) {
+ ExtdocProvider mock = mock(ExtdocProvider.class);
+ when(mock.getId()).thenReturn(name);
+ return mock;
+ }
+
@Test
public void moveOnItselfBefore() {
view.moveBefore(1, 1);
diff --git a/tests/org.eclipse.recommenders.tests.extdoc.rcp/src/org/eclipse/recommenders/tests/extdoc/ExtdocProviderStoreTest.java b/tests/org.eclipse.recommenders.tests.extdoc.rcp/src/org/eclipse/recommenders/tests/extdoc/ExtdocProviderStoreTest.java
new file mode 100644
index 0000000..a661eca
--- /dev/null
+++ b/tests/org.eclipse.recommenders.tests.extdoc.rcp/src/org/eclipse/recommenders/tests/extdoc/ExtdocProviderStoreTest.java
@@ -0,0 +1,105 @@
+/**
+ * Copyright (c) 2010, 2012 Darmstadt University of Technology.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Patrick Gottschaemmer, Olav Lenz - initial tests
+ */
+package org.eclipse.recommenders.tests.extdoc;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.List;
+
+import org.eclipse.recommenders.extdoc.rcp.providers.ExtdocProvider;
+import org.eclipse.recommenders.internal.extdoc.rcp.ui.ExtdocPreferences;
+import org.eclipse.recommenders.internal.extdoc.rcp.ui.ExtdocView;
+import org.eclipse.recommenders.internal.extdoc.rcp.ui.SubscriptionManager;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.google.common.collect.Lists;
+import com.google.common.eventbus.EventBus;
+
+public class ExtdocProviderStoreTest {
+
+ private ExtdocView view;
+ private ExtdocProvider provider0;
+ private ExtdocProvider provider1;
+ private ExtdocProvider provider2;
+ private ExtdocProvider additionalProvider;
+ private List<ExtdocProvider> providers;
+ private ExtdocPreferences preferences;
+
+ @Before
+ public void setup() {
+ preferences = new ExtdocPreferences();
+
+ boolean nodeCleared = preferences.clearProviderRankingPreferences();
+ if (!nodeCleared) {
+ fail("Test fails because of clearing of ProviderRanking store failed.");
+ }
+
+ provider0 = createProviderMock("provider0");
+ provider1 = createProviderMock("provider1");
+ provider2 = createProviderMock("provider2");
+ additionalProvider = createProviderMock("additionalProvider");
+
+ providers = Lists.newArrayList(provider0, provider1, provider2);
+ view = createView(providers);
+ }
+
+ private ExtdocProvider createProviderMock(String name) {
+ ExtdocProvider mock = mock(ExtdocProvider.class);
+ when(mock.getId()).thenReturn(name);
+ return mock;
+ }
+
+ private ExtdocView createView(List<ExtdocProvider> providers) {
+ EventBus bus = mock(EventBus.class);
+
+ SubscriptionManager subManger = mock(SubscriptionManager.class);
+ return new ExtdocView(bus, subManger, providers, preferences);
+ }
+
+ @Test
+ public void removeProvider() {
+ view.storeProviderRanking();
+
+ providers.remove(provider1);
+ view = createView(providers);
+
+ List<ExtdocProvider> expectedProviders = Lists.newArrayList(provider0, provider2);
+ assertEquals(view.getProviderRanking(), expectedProviders);
+ }
+
+ @Test
+ public void addProvider() {
+ view.storeProviderRanking();
+
+ providers.add(1, additionalProvider);
+ view = createView(providers);
+
+ List<ExtdocProvider> expectedProviders = Lists.newArrayList(provider0, provider1, provider2, additionalProvider);
+ assertEquals(view.getProviderRanking(), expectedProviders);
+ }
+
+ @Test
+ public void modifyProviderOrder() {
+ view.moveAfter(0, 1);
+ view.storeProviderRanking();
+
+ providers.remove(provider2);
+ providers.add(additionalProvider);
+ view = createView(providers);
+
+ List<ExtdocProvider> expectedProviders = Lists.newArrayList(provider1, provider0, additionalProvider);
+ assertEquals(view.getProviderRanking(), expectedProviders);
+ }
+} \ No newline at end of file