1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 22:52:11 +02:00

Bug 425595 - Opening 'C/C++ General-> Formatter' of a project properties

cuases NPE
This commit is contained in:
Sergey Prigogin 2014-03-03 14:46:40 -08:00
parent 612cecbc3c
commit f36a34d6a5

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2000, 2013 QNX Software Systems and others. * Copyright (c) 2000, 2014 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -13,7 +13,6 @@
package org.eclipse.cdt.internal.ui.preferences.formatter; package org.eclipse.cdt.internal.ui.preferences.formatter;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Observable; import java.util.Observable;
@ -68,7 +67,7 @@ public class CustomCodeFormatterBlock extends Observable {
fPrefs= scope.getNode(CCorePlugin.PLUGIN_ID); fPrefs= scope.getNode(CCorePlugin.PLUGIN_ID);
fDefaultFormatterId= defaults.get(CCorePreferenceConstants.CODE_FORMATTER, null); fDefaultFormatterId= defaults.get(CCorePreferenceConstants.CODE_FORMATTER, null);
if (fDefaultFormatterId == null) { if (fDefaultFormatterId == null) {
// backward compatibility: use UI prefs // Backward compatibility: use UI prefs
IEclipsePreferences instance= access.getInstanceScope().getNode(CUIPlugin.PLUGIN_ID); IEclipsePreferences instance= access.getInstanceScope().getNode(CUIPlugin.PLUGIN_ID);
fDefaultFormatterId= instance.get(CCorePreferenceConstants.CODE_FORMATTER, null); fDefaultFormatterId= instance.get(CCorePreferenceConstants.CODE_FORMATTER, null);
if (fDefaultFormatterId != null) { if (fDefaultFormatterId != null) {
@ -102,15 +101,8 @@ public class CustomCodeFormatterBlock extends Observable {
return; return;
} }
fFormatterCombo.clearSelection(); fFormatterCombo.clearSelection();
fFormatterCombo.setText(DEFAULT); String formatter = getFormatterById(fDefaultFormatterId);
Iterator<Map.Entry<String, String>> iterator = idMap.entrySet().iterator(); fFormatterCombo.setText(formatter);
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
String val = entry.getValue();
if (val != null && val.equals(fDefaultFormatterId)) {
fFormatterCombo.setText(entry.getKey());
}
}
handleFormatterChanged(); handleFormatterChanged();
} }
@ -132,16 +124,15 @@ public class CustomCodeFormatterBlock extends Observable {
if (fFormatterCombo == null) { if (fFormatterCombo == null) {
return fPrefs.get(CCorePreferenceConstants.CODE_FORMATTER, fDefaultFormatterId); return fPrefs.get(CCorePreferenceConstants.CODE_FORMATTER, fDefaultFormatterId);
} }
String formatterId= idMap.get(fFormatterCombo.getText()); return idMap.get(fFormatterCombo.getText());
return formatterId;
} }
public Control createContents(Composite parent) { public Control createContents(Composite parent) {
if (getNumberOfAvailableFormatters() == 0) { if (idMap.size() == 1) {
return parent; return parent; // No selector is needed since there is only one formatter.
} }
Composite composite = ControlFactory.createGroup(parent, FormatterMessages.CustomCodeFormatterBlock_formatter_name, 1); Composite composite = ControlFactory.createGroup(parent, FormatterMessages.CustomCodeFormatterBlock_formatter_name, 1);
((GridData)composite.getLayoutData()).horizontalSpan = 5; ((GridData) composite.getLayoutData()).horizontalSpan = 5;
PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, ICHelpContextIds.CODEFORMATTER_PREFERENCE_PAGE); PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, ICHelpContextIds.CODEFORMATTER_PREFERENCE_PAGE);
@ -154,9 +145,8 @@ public class CustomCodeFormatterBlock extends Observable {
handleFormatterChanged(); handleFormatterChanged();
} }
}); });
Iterator<String> items = idMap.keySet().iterator(); for (String item : idMap.keySet()) {
while (items.hasNext()) { fFormatterCombo.add(item);
fFormatterCombo.add(items.next());
} }
final String noteTitle= FormatterMessages.CustomCodeFormatterBlock_formatter_note; final String noteTitle= FormatterMessages.CustomCodeFormatterBlock_formatter_note;
@ -175,27 +165,29 @@ public class CustomCodeFormatterBlock extends Observable {
} }
private void initDefault() { private void initDefault() {
boolean init = false; if (fFormatterCombo == null) {
return;
}
String formatterID= fPrefs.get(CCorePreferenceConstants.CODE_FORMATTER, fDefaultFormatterId); String formatterID= fPrefs.get(CCorePreferenceConstants.CODE_FORMATTER, fDefaultFormatterId);
if (formatterID != null) { fFormatterCombo.setText(getFormatterById(formatterID));
Iterator<Map.Entry<String, String>> iterator = idMap.entrySet().iterator(); }
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next(); private String getFormatterById(String formatterId) {
String formatter = DEFAULT;
if (formatterId != null) {
for (Map.Entry<String, String> entry : idMap.entrySet()) {
String val = entry.getValue(); String val = entry.getValue();
if (val != null && val.equals(formatterID)) { if (formatterId.equals(val)) {
fFormatterCombo.setText(entry.getKey()); formatter = entry.getKey();
init = true; break;
} }
} }
} }
if (!init) { return formatter;
formatterID= null;
fFormatterCombo.setText(DEFAULT);
}
} }
private void initializeFormatters() { private void initializeFormatters() {
idMap = new HashMap<String, String>(); idMap = new HashMap<>();
idMap.put(DEFAULT, CCorePreferenceConstants.DEFAULT_CODE_FORMATTER); idMap.put(DEFAULT, CCorePreferenceConstants.DEFAULT_CODE_FORMATTER);
IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(CCorePlugin.PLUGIN_ID, CCorePlugin.FORMATTER_EXTPOINT_ID); IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(CCorePlugin.PLUGIN_ID, CCorePlugin.FORMATTER_EXTPOINT_ID);
if (point != null) { if (point != null) {
@ -210,8 +202,4 @@ public class CustomCodeFormatterBlock extends Observable {
} }
} }
} }
private final int getNumberOfAvailableFormatters() {
return idMap.size() - 1;
}
} }