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.
19 package org
.intellij
.images
.ui
;
21 import org
.intellij
.images
.editor
.ImageDocument
;
22 import org
.intellij
.images
.options
.GridOptions
;
23 import org
.intellij
.images
.options
.TransparencyChessboardOptions
;
24 import org
.jetbrains
.annotations
.NonNls
;
27 import javax
.swing
.event
.ChangeEvent
;
28 import javax
.swing
.event
.ChangeListener
;
30 import java
.awt
.image
.BufferedImage
;
31 import java
.util
.HashSet
;
35 * Image component is draw image box with effects.
37 * @author <a href="mailto:aefimov.box@gmail.com">Alexey Efimov</a>
39 public class ImageComponent
extends JComponent
{
41 private static final String TRANSPARENCY_CHESSBOARD_CELL_SIZE_PROP
= "TransparencyChessboard.cellSize";
43 private static final String TRANSPARENCY_CHESSBOARD_WHITE_COLOR_PROP
= "TransparencyChessboard.whiteColor";
45 private static final String TRANSPARENCY_CHESSBOARD_BLACK_COLOR_PROP
= "TransparencyChessboard.blackColor";
47 private static final String TRANSPARENCY_CHESSBOARD_VISIBLE_PROP
= "TransparencyChessboard.visible";
49 private static final String GRID_LINE_ZOOM_FACTOR_PROP
= "Grid.lineZoomFactor";
51 private static final String GRID_LINE_SPAN_PROP
= "Grid.lineSpan";
53 private static final String GRID_LINE_COLOR_PROP
= "Grid.lineColor";
55 private static final String GRID_VISIBLE_PROP
= "Grid.visible";
62 private static final String uiClassID
= "ImageComponentUI";
65 UIManager
.getDefaults().put(uiClassID
, ImageComponentUI
.class.getName());
68 private final ImageDocument document
= new ImageDocumentImpl();
69 private final Grid grid
= new Grid();
70 private final Chessboard chessboard
= new Chessboard();
72 public ImageComponent() {
76 public ImageDocument
getDocument() {
80 public void setTransparencyChessboardCellSize(int cellSize
) {
81 int oldValue
= chessboard
.getCellSize();
82 if (oldValue
!= cellSize
) {
83 chessboard
.setCellSize(cellSize
);
84 firePropertyChange(TRANSPARENCY_CHESSBOARD_CELL_SIZE_PROP
, oldValue
, cellSize
);
88 public void setTransparencyChessboardWhiteColor(Color color
) {
89 Color oldValue
= chessboard
.getWhiteColor();
90 if (oldValue
!= null && !oldValue
.equals(color
) || oldValue
== null && color
!= null) {
91 chessboard
.setWhiteColor(color
);
92 firePropertyChange(TRANSPARENCY_CHESSBOARD_WHITE_COLOR_PROP
, oldValue
, color
);
96 public void setTransparencyChessboardBlankColor(Color color
) {
97 Color oldValue
= chessboard
.getBlackColor();
98 if (oldValue
!= null && !oldValue
.equals(color
) || oldValue
== null && color
!= null) {
99 chessboard
.setBlackColor(color
);
100 firePropertyChange(TRANSPARENCY_CHESSBOARD_BLACK_COLOR_PROP
, oldValue
, color
);
104 public void setTransparencyChessboardVisible(boolean visible
) {
105 boolean oldValue
= chessboard
.isVisible();
106 if (oldValue
!= visible
) {
107 chessboard
.setVisible(visible
);
108 firePropertyChange(TRANSPARENCY_CHESSBOARD_VISIBLE_PROP
, oldValue
, visible
);
112 public int getTransparencyChessboardCellSize() {
113 return chessboard
.getCellSize();
116 public Color
getTransparencyChessboardWhiteColor() {
117 return chessboard
.getWhiteColor();
120 public Color
getTransparencyChessboardBlackColor() {
121 return chessboard
.getBlackColor();
124 public boolean isTransparencyChessboardVisible() {
125 return chessboard
.isVisible();
128 public void setGridLineZoomFactor(int lineZoomFactor
) {
129 int oldValue
= grid
.getLineZoomFactor();
130 if (oldValue
!= lineZoomFactor
) {
131 grid
.setLineZoomFactor(lineZoomFactor
);
132 firePropertyChange(GRID_LINE_ZOOM_FACTOR_PROP
, oldValue
, lineZoomFactor
);
136 public void setGridLineSpan(int lineSpan
) {
137 int oldValue
= grid
.getLineSpan();
138 if (oldValue
!= lineSpan
) {
139 grid
.setLineSpan(lineSpan
);
140 firePropertyChange(GRID_LINE_SPAN_PROP
, oldValue
, lineSpan
);
144 public void setGridLineColor(Color color
) {
145 Color oldValue
= grid
.getLineColor();
146 if (oldValue
!= null && !oldValue
.equals(color
) || oldValue
== null && color
!= null) {
147 grid
.setLineColor(color
);
148 firePropertyChange(GRID_LINE_COLOR_PROP
, oldValue
, color
);
152 public void setGridVisible(boolean visible
) {
153 boolean oldValue
= grid
.isVisible();
154 if (oldValue
!= visible
) {
155 grid
.setVisible(visible
);
156 firePropertyChange(GRID_VISIBLE_PROP
, oldValue
, visible
);
160 public int getGridLineZoomFactor() {
161 return grid
.getLineZoomFactor();
164 public int getGridLineSpan() {
165 return grid
.getLineSpan();
168 public Color
getGridLineColor() {
169 return grid
.getLineColor();
172 public boolean isGridVisible() {
173 return grid
.isVisible();
176 public void setCanvasSize(int width
, int height
) {
177 setSize(width
+ 4, height
+ 4);
180 public void setCanvasSize(Dimension dimension
) {
181 setCanvasSize(dimension
.width
, dimension
.height
);
184 public Dimension
getCanvasSize() {
185 Dimension size
= getSize();
186 return new Dimension(size
.width
- 4, size
.height
- 4);
189 public String
getUIClassID() {
193 public void updateUI() {
194 setUI(UIManager
.getUI(this));
197 private static final class ImageDocumentImpl
implements ImageDocument
{
198 private final Set
<ChangeListener
> listeners
= new HashSet
<ChangeListener
>(0);
199 private BufferedImage image
;
200 private String format
;
201 private Image renderer
;
203 public Image
getRenderer() {
207 public BufferedImage
getValue() {
211 public void setValue(BufferedImage image
) {
213 this.renderer
= image
!= null ? Toolkit
.getDefaultToolkit().createImage(image
.getSource()) : null;
214 fireChangeEvent(new ChangeEvent(this));
217 public String
getFormat() {
222 public void setFormat(String format
) {
223 this.format
= format
;
224 fireChangeEvent(new ChangeEvent(this));
227 private void fireChangeEvent(ChangeEvent e
) {
228 for (ChangeListener listener
: listeners
) {
229 listener
.stateChanged(e
);
233 public void addChangeListener(ChangeListener listener
) {
234 listeners
.add(listener
);
237 public void removeChangeListener(ChangeListener listener
) {
238 listeners
.remove(listener
);
242 private static final class Chessboard
{
243 private int cellSize
= TransparencyChessboardOptions
.DEFAULT_CELL_SIZE
;
244 private Color whiteColor
= TransparencyChessboardOptions
.DEFAULT_WHITE_COLOR
;
245 private Color blackColor
= TransparencyChessboardOptions
.DEFAULT_BLACK_COLOR
;
246 private boolean visible
= false;
248 public int getCellSize() {
252 public void setCellSize(int cellSize
) {
253 this.cellSize
= cellSize
;
256 public Color
getWhiteColor() {
260 public void setWhiteColor(Color whiteColor
) {
261 this.whiteColor
= whiteColor
;
264 public Color
getBlackColor() {
268 public void setBlackColor(Color blackColor
) {
269 this.blackColor
= blackColor
;
272 public boolean isVisible() {
276 public void setVisible(boolean visible
) {
277 this.visible
= visible
;
281 private static final class Grid
{
282 private int lineZoomFactor
= GridOptions
.DEFAULT_LINE_ZOOM_FACTOR
;
283 private int lineSpan
= GridOptions
.DEFAULT_LINE_SPAN
;
284 private Color lineColor
= GridOptions
.DEFAULT_LINE_COLOR
;
285 private boolean visible
= false;
287 public int getLineZoomFactor() {
288 return lineZoomFactor
;
291 public void setLineZoomFactor(int lineZoomFactor
) {
292 this.lineZoomFactor
= lineZoomFactor
;
295 public int getLineSpan() {
299 public void setLineSpan(int lineSpan
) {
300 this.lineSpan
= lineSpan
;
303 public Color
getLineColor() {
307 public void setLineColor(Color lineColor
) {
308 this.lineColor
= lineColor
;
311 public boolean isVisible() {
315 public void setVisible(boolean visible
) {
316 this.visible
= visible
;