2 * Copyright 2004-2005 Alexey Efimov
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.editor.impl;
18 import com.intellij.openapi.fileEditor.FileEditorManager;
19 import com.intellij.openapi.project.Project;
20 import com.intellij.openapi.vfs.*;
21 import org.intellij.images.editor.ImageDocument;
22 import org.intellij.images.editor.ImageEditor;
23 import org.intellij.images.editor.ImageZoomModel;
24 import org.intellij.images.fileTypes.ImageFileTypeManager;
25 import org.intellij.images.thumbnail.actionSystem.ThumbnailViewActions;
26 import org.intellij.images.vfs.IfsUtil;
27 import org.jetbrains.annotations.NotNull;
32 * Image viewer implementation.
34 * @author <a href="mailto:aefimov.box@gmail.com">Alexey Efimov</a>
36 final class ImageEditorImpl extends VirtualFileAdapter implements ImageEditor {
37 private final Project project;
38 private final VirtualFile file;
39 private final ImageEditorUI editorUI;
40 private boolean disposed;
42 ImageEditorImpl(@NotNull Project project, @NotNull VirtualFile file) {
43 this.project = project;
46 editorUI = new ImageEditorUI(this);
48 VirtualFileManager.getInstance().addVirtualFileListener(this);
53 private void setValue(VirtualFile file) {
55 editorUI.setImage(IfsUtil.getImage(file), IfsUtil.getFormat(file));
58 // Error loading image file
59 editorUI.setImage(null, null);
63 public boolean isValid() {
64 ImageDocument document = editorUI.getImageComponent().getDocument();
65 return document.getValue() != null;
68 public JComponent getComponent() {
72 public JComponent getContentComponent() {
73 return editorUI.getImageComponent();
77 public VirtualFile getFile() {
82 public Project getProject() {
86 public ImageDocument getDocument() {
87 return editorUI.getImageComponent().getDocument();
90 public void setTransparencyChessboardVisible(boolean visible) {
91 editorUI.getImageComponent().setTransparencyChessboardVisible(visible);
95 public boolean isTransparencyChessboardVisible() {
96 return editorUI.getImageComponent().isTransparencyChessboardVisible();
99 public boolean isEnabledForActionPlace(String place) {
100 // Disable for thumbnails action
101 return !ThumbnailViewActions.ACTION_PLACE.equals(place);
104 public void setGridVisible(boolean visible) {
105 editorUI.getImageComponent().setGridVisible(visible);
109 public boolean isGridVisible() {
110 return editorUI.getImageComponent().isGridVisible();
113 public boolean isDisposed() {
117 public ImageZoomModel getZoomModel() {
118 return editorUI.getZoomModel();
121 public void dispose() {
123 VirtualFileManager.getInstance().removeVirtualFileListener(this);
127 public void propertyChanged(@NotNull VirtualFilePropertyEvent event) {
128 super.propertyChanged(event);
129 if (file.equals(event.getFile())) {
131 file.refresh(true, false, new Runnable() {
133 if (ImageFileTypeManager.getInstance().isImage(file)) {
138 FileEditorManager editorManager = FileEditorManager.getInstance(project);
139 editorManager.closeFile(file);
146 public void contentsChanged(@NotNull VirtualFileEvent event) {
147 super.contentsChanged(event);
148 if (file.equals(event.getFile())) {
150 file.refresh(true, false, new Runnable() {