Search in sources :

Example 1 with Taggable

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

the class CustomGraphicsDetailPanel method valueChanged.

@Override
public void valueChanged(ListSelectionEvent e) {
    if (!(e.getSource() instanceof CustomGraphicsBrowser) || e.getValueIsAdjusting())
        return;
    final CustomGraphicsBrowser browser = (CustomGraphicsBrowser) e.getSource();
    cg = (CyCustomGraphics) browser.getSelectedValue();
    if (cg == null) {
        imageViewPanel.setImage((Image) null);
        heightTextField.setText(null);
        widthTextField.setText(null);
        nameTextField.setText(null);
        nameTextField.setToolTipText(null);
        tagTextField.setText(null);
        return;
    }
    final Image img = cg.getRenderedImage();
    // Set up detail panel
    imageViewPanel.setImage(img);
    heightTextField.setText(Integer.toString(img.getHeight(null)));
    widthTextField.setText(Integer.toString(img.getWidth(null)));
    nameTextField.setText(cg.getDisplayName());
    nameTextField.setToolTipText(cg.getDisplayName());
    if (cg instanceof Taggable) {
        final Collection<String> tags = ((Taggable) cg).getTags();
        final int tagCount = tags.size();
        int counter = 0;
        final StringBuilder tagBuilder = new StringBuilder();
        for (String tag : tags) {
            tagBuilder.append(tag);
            counter++;
            if (tagCount != counter)
                tagBuilder.append(", ");
        }
        tagTextField.setText(tagBuilder.toString());
        tagTextField.setToolTipText(tagBuilder.toString());
    }
}
Also used : Image(java.awt.Image) Taggable(org.cytoscape.ding.customgraphics.Taggable)

Example 2 with Taggable

use of org.cytoscape.ding.customgraphics.Taggable 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)

Aggregations

Taggable (org.cytoscape.ding.customgraphics.Taggable)2 Image (java.awt.Image)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)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 URLImageCustomGraphics (org.cytoscape.ding.customgraphics.bitmap.URLImageCustomGraphics)1