All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.alantea.swing.tree.TreeObject Maven / Gradle / Ivy

The newest version!
package net.alantea.swing.tree;

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.util.LinkedList;
import java.util.List;

import javax.swing.Action;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.tree.DefaultMutableTreeNode;

import net.alantea.swing.Application;
import net.alantea.swing.action.ActionManager;
import net.alantea.swing.edition.ElementEditorTab;
import net.alantea.swing.misc.Methods;
import net.alantea.swing.misc.ResourceManager;

@SuppressWarnings("serial")
public abstract class TreeObject extends DefaultMutableTreeNode
{
   private List listeners = new LinkedList<>();
   private JPopupMenu menu;
   private String image;

   public TreeObject(T entity)
   {
      this.setUserObject(entity);
   }
   
   @SuppressWarnings("unchecked")
   public T getEntity()
   {
      return (T) this.getUserObject();
   }

   public abstract String getName();

   public abstract String getDescription();

   public abstract void setDescription(String value);

   public void setImage(String image)
   {
      this.image = image;
      fireChanged();
   }

   public void setParent(TreeParent parent)
   {
      this.parent = parent;
      fireChanged();
   }

   /* (non-Javadoc)
    * @see java.lang.Object#toString()
    */
   public String toString()
   {
      return getName();
   }

   /**
    * Adds the listener.
    *
    * @param listener the listener
    */
   public void addListener(TreeListener listener)
   {
      if (!listeners.contains(listener))
      {
         listeners.add(listener);
      }
   }
   
   /**
    * Removes the listener.
    *
    * @param listener the listener
    */
   public void removeListener(TreeListener listener)
   {
      if (listeners.contains(listener))
      {
         listeners.remove(listener);
      }
   }

   public void onMenuShown(NavigationTree tree, JPopupMenu menu)
   {
      this.menu = menu;
   }

   protected void addMenuItem(String text, Runnable running)
   {
      Action action = ActionManager.createAction(text, running);
      addMenuItem(text, action);
   }

   protected void addMenuItem(String text, Action listener)
   {
      JMenuItem newItem = new JMenuItem(text);
      menu.add(newItem);
      if (listener != null)
      {
         listener.putValue(Action.NAME, text);
         newItem.setAction(listener);
      }
      else
      {
         newItem.setText(text);
         newItem.setEnabled(false);
      }
   }

   protected void addMenuSeparator(JPopupMenu menu)
   {
      menu.addSeparator();
   }
   
   /**
    * Fire the change.
    */
   protected void fireChanged()
   {
      for (TreeListener listener : listeners)
      {
         try
         {
            listener.changed(this);
         }
         catch (Exception e)
         {
            // silently ignore it
         }
      }
   }
   
   protected ElementEditorTab initializeEditorTab()
   {
      return null;
   }
   
   protected void renameObjectEvent(ActionEvent e)
   {
      String param = Methods.ask(Application.getInstance(), "InputDialogs.rename", getName());
      setName(param);
   }
   
   protected abstract void setName(String newName);
   
   public Image getImage()
   {
      if (image != null)
      {
         return ResourceManager.getImage(image, 24);
      }
      return null;
   }
   
   public NavigationTree getNavigationTree()
   {
      return null;
   }

   
   protected void editObjectEvent()
   {
      initializeEditorTab();
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy