/******************* * Class QwertyPanel (v7) offers basic keyboard-style entry from a gridbag of onscreen * buttons. All letters are upper-case only. Enter, dot, hyphen, space and backspace * are also available. At the top left there is a button that switches between * qwerty and alphabetic layout. There is a method to re-label any of the four 'soft' * Fkeys in the top row. They default to F1 thru F4 - if they are re-labeled as "" * they are automatically disabled. There are also methods to enable/disable a single * key or all keys, to add and remove ActionListeners, to switch between layouts and to * interrogate which layout is being used and whether a particular key is enabled. * The colors used for enabled and disabled buttons can be set in the overloaded constructor * and are set as public in case you want to change them after instantiation. This is made * easier to achieve by subclassing Button as QPButton and adding relevant functionality * to its constructor. ActionCommands passed are the strings that appear on the face * of the buttons. * * This version now includes numbers too. * * Constructors: QwertyPanel(), * QwertyPanel(int LAYOUT_TYPE), * QwertyPanel(int LAYOUT_TYPE, Color enabledColor, Color disabledColor) * * Public methods: void setFkey(int number, String label) * void setKeyEnabled(String label, boolean state) * void enableAllKeys() * void disableAllKeys() * boolean isKeyEnabled(String label) * boolean isQwertyLayout() * void setLayout(int LAYOUT_TYPE) * void addActionListener(ActionListener l) * void removeActionListener(ActionListener l) * * The previous version used two panels (one for each key layout) treated as a CardLayout. * This revised version uses one panel only and re-labels the keys to achieve different * key layouts. I found it more difficult in terms of logic and data management, * especially concerning keeping a record of which keys are dis/enabled. But it works * faster and was an interesting challenge. * * Andy Murray 23/11/02 */ import java.awt.*; import java.applet.*; import java.awt.event.*; import java.util.StringTokenizer; public class QwertyPanel extends Panel implements ActionListener { private Panel buttonPanel; private GridBagLayout bag; private GridBagConstraints c; private QPButton [] btn; private int totalButtons; public Color enabledColor, disabledColor; private Font boldFont, italicFont; private final StringTokenizer qwertyST, alphaST; private String [] qwertyLabels, alphaLabels; private boolean [] enabledFlags;//indexed by alpha position public static final int ALPHA_LAYOUT =0, QWERTY_LAYOUT =1; TextField peek;//***************for debugging /** * This QwertyPanel constructor allows for the fullest num of instance variables */ QwertyPanel(int LAYOUT_TYPE, Color enabledColor, Color disabledColor) { totalButtons=46; peek = new TextField(40); alphaST = new StringTokenizer("dummy,F1,F2,F3,F4,1,2,3,4,5,6,7,8,9,0,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,Enter,T,U,V,W,X,Y,Z,.,-,<<<,Space", ","); qwertyST = new StringTokenizer("dummy,F1,F2,F3,F4,1,2,3,4,5,6,7,8,9,0,Q,W,E,R,T,Y,U,I,O,P,A,S,D,F,G,H,J,K,L,Enter,Z,X,C,V,B,N,M,.,-,<<<,Space", ","); qwertyLabels = new String[totalButtons]; alphaLabels = new String[totalButtons]; enabledFlags = new boolean[totalButtons]; for (int i=0;i0) && (number<5) ) { btn[number].setLabel(label); if (label.length()==0) {btn[number].setEnabled(false);} } } /** * The setKeyEnabled method allows any QwertyPanel button (except the layout switch) * to be enabled or disabled. Both states have an associated background color. When * switching between alpha and qwerty layout, it is the identity of a button that * is dis/enabled, not a fixed position in the grid. */ // this was the brain-tangling bit! public void setKeyEnabled(String label, boolean state) { int visibleIndex, logicalIndex; visibleIndex = getBtnIndexFromLabel(label); if (visibleIndex>-1) { // peek.setText("vis index="+visibleIndex); //************************** logicalIndex=(isQwertyLayout()) ? qwertyToAlpha(visibleIndex) : visibleIndex; btn[visibleIndex].setEnabled(state); enabledFlags[logicalIndex]=state; } } /** * The isKeyEnabled method allows you to check the enabled/disabled state of any * button by referencing its label. The method returns true for an enabled button. */ public boolean isKeyEnabled(String label) { if (label.equals("")) { return false; } else { int index = getBtnIndexFromLabel(label); if (isQwertyLayout()) { index=qwertyToAlpha(index);} return (index>-1) ? enabledFlags[index] : false; } } /** * The enableAllKeys method does what it says on the tin, except that it doesn't * enable buttons with "" label (Fkeys can be set to have no label) */ public void enableAllKeys() { for (int i=1;i0)); enabledFlags[i]=true; } } /** * The disableAllKeys method does what it says on the tin, except that it doesn't * disable the alpha/qwerty switch. */ public void disableAllKeys() { for (int i=1;i