Skip to content

Commit

Permalink
Add ConfigurationListener, GlobalConfiguration
Browse files Browse the repository at this point in the history
The LocalConfiguration already has a facility for listeners, add the
same for the Configuration held by the platform by introducing a
GlobalConfiguration interface.

Rename LocalConfigurationListener to just ConfigurationListener.
  • Loading branch information
albertdev committed Apr 21, 2014
1 parent f8e93be commit 49820c7
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@
import java.util.EnumSet;

import net.sourceforge.vrapper.keymap.KeyStroke;
import net.sourceforge.vrapper.platform.Configuration;
import net.sourceforge.vrapper.platform.Configuration.Option;
import net.sourceforge.vrapper.platform.Configuration.OptionScope;
import net.sourceforge.vrapper.platform.FileService;
import net.sourceforge.vrapper.platform.GlobalConfiguration;
import net.sourceforge.vrapper.platform.HistoryService;
import net.sourceforge.vrapper.platform.KeyMapProvider;
import net.sourceforge.vrapper.platform.Platform;
import net.sourceforge.vrapper.platform.PlatformSpecificStateProvider;
import net.sourceforge.vrapper.platform.ServiceProvider;
import net.sourceforge.vrapper.platform.SimpleConfiguration;
import net.sourceforge.vrapper.platform.UserInterfaceService;
import net.sourceforge.vrapper.platform.ViewportService;
import net.sourceforge.vrapper.utils.DefaultKeyMapProvider;
import net.sourceforge.vrapper.utils.ViewPortInformation;
import net.sourceforge.vrapper.vim.DefaultEditorAdaptor;
import net.sourceforge.vrapper.vim.EditorAdaptor;
import net.sourceforge.vrapper.vim.Options;
import net.sourceforge.vrapper.vim.SimpleGlobalConfiguration;
import net.sourceforge.vrapper.vim.TextObjectProvider;
import net.sourceforge.vrapper.vim.register.RegisterManager;
import net.sourceforge.vrapper.vim.register.SimpleRegister;
Expand All @@ -44,7 +44,7 @@ public class VimTestCase {
@Mock protected HistoryService historyService;
@Mock protected ServiceProvider serviceProvider;
@Mock protected PlatformSpecificStateProvider platformSpecificStateProvider;
protected Configuration configuration;
protected GlobalConfiguration configuration;
protected TestTextContent content;
protected TestCursorAndSelection cursorAndSelection;
protected EditorAdaptor adaptor;
Expand All @@ -63,7 +63,7 @@ public void initMocks() {
content = spy(new TestTextContent(cursorAndSelection));
cursorAndSelection.setContent(content);
keyMapProvider = spy(new DefaultKeyMapProvider());
configuration = spy(new SimpleConfiguration());
configuration = spy(new SimpleGlobalConfiguration());
when(configuration.getNewLine()).thenReturn("\n");
for (Option<Boolean> o : Options.BOOLEAN_OPTIONS) {
// Use defaults for local options.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package net.sourceforge.vrapper.platform;

import net.sourceforge.vrapper.vim.ConfigurationListener;

/**
* Interface for the global configuration.
*/
public interface GlobalConfiguration extends Configuration {

public void removeListener(ConfigurationListener listener);

/** Add a listener which gets called if a setting is set globally. */
public void addListener(ConfigurationListener listener);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface Platform {
ServiceProvider getServiceProvider();
KeyMapProvider getKeyMapProvider();
UnderlyingEditorSettings getUnderlyingEditorSettings();
Configuration getConfiguration();
GlobalConfiguration getConfiguration();
PlatformSpecificStateProvider getPlatformSpecificStateProvider(
TextObjectProvider textObjectProvider);
PlatformSpecificModeProvider getPlatformSpecificModeProvider();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import net.sourceforge.vrapper.platform.TextContent;
import net.sourceforge.vrapper.platform.UserInterfaceService;
import net.sourceforge.vrapper.vim.LocalConfiguration;
import net.sourceforge.vrapper.vim.LocalConfigurationListener;
import net.sourceforge.vrapper.vim.ConfigurationListener;
import net.sourceforge.vrapper.vim.Options;

/**
Expand All @@ -24,7 +24,7 @@ public UnmodifiableTextContentDecorator(TextContent target, LocalConfiguration c
this.textContent = target;
this.uiService = platform.getUserInterfaceService();
this.editable = platform.getFileService().isEditable();
configuration.addListener(new LocalConfigurationListener() {
configuration.addListener(new ConfigurationListener() {
@Override
public <T> void optionChanged(Option<T> option, T oldValue, T newValue) {
if (option.equals(Options.MODIFIABLE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import net.sourceforge.vrapper.platform.Configuration.Option;

/** Listener which gets called when configuration changes in currently active editor. */
public interface LocalConfigurationListener {
/**
* Listener which gets called when configuration changes.
*/
public interface ConfigurationListener {
public <T> void optionChanged(Option<T> option, T oldValue, T newValue);
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public DefaultEditorAdaptor(final Platform editor, final RegisterManager registe
this.globalRegisterManager = registerManager;
this.serviceProvider = editor.getServiceProvider();
this.editorSettings = editor.getUnderlyingEditorSettings();
final LocalConfigurationListener listener = new LocalConfigurationListener() {
final ConfigurationListener listener = new ConfigurationListener() {

@Override
public <T> void optionChanged(final Option<T> option, final T oldValue, final T newValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

public interface LocalConfiguration extends Configuration {

public void removeListener(LocalConfigurationListener listener);
public void removeListener(ConfigurationListener listener);

public <T> void setLocal(Option<T> key, T value);

public void addListener(LocalConfigurationListener listener);
/** Listener which is called if a setting changes in the current editor. */
public void addListener(ConfigurationListener listener);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package net.sourceforge.vrapper.vim;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import net.sourceforge.vrapper.platform.GlobalConfiguration;
import net.sourceforge.vrapper.platform.SimpleConfiguration;

/**
* Configuration implementation used for holding the global settings which are used as defaults in
* new editors.
*/
// This class is implemented in core as it doesn't really depend on platform-specific code.
public class SimpleGlobalConfiguration extends SimpleConfiguration implements GlobalConfiguration {

protected List<ConfigurationListener> listeners =
new CopyOnWriteArrayList<ConfigurationListener>();

@Override
public <T> void set(Option<T> key, T value) {
T oldValue = super.get(key);
super.set(key, value);
for (ConfigurationListener listener : listeners) {
listener.optionChanged(key, oldValue, value);
}
}

public void addListener(ConfigurationListener listener) {
listeners.add(listener);
}

public void removeListener(ConfigurationListener listener) {
listeners.remove(listener);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
import net.sourceforge.vrapper.platform.SimpleConfiguration;
import net.sourceforge.vrapper.platform.SimpleConfiguration.NewLine;

/** Wraps a {@link Configuration}, allowing to notify {@link LocalConfigurationListener}. */
/** Wraps a {@link Configuration}, allowing to notify {@link ConfigurationListener}. */
public class SimpleLocalConfiguration implements LocalConfiguration {

protected Configuration sharedConfiguration;
protected SimpleConfiguration localConfiguration = new SimpleConfiguration();

protected String newLine;

protected List<LocalConfigurationListener> listeners =
new CopyOnWriteArrayList<LocalConfigurationListener>();
protected List<ConfigurationListener> listeners =
new CopyOnWriteArrayList<ConfigurationListener>();

public SimpleLocalConfiguration(Configuration configuration) {
sharedConfiguration = configuration;
Expand Down Expand Up @@ -47,7 +47,7 @@ public <T> void set(Option<T> key, T value) {
oldValue = localConfiguration.get(key);
localConfiguration.set(key, value);
}
for (LocalConfigurationListener listener : listeners) {
for (ConfigurationListener listener : listeners) {
listener.optionChanged(key, oldValue, value);
}
}
Expand All @@ -62,7 +62,7 @@ public <T> void setLocal(Option<T> key, T value) {
oldValue = sharedConfiguration.get(key);
}
localConfiguration.set(key, value);
for (LocalConfigurationListener listener : listeners) {
for (ConfigurationListener listener : listeners) {
listener.optionChanged(key, oldValue, value);
}
}
Expand All @@ -81,11 +81,11 @@ public <T> boolean isSet(Option<T> key) {
return localConfiguration.isSet(key);
}

public void addListener(LocalConfigurationListener listener) {
public void addListener(ConfigurationListener listener) {
listeners.add(listener);
}

public void removeListener(LocalConfigurationListener listener) {
public void removeListener(ConfigurationListener listener) {
listeners.remove(listener);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import net.sourceforge.vrapper.utils.SearchResult;
import net.sourceforge.vrapper.utils.VimUtils;
import net.sourceforge.vrapper.vim.EditorAdaptor;
import net.sourceforge.vrapper.vim.LocalConfigurationListener;
import net.sourceforge.vrapper.vim.ConfigurationListener;
import net.sourceforge.vrapper.vim.Options;
import net.sourceforge.vrapper.vim.commands.Command;
import net.sourceforge.vrapper.vim.commands.MotionCommand;
Expand All @@ -22,7 +22,7 @@
public class SearchMode extends AbstractCommandLineMode {

protected class SearchConfigurationListener implements
LocalConfigurationListener {
ConfigurationListener {

private EditorAdaptor vim;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
import net.sourceforge.vrapper.keymap.KeyStroke;
import net.sourceforge.vrapper.keymap.SpecialKey;
import net.sourceforge.vrapper.keymap.vim.SimpleKeyStroke;
import net.sourceforge.vrapper.platform.Configuration;
import net.sourceforge.vrapper.platform.SimpleConfiguration;
import net.sourceforge.vrapper.platform.GlobalConfiguration;
import net.sourceforge.vrapper.utils.CaretType;
import net.sourceforge.vrapper.vim.DefaultEditorAdaptor;
import net.sourceforge.vrapper.vim.EditorAdaptor;
import net.sourceforge.vrapper.vim.Options;
import net.sourceforge.vrapper.vim.SimpleGlobalConfiguration;
import net.sourceforge.vrapper.vim.modes.AbstractVisualMode;
import net.sourceforge.vrapper.vim.modes.InsertMode;
import net.sourceforge.vrapper.vim.modes.LinewiseVisualMode;
Expand Down Expand Up @@ -123,8 +123,8 @@ private VimInputInterceptorFactory() { /* NOP */ }
}


private static final GlobalConfiguration sharedConfiguration = new SimpleGlobalConfiguration();
private static final RegisterManager globalRegisterManager = new SWTRegisterManager(PlatformUI.getWorkbench().getDisplay());
private static final Configuration sharedConfiguration = new SimpleConfiguration();

public InputInterceptor createInterceptor(AbstractTextEditor abstractTextEditor, ITextViewer textViewer) {
EclipsePlatform platform = new EclipsePlatform(abstractTextEditor, textViewer, sharedConfiguration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.sourceforge.vrapper.platform.Configuration;
import net.sourceforge.vrapper.platform.CursorService;
import net.sourceforge.vrapper.platform.FileService;
import net.sourceforge.vrapper.platform.GlobalConfiguration;
import net.sourceforge.vrapper.platform.HighlightingService;
import net.sourceforge.vrapper.platform.HistoryService;
import net.sourceforge.vrapper.platform.Platform;
Expand Down Expand Up @@ -52,7 +53,7 @@ public class EclipsePlatform implements Platform {
private final EclipseUserInterfaceService userInterfaceService;
private final DefaultKeyMapProvider keyMapProvider;
private final UnderlyingEditorSettings underlyingEditorSettings;
private final Configuration configuration;
private final GlobalConfiguration configuration;
private final AbstractTextEditor underlyingEditor;
private final HighlightingService highlightingService;
private final SearchAndReplaceService searchAndReplaceService;
Expand All @@ -61,7 +62,7 @@ public class EclipsePlatform implements Platform {
private static final Map<String, PlatformSpecificTextObjectProvider> textObjProviderCache = new ConcurrentHashMap<String, PlatformSpecificTextObjectProvider>();

public EclipsePlatform(final AbstractTextEditor abstractTextEditor,
final ITextViewer textViewer, final Configuration sharedConfiguration) {
final ITextViewer textViewer, final GlobalConfiguration sharedConfiguration) {
underlyingEditor = abstractTextEditor;
configuration = sharedConfiguration;
textContent = new EclipseTextContent(textViewer);
Expand Down Expand Up @@ -145,7 +146,7 @@ public UnderlyingEditorSettings getUnderlyingEditorSettings() {
}

@Override
public Configuration getConfiguration() {
public GlobalConfiguration getConfiguration() {
return configuration;
}

Expand Down

0 comments on commit 49820c7

Please sign in to comment.