java.com.generationjava.awt.BasicApplication Maven / Gradle / Ivy
The newest version!
package com.generationjava.awt;
import com.generationjava.awt.GJMenuItem;
import com.generationjava.awt.ReportEvent;
import com.generationjava.awt.RequestEvent;
import com.generationjava.awt.MulticastRequestEvent;
import com.generationjava.awt.InformationListener;
import java.awt.Frame;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.Menu;
import java.awt.MenuShortcut;
import java.awt.FileDialog;
import java.util.Hashtable;
import java.util.Vector;
import java.util.Enumeration;
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.
*/
public abstract class BasicApplication extends Frame implements WindowListener, ActionListener {
private String currentDir;
private Object currentObj;
private Hashtable openList;
private Menu openMenu;
private Vector listeners;
private Hashtable menus;
// used to signify whether the ctrl-,/ctrl-. is in effect.
private boolean existsBefore;
private boolean existsAfter;
public BasicApplication(String title) {
super(title);
openList = new Hashtable();
menus = new Hashtable();
MenuBar bar = new MenuBar();
this.setMenuBar(bar);
registerMenuItem("File", "Open", "O");
registerMenuItem("File","Close","Q");
createNewBlock("File",0);
registerMenuItem("File","Exit","E");
openMenu = (Menu)registerMenu("Opened");
bar.setHelpMenu((Menu)registerMenu("Help"));
registerMenuItem("Help","About","Shift-A");
registerMenuItem("Help","Licence","Shift-L");
registerMenuItem("Help","QuickHelp","Shift-H");
/*
Menu m = new Menu( getLabel("File") );
m.addActionListener(this);
bar.add(m);
MenuItem mi = new GJMenuItem( getLabel("Open"), new MenuShortcut('O'), "Open" );
m.add(mi);
mi = new GJMenuItem( getLabel("Close"), new MenuShortcut('Q'), "Close" );
m.add(mi);
mi = new GJMenuItem( getLabel("Exit"), new MenuShortcut('E'), "Exit" );
m.add(mi);
openMenu = new Menu( getLabel("Opened") );
openMenu.addActionListener(this);
bar.add(openMenu);
m = new Menu( getLabel("Help") );
m.addActionListener(this);
bar.setHelpMenu(m);
mi = new GJMenuItem( getLabel("About"), new MenuShortcut('A',true), "About" );
m.add(mi);
*/
}
// Menu Handling Code
// Ported back from JBasicApplication
private void addTopLevelMenu(Menu menu) {
// push after help menu
getMenuBar().add(menu);
}
// will not return null
private Menu getMenu(String fqname) {
Menu m = (Menu)menus.get(fqname);
if(m == null) {
m = createMenu(getLabel(getMenuName(fqname)));
// TODO: Doesn't include the delimiter
String parentName = StringUtils.substringBefore(fqname,".");
if("".equals(parentName)) {
addTopLevelMenu(m);
} else {
Menu 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 separator
protected void createNewBlock(String menuName, int block) {
Menu 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) {
Menu menu = getMenu(subMenuName);
menu.setShortcut( new MenuShortcut((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) {
Menu parent = getMenu(menuName);
int idx = getBlockIndex(parent,block);
Menu menu = createMenu( getLabel(subMenuName) );
if(mnemonic != null) {
menu.setShortcut( new MenuShortcut((int)mnemonic.charAt(0)));
}
parent.insert(menu, idx);
return menu;
}
private Menu createMenu(String menuName) {
Menu menu = new Menu( menuName);
menu.setShortcut( new MenuShortcut((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);
MenuItem mi = new GJMenuItem( label, itemName );
// set up accelerator?
if(ch != null) {
int mask = 0;
if(ch.indexOf("Shift") != -1) {
mask |= ActionEvent.SHIFT_MASK;
}
/* Not in AWT.
if(true) {
mask |= ActionEvent.CTRL_MASK;
}
*/
int key = (int)ch.charAt(ch.length()-1);
// mi.setAccelerator( KeyStroke.getKeyStroke(key, mask) );
mi.setShortcut( new MenuShortcut( key, (mask != 0) ) );
}
mi.addActionListener(this);
return registerMenuItem(menuName, mi, block);
}
protected Object registerMenuItem(String menuName, MenuItem item, int block) {
Menu m = (Menu)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(Menu menu, int block) {
if(block == -1) {
return 0;
}
/* Not available in AWT. Need to clone functionality here
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