From 7a81aa09375fa9c009697cc999a04f4bb1f426ed Mon Sep 17 00:00:00 2001 From: Dmitry Avdeev Date: Tue, 22 Mar 2011 19:02:19 +0300 Subject: [PATCH] TypePresentationService: interface extracted --- .../intellij/ide/TypePresentationService.java | 120 ++-------- .../presentation/PresentationTemplate.java | 35 --- .../PresentationTemplateImpl.java | 94 -------- .../ide/TypePresentationServiceImpl.java | 206 ++++++++++++++++++ .../src/META-INF/LangExtensions.xml | 3 + .../impl/ElementPresentationTemplateImpl.java | 4 +- 6 files changed, 232 insertions(+), 230 deletions(-) delete mode 100644 platform/lang-api/src/com/intellij/ide/presentation/PresentationTemplate.java delete mode 100644 platform/lang-api/src/com/intellij/ide/presentation/PresentationTemplateImpl.java create mode 100644 platform/lang-impl/src/com/intellij/ide/TypePresentationServiceImpl.java diff --git a/platform/lang-api/src/com/intellij/ide/TypePresentationService.java b/platform/lang-api/src/com/intellij/ide/TypePresentationService.java index 4d25772a88f9..9d5410a797d0 100644 --- a/platform/lang-api/src/com/intellij/ide/TypePresentationService.java +++ b/platform/lang-api/src/com/intellij/ide/TypePresentationService.java @@ -1,115 +1,37 @@ +/* + * Copyright 2000-2011 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.intellij.ide; -import com.intellij.ide.presentation.Presentation; -import com.intellij.ide.presentation.PresentationTemplate; -import com.intellij.ide.presentation.PresentationTemplateImpl; -import com.intellij.openapi.extensions.Extensions; -import com.intellij.openapi.util.NullableLazyValue; -import com.intellij.util.containers.ConcurrentFactoryMap; -import com.intellij.util.containers.ContainerUtil; -import com.intellij.util.containers.FactoryMap; +import com.intellij.openapi.components.ServiceManager; import org.jetbrains.annotations.Nullable; import javax.swing.*; -import java.util.HashMap; -import java.util.LinkedHashSet; -import java.util.Map; -import java.util.Set; /** * @author peter */ -public class TypePresentationService { - - @Nullable - public Icon getTypeIcon(Class type) { - Set templates = mySuperClasses.get(type); - for (PresentationTemplate template : templates) { - Icon icon = template.getIcon(null, 0); - if (icon != null) return icon; - } - return null; - } - - @Nullable - public String getTypePresentableName(Class type) { - Set templates = mySuperClasses.get(type); - for (PresentationTemplate template : templates) { - String typeName = template.getTypeName(); - if (typeName != null) return typeName; - } - return null; - } +public abstract class TypePresentationService { public static TypePresentationService getService() { - return ourInstance; - } - - public TypePresentationService() { - for(TypeIconEP ep: Extensions.getExtensions(TypeIconEP.EP_NAME)) { - myIcons.put(ep.className, ep.getIcon()); - } - for(TypeNameEP ep: Extensions.getExtensions(TypeNameEP.EP_NAME)) { - myNames.put(ep.className, ep.getTypeName()); - } + return ServiceManager.getService(TypePresentationService.class); } @Nullable - private PresentationTemplate createPresentationTemplate(Class type) { - Presentation presentation = type.getAnnotation(Presentation.class); - if (presentation != null) { - return new PresentationTemplateImpl(presentation, type); - } - final NullableLazyValue icon = myIcons.get(type.getName()); - final NullableLazyValue typeName = myNames.get(type.getName()); - if (icon != null || typeName != null) { - return new PresentationTemplate() { - @Override - public Icon getIcon(Object o, int flags) { - return icon == null ? null : icon.getValue(); - } - - @Override - public String getName(Object o) { - return null; - } - - @Override - public String getTypeName() { - return typeName == null ? null : typeName.getValue(); - } - }; - } - return null; - } - - private static final TypePresentationService ourInstance = new TypePresentationService(); - - private final Map> myIcons = new HashMap>(); - private final Map> myNames = new HashMap>(); - @SuppressWarnings({"MismatchedQueryAndUpdateOfCollection"}) - private final FactoryMap> mySuperClasses = new ConcurrentFactoryMap>() { - @Override - protected Set create(Class key) { - LinkedHashSet templates = new LinkedHashSet(); - walkSupers(key, new LinkedHashSet(), templates); - return templates; - } - - private void walkSupers(Class aClass, Set classes, Set templates) { - if (!classes.add(aClass)) { - return; - } - ContainerUtil.addIfNotNull(createPresentationTemplate(aClass), templates); - final Class superClass = aClass.getSuperclass(); - if (superClass != null) { - walkSupers(superClass, classes, templates); - } - - for (Class intf : aClass.getInterfaces()) { - walkSupers(intf, classes, templates); - } - } - }; + public abstract Icon getTypeIcon(Class type); + @Nullable + public abstract String getTypePresentableName(Class type); } diff --git a/platform/lang-api/src/com/intellij/ide/presentation/PresentationTemplate.java b/platform/lang-api/src/com/intellij/ide/presentation/PresentationTemplate.java deleted file mode 100644 index e322db7416e8..000000000000 --- a/platform/lang-api/src/com/intellij/ide/presentation/PresentationTemplate.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2000-2011 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.intellij.ide.presentation; - -import org.jetbrains.annotations.Nullable; - -import javax.swing.*; - -/** - * @author Dmitry Avdeev - */ -public interface PresentationTemplate { - - @Nullable - Icon getIcon(Object o, int flags); - - @Nullable - String getName(Object o); - - @Nullable - String getTypeName(); -} diff --git a/platform/lang-api/src/com/intellij/ide/presentation/PresentationTemplateImpl.java b/platform/lang-api/src/com/intellij/ide/presentation/PresentationTemplateImpl.java deleted file mode 100644 index 8b656cd0d5dc..000000000000 --- a/platform/lang-api/src/com/intellij/ide/presentation/PresentationTemplateImpl.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright 2000-2011 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.intellij.ide.presentation; - -import com.intellij.openapi.util.IconLoader; -import com.intellij.openapi.util.NullableLazyValue; -import com.intellij.openapi.util.text.StringUtil; -import org.jetbrains.annotations.Nullable; - -import javax.swing.*; - -/** - * @author Dmitry Avdeev - */ -public class PresentationTemplateImpl implements PresentationTemplate { - - @Override - @Nullable - public Icon getIcon(Object o, int flags) { - PresentationIconProvider iconProvider = myIconProvider.getValue(); - return iconProvider == null ? myIcon.getValue() : iconProvider.getIcon(o, flags); - } - - @Override - @Nullable - public String getTypeName() { - return StringUtil.isEmpty(myPresentation.typeName()) ? null : myPresentation.typeName(); - } - - @Override - @Nullable - public String getName(Object o) { - PresentationNameProvider namer = myNameProvider.getValue(); - return namer == null ? null : namer.getName(o); - } - - public PresentationTemplateImpl(Presentation presentation, Class aClass) { - this.myPresentation = presentation; - myClass = aClass; - } - - private final Presentation myPresentation; - private final Class myClass; - - private final NullableLazyValue myIcon = new NullableLazyValue() { - @Override - protected Icon compute() { - if (StringUtil.isEmpty(myPresentation.icon())) return null; - return IconLoader.getIcon(myPresentation.icon(), myClass); - } - }; - - private final NullableLazyValue myNameProvider = new NullableLazyValue() { - @Override - protected PresentationNameProvider compute() { - Class aClass = myPresentation.nameProviderClass(); - - try { - return aClass == PresentationNameProvider.class ? null : aClass.newInstance(); - } - catch (Exception e) { - return null; - } - } - }; - - private final NullableLazyValue myIconProvider = new NullableLazyValue() { - @Override - protected PresentationIconProvider compute() { - Class aClass = myPresentation.iconProviderClass(); - - try { - return aClass == PresentationIconProvider.class ? null : aClass.newInstance(); - } - catch (Exception e) { - return null; - } - } - }; - -} diff --git a/platform/lang-impl/src/com/intellij/ide/TypePresentationServiceImpl.java b/platform/lang-impl/src/com/intellij/ide/TypePresentationServiceImpl.java new file mode 100644 index 000000000000..653fd222d2bb --- /dev/null +++ b/platform/lang-impl/src/com/intellij/ide/TypePresentationServiceImpl.java @@ -0,0 +1,206 @@ +/* + * Copyright 2000-2011 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.ide; + +import com.intellij.ide.presentation.*; +import com.intellij.openapi.extensions.Extensions; +import com.intellij.openapi.util.IconLoader; +import com.intellij.openapi.util.NullableLazyValue; +import com.intellij.openapi.util.text.StringUtil; +import com.intellij.util.containers.ConcurrentFactoryMap; +import com.intellij.util.containers.ContainerUtil; +import com.intellij.util.containers.FactoryMap; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.Map; +import java.util.Set; + +/** + * @author peter + */ +public class TypePresentationServiceImpl extends TypePresentationService { + + @Override@Nullable + public Icon getTypeIcon(Class type) { + Set templates = mySuperClasses.get(type); + for (PresentationTemplate template : templates) { + Icon icon = template.getIcon(null, 0); + if (icon != null) return icon; + } + return null; + } + + @Override@Nullable + public String getTypePresentableName(Class type) { + Set templates = mySuperClasses.get(type); + for (PresentationTemplate template : templates) { + String typeName = template.getTypeName(); + if (typeName != null) return typeName; + } + return null; + } + + public TypePresentationServiceImpl() { + for(TypeIconEP ep: Extensions.getExtensions(TypeIconEP.EP_NAME)) { + myIcons.put(ep.className, ep.getIcon()); + } + for(TypeNameEP ep: Extensions.getExtensions(TypeNameEP.EP_NAME)) { + myNames.put(ep.className, ep.getTypeName()); + } + } + + @Nullable + private PresentationTemplate createPresentationTemplate(Class type) { + Presentation presentation = type.getAnnotation(Presentation.class); + if (presentation != null) { + return new PresentationTemplateImpl(presentation, type); + } + final NullableLazyValue icon = myIcons.get(type.getName()); + final NullableLazyValue typeName = myNames.get(type.getName()); + if (icon != null || typeName != null) { + return new PresentationTemplate() { + @Override + public Icon getIcon(Object o, int flags) { + return icon == null ? null : icon.getValue(); + } + + @Override + public String getName(Object o) { + return null; + } + + @Override + public String getTypeName() { + return typeName == null ? null : typeName.getValue(); + } + }; + } + return null; + } + + private final Map> myIcons = new HashMap>(); + private final Map> myNames = new HashMap>(); + @SuppressWarnings({"MismatchedQueryAndUpdateOfCollection"}) + private final FactoryMap> mySuperClasses = new ConcurrentFactoryMap>() { + @Override + protected Set create(Class key) { + LinkedHashSet templates = new LinkedHashSet(); + walkSupers(key, new LinkedHashSet(), templates); + return templates; + } + + private void walkSupers(Class aClass, Set classes, Set templates) { + if (!classes.add(aClass)) { + return; + } + ContainerUtil.addIfNotNull(createPresentationTemplate(aClass), templates); + final Class superClass = aClass.getSuperclass(); + if (superClass != null) { + walkSupers(superClass, classes, templates); + } + + for (Class intf : aClass.getInterfaces()) { + walkSupers(intf, classes, templates); + } + } + }; + + /** + * @author Dmitry Avdeev + */ + public static class PresentationTemplateImpl implements PresentationTemplate { + + @Override + @Nullable + public Icon getIcon(Object o, int flags) { + PresentationIconProvider iconProvider = myIconProvider.getValue(); + return iconProvider == null ? myIcon.getValue() : iconProvider.getIcon(o, flags); + } + + @Override + @Nullable + public String getTypeName() { + return StringUtil.isEmpty(myPresentation.typeName()) ? null : myPresentation.typeName(); + } + + @Override + @Nullable + public String getName(Object o) { + PresentationNameProvider namer = myNameProvider.getValue(); + return namer == null ? null : namer.getName(o); + } + + public PresentationTemplateImpl(Presentation presentation, Class aClass) { + this.myPresentation = presentation; + myClass = aClass; + } + + private final Presentation myPresentation; + private final Class myClass; + + private final NullableLazyValue myIcon = new NullableLazyValue() { + @Override + protected Icon compute() { + if (StringUtil.isEmpty(myPresentation.icon())) return null; + return IconLoader.getIcon(myPresentation.icon(), myClass); + } + }; + + private final NullableLazyValue myNameProvider = new NullableLazyValue() { + @Override + protected PresentationNameProvider compute() { + Class aClass = myPresentation.nameProviderClass(); + + try { + return aClass == PresentationNameProvider.class ? null : aClass.newInstance(); + } + catch (Exception e) { + return null; + } + } + }; + + private final NullableLazyValue myIconProvider = new NullableLazyValue() { + @Override + protected PresentationIconProvider compute() { + Class aClass = myPresentation.iconProviderClass(); + + try { + return aClass == PresentationIconProvider.class ? null : aClass.newInstance(); + } + catch (Exception e) { + return null; + } + } + }; + + } + + interface PresentationTemplate { + + @Nullable + Icon getIcon(Object o, int flags); + + @Nullable + String getName(Object o); + + @Nullable + String getTypeName(); + } +} diff --git a/platform/platform-resources/src/META-INF/LangExtensions.xml b/platform/platform-resources/src/META-INF/LangExtensions.xml index b009446fd8ba..5fb1d1d608aa 100644 --- a/platform/platform-resources/src/META-INF/LangExtensions.xml +++ b/platform/platform-resources/src/META-INF/LangExtensions.xml @@ -28,6 +28,9 @@ + + aClass) { super(presentation, aClass);