mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
employ Disposer & cleanup
This commit is contained in:
@@ -18,7 +18,6 @@ package org.intellij.images.editor;
|
||||
import com.intellij.openapi.Disposable;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.openapi.vfs.VirtualFileListener;
|
||||
import org.intellij.images.ui.ImageComponentDecorator;
|
||||
|
||||
import javax.swing.*;
|
||||
@@ -28,7 +27,8 @@ import javax.swing.*;
|
||||
*
|
||||
* @author <a href="mailto:aefimov.box@gmail.com">Alexey Efimov</a>
|
||||
*/
|
||||
public interface ImageEditor extends Disposable, VirtualFileListener, ImageComponentDecorator {
|
||||
public interface ImageEditor extends Disposable, ImageComponentDecorator {
|
||||
|
||||
VirtualFile getFile();
|
||||
|
||||
Project getProject();
|
||||
|
||||
@@ -34,125 +34,133 @@ import javax.swing.*;
|
||||
*
|
||||
* @author <a href="mailto:aefimov.box@gmail.com">Alexey Efimov</a>
|
||||
*/
|
||||
final class ImageEditorImpl extends VirtualFileAdapter implements ImageEditor {
|
||||
private final Project project;
|
||||
private final VirtualFile file;
|
||||
private final ImageEditorUI editorUI;
|
||||
private boolean disposed;
|
||||
final class ImageEditorImpl implements ImageEditor {
|
||||
private final Project project;
|
||||
private final VirtualFile file;
|
||||
private final ImageEditorUI editorUI;
|
||||
private boolean disposed;
|
||||
|
||||
ImageEditorImpl(@NotNull Project project, @NotNull VirtualFile file) {
|
||||
this.project = project;
|
||||
this.file = file;
|
||||
ImageEditorImpl(@NotNull Project project, @NotNull VirtualFile file) {
|
||||
this.project = project;
|
||||
this.file = file;
|
||||
|
||||
editorUI = new ImageEditorUI(this);
|
||||
editorUI = new ImageEditorUI(this);
|
||||
Disposer.register(this, editorUI);
|
||||
|
||||
VirtualFileManager.getInstance().addVirtualFileListener(this);
|
||||
VirtualFileManager.getInstance().addVirtualFileListener(new VirtualFileAdapter() {
|
||||
@Override
|
||||
public void propertyChanged(@NotNull VirtualFilePropertyEvent event) {
|
||||
ImageEditorImpl.this.propertyChanged(event);
|
||||
}
|
||||
|
||||
setValue(file);
|
||||
@Override
|
||||
public void contentsChanged(@NotNull VirtualFileEvent event) {
|
||||
ImageEditorImpl.this.contentsChanged(event);
|
||||
}
|
||||
}, this);
|
||||
|
||||
setValue(file);
|
||||
}
|
||||
|
||||
private void setValue(VirtualFile file) {
|
||||
try {
|
||||
editorUI.setImage(IfsUtil.getImage(file), IfsUtil.getFormat(file));
|
||||
}
|
||||
catch (Exception e) {
|
||||
// Error loading image file
|
||||
editorUI.setImage(null, null);
|
||||
}
|
||||
}
|
||||
|
||||
private void setValue(VirtualFile file) {
|
||||
try {
|
||||
editorUI.setImage(IfsUtil.getImage(file), IfsUtil.getFormat(file));
|
||||
public boolean isValid() {
|
||||
ImageDocument document = editorUI.getImageComponent().getDocument();
|
||||
return document.getValue() != null;
|
||||
}
|
||||
|
||||
public JComponent getComponent() {
|
||||
return editorUI;
|
||||
}
|
||||
|
||||
public JComponent getContentComponent() {
|
||||
return editorUI.getImageComponent();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public VirtualFile getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Project getProject() {
|
||||
return project;
|
||||
}
|
||||
|
||||
public ImageDocument getDocument() {
|
||||
return editorUI.getImageComponent().getDocument();
|
||||
}
|
||||
|
||||
public void setTransparencyChessboardVisible(boolean visible) {
|
||||
editorUI.getImageComponent().setTransparencyChessboardVisible(visible);
|
||||
editorUI.repaint();
|
||||
}
|
||||
|
||||
public boolean isTransparencyChessboardVisible() {
|
||||
return editorUI.getImageComponent().isTransparencyChessboardVisible();
|
||||
}
|
||||
|
||||
public boolean isEnabledForActionPlace(String place) {
|
||||
// Disable for thumbnails action
|
||||
return !ThumbnailViewActions.ACTION_PLACE.equals(place);
|
||||
}
|
||||
|
||||
public void setGridVisible(boolean visible) {
|
||||
editorUI.getImageComponent().setGridVisible(visible);
|
||||
editorUI.repaint();
|
||||
}
|
||||
|
||||
public boolean isGridVisible() {
|
||||
return editorUI.getImageComponent().isGridVisible();
|
||||
}
|
||||
|
||||
public boolean isDisposed() {
|
||||
return disposed;
|
||||
}
|
||||
|
||||
public ImageZoomModel getZoomModel() {
|
||||
return editorUI.getZoomModel();
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
disposed = true;
|
||||
}
|
||||
|
||||
void propertyChanged(@NotNull VirtualFilePropertyEvent event) {
|
||||
if (file.equals(event.getFile())) {
|
||||
// Change document
|
||||
file.refresh(true, false, new Runnable() {
|
||||
public void run() {
|
||||
if (ImageFileTypeManager.getInstance().isImage(file)) {
|
||||
setValue(file);
|
||||
}
|
||||
else {
|
||||
setValue(null);
|
||||
// Close editor
|
||||
FileEditorManager editorManager = FileEditorManager.getInstance(project);
|
||||
editorManager.closeFile(file);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
// Error loading image file
|
||||
editorUI.setImage(null, null);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
ImageDocument document = editorUI.getImageComponent().getDocument();
|
||||
return document.getValue() != null;
|
||||
}
|
||||
|
||||
public JComponent getComponent() {
|
||||
return editorUI;
|
||||
}
|
||||
|
||||
public JComponent getContentComponent() {
|
||||
return editorUI.getImageComponent();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public VirtualFile getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Project getProject() {
|
||||
return project;
|
||||
}
|
||||
|
||||
public ImageDocument getDocument() {
|
||||
return editorUI.getImageComponent().getDocument();
|
||||
}
|
||||
|
||||
public void setTransparencyChessboardVisible(boolean visible) {
|
||||
editorUI.getImageComponent().setTransparencyChessboardVisible(visible);
|
||||
editorUI.repaint();
|
||||
}
|
||||
|
||||
public boolean isTransparencyChessboardVisible() {
|
||||
return editorUI.getImageComponent().isTransparencyChessboardVisible();
|
||||
}
|
||||
|
||||
public boolean isEnabledForActionPlace(String place) {
|
||||
// Disable for thumbnails action
|
||||
return !ThumbnailViewActions.ACTION_PLACE.equals(place);
|
||||
}
|
||||
|
||||
public void setGridVisible(boolean visible) {
|
||||
editorUI.getImageComponent().setGridVisible(visible);
|
||||
editorUI.repaint();
|
||||
}
|
||||
|
||||
public boolean isGridVisible() {
|
||||
return editorUI.getImageComponent().isGridVisible();
|
||||
}
|
||||
|
||||
public boolean isDisposed() {
|
||||
return disposed;
|
||||
}
|
||||
|
||||
public ImageZoomModel getZoomModel() {
|
||||
return editorUI.getZoomModel();
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
Disposer.dispose(editorUI);
|
||||
VirtualFileManager.getInstance().removeVirtualFileListener(this);
|
||||
disposed = true;
|
||||
}
|
||||
|
||||
public void propertyChanged(@NotNull VirtualFilePropertyEvent event) {
|
||||
super.propertyChanged(event);
|
||||
if (file.equals(event.getFile())) {
|
||||
// Change document
|
||||
file.refresh(true, false, new Runnable() {
|
||||
public void run() {
|
||||
if (ImageFileTypeManager.getInstance().isImage(file)) {
|
||||
setValue(file);
|
||||
} else {
|
||||
setValue(null);
|
||||
// Close editor
|
||||
FileEditorManager editorManager = FileEditorManager.getInstance(project);
|
||||
editorManager.closeFile(file);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public void contentsChanged(@NotNull VirtualFileEvent event) {
|
||||
super.contentsChanged(event);
|
||||
if (file.equals(event.getFile())) {
|
||||
// Change document
|
||||
file.refresh(true, false, new Runnable() {
|
||||
public void run() {
|
||||
setValue(file);
|
||||
}
|
||||
});
|
||||
void contentsChanged(@NotNull VirtualFileEvent event) {
|
||||
if (file.equals(event.getFile())) {
|
||||
// Change document
|
||||
file.refresh(true, false, new Runnable() {
|
||||
public void run() {
|
||||
setValue(file);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,9 +15,6 @@
|
||||
*/
|
||||
package org.intellij.images.editor.impl;
|
||||
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import org.intellij.images.editor.ImageEditor;
|
||||
import org.intellij.images.options.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -29,18 +26,7 @@ import java.awt.image.BufferedImage;
|
||||
* @author <a href="mailto:aefimov.box@gmail.com">Alexey Efimov</a>
|
||||
*/
|
||||
public final class ImageEditorManagerImpl {
|
||||
private ImageEditorManagerImpl() {}
|
||||
|
||||
/**
|
||||
* Create image viewer editor. Don't forget release editor by {@link #releaseImageEditor(ImageEditor)} method.
|
||||
*
|
||||
* @param project Project
|
||||
* @param file File
|
||||
* @return Image editor for file
|
||||
*/
|
||||
@NotNull
|
||||
public static ImageEditor createImageEditor(@NotNull Project project, @NotNull VirtualFile file) {
|
||||
return new ImageEditorImpl(project, file);
|
||||
private ImageEditorManagerImpl() {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -56,15 +42,4 @@ public final class ImageEditorManagerImpl {
|
||||
ui.setImage(image, null);
|
||||
return ui;
|
||||
}
|
||||
|
||||
/**
|
||||
* Release editor. Disposing caches and other resources allocated in creation.
|
||||
*
|
||||
* @param editor Editor to release.
|
||||
*/
|
||||
public static void releaseImageEditor(@NotNull ImageEditor editor) {
|
||||
if (!editor.isDisposed()) {
|
||||
editor.dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,14 +21,13 @@ import com.intellij.openapi.fileEditor.FileEditorLocation;
|
||||
import com.intellij.openapi.fileEditor.FileEditorState;
|
||||
import com.intellij.openapi.fileEditor.FileEditorStateLevel;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Disposer;
|
||||
import com.intellij.openapi.util.UserDataHolderBase;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.openapi.vfs.VirtualFileManager;
|
||||
import org.intellij.images.editor.ImageEditor;
|
||||
import org.intellij.images.editor.ImageFileEditor;
|
||||
import org.intellij.images.editor.ImageZoomModel;
|
||||
import org.intellij.images.options.*;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import javax.swing.*;
|
||||
@@ -40,97 +39,93 @@ import java.beans.PropertyChangeListener;
|
||||
* @author <a href="mailto:aefimov.box@gmail.com">Alexey Efimov</a>
|
||||
*/
|
||||
final class ImageFileEditorImpl extends UserDataHolderBase implements ImageFileEditor {
|
||||
@NonNls
|
||||
private static final String NAME = "ImageFileEditor";
|
||||
private final ImageEditor imageEditor;
|
||||
private static final String NAME = "ImageFileEditor";
|
||||
|
||||
ImageFileEditorImpl(@NotNull Project project, @NotNull VirtualFile file) {
|
||||
imageEditor = ImageEditorManagerImpl.createImageEditor(project, file);
|
||||
private final ImageEditor imageEditor;
|
||||
|
||||
// Append file listener
|
||||
VirtualFileManager.getInstance().addVirtualFileListener(imageEditor);
|
||||
ImageFileEditorImpl(@NotNull Project project, @NotNull VirtualFile file) {
|
||||
imageEditor = new ImageEditorImpl(project, file);
|
||||
Disposer.register(this, imageEditor);
|
||||
|
||||
// Set background and grid default options
|
||||
Options options = OptionsManager.getInstance().getOptions();
|
||||
EditorOptions editorOptions = options.getEditorOptions();
|
||||
GridOptions gridOptions = editorOptions.getGridOptions();
|
||||
TransparencyChessboardOptions transparencyChessboardOptions = editorOptions.getTransparencyChessboardOptions();
|
||||
imageEditor.setGridVisible(gridOptions.isShowDefault());
|
||||
imageEditor.setTransparencyChessboardVisible(transparencyChessboardOptions.isShowDefault());
|
||||
// Set background and grid default options
|
||||
Options options = OptionsManager.getInstance().getOptions();
|
||||
EditorOptions editorOptions = options.getEditorOptions();
|
||||
GridOptions gridOptions = editorOptions.getGridOptions();
|
||||
TransparencyChessboardOptions transparencyChessboardOptions = editorOptions.getTransparencyChessboardOptions();
|
||||
imageEditor.setGridVisible(gridOptions.isShowDefault());
|
||||
imageEditor.setTransparencyChessboardVisible(transparencyChessboardOptions.isShowDefault());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public JComponent getComponent() {
|
||||
return imageEditor.getComponent();
|
||||
}
|
||||
|
||||
public JComponent getPreferredFocusedComponent() {
|
||||
return imageEditor.getContentComponent();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return NAME;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public FileEditorState getState(@NotNull FileEditorStateLevel level) {
|
||||
ImageZoomModel zoomModel = imageEditor.getZoomModel();
|
||||
return new ImageFileEditorState(
|
||||
imageEditor.isTransparencyChessboardVisible(),
|
||||
imageEditor.isGridVisible(),
|
||||
zoomModel.getZoomFactor());
|
||||
}
|
||||
|
||||
public void setState(@NotNull FileEditorState state) {
|
||||
if (state instanceof ImageFileEditorState) {
|
||||
ImageFileEditorState editorState = (ImageFileEditorState)state;
|
||||
ImageZoomModel zoomModel = imageEditor.getZoomModel();
|
||||
imageEditor.setTransparencyChessboardVisible(editorState.isBackgroundVisible());
|
||||
imageEditor.setGridVisible(editorState.isGridVisible());
|
||||
zoomModel.setZoomFactor(editorState.getZoomFactor());
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public JComponent getComponent() {
|
||||
return imageEditor.getComponent();
|
||||
}
|
||||
public boolean isModified() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public JComponent getPreferredFocusedComponent() {
|
||||
return imageEditor.getContentComponent();
|
||||
}
|
||||
public boolean isValid() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return NAME;
|
||||
}
|
||||
public void selectNotify() {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public FileEditorState getState(@NotNull FileEditorStateLevel level) {
|
||||
ImageZoomModel zoomModel = imageEditor.getZoomModel();
|
||||
return new ImageFileEditorState(
|
||||
imageEditor.isTransparencyChessboardVisible(),
|
||||
imageEditor.isGridVisible(),
|
||||
zoomModel.getZoomFactor());
|
||||
}
|
||||
public void deselectNotify() {
|
||||
}
|
||||
|
||||
public void setState(@NotNull FileEditorState state) {
|
||||
if (state instanceof ImageFileEditorState) {
|
||||
ImageFileEditorState editorState = (ImageFileEditorState) state;
|
||||
ImageZoomModel zoomModel = imageEditor.getZoomModel();
|
||||
imageEditor.setTransparencyChessboardVisible(editorState.isBackgroundVisible());
|
||||
imageEditor.setGridVisible(editorState.isGridVisible());
|
||||
zoomModel.setZoomFactor(editorState.getZoomFactor());
|
||||
}
|
||||
}
|
||||
public void addPropertyChangeListener(@NotNull PropertyChangeListener listener) {
|
||||
}
|
||||
|
||||
public boolean isModified() {
|
||||
return false;
|
||||
}
|
||||
public void removePropertyChangeListener(@NotNull PropertyChangeListener listener) {
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
return true;
|
||||
}
|
||||
public BackgroundEditorHighlighter getBackgroundHighlighter() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void selectNotify() {
|
||||
}
|
||||
public FileEditorLocation getCurrentLocation() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void deselectNotify() {
|
||||
}
|
||||
public StructureViewBuilder getStructureViewBuilder() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void addPropertyChangeListener(@NotNull PropertyChangeListener listener) {
|
||||
}
|
||||
public void dispose() {
|
||||
}
|
||||
|
||||
public void removePropertyChangeListener(@NotNull PropertyChangeListener listener) {
|
||||
}
|
||||
|
||||
public BackgroundEditorHighlighter getBackgroundHighlighter() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public FileEditorLocation getCurrentLocation() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public StructureViewBuilder getStructureViewBuilder() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
VirtualFileManager.getInstance().removeVirtualFileListener(imageEditor);
|
||||
ImageEditorManagerImpl.releaseImageEditor(imageEditor);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public ImageEditor getImageEditor() {
|
||||
return imageEditor;
|
||||
}
|
||||
@NotNull
|
||||
public ImageEditor getImageEditor() {
|
||||
return imageEditor;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user