Revert "Debugger: Image/Icon viewer based on standard image viewer"
[idea/community.git] / images / src / org / intellij / images / editor / impl / ImageEditorImpl.java
1 /*
2  * Copyright 2004-2005 Alexey Efimov
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.intellij.images.editor.impl;
17
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.options.*;
26 import org.intellij.images.thumbnail.actionSystem.ThumbnailViewActions;
27 import org.intellij.images.ui.ImageComponent;
28 import org.intellij.images.vfs.IfsUtil;
29 import org.jetbrains.annotations.NotNull;
30
31 import javax.swing.*;
32 import java.awt.*;
33 import java.awt.image.BufferedImage;
34 import java.beans.PropertyChangeEvent;
35 import java.beans.PropertyChangeListener;
36
37 /**
38  * Image viewer implementation.
39  *
40  * @author <a href="mailto:aefimov.box@gmail.com">Alexey Efimov</a>
41  */
42 final class ImageEditorImpl extends VirtualFileAdapter implements ImageEditor {
43     private final PropertyChangeListener optionsChangeListener = new OptionsChangeListener();
44     private final Project project;
45     private final VirtualFile file;
46     private final ImageEditorUI editorUI;
47     private boolean disposed;
48
49     ImageEditorImpl(@NotNull Project project, @NotNull VirtualFile file) {
50         this.project = project;
51         this.file = file;
52
53         // Options
54         Options options = OptionsManager.getInstance().getOptions();
55         editorUI = new ImageEditorUI(this, options.getEditorOptions());
56         options.addPropertyChangeListener(optionsChangeListener);
57
58         VirtualFileManager.getInstance().addVirtualFileListener(this);
59
60         setValue(file);
61     }
62
63     private void setValue(VirtualFile file) {
64         ImageDocument document = editorUI.getImageComponent().getDocument();
65         try {
66             BufferedImage previousImage = document.getValue();
67             BufferedImage image = IfsUtil.getImage(file);
68             document.setValue(image);
69             document.setFormat(IfsUtil.getFormat(file));
70             ImageZoomModel zoomModel = getZoomModel();
71             if (image != null && (previousImage == null || !zoomModel.isZoomLevelChanged())) {
72                 // Set smart zooming behaviour on open
73                 Options options = OptionsManager.getInstance().getOptions();
74                 ZoomOptions zoomOptions = options.getEditorOptions().getZoomOptions();
75                 // Open as actual size
76                 zoomModel.setZoomFactor(1.0d);
77
78                 if (zoomOptions.isSmartZooming()) {
79                     Dimension prefferedSize = zoomOptions.getPrefferedSize();
80                     if (prefferedSize.width > image.getWidth() && prefferedSize.height > image.getHeight()) {
81                         // Resize to preffered size
82                         // Calculate zoom factor
83
84                         double factor = (prefferedSize.getWidth() / (double) image.getWidth() + prefferedSize.getHeight() / (double) image.getHeight()) / 2.0d;
85                         zoomModel.setZoomFactor(Math.ceil(factor));
86                     }
87                 }
88             }
89         } catch (Exception e) {
90             // Error loading image file
91             document.setValue(null);
92         }
93     }
94
95     public boolean isValid() {
96         ImageDocument document = editorUI.getImageComponent().getDocument();
97         return document.getValue() != null;
98     }
99
100     public JComponent getComponent() {
101         return editorUI;
102     }
103
104     public JComponent getContentComponent() {
105         return editorUI.getImageComponent();
106     }
107
108     @NotNull
109     public VirtualFile getFile() {
110         return file;
111     }
112
113     @NotNull
114     public Project getProject() {
115         return project;
116     }
117
118     public ImageDocument getDocument() {
119         return editorUI.getImageComponent().getDocument();
120     }
121
122     public void setTransparencyChessboardVisible(boolean visible) {
123         editorUI.getImageComponent().setTransparencyChessboardVisible(visible);
124         editorUI.repaint();
125     }
126
127     public boolean isTransparencyChessboardVisible() {
128         return editorUI.getImageComponent().isTransparencyChessboardVisible();
129     }
130
131     public boolean isEnabledForActionPlace(String place) {
132         // Disable for thumbnails action
133         return !ThumbnailViewActions.ACTION_PLACE.equals(place);
134     }
135
136     public void setGridVisible(boolean visible) {
137         editorUI.getImageComponent().setGridVisible(visible);
138         editorUI.repaint();
139     }
140
141     public boolean isGridVisible() {
142         return editorUI.getImageComponent().isGridVisible();
143     }
144
145     public boolean isDisposed() {
146         return disposed;
147     }
148
149     public ImageZoomModel getZoomModel() {
150         return editorUI.getZoomModel();
151     }
152
153     public void dispose() {
154         Options options = OptionsManager.getInstance().getOptions();
155         options.removePropertyChangeListener(optionsChangeListener);
156         editorUI.dispose();
157         VirtualFileManager.getInstance().removeVirtualFileListener(this);
158         disposed = true;
159     }
160
161     public void propertyChanged(@NotNull VirtualFilePropertyEvent event) {
162         super.propertyChanged(event);
163         if (file.equals(event.getFile())) {
164             // Change document
165             file.refresh(true, false, new Runnable() {
166                 public void run() {
167                     if (ImageFileTypeManager.getInstance().isImage(file)) {
168                         setValue(file);
169                     } else {
170                         setValue(null);
171                         // Close editor
172                         FileEditorManager editorManager = FileEditorManager.getInstance(project);
173                         editorManager.closeFile(file);
174                     }
175                 }
176             });
177         }
178     }
179
180     public void contentsChanged(@NotNull VirtualFileEvent event) {
181         super.contentsChanged(event);
182         if (file.equals(event.getFile())) {
183             // Change document
184             file.refresh(true, false, new Runnable() {
185                 public void run() {
186                     setValue(file);
187                 }
188             });
189         }
190     }
191
192     private class OptionsChangeListener implements PropertyChangeListener {
193         public void propertyChange(PropertyChangeEvent evt) {
194             Options options = (Options) evt.getSource();
195             EditorOptions editorOptions = options.getEditorOptions();
196             TransparencyChessboardOptions chessboardOptions = editorOptions.getTransparencyChessboardOptions();
197             GridOptions gridOptions = editorOptions.getGridOptions();
198
199             ImageComponent imageComponent = editorUI.getImageComponent();
200             imageComponent.setTransparencyChessboardCellSize(chessboardOptions.getCellSize());
201             imageComponent.setTransparencyChessboardWhiteColor(chessboardOptions.getWhiteColor());
202             imageComponent.setTransparencyChessboardBlankColor(chessboardOptions.getBlackColor());
203             imageComponent.setGridLineZoomFactor(gridOptions.getLineZoomFactor());
204             imageComponent.setGridLineSpan(gridOptions.getLineSpan());
205             imageComponent.setGridLineColor(gridOptions.getLineColor());
206         }
207     }
208 }