[4] | 1 | /* |
---|
| 2 | * This program is free software; you can redistribute it and/or modify |
---|
| 3 | * it under the terms of the GNU General Public License as published by |
---|
| 4 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 5 | * (at your option) any later version. |
---|
| 6 | * |
---|
| 7 | * This program is distributed in the hope that it will be useful, |
---|
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 10 | * GNU General Public License for more details. |
---|
| 11 | * |
---|
| 12 | * You should have received a copy of the GNU General Public License |
---|
| 13 | * along with this program; if not, write to the Free Software |
---|
| 14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | /* |
---|
| 18 | * JComponentWriter.java |
---|
| 19 | * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand |
---|
| 20 | * |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | package weka.gui.visualize; |
---|
| 24 | |
---|
| 25 | import java.io.File; |
---|
| 26 | import javax.swing.JComponent; |
---|
| 27 | |
---|
| 28 | /** |
---|
| 29 | * This class takes any JComponent and outputs it to a file. Scaling is by |
---|
| 30 | * default enabled. Derived classes only need to override the following |
---|
| 31 | * methods: |
---|
| 32 | * <ul> |
---|
| 33 | * <li><code>getDescription()</code></li> |
---|
| 34 | * <li><code>getExtension()</code></li> |
---|
| 35 | * <li><code>generateOutput()</code></li> |
---|
| 36 | * <li><code></code></li> |
---|
| 37 | * </ul> |
---|
| 38 | * |
---|
| 39 | * @see #setScalingEnabled(boolean) |
---|
| 40 | * @author FracPete (fracpete at waikato dot ac dot nz) |
---|
| 41 | * @version $Revision: 1.4 $ |
---|
| 42 | */ |
---|
| 43 | public abstract class JComponentWriter { |
---|
| 44 | /** whether to print some debug information */ |
---|
| 45 | protected final static boolean DEBUG = false; |
---|
| 46 | |
---|
| 47 | /** output if we're in debug mode */ |
---|
| 48 | static { |
---|
| 49 | if (DEBUG) |
---|
| 50 | System.err.println(JComponentWriter.class.getName() + ": DEBUG ON"); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | /** the component to print in the output format */ |
---|
| 54 | private JComponent component; |
---|
| 55 | |
---|
| 56 | /** the file to write the output stream to */ |
---|
| 57 | private File outputFile; |
---|
| 58 | |
---|
| 59 | /** the x scale factor */ |
---|
| 60 | protected double m_xScale; |
---|
| 61 | |
---|
| 62 | /** the y scale factor */ |
---|
| 63 | protected double m_yScale; |
---|
| 64 | |
---|
| 65 | /** whether scaling is enabled */ |
---|
| 66 | protected boolean m_ScalingEnabled; |
---|
| 67 | |
---|
| 68 | /** whether to use custom dimensions */ |
---|
| 69 | protected boolean m_UseCustomDimensions; |
---|
| 70 | |
---|
| 71 | /** the custom width */ |
---|
| 72 | protected int m_CustomWidth; |
---|
| 73 | |
---|
| 74 | /** the custom height */ |
---|
| 75 | protected int m_CustomHeight; |
---|
| 76 | |
---|
| 77 | /** |
---|
| 78 | * initializes the object |
---|
| 79 | */ |
---|
| 80 | public JComponentWriter() { |
---|
| 81 | this(null); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | /** |
---|
| 85 | * initializes the object with the given Component |
---|
| 86 | * |
---|
| 87 | * @param c the component to print in the output format |
---|
| 88 | */ |
---|
| 89 | public JComponentWriter(JComponent c) { |
---|
| 90 | this(c, null); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | /** |
---|
| 94 | * initializes the object with the given Component and filename |
---|
| 95 | * |
---|
| 96 | * @param c the component to print in the output format |
---|
| 97 | * @param f the file to store the output in |
---|
| 98 | */ |
---|
| 99 | public JComponentWriter(JComponent c, File f) { |
---|
| 100 | component = c; |
---|
| 101 | outputFile = f; |
---|
| 102 | |
---|
| 103 | initialize(); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | /** |
---|
| 107 | * further initialization can take place here |
---|
| 108 | */ |
---|
| 109 | protected void initialize() { |
---|
| 110 | m_xScale = 1.0; |
---|
| 111 | m_yScale = 1.0; |
---|
| 112 | m_ScalingEnabled = true; |
---|
| 113 | m_UseCustomDimensions = false; |
---|
| 114 | m_CustomWidth = -1; |
---|
| 115 | m_CustomHeight = -1; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | /** |
---|
| 119 | * sets the component to print to an output format |
---|
| 120 | * |
---|
| 121 | * @param c the component to print |
---|
| 122 | */ |
---|
| 123 | public void setComponent(JComponent c) { |
---|
| 124 | component = c; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | /** |
---|
| 128 | * returns the component that is stored in the output format |
---|
| 129 | * |
---|
| 130 | * @return the component to print |
---|
| 131 | */ |
---|
| 132 | public JComponent getComponent() { |
---|
| 133 | return component; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | /** |
---|
| 137 | * sets the file to store the output in |
---|
| 138 | * |
---|
| 139 | * @param f the file to store the output in |
---|
| 140 | */ |
---|
| 141 | public void setFile(File f) { |
---|
| 142 | outputFile = f; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | /** |
---|
| 146 | * returns the file being used for storing the output |
---|
| 147 | * |
---|
| 148 | * @return the file to store the output in |
---|
| 149 | */ |
---|
| 150 | public File getFile() { |
---|
| 151 | return outputFile; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | /** |
---|
| 155 | * returns the name of the writer, to display in the FileChooser. |
---|
| 156 | * must be overridden in the derived class. |
---|
| 157 | * |
---|
| 158 | * @return the name of the writer |
---|
| 159 | */ |
---|
| 160 | public abstract String getDescription(); |
---|
| 161 | |
---|
| 162 | /** |
---|
| 163 | * returns the extension (incl. ".") of the output format, to use in the |
---|
| 164 | * FileChooser. |
---|
| 165 | * must be overridden in the derived class. |
---|
| 166 | * |
---|
| 167 | * @return the file extension |
---|
| 168 | */ |
---|
| 169 | public abstract String getExtension(); |
---|
| 170 | |
---|
| 171 | /** |
---|
| 172 | * whether scaling is enabled or ignored |
---|
| 173 | * |
---|
| 174 | * @return true if scaling is enabled |
---|
| 175 | */ |
---|
| 176 | public boolean getScalingEnabled() { |
---|
| 177 | return m_ScalingEnabled; |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | /** |
---|
| 181 | * sets whether to enable scaling |
---|
| 182 | * |
---|
| 183 | * @param enabled whether scaling is enabled |
---|
| 184 | */ |
---|
| 185 | public void setScalingEnabled(boolean enabled) { |
---|
| 186 | m_ScalingEnabled = enabled; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | /** |
---|
| 190 | * sets the scale factor - is ignored since we always create a screenshot! |
---|
| 191 | * @param x the scale factor for the x-axis |
---|
| 192 | * @param y the scale factor for the y-axis |
---|
| 193 | */ |
---|
| 194 | public void setScale(double x, double y) { |
---|
| 195 | if (getScalingEnabled()) { |
---|
| 196 | m_xScale = x; |
---|
| 197 | m_yScale = y; |
---|
| 198 | } |
---|
| 199 | else { |
---|
| 200 | m_xScale = 1.0; |
---|
| 201 | m_yScale = 1.0; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | if (DEBUG) |
---|
| 205 | System.err.println("xScale = " + m_xScale + ", yScale = " + m_yScale); |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | /** |
---|
| 209 | * returns the scale factor for the x-axis |
---|
| 210 | * |
---|
| 211 | * @return the scale scale factor for the x-axis |
---|
| 212 | */ |
---|
| 213 | public double getXScale() { |
---|
| 214 | return m_xScale; |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | /** |
---|
| 218 | * returns the scale factor for the y-axis |
---|
| 219 | * |
---|
| 220 | * @return the scale scale factor for the y-axis |
---|
| 221 | */ |
---|
| 222 | public double getYScale() { |
---|
| 223 | return m_xScale; |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | /** |
---|
| 227 | * whether custom dimensions are to used for the size of the image |
---|
| 228 | * |
---|
| 229 | * @return true if custom dimensions are used |
---|
| 230 | */ |
---|
| 231 | public boolean getUseCustomDimensions() { |
---|
| 232 | return m_UseCustomDimensions; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | /** |
---|
| 236 | * sets whether to use custom dimensions for the image |
---|
| 237 | * |
---|
| 238 | * @param value whether custom dimensions are used |
---|
| 239 | */ |
---|
| 240 | public void setUseCustomDimensions(boolean value) { |
---|
| 241 | m_UseCustomDimensions = value; |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | /** |
---|
| 245 | * sets the custom width to use |
---|
| 246 | * |
---|
| 247 | * @param value the width to use |
---|
| 248 | * @see #m_UseCustomDimensions |
---|
| 249 | */ |
---|
| 250 | public void setCustomWidth(int value) { |
---|
| 251 | m_CustomWidth = value; |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | /** |
---|
| 255 | * gets the custom width currently used |
---|
| 256 | * |
---|
| 257 | * @return the custom width currently used |
---|
| 258 | * @see #m_UseCustomDimensions |
---|
| 259 | */ |
---|
| 260 | public int getCustomWidth() { |
---|
| 261 | return m_CustomWidth; |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | /** |
---|
| 265 | * sets the custom height to use |
---|
| 266 | * |
---|
| 267 | * @param value the height to use |
---|
| 268 | * @see #m_UseCustomDimensions |
---|
| 269 | */ |
---|
| 270 | public void setCustomHeight(int value) { |
---|
| 271 | m_CustomHeight = value; |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | /** |
---|
| 275 | * gets the custom height currently used |
---|
| 276 | * |
---|
| 277 | * @return the custom height currently used |
---|
| 278 | * @see #m_UseCustomDimensions |
---|
| 279 | */ |
---|
| 280 | public int getCustomHeight() { |
---|
| 281 | return m_CustomHeight; |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | /** |
---|
| 285 | * generates the actual output |
---|
| 286 | * |
---|
| 287 | * @throws Exception if something goes wrong |
---|
| 288 | */ |
---|
| 289 | protected abstract void generateOutput() throws Exception; |
---|
| 290 | |
---|
| 291 | /** |
---|
| 292 | * saves the current component to the currently set file.<p> |
---|
| 293 | * <b>Note:</b> this method calls <code>generateOutput()</code> which needs |
---|
| 294 | * to be overriden in subclasses! |
---|
| 295 | * @throws Exception if either the file or the component is <code>null</code> |
---|
| 296 | */ |
---|
| 297 | public void toOutput() throws Exception { |
---|
| 298 | int oldWidth; |
---|
| 299 | int oldHeight; |
---|
| 300 | |
---|
| 301 | if (getFile() == null) |
---|
| 302 | throw new Exception("The file is not set!"); |
---|
| 303 | if (getComponent() == null) |
---|
| 304 | throw new Exception("The component is not set!"); |
---|
| 305 | |
---|
| 306 | // backup original dimensions and set custom ones if necessary |
---|
| 307 | oldWidth = getComponent().getWidth(); |
---|
| 308 | oldHeight = getComponent().getHeight(); |
---|
| 309 | if (getUseCustomDimensions()) |
---|
| 310 | getComponent().setSize(getCustomWidth(), getCustomHeight()); |
---|
| 311 | |
---|
| 312 | generateOutput(); |
---|
| 313 | |
---|
| 314 | // restore original dimensions |
---|
| 315 | if (getUseCustomDimensions()) |
---|
| 316 | getComponent().setSize(oldWidth, oldHeight); |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | /** |
---|
| 320 | * outputs the given component with the given writer in the specified file |
---|
| 321 | * |
---|
| 322 | * @param writer the writer to use |
---|
| 323 | * @param comp the component to output |
---|
| 324 | * @param file the file to store the output in |
---|
| 325 | * @throws Exception if component of file are <code>null</code> |
---|
| 326 | */ |
---|
| 327 | public static void toOutput(JComponentWriter writer, JComponent comp, File file) throws Exception { |
---|
| 328 | toOutput(writer, comp, file, -1, -1); |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | /** |
---|
| 332 | * outputs the given component with the given writer in the specified file. |
---|
| 333 | * If width and height are different from -1 then these sizes are used, |
---|
| 334 | * otherwise the current ones of the component |
---|
| 335 | * |
---|
| 336 | * @param writer the writer to use |
---|
| 337 | * @param comp the component to output |
---|
| 338 | * @param file the file to store the output in |
---|
| 339 | * @param width custom width, -1 uses the component's one |
---|
| 340 | * @param height custom height, -1 uses the component's one |
---|
| 341 | * @throws Exception if component or file are <code>null</code> |
---|
| 342 | */ |
---|
| 343 | public static void toOutput(JComponentWriter writer, JComponent comp, File file, int width, int height) throws Exception { |
---|
| 344 | writer.setComponent(comp); |
---|
| 345 | writer.setFile(file); |
---|
| 346 | |
---|
| 347 | // custom dimensions? |
---|
| 348 | if ((width != -1) && (height != -1)) { |
---|
| 349 | writer.setUseCustomDimensions(true); |
---|
| 350 | writer.setCustomWidth(width); |
---|
| 351 | writer.setCustomHeight(height); |
---|
| 352 | } |
---|
| 353 | |
---|
| 354 | writer.toOutput(); |
---|
| 355 | } |
---|
| 356 | } |
---|