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

net.alantea.swing.Application Maven / Gradle / Ivy

The newest version!
package net.alantea.swing;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;

import net.alantea.liteprops.ChangeListener;
import net.alantea.liteprops.Property;
import net.alantea.swing.action.SelectionManager;
import net.alantea.swing.misc.SwingUtils;
import net.alantea.swing.panels.MainContent;
import net.alantea.swing.ribbon.Ribbon;
import net.alantea.utils.MultiMessages;

@SuppressWarnings("serial")
public class Application extends JFrame
{
   private static Application instance;
   private static MainContent mainContent;
   private static Ribbon ribbon;
   private static SelectionManager selectionManager = new SelectionManager();
   
   private static Property globalSelection = new Property();

   static
   {
      globalSelection.addListener(selectionManager);
   }
   
   /**
    * Create the application window.
    */
   public Application()
   {
      MultiMessages.addAssociatedBundle(this);
      this.setLayout(new BorderLayout());
      this.setTitle(MultiMessages.get("Main.title"));
      
      createContents();
//      addStatusLine();
   }
   
   public static Application getInstance()
   {
      return instance;
   }
   
   public static void applyFontFactor()
   {
      SwingUtils.setUIFontPercentSize(getFontFactor());
   }

   public static void prepareFontFactor(double percent)
   {
      net.alantea.tools.configuration.Configuration.setConfigurationValue("fontFactor", ""+ percent);
   }

   public static double getFontFactor()
   {
      String size = net.alantea.tools.configuration.Configuration.getConfigurationValue("fontFactor");
      if (size != null)
      {
         try
         {
            return Double.parseDouble(size);
         }
         catch (NumberFormatException e)
         {
         }
      }
      return 1.0;
   }

   /**
    * Create contents of the application window.
    *
    * @return the main content
    */
   protected MainContent createContents()
   {
      UIManager.put("Tree.rendererFillBackground", false);
      UIManager.put("Tree.paintLines", Boolean.FALSE);
      createActions();
      mainContent = createMainContent();
      add(mainContent);
      ribbon = mainContent.getRibbon();
      return mainContent;
   }

   /**
    * Create the actions.
    * 
    */
   protected void createActions()
   {
      // For subclasses
   }

   /**
    * Create the main content.
    *
    * @return the main content
    */
   protected MainContent createMainContent()
   {
      MainContent ret = new MainContent();
      add(ret, BorderLayout.CENTER);
      return ret;
   }

   /**
    * Launch the application.
    */
   public void start()
   {
        //Display the window.
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        toFront();
   }
   
   public static MainContent getMainContent()
   {
      return mainContent;
   }

   public static void setMainContent(MainContent content)
   {
      mainContent = content;
   }
   
   public static Ribbon getRibbon()
   {
      return ribbon;
   }

   public static void setRibbon(Ribbon content)
   {
      ribbon = content;
   }
   
   public static JTabbedPane getInfoFolder()
   {
      return mainContent.getInfoFolder();
   }
   
   public static JTabbedPane getTabFolder()
   {
      return mainContent.getTabFolder();
   }
   
   public static void addSelectionListener(Class targetClass, ChangeListener listener)
   {
      selectionManager.addSelectionListener(targetClass, listener);
   }
   
   public static void removeSelectionListener(ChangeListener listener)
   {
      selectionManager.removeSelectionListener(listener);
   }
   
   public static void removeSelectionListener(Class targetClass, ChangeListener listener)
   {
      selectionManager.removeSelectionListener(targetClass, listener);
   }
   
   public static void setGlobalSelection(Object selection)
   {
      globalSelection.set(selection);
   }
   
   public static Object getGlobalSelection()
   {
      return globalSelection.get();
   }
}