⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 resultspanel.java

📁 基于MPEG 7 标准,符合未来语义网架构,很值得参考
💻 JAVA
字号:
/*
 * This file is part of Caliph & Emir.
 *
 * Caliph & Emir is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Caliph & Emir is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Caliph & Emir; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Copyright statement:
 * --------------------
 * (c) 2002-2005 by Mathias Lux (mathias@juggle.at)
 * http://www.juggle.at, http://caliph-emir.sourceforge.net
 */
package at.lux.fotoretrieval.panels;

import at.lux.fotoretrieval.ResultListEntry;
import at.lux.fotoretrieval.RetrievalToolkit;
import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.transform.JDOMResult;
import org.jdom.transform.JDOMSource;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.stream.StreamSource;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Iterator;
import java.util.Vector;

/**
 * ResultsPanel
 *
 * @author Mathias Lux, mathias@juggle.at
 */
public class ResultsPanel extends JPanel {
    private static int max = 20;
    private Vector results;
    private JProgressBar progress;
    private boolean show_thumbs = true;
    private QualityConstraintPanel qualityConstraints = null;
    private static DecimalFormat df;

    static {
        df = ((DecimalFormat) DecimalFormat.getInstance());
        df.setMaximumFractionDigits(2);
    }
//    private StreamSource streamSource = null;

    public ResultsPanel(Vector results, JProgressBar progress) {
        this.results = results;
        this.progress = progress;
        init();
    }

    public ResultsPanel(Vector results, JProgressBar progress, QualityConstraintPanel qualityConstraints) {
        this.results = results;
        this.progress = progress;
        this.qualityConstraints = qualityConstraints;
        init();
    }

    public ResultsPanel(Vector results) {
        this.results = results;
        this.progress = null;
        init();
    }

    private void init() {
        this.setLayout(new BorderLayout());
        JPanel thumbs = new JPanel(new GridLayout(0, 1));
        JPanel descriptions = new JPanel(new GridLayout(0, 1));
        JPanel resultsPanel = new JPanel(new BorderLayout());
        DecimalFormat df = (DecimalFormat) NumberFormat.getInstance();
        df.setMaximumFractionDigits(2);
//        df.setMaximumIntegerDigits(3);

        if (progress != null) {
            progress.setMinimum(0);
            progress.setMaximum(Math.min(max, results.size()));
            progress.setValue(0);
            progress.setString("Formatting results");
        }

        int count = 0;
        File file;
        String strDesc;
        ResultThumbnailPanel thumb = null;
        for (Iterator i = results.iterator(); i.hasNext();) {
            ResultListEntry entry = (ResultListEntry) i.next();
            if (entry.getFilePath() != null) {
                try {
//                long time = System.currentTimeMillis();

//                file = new File();
                    thumb = null;
                    int qualityRating = 1;
                    if (show_thumbs) {
                        if ((entry.getThumbPath() == null)) {
                            thumb = new ResultThumbnailPanel(entry.getFilePath());
                        } else {
                            boolean isValid = true;
                            URL thumbURL = new URI(entry.getThumbPath()).toURL();
                            try {
                                thumbURL.openStream();
                            } catch (IOException ioex) {
                                // so the url stored in the mPEG-7 file is false
                                isValid = false;
//                                System.err.println("Did not find thumbnail: " + thumbURL.toString());
                            }
                            if (isValid == false) {
                                // now we try and see if the file is in the directory of the description:
                                String path = entry.getDescriptionPath().substring(0, entry.getDescriptionPath().lastIndexOf('\\'));
                                String thumbnailFile = entry.getThumbPath().substring(entry.getThumbPath().lastIndexOf('/') + 1);
                                String x = path + "\\" + thumbnailFile;
//                                System.out.println(x);
                                File thumbTwemp = new File(x);
                                if (thumbTwemp.exists()) {
                                    // yes it's here ...
                                    thumbURL = thumbTwemp.toURL();
                                    isValid = true;
                                }
                            }
                            try {
                                if (isValid) {
                                    thumb = new ResultThumbnailPanel(ImageIO.read(thumbURL), thumbURL.toString().replaceAll("tn_", ""));
                                } else {
                                    File f = new File(entry.getDescriptionPath());
//                                    debug(f.getParent() + thumbURL.getFile().substring(thumbURL.getFile().lastIndexOf('/')));
                                    String thumbPath = f.getParent() + thumbURL.getFile().substring(thumbURL.getFile().lastIndexOf('/'));
                                    File inputFile = new File(thumbPath);
                                    thumb = new ResultThumbnailPanel(ImageIO.read(inputFile), thumbPath.replaceAll("tn_", ""));
                                }
                            } catch (IOException e) {
                                thumb = new ResultThumbnailPanel(entry.getFilePath());
                            }
                        }
                    }
                    /*
                    // old method ...
                    String doc = "<html><i>Relevance: </i>" + df.format(entry.getRelevance()) + "<br>";
                    doc += "<i>Image: </i>" + entry.getImageFilePath() + ", "  + entry.getImageSize() +  "<br>";
                    doc += "<i>Creator: </i>" + entry.getCreatorName() + "<br>";
                    doc += "<i>Creation time: </i>" + entry.getCreationTime() + "<br>";
                    doc += "<i>Description: </i>" + entry.getFreeTextDescription() + "<br>";
                    doc += "<i>Semantics: </i>" + entry.getSemanticDescriptionString() + "<br>";
                    doc += "</html>";
                    */
                    JPanel descPanel = new JPanel(new BorderLayout());
                    JPanel namesPanel = new JPanel(new GridLayout(0, 1));
                    JPanel valuesPanel = new JPanel(new GridLayout(0, 1));

                    Font plainFont = this.getFont().deriveFont(Font.PLAIN);

                    namesPanel.add(new JLabel("Relevance: "));
                    namesPanel.add(new JLabel("Image: "));
                    namesPanel.add(new JLabel("Creator: "));
                    namesPanel.add(new JLabel("Time: "));
                    namesPanel.add(new JLabel("Description: "));
                    namesPanel.add(new JLabel("Semantics: "));

                    valuesPanel.add(new JLabel(df.format(entry.getRelevance())));
                    valuesPanel.add(new JLabel(entry.getImageFilePath() + ", " + entry.getImageSize()));
                    valuesPanel.add(new JLabel(entry.getCreatorName()));
                    valuesPanel.add(new JLabel(entry.getCreationTime()));
                    valuesPanel.add(new JLabel(entry.getFreeTextDescription()));
                    valuesPanel.add(new JLabel(entry.getSemanticDescriptionString()));

                    Component[] labels = valuesPanel.getComponents();
                    for (int j = 0; j < labels.length; j++) {
                        Component label = labels[j];
                        if (label instanceof JLabel) {
                            label.setFont(plainFont);
                        }
                    }

                    descPanel.add(namesPanel, BorderLayout.WEST);
                    descPanel.add(valuesPanel, BorderLayout.CENTER);

//                    String doc = entry.getHTMLSummary();
                    // String fpath = entry.getFilePath();
//                    JLabel desc;
//                    debug(doc);
//                    if (doc != null) {
//                        desc = new JLabel(doc);
//                    } else {
//                        desc = new JLabel("No description available!");
//                    }
                    JPanel rowPanel = new JPanel(new BorderLayout());
                    if (thumb != null) rowPanel.add(thumb, BorderLayout.WEST);
                    rowPanel.add(descPanel, BorderLayout.CENTER);
                    rowPanel.add(new JSeparator(), BorderLayout.SOUTH);
                    JPanel tmpPanel = new JPanel(new BorderLayout());
                    boolean add = false;
//                    if (entry!=null) debug("entry is not NULL");
//                    if (qualityConstraints!=null) debug("qualityConstraints is not NULL");
//                    debug("Quality: " + entry.getQuality() + ", Mode: " + qualityConstraints.getMode());
                    if (qualityConstraints != null) {
                        if ((entry.getQuality() < 0 && qualityConstraints.getMode() != 2)) {
                            // Die Beschreibung enth鋖t keine Qualit鋞sbewertung, daher wird das File immer angezeigt,
                            // ausser man sucht nach einer bestimmten Qualit鋞 (genau)
                            add = true;
                        } else {
                            if (qualityConstraints.getMode() == 0) {
                                // eine untere Grenze wurde angegeben ...
                                if (entry.getQuality() >= qualityConstraints.getQuality()) {
                                    add = true;
                                }
                            } else if (qualityConstraints.getMode() == 1) {
                                // eine obere Grenze wurd angegeben
                                if (entry.getQuality() <= qualityConstraints.getQuality()) {
                                    add = true;
                                }
                            } else if (qualityConstraints.getMode() == 2) {
                                // eine bestimmte qualit鋞sstufe wird gesucht ...
                                if (entry.getQuality() == qualityConstraints.getQuality()) {
                                    add = true;
                                }
                            }

                        }
                    } else {
                        add = true;
                    }
                    if (add) {
                        tmpPanel.add(rowPanel, BorderLayout.NORTH);
                        descriptions.add(tmpPanel);
                    }
                    if (progress != null) {
                        progress.setValue(count++);
                    }
//                System.out.println("One row: " + (System.currentTimeMillis() - time) + " ms");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (count > max) break;
        }
        resultsPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
//        resultsPanel.add(thumbs, BorderLayout.WEST);
        resultsPanel.add(descriptions, BorderLayout.CENTER);

        this.add(new JScrollPane(resultsPanel), BorderLayout.CENTER);
    }

    public Image getImage(String FileName) {
        // Load image
        Image loadedImage = Toolkit.getDefaultToolkit().getImage(FileName);
        // Wait until loaded
        try {
            MediaTracker mediaTracker = new MediaTracker(this);
            mediaTracker.addImage(loadedImage, 0);
            mediaTracker.waitForID(0);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//         Return loaded image
        return loadedImage;
    }

    private Document transform(Document in) throws JDOMException {

        Document d = null;
        try {
            Transformer transformer = TransformerFactory.newInstance().
                    newTransformer(new StreamSource(RetrievalToolkit.class.getResource("data/html-short.xsl").openStream()));
            JDOMResult out = new JDOMResult();
            transformer.transform(new JDOMSource(in), out);
            d = out.getDocument();
        } catch (TransformerException e) {
            debug("Transformation failed, TransformerException: " + e.getMessageAndLocation());
        } catch (TransformerFactoryConfigurationError error) {
            debug("Transformation failed, " + error.toString());
        } catch (IOException e) {
            debug("Transformation failed, IOException: " + e.getMessage());
        }
        return d;
    }

    private void debug(String message) {
        System.out.println("[at.lux.fotoretrieval.panels.ResultsPanel] " + message);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -