1 /*
   2  * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.javafx.pgstub;
  27 
  28 import java.util.HashMap;
  29 import java.util.Map;
  30 
  31 import com.sun.javafx.runtime.async.AsyncOperation;
  32 import com.sun.javafx.runtime.async.AsyncOperationListener;
  33 import com.sun.javafx.tk.ImageLoader;
  34 import com.sun.javafx.tk.PlatformImage;
  35 
  36 public final class StubImageLoaderFactory {
  37     private final Map<Object, StubPlatformImageInfo> imageInfos;
  38 
  39     private StubAsyncImageLoader lastAsyncLoader;
  40 
  41     private final ImageLoader ERROR_IMAGE_LOADER =
  42             new ImageLoader() {
  43                 private final Exception exception =
  44                             new Exception("Loading failed");
  45 
  46                 @Override
  47                 public Exception getException() {
  48                     return exception;
  49                 }
  50 
  51                 @Override
  52                 public int getFrameCount() {
  53                     throw new IllegalStateException();
  54                 }
  55 
  56                 @Override
  57                 public PlatformImage getFrame(int i) {
  58                     throw new IllegalStateException();
  59                 }
  60 
  61                 @Override
  62                 public int getFrameDelay(int i) {
  63                     throw new IllegalStateException();
  64                 }
  65 
  66                 @Override
  67                 public int getLoopCount() {
  68                     throw new IllegalStateException();
  69                 }
  70 
  71                 @Override
  72                 public int getWidth() {
  73                     throw new IllegalStateException();
  74                 }
  75 
  76                 @Override
  77                 public int getHeight() {
  78                     throw new IllegalStateException();
  79                 }
  80             };
  81 
  82     public StubImageLoaderFactory() {
  83         imageInfos = new HashMap<Object, StubPlatformImageInfo>();
  84     }
  85 
  86     public void reset() {
  87         imageInfos.clear();
  88         lastAsyncLoader = null;
  89     }
  90 
  91     public void registerImage(final Object source,
  92                               final StubPlatformImageInfo imageInfo) {
  93         imageInfos.put(source, imageInfo);
  94     }
  95 
  96     public StubAsyncImageLoader getLastAsyncImageLoader() {
  97         return lastAsyncLoader;
  98     }
  99 
 100     public ImageLoader createImageLoader(final Object source,
 101                                          final int loadWidth,
 102                                          final int loadHeight,
 103                                          final boolean preserveRatio,
 104                                          final boolean smooth) {
 105         final StubPlatformImageInfo imageInfo = imageInfos.get(source);
 106         if (imageInfo == null) {
 107             return ERROR_IMAGE_LOADER;
 108         }
 109 
 110         return new StubImageLoader(source, imageInfo, loadWidth, loadHeight,
 111                                    preserveRatio, smooth);
 112     }
 113 
 114     public AsyncOperation createAsyncImageLoader(
 115             final AsyncOperationListener listener,
 116             final String url, final int loadWidth, final int loadHeight,
 117             final boolean preserveRatio, final boolean smooth) {
 118         final ImageLoader imageLoader =
 119                 createImageLoader(url, loadWidth, loadHeight,
 120                                   preserveRatio, smooth);
 121         final StubAsyncImageLoader asyncLoader =
 122                 new StubAsyncImageLoader(imageLoader, listener);
 123 
 124         lastAsyncLoader = asyncLoader;
 125         return asyncLoader;
 126     }
 127 }