package de.onyxbits.scenepainter.gui;

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import javax.swing.event.*;
import javax.swing.tree.*;

import de.onyxbits.scenepainter.scene.*;
import de.onyxbits.scenepainter.scene.elements.*;
import de.onyxbits.scenepainter.gui.dialogs.*;

/**
 * Brings the editor(s) to the front that can edit the most recently selected
 * element. Multiple editors can be bound to a single element and editors
 * may take priority over each other.
 */
class EditorRaiser implements TreeSelectionListener {

  private JDesktopPane workspace;
  
  /**
   * This structure maps elements to the editors. This is name based
   * because this class came in late and using classnames allows us
   * to do this without redesigning anything. It's also the most flexible
   * way of doing things at the neglectable disadvantage of having to know
   * and hardcode classnames (but you don't get around this anyway in one
   * form or the other, if you want to have priorities).
   * 
   * mapping[x][0] always contains the element name. All subsequent
   * entries contain the editors that should be raised for that element in
   * order of importance. The most important editor is the last one in
   * the array, which will be raised last and therefore ends up on top.
   */
  private String mapping[][] = {
    {
      "de.onyxbits.scenepainter.scene.elements.CompositeSceneElement",
      "de.onyxbits.scenepainter.gui.dialogs.transformer.View",
      "de.onyxbits.scenepainter.gui.dialogs.composite.View",
    },
    {
      "de.onyxbits.scenepainter.scene.elements.NenyaBubbleSceneElement",
      "de.onyxbits.scenepainter.gui.dialogs.transformer.View",
      "de.onyxbits.scenepainter.gui.dialogs.nenyabubble.View",
    },
    {
      "de.onyxbits.scenepainter.scene.elements.NenyaComponentSceneElement",
      "de.onyxbits.scenepainter.gui.dialogs.transformer.View",
      "de.onyxbits.scenepainter.gui.dialogs.nenyacolor.View",
      "de.onyxbits.scenepainter.gui.dialogs.nenyacomponent.View",
    },
    {
      "de.onyxbits.scenepainter.scene.elements.NenyaImageSceneElement",
      "de.onyxbits.scenepainter.gui.dialogs.transformer.View",
      "de.onyxbits.scenepainter.gui.dialogs.nenyacolor.View",
      "de.onyxbits.scenepainter.gui.dialogs.nenyaimage.View",
    },
    {
      "de.onyxbits.scenepainter.scene.elements.NenyaTileSceneElement",
      "de.onyxbits.scenepainter.gui.dialogs.transformer.View",
      "de.onyxbits.scenepainter.gui.dialogs.nenyacolor.View",
      "de.onyxbits.scenepainter.gui.dialogs.nenyatile.View",
    },
    {
      "de.onyxbits.scenepainter.scene.elements.SamskiverLabelSceneElement",
      "de.onyxbits.scenepainter.gui.dialogs.transformer.View",
      "de.onyxbits.scenepainter.gui.dialogs.samslabel.View",
    },
    
  };
  
  public EditorRaiser(JDesktopPane workspace) {
    this.workspace=workspace;
  }

  public void valueChanged(TreeSelectionEvent e) {
    try {
      TreePath tp = e.getNewLeadSelectionPath();
      SceneElement se = ((SceneNode)tp.getLastPathComponent()).getSceneElement();
      JInternalFrame[] allFrames = workspace.getAllFrames();
      
      for (int x=0;x<mapping.length;x++) {
        if (se.getClass().getName().equals(mapping[x][0])) {
          for (int y=1;y<mapping[x].length;y++) {
            for (int z=0;z<allFrames.length;z++) {
              Container content = allFrames[z].getContentPane();
              String name = content.getClass().getName();
              if (name.equals(mapping[x][y])) {
                allFrames[z].toFront();
                break; // For the sake of performance XD!!
              }
            }
          }
        }
      }
    }
    catch (Exception exp) {
      // Don't care
    }
  }
  
}
