Package graphmatcher.helper

Source Code of graphmatcher.helper.GraphLoader

package graphmatcher.helper;

import graphmatcher.graph.Graph;

import java.awt.Component;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Vector;

import javax.swing.JFileChooser;

import org.apache.log4j.Logger;

public class GraphLoader {
  private static Logger logger = Logger.getRootLogger();
  private static File workingDir;

  static {
    File file = new File("Z:/Diplomarbeit/Strokes");
    if (file.exists()) {
      workingDir = file;
    }
    file = new File("C:/Diplomarbeit/Schriftzeichen");
    if (file.exists()) {
      workingDir = file;
    }
    logger.info("Stroke-Verzeichnis: " + file);
  }

  public static boolean openGraphs(Component parent, Vector<Graph> graphs) {
    JFileChooser chooser = new JFileChooser(workingDir);

    GraphFileFilter filter = new GraphFileFilter();
    chooser.setFileFilter(filter);
    chooser.setMultiSelectionEnabled(true);

    int returnVal = chooser.showOpenDialog(parent);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
      File[] files = chooser.getSelectedFiles();
      Graph g;

      for (int i = 0; i < files.length; i++) {
        try {
          g = new Graph(files[i]);
          graphs.add(g);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
      return true;
    }
    return false;
  }

  public static Graph loadGraph(String dir, String name) {
    String path = workingDir.getAbsolutePath() + "\\" + dir + "\\" + name + ".graph";
    System.out.println(path);
    File file = new File(path);
    Graph result;
    try {
      result = new Graph(file);
    } catch (NumberFormatException e) {
      e.printStackTrace();
      return null;
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    }
    return result;
  }

  /**
   *
   * @param folderName
   *            Verzeichnis
   * @param numberOfGraphs
   *            Anzahl der zu ladenden Graphen
   * @return
   */
  public static Vector<Graph> loadGraphs(String folderName, int numberOfGraphs, int firstNGraphs) {
    logger.info("lade Graphen aus Ordner " + folderName);
    Vector<Graph> result = new Vector<Graph>();
    File[] folders = workingDir.listFiles();
    FilenameFilter filter = new FilenameFilter() {
      @Override
      public boolean accept(File dir, String name) {
        if (name.endsWith(".graph")) {
          return true;
        }
        return false;
      }
    };
    for (File folder : folders) {
      if (!folder.getName().startsWith(folderName)) {
        continue;
      }
      File[] files = folder.listFiles(filter);
      int numberOfRelevantGraphsInDir = Math.min(files.length, firstNGraphs);
      logger.info("Anzahl Graphen im Verzeichnis: " + numberOfRelevantGraphsInDir);
      int stepSize;
      if (numberOfGraphs >= numberOfRelevantGraphsInDir) {
        stepSize = 1;
      } else {
        stepSize = numberOfRelevantGraphsInDir / numberOfGraphs;
      }

      int counter = 0;
      for (int i = 0; i < numberOfRelevantGraphsInDir; i += stepSize) {
        File graphFile = files[i];
        if (counter >= numberOfGraphs) {
          break;
        }
        try {
          Graph graph = new Graph(graphFile);
          result.add(graph);
        } catch (NumberFormatException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    return result;
  }
}
TOP

Related Classes of graphmatcher.helper.GraphLoader

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.