Skip to content
This repository has been archived by the owner on Apr 3, 2018. It is now read-only.

Commit

Permalink
Fixed GoEditor setup of SourceViewConfiguration and pref store init.
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-medeiros committed May 14, 2014
1 parent 8b8474e commit 594d813
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*******************************************************************************
* Copyright (c) 2014, 2014 Bruno Medeiros and other Contributors.
* 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:
* Bruno Medeiros - initial API and implementation
*******************************************************************************/
package melnorme.lang.ide.ui.editor;


import java.util.ArrayList;
import java.util.List;

import melnorme.lang.ide.ui.LangUIPlugin;
import melnorme.utilbox.misc.ArrayUtil;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.ISourceViewerExtension2;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.editors.text.EditorsUI;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.editors.text.TextSourceViewerConfiguration;
import org.eclipse.ui.texteditor.ChainedPreferenceStore;

public abstract class AbstractLangEditor extends TextEditor {

public AbstractLangEditor() {
super();
}

@Override
protected void initializeEditor() {
super.initializeEditor();
}

@Override
protected void doSetInput(IEditorInput input) throws CoreException {
super.doSetInput(input);

ISourceViewer sourceViewer = getSourceViewer();

if (!(sourceViewer instanceof ISourceViewerExtension2)) {
setPreferenceStore(createCombinedPreferenceStore(input));
} else {
ISourceViewerExtension2 sourceViewerExt2 = (ISourceViewerExtension2) sourceViewer;

getSourceViewerDecorationSupport(sourceViewer).uninstall();
sourceViewerExt2.unconfigure();

setPreferenceStore(createCombinedPreferenceStore(input));

sourceViewer.configure(getSourceViewerConfiguration());
getSourceViewerDecorationSupport(sourceViewer).install(getPreferenceStore());
}

internalDoSetInput(input);
}

@Override
protected void setPreferenceStore(IPreferenceStore store) {
super.setPreferenceStore(store);
setSourceViewerConfiguration(createSourceViewerConfiguration());
}

@SuppressWarnings("unused")
protected void internalDoSetInput(IEditorInput input) {
}

protected abstract TextSourceViewerConfiguration createSourceViewerConfiguration();

protected IPreferenceStore createCombinedPreferenceStore(IEditorInput input) {
List<IPreferenceStore> stores = new ArrayList<IPreferenceStore>(4);

// TODO: add project pref scope
IProject project = EditorUtils.getProject(input);

stores.add(LangUIPlugin.getInstance().getPreferenceStore());
stores.add(LangUIPlugin.getInstance().getCorePreferenceStore());
stores.add(EditorsUI.getPreferenceStore());

return new ChainedPreferenceStore(ArrayUtil.createFrom(stores, IPreferenceStore.class));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*******************************************************************************
* Copyright (c) 2014, 2014 Bruno Medeiros and other Contributors.
* 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:
* Bruno Medeiros - initial API and implementation
*******************************************************************************/
package melnorme.lang.ide.ui.editor;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IFileEditorInput;

public class EditorUtils {

public static IProject getProject(IEditorInput input) {
if(input instanceof IFileEditorInput) {
return ((IFileEditorInput) input).getFile().getProject();
}

IResource resource = (IResource) input.getAdapter(IResource.class);
if(resource != null) {
return resource.getProject();
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,41 @@

import java.util.ResourceBundle;

import melnorme.lang.ide.ui.editor.AbstractLangEditor;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.IDocumentExtension3;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.source.DefaultCharacterPairMatcher;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.editors.text.TextSourceViewerConfiguration;
import org.eclipse.ui.texteditor.SourceViewerDecorationSupport;
import org.eclipse.ui.views.contentoutline.IContentOutlinePage;

import com.googlecode.goclipse.ui.GoUIPlugin;
import com.googlecode.goclipse.ui.GoUIPreferenceConstants;

public class GoEditor extends AbstractLangEditor {

public class GoEditor extends TextEditor {
public final static String EDITOR_MATCHING_BRACKETS = "matchingBrackets";
public final static String EDITOR_MATCHING_BRACKETS_COLOR = "matchingBracketsColor";
public static final String EDITOR_ID = "com.googlecode.goclipse.editors.Editor";

// the org.eclipse.ui.contexts ID:
private static final String EDITOR_CONTEXT_ID = "com.googlecode.goclipse.editor";

private static final String BUNDLE_ID = "com.googlecode.goclipse.editors.GoEditorMessages";

public static final String EDITOR_CONTEXT = "#GoEditorContext";
public static final String RULER_CONTEXT = "#GoEditorRulerContext";

private static ResourceBundle editorResourceBundle = ResourceBundle.getBundle(BUNDLE_ID);

private IPropertyChangeListener changeListener;
private DefaultCharacterPairMatcher matcher;
private EditorImageUpdater imageUpdater;

Expand All @@ -46,35 +47,24 @@ public class GoEditor extends TextEditor {
*/
public GoEditor() {

setSourceViewerConfiguration(new GoEditorSourceViewerConfiguration(this, getPreferenceStore()));

setKeyBindingScopes(new String[] {"com.googlecode.goclipse.editor"});

changeListener = new IPropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent event) {
// SysUtils.debug("Preference Change Event");
// if (event.getProperty().equals(PreferenceConstants.FIELD_USE_HIGHLIGHTING)) {
// setSourceViewerConfiguration(new Configuration(colorManager));
// setDocumentProvider(new DocumentProvider());
// initializeEditor();
// }
}
};

// TODO: Listen to specific property events only
GoUIPlugin.getPrefStore().addPropertyChangeListener(changeListener);
GoUIPlugin.getCorePrefStore().addPropertyChangeListener(changeListener);
setKeyBindingScopes(new String[] {EDITOR_CONTEXT_ID});

}

@Override
protected void initializeEditor() {
super.initializeEditor();

setEditorContextMenuId(EDITOR_CONTEXT);
setRulerContextMenuId(RULER_CONTEXT);
}

@Override
protected TextSourceViewerConfiguration createSourceViewerConfiguration() {
return new GoEditorSourceViewerConfiguration(this, getPreferenceStore());
}


@Override
protected void setTitleImage(Image image) {
super.setTitleImage(image);
Expand Down Expand Up @@ -109,13 +99,9 @@ protected void configureSourceViewerDecorationSupport(SourceViewerDecorationSupp
char[] matchChars = {'(', ')', '[', ']', '{', '}'}; //which brackets to match
matcher = new DefaultCharacterPairMatcher(matchChars, IDocumentExtension3.DEFAULT_PARTITIONING);
support.setCharacterPairMatcher(matcher);
support.setMatchingCharacterPainterPreferenceKeys(EDITOR_MATCHING_BRACKETS,
EDITOR_MATCHING_BRACKETS_COLOR);

//Enable bracket highlighting in the preference store
IPreferenceStore store = getPreferenceStore();
store.setDefault(EDITOR_MATCHING_BRACKETS, true);
store.setDefault(EDITOR_MATCHING_BRACKETS_COLOR, "128,128,128");
support.setMatchingCharacterPainterPreferenceKeys(
GoUIPreferenceConstants.EDITOR_MATCHING_BRACKETS,
GoUIPreferenceConstants.EDITOR_MATCHING_BRACKETS_COLOR);
}

@Override
Expand Down Expand Up @@ -156,8 +142,6 @@ public void replaceText(String newText) {

@Override
public void dispose() {
GoUIPlugin.getPrefStore().removePropertyChangeListener(changeListener);
GoUIPlugin.getCorePrefStore().removePropertyChangeListener(changeListener);

if (imageUpdater != null) {
imageUpdater.dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ public interface GoUIPreferenceConstants {
public static final String FIELD_SYNTAX_TEXT_COLOR = "syntax_highlighting_text_color";
public static final String FIELD_SYNTAX_TEXT_STYLE = "syntax_highlighting_text_style";

public static final String EDITOR_MATCHING_BRACKETS = "matchingBrackets";
public static final String EDITOR_MATCHING_BRACKETS_COLOR = "matchingBracketsColor";

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
import melnorme.lang.ide.ui.editor.text.LangAutoEditPreferenceConstants;

import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
import org.eclipse.core.runtime.preferences.DefaultScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.StringConverter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.RGB;

import com.googlecode.goclipse.editors.IColorDefaults;

public class GoUIPreferencesInitializer extends AbstractPreferenceInitializer {
public class GoUIPreferencesInitializer extends AbstractPreferenceInitializer
implements GoUIPreferenceConstants {

@Override
public void initializeDefaultPreferences() {
Expand All @@ -44,23 +44,25 @@ public void initializeDefaultPreferences() {
store.setDefault(LangAutoEditPreferenceConstants.AE_PARENTHESES_AS_BLOCKS, true);


IEclipsePreferences coreDefaults = DefaultScope.INSTANCE.getNode(GoUIPlugin.PLUGIN_ID);
store.setDefault(FIELD_USE_HIGHLIGHTING, true);
store.setDefault(FIELD_SYNTAX_KEYWORD_COLOR, StringConverter.asString(IColorDefaults.KEYWORD));
store.setDefault(FIELD_SYNTAX_VALUE_COLOR, StringConverter.asString(IColorDefaults.VALUE));
store.setDefault(FIELD_SYNTAX_PRIMITIVE_COLOR, StringConverter.asString(IColorDefaults.PRIMITIVE));
store.setDefault(FIELD_SYNTAX_COMMENT_COLOR, StringConverter.asString(IColorDefaults.COMMENT));
store.setDefault(FIELD_SYNTAX_BUILTIN_FUNCTION_COLOR, StringConverter.asString(IColorDefaults.BUILTIN_FUNCTION));
store.setDefault(FIELD_SYNTAX_STRING_COLOR, StringConverter.asString(IColorDefaults.STRING));
store.setDefault(FIELD_SYNTAX_MULTILINE_STRING_COLOR, StringConverter.asString(IColorDefaults.MULTILINE_STRING));
store.setDefault(FIELD_SYNTAX_KEYWORD_STYLE, SWT.BOLD );
store.setDefault(FIELD_SYNTAX_VALUE_STYLE, SWT.BOLD|SWT.ITALIC);
store.setDefault(FIELD_SYNTAX_PRIMITIVE_STYLE, SWT.ITALIC );
store.setDefault(FIELD_SYNTAX_COMMENT_STYLE, SWT.NORMAL );
store.setDefault(FIELD_SYNTAX_BUILTIN_FUNCTION_STYLE, SWT.BOLD );
store.setDefault(FIELD_SYNTAX_STRING_STYLE, SWT.NORMAL );
store.setDefault(FIELD_SYNTAX_MULTILINE_STRING_STYLE, SWT.NORMAL );

coreDefaults.putBoolean(GoUIPreferenceConstants.FIELD_USE_HIGHLIGHTING, true);
coreDefaults.put(GoUIPreferenceConstants.FIELD_SYNTAX_KEYWORD_COLOR, StringConverter.asString(IColorDefaults.KEYWORD));
coreDefaults.put(GoUIPreferenceConstants.FIELD_SYNTAX_VALUE_COLOR, StringConverter.asString(IColorDefaults.VALUE));
coreDefaults.put(GoUIPreferenceConstants.FIELD_SYNTAX_PRIMITIVE_COLOR, StringConverter.asString(IColorDefaults.PRIMITIVE));
coreDefaults.put(GoUIPreferenceConstants.FIELD_SYNTAX_COMMENT_COLOR, StringConverter.asString(IColorDefaults.COMMENT));
coreDefaults.put(GoUIPreferenceConstants.FIELD_SYNTAX_BUILTIN_FUNCTION_COLOR, StringConverter.asString(IColorDefaults.BUILTIN_FUNCTION));
coreDefaults.put(GoUIPreferenceConstants.FIELD_SYNTAX_STRING_COLOR, StringConverter.asString(IColorDefaults.STRING));
coreDefaults.put(GoUIPreferenceConstants.FIELD_SYNTAX_MULTILINE_STRING_COLOR, StringConverter.asString(IColorDefaults.MULTILINE_STRING));
coreDefaults.putInt(GoUIPreferenceConstants.FIELD_SYNTAX_KEYWORD_STYLE, SWT.BOLD );
coreDefaults.putInt(GoUIPreferenceConstants.FIELD_SYNTAX_VALUE_STYLE, SWT.BOLD|SWT.ITALIC);
coreDefaults.putInt(GoUIPreferenceConstants.FIELD_SYNTAX_PRIMITIVE_STYLE, SWT.ITALIC );
coreDefaults.putInt(GoUIPreferenceConstants.FIELD_SYNTAX_COMMENT_STYLE, SWT.NORMAL );
coreDefaults.putInt(GoUIPreferenceConstants.FIELD_SYNTAX_BUILTIN_FUNCTION_STYLE, SWT.BOLD );
coreDefaults.putInt(GoUIPreferenceConstants.FIELD_SYNTAX_STRING_STYLE, SWT.NORMAL );
coreDefaults.putInt(GoUIPreferenceConstants.FIELD_SYNTAX_MULTILINE_STRING_STYLE, SWT.NORMAL );
store.setDefault(EDITOR_MATCHING_BRACKETS, true);
store.setDefault(EDITOR_MATCHING_BRACKETS_COLOR, StringConverter.asString(new RGB(128, 128, 128)));

}

}

0 comments on commit 594d813

Please sign in to comment.