From 2494fd44b7c9827b9475e82a6d4c9ac628057bb0 Mon Sep 17 00:00:00 2001 From: Markus Schorn Date: Wed, 12 Jul 2006 13:15:28 +0000 Subject: [PATCH] Fix for bug 147644, file type preferences UI. --- .../ui/preferences/CFileTypeAssociation.java | 58 ++++++++++++- .../CFileTypesPreferenceBlock.java | 86 ++++++++++++++++--- .../preferences/CFileTypesPropertyPage.java | 3 + .../PreferencesMessages.properties | 2 + 4 files changed, 135 insertions(+), 14 deletions(-) diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/CFileTypeAssociation.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/CFileTypeAssociation.java index 3fd59dcc141..72b9b2f722c 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/CFileTypeAssociation.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/CFileTypeAssociation.java @@ -15,10 +15,6 @@ import org.eclipse.core.runtime.content.IContentType; public class CFileTypeAssociation { - private String fSpec; - private int fType; - private IContentType fContentType; - public CFileTypeAssociation(String spec, int type, IContentType contentType) { super(); fSpec = spec; @@ -26,6 +22,53 @@ public class CFileTypeAssociation { fContentType = contentType; } + private String fSpec; + private int fType; + private IContentType fContentType; + + public int hashCode() { + final int PRIME = 31; + int result = 1; + result = PRIME * result + ((fContentType == null) ? 0 : fContentType.getId().hashCode()); + result = PRIME * result + ((fSpec == null) ? 0 : fSpec.hashCode()); + result = PRIME * result + fType; + return result; + } + + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + final CFileTypeAssociation other = (CFileTypeAssociation) obj; + if (!fContentType.getId().equals(other.fContentType.getId())) { + return false; + } + if (!fSpec.equals(other.fSpec)) { + return false; + } + if (fType != other.fType) { + return false; + } + return true; + } + + public boolean equalsIgnoreCaseOfSpec(CFileTypeAssociation other) { + if (!fContentType.getId().equals(other.fContentType.getId())) { + return false; + } + if (!fSpec.equalsIgnoreCase(other.fSpec)) { + return false; + } + if (fType != other.fType) { + return false; + } + return true; + } + + /** * @return Returns the fSettings. */ @@ -72,4 +115,11 @@ public class CFileTypeAssociation { return fContentType.getName(); } + /** + * Returns {@link IContentType#FILE_NAME_SPEC} or {@link IContentType#FILE_EXTENSION_SPEC}. + */ + public int getFileSpecType() { + return fType & (IContentType.FILE_NAME_SPEC | IContentType.FILE_EXTENSION_SPEC); + } + } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/CFileTypesPreferenceBlock.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/CFileTypesPreferenceBlock.java index a1017c08361..4b4eda19567 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/CFileTypesPreferenceBlock.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/CFileTypesPreferenceBlock.java @@ -11,6 +11,7 @@ package org.eclipse.cdt.internal.ui.preferences; import java.util.ArrayList; +import java.util.Collection; import java.util.Iterator; import java.util.List; @@ -23,6 +24,8 @@ import org.eclipse.core.runtime.content.IContentType; import org.eclipse.core.runtime.content.IContentTypeManager; import org.eclipse.core.runtime.content.IContentTypeSettings; import org.eclipse.core.runtime.preferences.IScopeContext; +import org.eclipse.jface.dialogs.ErrorDialog; +import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.viewers.ColumnWeightData; import org.eclipse.jface.viewers.ILabelProvider; import org.eclipse.jface.viewers.ILabelProviderListener; @@ -50,6 +53,7 @@ import org.eclipse.swt.widgets.TableColumn; import org.eclipse.cdt.core.model.CoreModel; +import org.eclipse.cdt.internal.ui.util.Messages; import org.eclipse.cdt.internal.ui.util.PixelConverter; import org.eclipse.cdt.internal.ui.util.SWTUtil; @@ -345,11 +349,11 @@ public class CFileTypesPreferenceBlock { if (fInput != null) { context = new ProjectScope(fInput); } - addAssociations(add, context); removeAssociations(rem, context); + addAssociations(add, context); } - protected void addAssociations(CFileTypeAssociation[] add, IScopeContext context) { + final protected void addAssociations(CFileTypeAssociation[] add, IScopeContext context) { for (int i = 0; i < add.length; ++i) { CFileTypeAssociation assoc = add[i]; String spec = assoc.getSpec(); @@ -447,7 +451,7 @@ public class CFileTypesPreferenceBlock { pattern = pattern.substring(2); type = IContentType.FILE_EXTENSION_SPEC; } - return new CFileTypeAssociation(pattern, type, contentType); + return new CFileTypeAssociation(pattern, type | IContentType.IGNORE_PRE_DEFINED, contentType); } protected void handleSelectionChanged() { @@ -466,35 +470,97 @@ public class CFileTypesPreferenceBlock { } } - protected void handleAdd() { + final protected void handleAdd() { CFileTypeAssociation assoc = null; CFileTypeDialog dlg = new CFileTypeDialog(fBtnNew.getParent().getShell()); if (Window.OK == dlg.open()) { assoc = createAssociation(dlg.getPattern(), dlg.getContentType()); - if (null != assoc) { + if (handleAdd(assoc)) { fAssocViewer.add(assoc); - fAddAssoc.add(assoc); - fRemoveAssoc.remove(assoc); setDirty(true); } } } - protected void handleRemove() { + private boolean handleAdd(CFileTypeAssociation assoc) { + // assoc is marked to be added. + if (containsIgnoreCaseOfSpec(fAddAssoc, assoc)) { + reportDuplicateAssociation(assoc); + return false; + } + // assoc exists, but is marked to be removed. + if (containsIgnoreCaseOfSpec(fRemoveAssoc, assoc)) { + if (!fRemoveAssoc.remove(assoc)) { + fAddAssoc.add(assoc); + } + return true; + } + + // analyze current settings + IContentTypeSettings settings; + if (fInput == null) { + settings= assoc.getContentType(); + } + else { + try { + settings= assoc.getContentType().getSettings(new ProjectScope(fInput)); + } catch (CoreException e) { + ErrorDialog.openError(fBtnNew.getParent().getShell(), + PreferencesMessages.getString("CFileTypesPreferenceBlock.addAssociationError.title"), //$NON-NLS-1$ + null, e.getStatus()); + return false; + } + } + String newSpec= assoc.getSpec(); + String[] specs= settings.getFileSpecs(assoc.getFileSpecType()); + for (int i = 0; i < specs.length; i++) { + String spec = specs[i]; + if (spec.equalsIgnoreCase(newSpec)) { + reportDuplicateAssociation(assoc); + return false; + } + } + fAddAssoc.add(assoc); + return true; + } + + private boolean containsIgnoreCaseOfSpec(Collection collection, CFileTypeAssociation assoc) { + for (Iterator iter = collection.iterator(); iter.hasNext(); ) { + CFileTypeAssociation existing = (CFileTypeAssociation) iter.next(); + if (assoc.equalsIgnoreCaseOfSpec(existing)) { + return true; + } + } + return false; + } + + private void reportDuplicateAssociation(CFileTypeAssociation assoc) { + MessageDialog.openError(fBtnNew.getParent().getShell(), + PreferencesMessages.getString("CFileTypesPreferenceBlock.addAssociationError.title"), //$NON-NLS-1$ + Messages.format(PreferencesMessages.getString("CFileTypesPreferenceBlock.addAssociationErrorMessage"), //$NON-NLS-1$ + assoc.getPattern(), assoc.getContentType().getName())); + } + + final protected void handleRemove() { IStructuredSelection sel = getSelection(); if ((null != sel) && (!sel.isEmpty())) { for (Iterator iter = sel.iterator(); iter.hasNext();) { CFileTypeAssociation assoc = (CFileTypeAssociation) iter.next(); + handleRemove(assoc); fAssocViewer.remove(assoc); - fAddAssoc.remove(assoc); - fRemoveAssoc.add(assoc); setDirty(true); } } } + private void handleRemove(CFileTypeAssociation assoc) { + if (!fAddAssoc.remove(assoc)) { + fRemoveAssoc.add(assoc); + } + } + private IStructuredSelection getSelection() { return (IStructuredSelection) fAssocViewer.getSelection(); } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/CFileTypesPropertyPage.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/CFileTypesPropertyPage.java index 7aba75dd033..dd7a5faf5da 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/CFileTypesPropertyPage.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/CFileTypesPropertyPage.java @@ -187,6 +187,9 @@ public class CFileTypesPropertyPage extends PropertyPage { CCorePlugin.setUseProjectSpecificContentTypes(project, useProjectContentTypes); computeEvents(project); } + if (useProjectContentTypes) { + fPrefsBlock.performOk(); + } return super.performOk(); } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/PreferencesMessages.properties b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/PreferencesMessages.properties index 7014859a6f6..17c543b086a 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/PreferencesMessages.properties +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/PreferencesMessages.properties @@ -144,6 +144,8 @@ CFileTypesPreferenceBlock.New...=New... CFileTypesPreferenceBlock.Remove=Remove CFileTypesPreferencePage.colTitlePattern=Filename CFileTypesPreferencePage.colTitleDescription=Description +CFileTypesPreferenceBlock.addAssociationError.title=Create file association +CFileTypesPreferenceBlock.addAssociationErrorMessage=The association between ''{0}'' and ''{1}'' already exists CFileTypesPreferencePage.colTitleStatus=Status CFileTypesPreferencePage.userDefined=User Defined CFileTypesPreferencePage.preDefined=Locked