<commit_before>/*
 * Copyright 2009-2011 Collaborative Research Centre SFB 632 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.hu_berlin.german.korpling.annis.kickstarter;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import javax.imageio.ImageIO;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;

import org.slf4j.LoggerFactory;

import annis.AnnisBaseRunner;
import annis.administration.CorpusAdministration;
import annis.administration.ImportStatus;
import annis.utils.Utils;

/**
 *
 * @author thomas
 */
public class MainFrame extends javax.swing.JFrame
{
  
  private static final org.slf4j.Logger log = LoggerFactory.getLogger(
    MainFrame.class);

  private class MainFrameWorker extends SwingWorker<String, String>
  {
    
    private final KickstartRunner delegate = new KickstartRunner();

    @Override
    protected String doInBackground() throws Exception
    {
      delegate.resetRunner();
      setProgress(1);
      try
      {
        delegate.startService();
        setProgress(2);
        delegate.startJetty();
      }
      catch (Exception ex)
      {
        return ex.getLocalizedMessage();
      }
      return "";
    }


    @Override
    protected void done()
    {
      try 
      {
        final String result = get();
        SwingUtilities.invokeLater(new Runnable()
        {
          @Override
          public void run()
          {
            handleServiceStartResult(result);
          }
        });
      }
      catch (InterruptedException ex)
      {
        log.error(null, ex);
      }
      catch (ExecutionException ex)
      {
        log.error(null, ex);
      }
    }
    
    public void setTimeoutDisabled(boolean disabled)
    {
      delegate.setTimeoutDisabled(disabled);
    }
    
  } //end MainFrameWorker class
  private CorpusAdministration corpusAdministration;
  private MainFrameWorker serviceWorker;
  private boolean wasStarted = false;
  
  
  /**
   * Creates new form MainFrame
   */
  public MainFrame()
  {
    Integer[] sizes = new Integer[]
    {
      192, 128, 64, 48, 32, 16, 14
    };
    List<Image> allImages = new LinkedList<Image>();

    for (int s : sizes)
    {
      try
      {
        BufferedImage imgIcon = ImageIO.read(MainFrame.class.getResource(
          "logo/annis_" + s + ".png"));
        allImages.add(imgIcon);
      }
      catch (IOException ex)
      {
        log.error(null, ex);
      }
    }
    this.setIconImages(allImages);

    // find the location of the kickstarter
    if(System.getProperty("annis.home") == null)
    {
      try
      {
       URL classLocation = getClass().getProtectionDomain().getCodeSource().getLocation();
       File jarFile = new File(classLocation.toURI());
       // check if this is an actual jar file or only a folder
       if(jarFile.isFile())
       {
         System.setProperty("annis.home", jarFile.getParent());
       }
       else
       {
         // fallback to current working directory
         System.setProperty("annis.home", ".");
       }
      }
      catch(SecurityException | URISyntaxException ex)
      {
        log.warn("Could not reliable get the location of ANNIS Kickstarter, fallback to working directory is used.", ex);
        // fallback to current working directory
        System.setProperty("annis.home", ".");
      }
    }
    

    // init corpusAdministration
    this.corpusAdministration =
      (CorpusAdministration) AnnisBaseRunner.getBean("corpusAdministration",
      true, "file:"
      + Utils.getAnnisFile("conf/spring/Admin.xml").getAbsolutePath());

    try
    {
      for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels())
      {
        if ("Nimbus".equals(info.getName()))
        {
          UIManager.setLookAndFeel(info.getClassName());
          break;
        }
      }
    }
    catch (ClassNotFoundException | IllegalAccessException | InstantiationException | UnsupportedLookAndFeelException ex)
    {
      log.error(null, ex);
    }

    initComponents();


    serviceWorker = new MainFrameWorker();
    serviceWorker.addPropertyChangeListener(new PropertyChangeListener()
    {

      public void propertyChange(PropertyChangeEvent evt)
      {
        if (serviceWorker.getProgress() == 1)
        {
          pbStart.setIndeterminate(true);
          lblStatusService.setText("<html>Starting ANNIS...</html>");
          lblStatusService.setIcon(
            new javax.swing.ImageIcon(
            getClass().getResource(
            "/de/hu_berlin/german/korpling/annis/kickstarter/crystal_icons/quick_restart.png")));<commit_msg>Set slightly bigger font size on larger resolutions.<commit_after>/*
 * Copyright 2009-2011 Collaborative Research Centre SFB 632 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.hu_berlin.german.korpling.annis.kickstarter;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import javax.imageio.ImageIO;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;

import org.slf4j.LoggerFactory;

import annis.AnnisBaseRunner;
import annis.administration.CorpusAdministration;
import annis.administration.ImportStatus;
import annis.utils.Utils;
import java.awt.Font;
import java.awt.Toolkit;
import javax.swing.UIDefaults;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;

/**
 *
 * @author thomas
 */
public class MainFrame extends javax.swing.JFrame
{
  
  private static final org.slf4j.Logger log = LoggerFactory.getLogger(
    MainFrame.class);

  private class MainFrameWorker extends SwingWorker<String, String>
  {
    
    private final KickstartRunner delegate = new KickstartRunner();

    @Override
    protected String doInBackground() throws Exception
    {
      delegate.resetRunner();
      setProgress(1);
      try
      {
        delegate.startService();
        setProgress(2);
        delegate.startJetty();
      }
      catch (Exception ex)
      {
        return ex.getLocalizedMessage();
      }
      return "";
    }


    @Override
    protected void done()
    {
      try 
      {
        final String result = get();
        SwingUtilities.invokeLater(new Runnable()
        {
          @Override
          public void run()
          {
            handleServiceStartResult(result);
          }
        });
      }
      catch (InterruptedException ex)
      {
        log.error(null, ex);
      }
      catch (ExecutionException ex)
      {
        log.error(null, ex);
      }
    }
    
    public void setTimeoutDisabled(boolean disabled)
    {
      delegate.setTimeoutDisabled(disabled);
    }
    
  } //end MainFrameWorker class
  private CorpusAdministration corpusAdministration;
  private MainFrameWorker serviceWorker;
  private boolean wasStarted = false;
  
  
  /**
   * Creates new form MainFrame
   */
  public MainFrame()
  {
    Integer[] sizes = new Integer[]
    {
      192, 128, 64, 48, 32, 16, 14
    };
    List<Image> allImages = new LinkedList<Image>();

    for (int s : sizes)
    {
      try
      {
        BufferedImage imgIcon = ImageIO.read(MainFrame.class.getResource(
          "logo/annis_" + s + ".png"));
        allImages.add(imgIcon);
      }
      catch (IOException ex)
      {
        log.error(null, ex);
      }
    }
    this.setIconImages(allImages);

    // find the location of the kickstarter
    if(System.getProperty("annis.home") == null)
    {
      try
      {
       URL classLocation = getClass().getProtectionDomain().getCodeSource().getLocation();
       File jarFile = new File(classLocation.toURI());
       // check if this is an actual jar file or only a folder
       if(jarFile.isFile())
       {
         System.setProperty("annis.home", jarFile.getParent());
       }
       else
       {
         // fallback to current working directory
         System.setProperty("annis.home", ".");
       }
      }
      catch(SecurityException | URISyntaxException ex)
      {
        log.warn("Could not reliable get the location of ANNIS Kickstarter, fallback to working directory is used.", ex);
        // fallback to current working directory
        System.setProperty("annis.home", ".");
      }
    }
    

    // init corpusAdministration
    this.corpusAdministration =
      (CorpusAdministration) AnnisBaseRunner.getBean("corpusAdministration",
      true, "file:"
      + Utils.getAnnisFile("conf/spring/Admin.xml").getAbsolutePath());

    try
    {
      UIManager.setLookAndFeel(new NimbusLookAndFeel()
      {
        @Override
        public UIDefaults getDefaults()
        {
          UIDefaults defaults = super.getDefaults();
          if(Toolkit.getDefaultToolkit().getScreenSize().width > 2000)
          {
            defaults.put("defaultFont", new Font(Font.SANS_SERIF, Font.PLAIN, 18));
          }
          return defaults;
        }
        
      });
    }
    catch (UnsupportedLookAndFeelException ex)
    {
      log.error(null, ex);
    }

    initComponents();


    serviceWorker = new MainFrameWorker();
    serviceWorker.addPropertyChangeListener(new PropertyChangeListener()
    {

      public void propertyChange(PropertyChangeEvent evt)
      {
        if (serviceWorker.getProgress() == 1)
        {
          pbStart.setIndeterminate(true);
          lblStatusService.setText("<html>Starting ANNIS...</html>");
          lblStatusService.setIcon(
            new javax.swing.ImageIcon(
            getClass().getResource(
            "/de/hu_berlin/german/korpling/annis/kickstarter/crystal_icons/quick_restart.png")));
{'fraction_non_alphanumeric': 0.09954908081859175, 'fraction_numerical': 0.006763787721123829, 'mean_word_length': 2.638170347003155, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2282059', 'n_tokens_mistral': 3378, 'n_tokens_neox': 3262, 'n_words': 773}