private static Image generateAndSaveIcon(Content content, int iconSize) { Image icon = null; try { icon = generateIcon(content, iconSize); if (icon == null) { return DEFAULT_ICON; } else { File f = getFile(content.getId()); if (f.exists()) { f.delete(); } ImageIO.write((BufferedImage) icon, "png", getFile(content.getId())); // NON-NLS } } catch (IOException ex) { logger.log(Level.WARNING, "Could not write cache thumbnail: " + content, ex); // NON-NLS } return icon; }
/** * Check if Solr has extracted content for a given node * * @param node * @return true if Solr has content, else false */ private boolean solrHasContent(Node node) { Content content = node.getLookup().lookup(Content.class); if (content == null) { return false; } final Server solrServer = KeywordSearch.getServer(); final long contentID = content.getId(); try { return solrServer.queryIsIndexed(contentID); } catch (NoOpenCoreException ex) { logger.log(Level.WARNING, "Couldn't determine whether content is supported.", ex); return false; } catch (SolrServerException ex) { logger.log(Level.WARNING, "Couldn't determine whether content is supported.", ex); return false; } }
/** * Get an icon of a specified size. * * @param content * @param iconSize * @return */ public static Image getIcon(Content content, int iconSize) { Image icon; // If a thumbnail file is already saved locally File file = getFile(content.getId()); if (file.exists()) { try { BufferedImage bicon = ImageIO.read(file); if (bicon == null) { icon = DEFAULT_ICON; } else if (bicon.getWidth() != iconSize) { icon = generateAndSaveIcon(content, iconSize); } else { icon = bicon; } } catch (IOException ex) { logger.log(Level.WARNING, "Error while reading image.", ex); // NON-NLS icon = DEFAULT_ICON; } } else { // Make a new icon icon = generateAndSaveIcon(content, iconSize); } return icon; }
/** * Get the cached file of the icon. Generates the icon and its file if it doesn't already exist, * so this method guarantees to return a file that exists. * * @param content * @param iconSize * @return */ public static File getIconFile(Content content, int iconSize) { if (getIcon(content, iconSize) != null) { return getFile(content.getId()); } return null; }
@Override protected String defaultVisit(Content cntnt) { return cntnt.getName() + ":" + Long.toString(cntnt.getId()); }
@Override public void setNode(final Node selectedNode) { // TODO why setNode() is called twice for the same node each time // to clear the viewer if (selectedNode == null) { currentNode = null; resetComponent(); return; } this.currentNode = selectedNode; // sources are custom markup from the node (if available) and default // markup is fetched from solr List<MarkupSource> sources = new ArrayList<MarkupSource>(); // add additional registered sources for this node sources.addAll(selectedNode.getLookup().lookupAll(MarkupSource.class)); if (solrHasContent(selectedNode)) { Content content = selectedNode.getLookup().lookup(Content.class); if (content == null) { return; } // add to page tracking if not there yet final long contentID = content.getId(); MarkupSource newSource = new MarkupSource() { private boolean inited = false; private int numPages = 0; private int currentPage = 0; private boolean hasChunks = false; @Override public int getCurrentPage() { return this.currentPage; } @Override public boolean hasNextPage() { return currentPage < numPages; } @Override public boolean hasPreviousPage() { return currentPage > 1; } @Override public int nextPage() { if (!hasNextPage()) { throw new IllegalStateException("No next page."); } ++currentPage; return currentPage; } @Override public int previousPage() { if (!hasPreviousPage()) { throw new IllegalStateException("No previous page."); } --currentPage; return currentPage; } @Override public boolean hasNextItem() { throw new UnsupportedOperationException("Not supported, not a searchable source."); } @Override public boolean hasPreviousItem() { throw new UnsupportedOperationException("Not supported, not a searchable source."); } @Override public int nextItem() { throw new UnsupportedOperationException("Not supported, not a searchable source."); } @Override public int previousItem() { throw new UnsupportedOperationException("Not supported, not a searchable source."); } @Override public int currentItem() { throw new UnsupportedOperationException("Not supported, not a searchable source."); } @Override public String getMarkup() { try { String content = StringEscapeUtils.escapeHtml( getSolrContent(selectedNode, currentPage, hasChunks)); return "<pre>" + content.trim() + "</pre>"; } catch (SolrServerException ex) { logger.log(Level.WARNING, "Couldn't get extracted content.", ex); return ""; } } @Override public String toString() { return "Extracted Content"; } @Override public boolean isSearchable() { return false; } @Override public String getAnchorPrefix() { return ""; } @Override public int getNumberHits() { return 0; } @Override public LinkedHashMap<Integer, Integer> getHitsPages() { return null; } @Override public int getNumberPages() { if (inited) { return this.numPages; } final Server solrServer = KeywordSearch.getServer(); try { numPages = solrServer.queryNumFileChunks(contentID); if (numPages == 0) { numPages = 1; hasChunks = false; } else { hasChunks = true; } inited = true; } catch (SolrServerException ex) { logger.log(Level.WARNING, "Could not get number of chunks: ", ex); } catch (NoOpenCoreException ex) { logger.log(Level.WARNING, "Could not get number of chunks: ", ex); } return numPages; } }; currentSource = newSource; sources.add(newSource); // init pages final int totalPages = currentSource.getNumberPages(); int currentPage = currentSource.getCurrentPage(); if (currentPage == 0 && currentSource.hasNextPage()) { currentSource.nextPage(); } updatePageControls(); } // first source will be the default displayed setPanel(sources); // If node has been selected before, return to the previous position scrollToCurrentHit(); }