/* LanguageTool, a natural language style checker 
 * Copyright (C) 2007 Daniel Naber (http://www.danielnaber.de)
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */
package org.languagetool.gui;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import javax.swing.filechooser.FileFilter;

import org.languagetool.Language;
import org.languagetool.language.LanguageBuilder;

/**
 * Dialog for managing externally loaded rules.
 * 
 * @author Daniel Naber
 */
public class LanguageManagerDialog implements ActionListener {

  private JDialog dialog;
  
  private JList list; 
  private JButton addButton;
  private JButton removeButton;
  private JButton closeButton;
  private final List<File> ruleFiles = new ArrayList<File>();
  
  private final Frame owner;
  //private ResourceBundle messages = null;
  
  public LanguageManagerDialog(Frame owner, List<Language> languages) {
    this.owner = owner;
    for (Language lang : languages) {
        for (final String ruleFile : lang.getRuleFileName()) {
            ruleFiles.add(new File(ruleFile));
        }
    }
    //messages = JLanguageTool.getMessageBundle();
  }
  
  public void show() {
    dialog = new JDialog(owner, true);
    dialog.setTitle(Messages.getString("guiLanguageManagerDialog"));
    
    // close dialog when user presses Escape key:
    // TODO: taken from ConfigurationDialog, avoid duplication:
    final KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
    final ActionListener actionListener = new ActionListener() {
      @Override
      @SuppressWarnings("unused")
      public void actionPerformed(ActionEvent actionEvent) {
        dialog.setVisible(false); 
      }
    };
    final JRootPane rootPane = dialog.getRootPane();
    rootPane.registerKeyboardAction(actionListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);

    final Container contentPane = dialog.getContentPane();
    contentPane.setLayout(new GridBagLayout());
    
    list = new JList(ruleFiles.toArray(new File[]{}));
    GridBagConstraints cons = new GridBagConstraints();
    cons.insets = new Insets(4, 4, 4, 4);
    cons.gridx = 0;
    cons.gridy = 0;
    cons.fill = GridBagConstraints.BOTH;
    cons.weightx = 2.0f;
    cons.weighty = 2.0f;
    contentPane.add(new JScrollPane(list), cons);
    
    cons = new GridBagConstraints();
    cons.insets = new Insets(4, 4, 4, 4);
    cons.fill = GridBagConstraints.HORIZONTAL;
    
    final JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridBagLayout());
    addButton = new JButton(Messages.getString("guiAddButton"));
    addButton.addActionListener(this);
    cons.gridx = 1;
    cons.gridy = 0;
    buttonPanel.add(addButton, cons);

    removeButton = new JButton(Messages.getString("guiRemoveButton"));
    removeButton.addActionListener(this);
    cons.gridx = 1;
    cons.gridy = 1;
    buttonPanel.add(removeButton, cons);

    closeButton = new JButton(Messages.getString("guiCloseButton"));
    closeButton.addActionListener(this);
    cons.gridx = 1;
    cons.gridy = 2;
    buttonPanel.add(closeButton, cons);

    cons.gridx = 1;
    cons.gridy = 0;
    cons = new GridBagConstraints();
    cons.anchor = GridBagConstraints.NORTH;
    contentPane.add(buttonPanel, cons);
    
    dialog.pack();
    dialog.setSize(300, 200);
    // center on screen:
    final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    final Dimension frameSize = dialog.getSize();
    dialog.setLocation(screenSize.width/2 - (frameSize.width/2), screenSize.height/2 - (frameSize.height/2));
    dialog.setVisible(true);
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == addButton) {
      final File ruleFile = Tools.openFileDialog(null, new XMLFileFilter());
      if (!isOnList(ruleFile)) {
          ruleFiles.add(ruleFile);      
          list.setListData(ruleFiles.toArray(new File[]{}));
      } else {
         JOptionPane jop = new JOptionPane();         
          JOptionPane.showMessageDialog(jop, Messages.getString("guiDuplicate"), 
                  Messages.getString("guiWarning"), JOptionPane.WARNING_MESSAGE);          
      }
    } else if (e.getSource() == removeButton) {
      if (list.getSelectedIndex() != -1) {
        ruleFiles.remove(list.getSelectedIndex());
        list.setListData(ruleFiles.toArray(new File[]{}));
      }
    } else if (e.getSource() == closeButton) {
      dialog.setVisible(false);
    } else {
      throw new IllegalArgumentException("Don't know how to handle " + e); //$NON-NLS-1$
    }
  }
  
  private boolean isOnList(final File file) {
      for (File f : ruleFiles) {
          if (f.equals(file)) {
              return true;
          }
      }
      return false;
  }
  
  /**
   * Return all external Languages.
   */
  List<Language> getLanguages() {
    final List<Language> languages = new ArrayList<Language>();
    for (File ruleFile : ruleFiles) {
      if (ruleFile != null) {
        final Language newLanguage = LanguageBuilder.makeAdditionalLanguage(ruleFile);
        languages.add(newLanguage);
      }
    }
    return languages;
  }
  
  static class XMLFileFilter extends FileFilter {
    @Override
    public boolean accept(final File f) {
      if (f.getName().startsWith("rules") && f.getName().toLowerCase().endsWith(".xml") 
              || f.isDirectory()) { //$NON-NLS-1$
        return true;
      }
      return false;
    }
    @Override
    public String getDescription() {
      return "*.xml"; //$NON-NLS-1$
    }
  }

}

@@ -5043,24 +5043,34 @@
    if (!
-isOnList
+ruleFiles.contains
 (ruleFil
@@ -5067,34 +5067,32 @@
 ns(ruleFile)) %7B%0A
-  
         ruleFile
@@ -5107,25 +5107,17 @@
 leFile);
-      %0A  
+%0A
         
@@ -5182,33 +5182,32 @@
  else %7B%0A        
- 
 JOptionPane jop 
@@ -5226,28 +5226,17 @@
 nPane();
-         %0A  
+%0A
         
@@ -5305,17 +5305,16 @@
 icate%22),
- 
 %0A       
@@ -5322,18 +5322,16 @@
         
-  
 Messages
@@ -5385,26 +5385,16 @@
 ESSAGE);
-          
 %0A      %7D
@@ -5801,193 +5801,8 @@
 %0A  %0A
-  private boolean isOnList(final File file) %7B%0A      for (File f : ruleFiles) %7B%0A          if (f.equals(file)) %7B%0A              return true;%0A          %7D%0A      %7D%0A      return false;%0A  %7D%0A  %0A
   /*

simplify code