Search in sources :

Example 1 with URLImageCustomGraphics

use of org.cytoscape.ding.customgraphics.bitmap.URLImageCustomGraphics in project cytoscape-impl by cytoscape.

the class CustomGraphicsDetailPanel method resetButtonActionPerformed.

private void resetButtonActionPerformed(ActionEvent evt) {
    if (cg == null || cg.getRenderedImage() == null)
        return;
    if (cg instanceof URLImageCustomGraphics) {
        final Image image = ((URLImageCustomGraphics) cg).resetImage();
        imageViewPanel.setImage(image);
        final int w = image.getWidth(null);
        final int h = image.getHeight(null);
        widthTextField.setText(Integer.toString(w));
        heightTextField.setText(Integer.toString(h));
        cg.setWidth(w);
        cg.setHeight(h);
        appManager.getCurrentNetworkView().updateView();
    }
}
Also used : Image(java.awt.Image) URLImageCustomGraphics(org.cytoscape.ding.customgraphics.bitmap.URLImageCustomGraphics)

Example 2 with URLImageCustomGraphics

use of org.cytoscape.ding.customgraphics.bitmap.URLImageCustomGraphics in project cytoscape-impl by cytoscape.

the class RestoreImageTask method restoreSampleImages.

private void restoreSampleImages() throws IOException {
    // Filter by display name
    final Collection<CyCustomGraphics> allGraphics = manager.getAllCustomGraphics();
    final Set<String> names = new HashSet<>();
    for (CyCustomGraphics<?> cg : allGraphics) names.add(cg.getDisplayName());
    for (final URL imageURL : defaultImageURLs) {
        final String[] parts = imageURL.getFile().split("/");
        final String dispNameString = parts[parts.length - 1];
        if (this.manager.getCustomGraphicsBySourceURL(imageURL) == null && !names.contains(dispNameString)) {
            final CyCustomGraphics<?> cg = new URLImageCustomGraphics<>(manager.getNextAvailableID(), imageURL.toString());
            if (cg != null) {
                manager.addCustomGraphics(cg, imageURL);
                cg.setDisplayName(dispNameString);
            }
        }
    }
}
Also used : CyCustomGraphics(org.cytoscape.view.presentation.customgraphics.CyCustomGraphics) URLImageCustomGraphics(org.cytoscape.ding.customgraphics.bitmap.URLImageCustomGraphics) URL(java.net.URL) HashSet(java.util.HashSet)

Example 3 with URLImageCustomGraphics

use of org.cytoscape.ding.customgraphics.bitmap.URLImageCustomGraphics in project cytoscape-impl by cytoscape.

the class CustomGraphicsManagerDialog method processFiles.

private void processFiles(final File[] files) {
    for (final File file : files) {
        BufferedImage img = null;
        if (file.isFile()) {
            try {
                img = ImageIO.read(file);
            } catch (IOException e) {
                logger.error("Could not read file: " + file.toString(), e);
                continue;
            }
        }
        if (img != null) {
            final CyCustomGraphics<?> cg = new URLImageCustomGraphics<>(manager.getNextAvailableID(), file.toString(), img);
            try {
                manager.addCustomGraphics(cg, file.toURI().toURL());
            } catch (MalformedURLException e) {
                e.printStackTrace();
                continue;
            }
            ((CustomGraphicsListModel) browser.getModel()).addElement(cg);
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) File(java.io.File) URLImageCustomGraphics(org.cytoscape.ding.customgraphics.bitmap.URLImageCustomGraphics) BufferedImage(java.awt.image.BufferedImage)

Example 4 with URLImageCustomGraphics

use of org.cytoscape.ding.customgraphics.bitmap.URLImageCustomGraphics in project cytoscape-impl by cytoscape.

the class RestoreImageTask method restoreImages.

private void restoreImages() {
    final CompletionService<BufferedImage> cs = new ExecutorCompletionService<BufferedImage>(imageLoaderService);
    imageHomeDirectory.mkdir();
    long startTime = System.currentTimeMillis();
    // Load metadata first.
    final Properties prop = new Properties();
    try {
        prop.load(new FileInputStream(new File(imageHomeDirectory, METADATA_FILE)));
        logger.info("Custom Graphics Image property file loaded from: " + imageHomeDirectory);
    } catch (Exception e) {
        logger.info("Custom Graphics Metadata was not found. (This is normal for the first time.)");
        // Restore process is not necessary.
        return;
    }
    if (this.imageHomeDirectory != null && imageHomeDirectory.isDirectory()) {
        final File[] imageFiles = imageHomeDirectory.listFiles();
        final Map<Future<BufferedImage>, String> fMap = new HashMap<>();
        final Map<Future<BufferedImage>, Long> fIdMap = new HashMap<>();
        final Map<Future<BufferedImage>, Set<String>> metatagMap = new HashMap<>();
        final Set<File> validFiles = new HashSet<>();
        try {
            for (File file : imageFiles) {
                if (file.toString().endsWith(IMAGE_EXT) == false)
                    continue;
                final String fileName = file.getName();
                final String key = fileName.split("\\.")[0];
                final String value = prop.getProperty(key);
                // Filter unnecessary files.
                if (value == null || value.contains("URLImageCustomGraphics") == false)
                    continue;
                final String[] imageProps = value.split(",");
                if (imageProps == null || imageProps.length < 2)
                    continue;
                String name = imageProps[2];
                if (name.contains("___"))
                    name = name.replace("___", ",");
                Future<BufferedImage> f = cs.submit(new LoadImageTask(file.toURI().toURL()));
                validFiles.add(file);
                fMap.put(f, name);
                fIdMap.put(f, Long.parseLong(imageProps[1]));
                String tagStr = null;
                if (imageProps.length > 3) {
                    tagStr = imageProps[3];
                    final Set<String> tags = new TreeSet<>();
                    String[] tagParts = tagStr.split("\\" + AbstractDCustomGraphics.LIST_DELIMITER);
                    for (String tag : tagParts) tags.add(tag.trim());
                    metatagMap.put(f, tags);
                }
            }
            for (File file : validFiles) {
                if (file.toString().endsWith(IMAGE_EXT) == false)
                    continue;
                final Future<BufferedImage> f = cs.take();
                final BufferedImage image = f.get();
                if (image == null)
                    continue;
                final CyCustomGraphics<?> cg = new URLImageCustomGraphics<>(fIdMap.get(f), fMap.get(f), image);
                if (cg instanceof Taggable && metatagMap.get(f) != null)
                    ((Taggable) cg).getTags().addAll(metatagMap.get(f));
                try {
                    final URL source = new URL(fMap.get(f));
                    if (source != null)
                        manager.addCustomGraphics(cg, source);
                } catch (MalformedURLException me) {
                    manager.addCustomGraphics(cg, null);
                }
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
    try {
        imageLoaderService.shutdown();
        imageLoaderService.awaitTermination(TIMEOUT, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    long endTime = System.currentTimeMillis();
    double sec = (endTime - startTime) / (1000.0);
    logger.info("Image loading process finished in " + sec + " sec.");
    logger.info("Currently,  " + (manager.getAllCustomGraphics().size() - 1) + " images are available.");
}
Also used : MalformedURLException(java.net.MalformedURLException) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) Properties(java.util.Properties) BufferedImage(java.awt.image.BufferedImage) URL(java.net.URL) TreeSet(java.util.TreeSet) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Future(java.util.concurrent.Future) Taggable(org.cytoscape.ding.customgraphics.Taggable) File(java.io.File) URLImageCustomGraphics(org.cytoscape.ding.customgraphics.bitmap.URLImageCustomGraphics)

Example 5 with URLImageCustomGraphics

use of org.cytoscape.ding.customgraphics.bitmap.URLImageCustomGraphics in project cytoscape-impl by cytoscape.

the class CustomGraphicsManagerImpl method getAllPersistantCustomGraphics.

/* (non-Javadoc)
	 * @see org.cytoscape.ding.customgraphicsmgr.internal.CGM#getAllPersistantCustomGraphics()
	 */
@Override
public Collection<CyCustomGraphics> getAllPersistantCustomGraphics() {
    Set<CyCustomGraphics> cgSet = new HashSet<>();
    for (CyCustomGraphics cg : getAllCustomGraphics()) {
        // Currently, we only export URLImageCustomGraphics to the session file.  This may change in the future...
        if (cg instanceof URLImageCustomGraphics) {
            URLImageCustomGraphics<?> urlCG = (URLImageCustomGraphics<?>) cg;
            // Don't serialize bundle-generated graphics
            if (urlCG.getSourceURL() != null && urlCG.getSourceURL().toString().startsWith("bundle:"))
                continue;
            cgSet.add(cg);
        }
    }
    return cgSet;
}
Also used : CyCustomGraphics(org.cytoscape.view.presentation.customgraphics.CyCustomGraphics) URLImageCustomGraphics(org.cytoscape.ding.customgraphics.bitmap.URLImageCustomGraphics) HashSet(java.util.HashSet)

Aggregations

URLImageCustomGraphics (org.cytoscape.ding.customgraphics.bitmap.URLImageCustomGraphics)5 HashSet (java.util.HashSet)3 BufferedImage (java.awt.image.BufferedImage)2 File (java.io.File)2 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 CyCustomGraphics (org.cytoscape.view.presentation.customgraphics.CyCustomGraphics)2 Image (java.awt.Image)1 FileInputStream (java.io.FileInputStream)1 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorCompletionService (java.util.concurrent.ExecutorCompletionService)1 Future (java.util.concurrent.Future)1 Taggable (org.cytoscape.ding.customgraphics.Taggable)1