package jetbrains.mps.ide.icons; /*Generated by MPS */ import jetbrains.mps.util.annotation.ToRemove; import org.apache.log4j.Logger; import org.apache.log4j.LogManager; import java.awt.MediaTracker; import javax.swing.Icon; import org.jetbrains.annotations.NonNls; import jetbrains.mps.vfs.IFile; import jetbrains.mps.vfs.FileSystem; import java.io.InputStream; import java.io.IOException; import org.apache.log4j.Level; import org.jetbrains.annotations.NotNull; import javax.swing.ImageIcon; /** * * @deprecated */ @Deprecated @ToRemove(version = 3.5) public class IconLoadHelper { private static final Logger LOG = LogManager.getLogger(IconLoadHelper.class); private static final int IMAGE_LOADED = ~(((MediaTracker.ABORTED | MediaTracker.ERRORED | MediaTracker.LOADING))); public static Icon loadIcon(@NonNls String iconPath) { IFile file = FileSystem.getInstance().getFileByPath(iconPath); return getIconFor(file); } private static Icon getIconFor(IFile file) { if (!(file.exists())) { return null; } InputStream is = null; try { is = file.openInputStream(); return loadIcon(is); } catch (IOException e) { if (LOG.isEnabledFor(Level.ERROR)) { LOG.error("", e); } return null; } finally { try { if (is != null) { is.close(); } } catch (IOException e) { if (LOG.isEnabledFor(Level.ERROR)) { LOG.error("", e); } } } } /*package*/ static Icon loadIcon(@NotNull InputStream is) { byte[] image = new byte[0]; try { image = new byte[is.available()]; } catch (IOException e) { if (LOG.isEnabledFor(Level.ERROR)) { LOG.error("", e); } } try { int current = 0; while (true) { int result = is.read(image, current, image.length - current); if (result == -1 || result == 0) { break; } else { current += result; } } } catch (IOException e) { if (LOG.isEnabledFor(Level.ERROR)) { LOG.error("", e); } } ImageIcon icon = new ImageIcon(image); if ((icon.getImageLoadStatus() & IMAGE_LOADED) != 0) { return icon; } return null; } }