mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-24 17:35:35 +02:00
Fix for bug 147644, file type preferences UI.
This commit is contained in:
parent
752e150205
commit
2494fd44b7
4 changed files with 135 additions and 14 deletions
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -187,6 +187,9 @@ public class CFileTypesPropertyPage extends PropertyPage {
|
|||
CCorePlugin.setUseProjectSpecificContentTypes(project, useProjectContentTypes);
|
||||
computeEvents(project);
|
||||
}
|
||||
if (useProjectContentTypes) {
|
||||
fPrefsBlock.performOk();
|
||||
}
|
||||
return super.performOk();
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue