| 1 | // Jomic - a viewer for comic book archives. |
| 2 | // Copyright (C) 2004-2011 Thomas Aglassinger |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | package net.sf.jomic.tools; |
| 17 | |
| 18 | import java.awt.Color; |
| 19 | |
| 20 | import javax.swing.UIManager; |
| 21 | |
| 22 | import org.apache.commons.logging.Log; |
| 23 | import org.apache.commons.logging.LogFactory; |
| 24 | |
| 25 | /** |
| 26 | * Settings to specify how to render an image depending on user preferences and window size. |
| 27 | * |
| 28 | * @author Thomas Aglassinger |
| 29 | */ |
| 30 | public class ImageRenderSettings extends BasicSettings |
| 31 | { |
| 32 | public static final String DEFAULT_FILL_COLOR = "#ffffff"; |
| 33 | public static final int DEFAULT_ROTATION = ImageTools.ROTATE_NONE; |
| 34 | public static final String DEFAULT_SCALE_MODE = ImageTools.SCALE_FIT; |
| 35 | |
| 36 | /** |
| 37 | * Color to use when filling the border in case two pages do not have a similar aspect ratio |
| 38 | * (specified using the HTML-like "#rrggbb" format). |
| 39 | */ |
| 40 | public static final String FILL_COLOR = "fillColor"; |
| 41 | public static final int MAX_ROTATION = 2; |
| 42 | public static final int MIN_ROTATION = -1; |
| 43 | public static final String ROTATION = "rotation"; |
| 44 | public static final String SCALE_MODE = "scaleMode"; |
| 45 | |
| 46 | /** |
| 47 | * Color use when selection an image or a part of it. |
| 48 | */ |
| 49 | public static final String SELECTION_COLOR = "selectionColor"; |
| 50 | |
| 51 | private ImageTools imageTools; |
| 52 | private Log logger; |
| 53 | private StringTools stringTools; |
| 54 | |
| 55 | public ImageRenderSettings() { |
| 56 | logger = LogFactory.getLog(ImageRenderSettings.class); |
| 57 | imageTools = ImageTools.instance(); |
| 58 | stringTools = StringTools.instance(); |
| 59 | setDefault(FILL_COLOR, DEFAULT_FILL_COLOR); |
| 60 | setDefault(ROTATION, DEFAULT_ROTATION); |
| 61 | setDefault(SCALE_MODE, DEFAULT_SCALE_MODE); |
| 62 | setDefault(SELECTION_COLOR, |
| 63 | stringTools.colorString(UIManager.getColor("TextArea.selectionBackground"))); |
| 64 | } |
| 65 | |
| 66 | public void setFillColor(Color newColor) { |
| 67 | assert newColor != null; |
| 68 | setColorProperty(FILL_COLOR, newColor); |
| 69 | } |
| 70 | |
| 71 | public void setRotation(int newRotation) { |
| 72 | setLimitedIntProperty(ROTATION, newRotation, MIN_ROTATION, MAX_ROTATION); |
| 73 | } |
| 74 | |
| 75 | public void setScaleMode(String mode) { |
| 76 | if (imageTools.isValidScaleMode(mode)) { |
| 77 | setProperty(SCALE_MODE, mode); |
| 78 | } else { |
| 79 | throw new IllegalArgumentException("scaleMode=" + mode); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | public void setSelectionColor(Color newColor) { |
| 84 | assert newColor != null; |
| 85 | setColorProperty(SELECTION_COLOR, newColor); |
| 86 | } |
| 87 | |
| 88 | public Color getFillColor() { |
| 89 | return getColorProperty(FILL_COLOR); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get the current rotation. |
| 94 | * |
| 95 | * @return one of ImageTools.ROTATE_* |
| 96 | * @see ImageTools#ROTATE_NONE |
| 97 | * @see ImageTools#ROTATE_CLOCKWISE |
| 98 | * @see ImageTools#ROTATE_COUNTERCLOCKWISE |
| 99 | * @see ImageTools#ROTATE_UPSIDE_DOWN |
| 100 | */ |
| 101 | public int getRotation() { |
| 102 | return getLimitedIntProperty(ROTATION, MIN_ROTATION, MAX_ROTATION); |
| 103 | } |
| 104 | |
| 105 | public String getScaleMode() { |
| 106 | String result = getProperty(SCALE_MODE); |
| 107 | |
| 108 | if (!imageTools.isValidScaleMode(result)) { |
| 109 | logger.warn( |
| 110 | "changing property " |
| 111 | + SCALE_MODE |
| 112 | + " from \"" |
| 113 | + result |
| 114 | + "\" to \"" |
| 115 | + DEFAULT_SCALE_MODE |
| 116 | + "\""); |
| 117 | result = DEFAULT_SCALE_MODE; |
| 118 | } |
| 119 | return result; |
| 120 | } |
| 121 | |
| 122 | public Color getSelectionColor() { |
| 123 | return getColorProperty(SELECTION_COLOR); |
| 124 | } |
| 125 | |
| 126 | public void rotateLeft() { |
| 127 | int newRotation = imageTools.getLeftRotation(getRotation()); |
| 128 | |
| 129 | if (logger.isDebugEnabled()) { |
| 130 | logger.debug("rotate left: rotation = " + newRotation); |
| 131 | } |
| 132 | setRotation(newRotation); |
| 133 | } |
| 134 | |
| 135 | public void rotateRight() { |
| 136 | int newRotation = imageTools.getRightRotation(getRotation()); |
| 137 | |
| 138 | if (logger.isDebugEnabled()) { |
| 139 | logger.debug("rotate right: rotation = " + newRotation); |
| 140 | } |
| 141 | setRotation(newRotation); |
| 142 | } |
| 143 | |
| 144 | } |