Skip to content

Commit

Permalink
per-project folding preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
danthe1st committed Jan 11, 2025
1 parent ad300ec commit 9eb9629
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 35 deletions.
11 changes: 11 additions & 0 deletions org.eclipse.jdt.ui/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,17 @@
<keywordReference id="org.eclipse.jdt.ui.saveparticipant"/>
</page>

<page
name="%editorFoldingPage"
class="org.eclipse.jdt.internal.ui.preferences.FoldingPreferencePage"
category="org.eclipse.jdt.ui.propertyPages.JavaEditorPropertyPage"
id="org.eclipse.jdt.ui.propertyPages.FoldingPreferencePage">
<enabledWhen>
<adapt type="org.eclipse.core.resources.IProject">
<test property="org.eclipse.core.resources.projectNature" value="org.eclipse.jdt.core.javanature"/>
</adapt>
</enabledWhen>
</page>
<page
name="%classpathContainerPageName"
class="org.eclipse.jdt.internal.ui.preferences.ClasspathContainerPreferencePage"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.preferences.IScopeContext;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.layout.PixelConverter;
Expand All @@ -46,8 +47,10 @@

import org.eclipse.jdt.internal.corext.util.Messages;

import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jdt.ui.PreferenceConstants;
import org.eclipse.jdt.ui.text.folding.IJavaFoldingPreferenceBlock;
import org.eclipse.jdt.ui.text.folding.IScopedJavaFoldingPreferenceBlock;

import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.preferences.OverlayPreferenceStore.OverlayKey;
Expand All @@ -61,7 +64,7 @@
*
* @since 3.0
*/
class FoldingConfigurationBlock implements IPreferenceConfigurationBlock {
class FoldingConfigurationBlock implements IPreferenceAndPropertyConfigurationBlock {

private static class ErrorPreferences implements IJavaFoldingPreferenceBlock {
private String fMessage;
Expand Down Expand Up @@ -117,14 +120,20 @@ public void dispose() {
private final Map<String, IJavaFoldingPreferenceBlock> fProviderPreferences;
private final Map<String, Control> fProviderControls;

private IScopeContext fContext;

public FoldingConfigurationBlock(OverlayPreferenceStore store) {
private boolean fIsProjectPreferencePage;


public FoldingConfigurationBlock(OverlayPreferenceStore store, IScopeContext context, boolean isProjectPreferencePage) {
Assert.isNotNull(store);
fStore= store;
fStore.addKeys(createOverlayStoreKeys());
fProviderDescriptors= createListModel();
fProviderPreferences= new HashMap<>();
fProviderControls= new HashMap<>();
this.fContext= context;
this.fIsProjectPreferencePage= isProjectPreferencePage;
}

private Map<String, JavaFoldingStructureProviderDescriptor> createListModel() {
Expand Down Expand Up @@ -289,6 +298,14 @@ void updateListDependencies() {
if (prefs == null) {
try {
prefs= desc.createPreferences();
if (fIsProjectPreferencePage) {
if (prefs instanceof IScopedJavaFoldingPreferenceBlock scopedPrefs) {
scopedPrefs.setScopeContext(fContext);
} else {
String message= Messages.format(PreferencesMessages.FoldingConfigurationBlock_error_project_prefs_not_supported, desc.getName());
prefs= new ErrorPreferences(message);
}
}
fProviderPreferences.put(id, prefs);
} catch (CoreException e) {
JavaPlugin.log(e);
Expand Down Expand Up @@ -369,4 +386,19 @@ private void restoreFromPreferences() {
else
fProviderViewer.setSelection(new StructuredSelection(provider), true);
}

@Override
public void disableProjectSettings() {
if(fContext != null) {
fContext.getNode(JavaUI.ID_PLUGIN).remove(PreferenceConstants.EDITOR_FOLDING_ENABLED);
}
}

@Override
public void enableProjectSettings() {
if(fContext != null) {
fContext.getNode(JavaUI.ID_PLUGIN).putBoolean(PreferenceConstants.EDITOR_FOLDING_ENABLED, fStore.getBoolean(PreferenceConstants.EDITOR_FOLDING_ENABLED));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,85 @@
package org.eclipse.jdt.internal.ui.preferences;


import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.core.runtime.preferences.IScopeContext;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ProjectScope;

import org.eclipse.ui.preferences.ScopedPreferenceStore;

import org.eclipse.jdt.ui.JavaUI;

import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
import org.eclipse.jdt.internal.ui.JavaPlugin;

/**
* The page for setting the editor options.
*/
public final class FoldingPreferencePage extends AbstractConfigurationBlockPreferencePage {
public final class FoldingPreferencePage extends AbstractConfigurationBlockPreferenceAndPropertyPage {

public static final String PROPERTY_PAGE_ID= "org.eclipse.jdt.ui.propertyPages.FoldingPreferencePage"; //$NON-NLS-1$
public static final String PREFERENCE_PAGE_ID= "org.eclipse.jdt.ui.preferences.FoldingPreferencePage"; //$NON-NLS-1$
private OverlayPreferenceStore fOverlayStore;


public FoldingPreferencePage() {
setDescription(PreferencesMessages.JavaEditorPreferencePage_folding_title);
}

/*
* @see org.eclipse.ui.internal.editors.text.AbstractConfigureationBlockPreferencePage#getHelpId()
* @see org.eclipse.jdt.internal.ui.preferences.AbstractConfigurationBlockPreferenceAndPropertyPage#getHelpId()
*/
@Override
protected String getHelpId() {
return IJavaHelpContextIds.JAVA_EDITOR_PREFERENCE_PAGE;
}

/*
* @see org.eclipse.ui.internal.editors.text.AbstractConfigurationBlockPreferencePage#setDescription()
*/
@Override
protected void setDescription() {
String description= PreferencesMessages.JavaEditorPreferencePage_folding_title;
setDescription(description);
protected IPreferenceAndPropertyConfigurationBlock createConfigurationBlock(IScopeContext context) {
ScopedPreferenceStore scopedStore= new ScopedPreferenceStore(context, JavaUI.ID_PLUGIN);
fOverlayStore= new OverlayPreferenceStore(
scopedStore,
new OverlayPreferenceStore.OverlayKey[] {});
FoldingConfigurationBlock foldingConfigurationBlock= new FoldingConfigurationBlock(fOverlayStore, context, isProjectPreferencePage());
fOverlayStore.load();
fOverlayStore.start();
return foldingConfigurationBlock;
}

/*
* @see org.org.eclipse.ui.internal.editors.text.AbstractConfigurationBlockPreferencePage#setPreferenceStore()
*/
@Override
protected void setPreferenceStore() {
setPreferenceStore(JavaPlugin.getDefault().getPreferenceStore());
protected boolean hasProjectSpecificOptions(IProject project) {
return JavaPlugin.getDefault().getFoldingStructureProviderRegistry().hasProjectSpecificOptions(new ProjectScope(project));
}

@Override
protected String getPreferencePageID() {
return PREFERENCE_PAGE_ID;
}

@Override
protected Label createDescriptionLabel(Composite parent) {
return null; // no description for new look.
protected String getPropertyPageID() {
return PROPERTY_PAGE_ID;
}

/*
* @see org.eclipse.ui.internal.editors.text.AbstractConfigureationBlockPreferencePage#createConfigurationBlock(org.eclipse.ui.internal.editors.text.OverlayPreferenceStore)
*/
@Override
protected IPreferenceConfigurationBlock createConfigurationBlock(OverlayPreferenceStore overlayPreferenceStore) {
return new FoldingConfigurationBlock(overlayPreferenceStore);
public boolean performOk() {
boolean result= super.performOk();
fOverlayStore.propagate();
return result;
}

@Override
public void performDefaults() {
fOverlayStore.loadDefaults();
super.performDefaults();

}

@Override
public void dispose() {
super.dispose();
fOverlayStore.stop();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,7 @@ private PreferencesMessages() {
public static String FoldingConfigurationBlock_combo_caption;
public static String FoldingConfigurationBlock_info_no_preferences;
public static String FoldingConfigurationBlock_error_not_exist;
public static String FoldingConfigurationBlock_error_project_prefs_not_supported;
public static String FoldingConfigurationBlock_warning_providerNotFound_resetToDefault;
public static String PropertiesFileEditorPreferencePage_key;
public static String PropertiesFileEditorPreferencePage_value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,7 @@ FoldingConfigurationBlock_enable= Enable f&olding
FoldingConfigurationBlock_combo_caption= Select folding to &use:
FoldingConfigurationBlock_info_no_preferences= The selected folding provider did not provide a preference control
FoldingConfigurationBlock_error_not_exist= The ''{0}'' folding provider does not exist.
FoldingConfigurationBlock_error_project_prefs_not_supported=The ''{0}'' folding provider does not support project specific preferences.
FoldingConfigurationBlock_warning_providerNotFound_resetToDefault= The ''{0}'' folding provider could not be found. Resetting to the default folding provider.

# Properties File Editor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import org.eclipse.core.runtime.preferences.IScopeContext;

import org.eclipse.jface.preference.IPreferenceStore;

import org.eclipse.ui.preferences.ScopedPreferenceStore;

import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jdt.ui.PreferenceConstants;
import org.eclipse.jdt.ui.text.folding.IJavaFoldingPreferenceBlock;
import org.eclipse.jdt.ui.text.folding.IScopedJavaFoldingPreferenceBlock;

import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.preferences.OverlayPreferenceStore;
Expand All @@ -45,7 +50,7 @@
*
* @since 3.0
*/
public class DefaultJavaFoldingPreferenceBlock implements IJavaFoldingPreferenceBlock {
public class DefaultJavaFoldingPreferenceBlock implements IScopedJavaFoldingPreferenceBlock {

private IPreferenceStore fStore;
private OverlayPreferenceStore fOverlayStore;
Expand Down Expand Up @@ -75,6 +80,16 @@ public DefaultJavaFoldingPreferenceBlock() {
fOverlayStore= new OverlayPreferenceStore(fStore, fKeys);
}

@Override
public void setScopeContext(IScopeContext context) {
if(context == null) {
fStore = JavaPlugin.getDefault().getPreferenceStore();
} else {
fStore= new ScopedPreferenceStore(context, JavaUI.ID_PLUGIN);
}
fOverlayStore= new OverlayPreferenceStore(fStore, fKeys);
}

private OverlayKey[] createKeys() {
ArrayList<OverlayKey> overlayKeys= new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.preferences.IScopeContext;

import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jdt.ui.PreferenceConstants;
import org.eclipse.jdt.ui.text.folding.IJavaFoldingPreferenceBlock;
import org.eclipse.jdt.ui.text.folding.IJavaFoldingStructureProvider;

Expand Down Expand Up @@ -116,4 +119,8 @@ public String getId() {
public String getName() {
return fName;
}

public boolean hasProjectSpecificOptions(IScopeContext context) {
return context.getNode(JavaUI.ID_PLUGIN).get(PreferenceConstants.EDITOR_FOLDING_ENABLED, null) != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.preferences.IScopeContext;

import org.eclipse.jface.preference.IPreferenceStore;

Expand Down Expand Up @@ -87,6 +88,17 @@ public JavaFoldingStructureProviderDescriptor getFoldingProviderDescriptor(Strin
* @return the current provider according to the preferences
*/
public IJavaFoldingStructureProvider getCurrentFoldingProvider() {
JavaFoldingStructureProviderDescriptor desc= getCurrentFoldingStructureProviderDescriptor();

try {
return desc.createProvider();
} catch (CoreException e) {
JavaPlugin.log(e);
return null;
}
}

private JavaFoldingStructureProviderDescriptor getCurrentFoldingStructureProviderDescriptor() {
IPreferenceStore preferenceStore= JavaPlugin.getDefault().getPreferenceStore();
String currentProviderId= preferenceStore.getString(PreferenceConstants.EDITOR_FOLDING_PROVIDER);
JavaFoldingStructureProviderDescriptor desc= getFoldingProviderDescriptor(currentProviderId);
Expand All @@ -103,13 +115,7 @@ public IJavaFoldingStructureProvider getCurrentFoldingProvider() {

preferenceStore.setToDefault(PreferenceConstants.EDITOR_FOLDING_PROVIDER);
}

try {
return desc.createProvider();
} catch (CoreException e) {
JavaPlugin.log(e);
return null;
}
return desc;
}

/**
Expand Down Expand Up @@ -142,4 +148,11 @@ public void reloadExtensions() {
}
}

public synchronized boolean hasProjectSpecificOptions(IScopeContext context) {
synchronized(this) {
ensureRegistered();
}

return getCurrentFoldingStructureProviderDescriptor().hasProjectSpecificOptions(context);
}
}
Loading

0 comments on commit 9eb9629

Please sign in to comment.