images plugin now part of a CE
[fedora-idea.git] / images / src / org / intellij / images / actions / EditExternalyAction.java
blob3399c400a95d28ebd87009c9944600d5cf8de23b
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.
17 /** $Id$ */
19 package org.intellij.images.actions;
21 import com.intellij.openapi.actionSystem.ActionPlaces;
22 import com.intellij.openapi.actionSystem.AnAction;
23 import com.intellij.openapi.actionSystem.AnActionEvent;
24 import com.intellij.openapi.actionSystem.PlatformDataKeys;
25 import com.intellij.openapi.project.Project;
26 import com.intellij.openapi.ui.Messages;
27 import com.intellij.openapi.util.SystemInfo;
28 import com.intellij.openapi.util.io.FileUtil;
29 import com.intellij.openapi.util.text.StringUtil;
30 import com.intellij.openapi.vfs.VfsUtil;
31 import com.intellij.openapi.vfs.VirtualFile;
32 import com.intellij.util.EnvironmentUtil;
33 import org.intellij.images.ImagesBundle;
34 import org.intellij.images.fileTypes.ImageFileTypeManager;
35 import org.intellij.images.options.Options;
36 import org.intellij.images.options.OptionsManager;
37 import org.intellij.images.options.impl.OptionsConfigurabe;
39 import java.io.File;
40 import java.io.IOException;
41 import java.util.Map;
42 import java.util.Set;
44 /**
45 * Open image file externaly.
47 * @author <a href="mailto:aefimov.box@gmail.com">Alexey Efimov</a>
49 public final class EditExternalyAction extends AnAction {
50 public void actionPerformed(AnActionEvent e) {
51 Project project = e.getData(PlatformDataKeys.PROJECT);
52 VirtualFile[] files = e.getData(PlatformDataKeys.VIRTUAL_FILE_ARRAY);
53 Options options = OptionsManager.getInstance().getOptions();
54 String executablePath = options.getExternalEditorOptions().getExecutablePath();
55 if (StringUtil.isEmpty(executablePath)) {
56 Messages.showErrorDialog(project,
57 ImagesBundle.message("error.empty.external.editor.path"),
58 ImagesBundle.message("error.title.empty.external.editor.path"));
59 OptionsConfigurabe.show(project);
60 } else {
61 if (files != null) {
62 Map<String, String> env = EnvironmentUtil.getEnviromentProperties();
63 Set<String> varNames = env.keySet();
64 for (String varName : varNames) {
65 if (SystemInfo.isWindows) {
66 executablePath = StringUtil.replace(executablePath, "%" + varName + "%", env.get(varName), true);
67 } else {
68 executablePath = StringUtil.replace(executablePath, "${" + varName + "}", env.get(varName), false);
71 executablePath = FileUtil.toSystemDependentName(executablePath);
72 File executable = new File(executablePath);
73 StringBuffer commandLine = new StringBuffer(executable.exists() ? executable.getAbsolutePath() : executablePath);
74 ImageFileTypeManager typeManager = ImageFileTypeManager.getInstance();
75 for (VirtualFile file : files) {
76 if (file.isInLocalFileSystem() && typeManager.isImage(file)) {
77 commandLine.append(" \"");
78 commandLine.append(VfsUtil.virtualToIoFile(file).getAbsolutePath());
79 commandLine.append('\"');
83 try {
84 File executableFile = new File(executablePath);
85 Runtime.getRuntime().exec(commandLine.toString(), null, executableFile.getParentFile());
86 } catch (IOException ex) {
87 Messages.showErrorDialog(project,
88 ex.getLocalizedMessage(),
89 ImagesBundle.message("error.title.launching.external.editor"));
90 OptionsConfigurabe.show(project);
96 public void update(AnActionEvent e) {
97 super.update(e);
99 VirtualFile[] files = e.getData(PlatformDataKeys.VIRTUAL_FILE_ARRAY);
100 final boolean isEnabled = isImages(files);
101 if (e.getPlace().equals(ActionPlaces.PROJECT_VIEW_POPUP)) {
102 e.getPresentation().setVisible(isEnabled);
104 else {
105 e.getPresentation().setEnabled(isEnabled);
109 private static boolean isImages(VirtualFile[] files) {
110 boolean isImagesFound = false;
111 if (files != null) {
112 ImageFileTypeManager typeManager = ImageFileTypeManager.getInstance();
113 for (VirtualFile file : files) {
114 boolean isImage = typeManager.isImage(file);
115 isImagesFound |= isImage;
116 if (!file.isInLocalFileSystem() || !isImage) {
117 return false;
121 return isImagesFound;