images plugin now part of a CE
[fedora-idea.git] / images / src / org / intellij / images / fileTypes / ImageDocumentationProvider.java
blob99661283b508dd4e78baf952284b4bbe09dafddd
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.intellij.images.fileTypes;
18 import com.intellij.lang.documentation.QuickDocumentationProvider;
19 import com.intellij.openapi.util.SystemInfo;
20 import com.intellij.openapi.vfs.VirtualFile;
21 import com.intellij.openapi.vfs.VirtualFileWithId;
22 import com.intellij.psi.PsiElement;
23 import com.intellij.psi.PsiFileSystemItem;
24 import com.intellij.util.indexing.FileBasedIndex;
25 import org.intellij.images.index.ImageInfoIndex;
27 import java.net.URI;
28 import java.net.URISyntaxException;
30 /**
31 * @author spleaner
33 public class ImageDocumentationProvider extends QuickDocumentationProvider {
34 private static final int MAX_IMAGE_SIZE = 300;
36 public String getQuickNavigateInfo(PsiElement element) {
37 return null;
40 @Override
41 public String generateDoc(PsiElement element, PsiElement originalElement) {
42 final String[] result = new String[] {null};
44 if (element instanceof PsiFileSystemItem && !((PsiFileSystemItem)element).isDirectory()) {
45 final VirtualFile file = ((PsiFileSystemItem)element).getVirtualFile();
46 if (file instanceof VirtualFileWithId) {
47 ImageInfoIndex.processValues(file, new FileBasedIndex.ValueProcessor<ImageInfoIndex.ImageInfo>() {
48 public boolean process(VirtualFile file, ImageInfoIndex.ImageInfo value) {
49 int imageWidth = value.width;
50 int imageHeight = value.height;
52 int maxSize = Math.max(value.width, value.height);
53 if (maxSize > MAX_IMAGE_SIZE) {
54 double scaleFactor = (double)MAX_IMAGE_SIZE / (double)maxSize;
55 imageWidth *= scaleFactor;
56 imageHeight *= scaleFactor;
58 try {
59 String path = file.getPath();
60 if (SystemInfo.isWindows) {
61 path = "/" + path;
63 final String url = new URI("file", null, path, null).toString();
64 result[0] = String.format("<html><body><img src=\"%s\" width=\"%s\" height=\"%s\"><p>%sx%s, %sbpp</p><body></html>", url, imageWidth,
65 imageHeight, value.width, value.height, value.bpp);
67 catch (URISyntaxException e) {
68 // nothing
70 return true;
72 }, element.getProject());
76 return result[0];