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

java.com.generationjava.swing.JBasicApplication Maven / Gradle / Ivy

The newest version!
package com.generationjava.swing;


import com.generationjava.awt.ReportEvent;
import com.generationjava.awt.RequestEvent;
import com.generationjava.awt.MulticastRequestEvent;
import com.generationjava.awt.InformationListener;

import java.awt.Component;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import javax.swing.JSeparator;
import javax.swing.KeyStroke;

import java.awt.FileDialog;

import java.util.Hashtable;
import java.util.Vector;
import java.util.Enumeration;

import com.generationjava.lang.StringW;
import org.apache.commons.lang.StringUtils;

/**
 * A simple framework which allows files to be opened and 
 * many files be in an open state. Thus files may also be closed.
 */
abstract public class JBasicApplication extends JFrame 
       implements WindowListener, ActionListener 
{

    public final int LOAD = FileDialog.LOAD;
    
    private String currentDir;
    private Object currentObj;
    private Hashtable openList;
    private JMenu openMenu;
    private Hashtable menus;
    
    private Vector listeners;

    // used to signify whether the ctrl-,/ctrl-. is in effect.
    private boolean existsBefore;
    private boolean existsAfter;
    
    public JBasicApplication(String title) {
        super(title);
        openList = new Hashtable();
        menus = new Hashtable();
        JMenuBar bar = new JMenuBar();
        this.setJMenuBar(bar);

        registerMenuItem("File","Open","O");
        registerMenuItem("File","Close","Q");
        createNewBlock("File",0);
        registerMenuItem("File","Exit","E");
        openMenu = (JMenu)registerMenu("Opened");
        registerMenuItem("Help","About","Shift-A");
        registerMenuItem("Help","Licence","Shift-L");
        registerMenuItem("Help","QuickHelp","Shift-H");
    }

    // Menu Handling Code
    private void addTopLevelMenu(JMenu menu) {
        // push after help menu
        getJMenuBar().add(menu);
    }
    // will not return null
    private JMenu getMenu(String fqname) {
        JMenu m = (JMenu)menus.get(fqname);
        if(m == null) {
            m = createMenu(getLabel(getMenuName(fqname)));
            String parentName = StringUtils.substringBefore(fqname,".");
            if("".equals(parentName)) {
                addTopLevelMenu(m);
            } else {
                JMenu parent = getMenu(parentName);
                // parent can't be null
                parent.add(m);
            }
            menus.put(fqname,m);
        }
        return m;
    }
    private String getMenuName(String fqname) {
        return StringUtils.substringAfter(fqname,".");
    }
    // basically inserts a JSeparator
    protected void createNewBlock(String menuName, int block) {
        JMenu menu = getMenu(menuName);
        int idx = getBlockIndex(menu, block);
        menu.insertSeparator(idx);
    }
    // Creates a sub-menu. If menuName is "", then goes in root.
    protected Object registerMenu(String subMenuName, char mnemonic) {
        JMenu menu = getMenu(subMenuName);
        menu.setMnemonic( (int)mnemonic );
        return menu;
    }
    protected Object registerMenu(String subMenuName) {
        return getMenu(subMenuName);
    }
    protected Object registerMenu(String menuName, String subMenuName, String mnemonic) {
        return registerMenu(menuName, subMenuName, -2, null);
    }
    protected Object registerMenu(String menuName, String subMenuName, int block, String mnemonic) {
        JMenu parent = getMenu(menuName);
        int idx = getBlockIndex(parent,block);
        JMenu menu = createMenu( getLabel(subMenuName) );
        if(mnemonic != null) {
            menu.setMnemonic((int)mnemonic.charAt(0));
        }
        parent.insert(menu, idx);
        return menu;
    }
    private JMenu createMenu(String menuName) {
        JMenu menu = new JMenu( menuName);
        menu.setMnemonic( (int)menuName.charAt(0) );
        return menu;
    }
    protected Object registerMenuItem(String menuName, String itemName) {
        return registerMenuItem(menuName, itemName, -2, null);
    }
    protected Object registerMenuItem(String menuName, String itemName, String ch) {
        return registerMenuItem(menuName, itemName, -2, ch);
    }
    protected Object registerMenuItem(String menuName, String itemName, int block) {
        return registerMenuItem(menuName, itemName, -2, null);
    }
    protected Object registerMenuItem(String menuName, String itemName, int block, String ch) {
        String label = getLabel(itemName);
        JMenuItem mi = new JMenuItem( label );
        // set up accelerator?
        if(ch != null) {
            int mask = 0;
            if(ch.indexOf("Shift") != -1) {
                mask |= ActionEvent.SHIFT_MASK;
            }
            if(true) {
                mask |= ActionEvent.CTRL_MASK;
            }
            int key = (int)ch.charAt(ch.length()-1);
            mi.setAccelerator( KeyStroke.getKeyStroke(key, mask) );
        }
        mi.addActionListener(this);
        return registerMenuItem(menuName, mi, block);
    }
    protected Object registerMenuItem(String menuName, JMenuItem item, int block) {
        JMenu m = (JMenu)getMenu(menuName);
        int idx = getBlockIndex(m, block);
        m.insert(item,idx);
        return m;
    }

    // -2 implies the end, -1 means right at the front
    private int getBlockIndex(JMenu menu, int block) {
        if(block == -1) {
            return 0;
        }
        Component[] comps = menu.getMenuComponents();
        if(block == -2) {
            return comps.length;
        }
        int i;
        int sz = comps.length;
        for(i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy