blob: 4c9b79429d75bdfe30597b1ed1d63d3af2c3fbc6 [file] [log] [blame]
/*
* Copyright (c) 2011 Google Inc.
*
* All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse
* Public License v1.0 which accompanies this distribution, and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/
package com.google.eclipse.protobuf.ui.labeling;
import static com.google.common.collect.Maps.newHashMap;
import static com.google.eclipse.protobuf.protobuf.Modifier.*;
import static java.util.Arrays.asList;
import java.util.*;
import org.eclipse.xtext.Keyword;
import com.google.eclipse.protobuf.protobuf.*;
import com.google.eclipse.protobuf.protobuf.Enum;
import com.google.eclipse.protobuf.protobuf.Package;
import com.google.inject.Singleton;
/**
* Registry of all images used in the 'Protocol Buffer' editor.
*
* @author alruiz@google.com (Alex Ruiz)
*/
@Singleton public class Images {
private static final String GIF_EXTENSION = ".gif";
private static final String DEFAULT_IMAGE = "empty.gif";
private static final Map<Modifier, String> IMAGES_BY_MODIFIER = newHashMap();
static {
IMAGES_BY_MODIFIER.put(OPTIONAL, "field-opt.gif");
IMAGES_BY_MODIFIER.put(REPEATED, "field-rep.gif");
IMAGES_BY_MODIFIER.put(REQUIRED, "field-req.gif");
}
private static final Map<Class<?>, String> IMAGES_BY_TYPE = newHashMap();
static {
IMAGES_BY_TYPE.put(Enum.class, "enum.gif");
IMAGES_BY_TYPE.put(TypeExtension.class, "extend.gif");
IMAGES_BY_TYPE.put(Extensions.class, "extensions.gif");
IMAGES_BY_TYPE.put(Group.class, "group.gif");
IMAGES_BY_TYPE.put(Import.class, "import.gif");
IMAGES_BY_TYPE.put(Literal.class, "literal.gif");
IMAGES_BY_TYPE.put(Message.class, "message.gif");
IMAGES_BY_TYPE.put(Option.class, "option.gif");
IMAGES_BY_TYPE.put(Package.class, "package.gif");
IMAGES_BY_TYPE.put(Rpc.class, "rpc.gif");
IMAGES_BY_TYPE.put(Service.class, "service.gif");
IMAGES_BY_TYPE.put(Syntax.class, "syntax.gif");
}
private static final List<String> STANDALONE_IMAGES = asList("extensions.gif", "imports.gif", "options.gif");
public String imageFor(Object o) {
if (o instanceof Keyword) {
Keyword k = (Keyword) o;
return imageFor(k);
}
if (o instanceof MessageField) {
MessageField f = (MessageField) o;
return imageFor(f.getModifier());
}
if (o instanceof String) {
return o + GIF_EXTENSION;
}
if (o instanceof Option) {
return imageFor(Option.class);
}
return imageFor(o.getClass());
}
public String imageFor(Class<?> type) {
String image = IMAGES_BY_TYPE.get(type);
if (image != null) {
return image;
}
Class<?>[] interfaces = type.getInterfaces();
if (interfaces == null || interfaces.length == 0) {
return DEFAULT_IMAGE;
}
return imageFor(interfaces[0]);
}
private String imageFor(Keyword keyword) {
String value = keyword.getValue();
Modifier m = Modifier.getByName(value);
String image = IMAGES_BY_MODIFIER.get(m);
if (image != null) {
return image;
}
String imageName = value + GIF_EXTENSION;
if (IMAGES_BY_TYPE.containsValue(imageName) || STANDALONE_IMAGES.contains(imageName)) {
return imageName;
}
return DEFAULT_IMAGE;
}
private String imageFor(Modifier modifier) {
String image = IMAGES_BY_MODIFIER.get(modifier);
if (image != null) {
return image;
}
return "field.gif";
}
public String defaultImage() {
return DEFAULT_IMAGE;
}
}