Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: mindbright/util/AWTConvenience.java


1   /******************************************************************************
2    *
3    * Copyright (c) 1998,99 by Mindbright Technology AB, Stockholm, Sweden.
4    *                 www.mindbright.se, info@mindbright.se
5    *
6    * This program is free software; you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation; either version 2 of the License, or
9    * (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   *****************************************************************************
17   * $Author: nallen $
18   * $Date: 2001/11/12 16:31:29 $
19   * $Name:  $
20   *****************************************************************************/
21  package mindbright.util;
22  
23  import java.awt.*;
24  import java.awt.event.*;
25  
26  public abstract class AWTConvenience {
27  
28      public static class CloseAction implements ActionListener {
29    Dialog dialog;
30    public CloseAction(Dialog dialog) {
31        this.dialog = dialog;
32    }
33    public void actionPerformed(ActionEvent e) {
34        dialog.setVisible(false);
35    }
36      }
37  
38      public static class CloseAdapter extends WindowAdapter {
39    Button b;
40    public CloseAdapter(Button b) {
41        this.b = b;
42    }
43    public void windowClosing(WindowEvent e) {
44        b.dispatchEvent(new ActionEvent(b, ActionEvent.ACTION_PERFORMED,
45                b.getActionCommand()));
46    }
47      }
48  
49      public static class OKCancelAdapter extends KeyAdapter {
50      
51    protected static boolean isMRJ = false;
52    
53    static {
54        try { // see <http://developer.apple.com/qa/java/java17.html>
55      isMRJ = (System.getProperty("mrj.version") != null);
56        } catch (Exception e) {
57      // applets may not be able to do this
58        }
59    }
60   
61    Button butOK;
62    Button butCancel;
63    
64    public OKCancelAdapter(Button ok, Button cancel) {
65        this.butOK = ok;
66        this.butCancel = cancel;
67    }
68    
69    protected void pushButton(Button target) {
70        if (isMRJ) { // see <http://developer.apple.com/qa/java/java01.html>
71      target.dispatchEvent(new KeyEvent(target, KeyEvent.KEY_PRESSED, 
72                System.currentTimeMillis(), 0, KeyEvent.VK_ENTER, 
73                (char)KeyEvent.VK_ENTER));
74        } else { // still can work, just no visual feedback
75      target.dispatchEvent(new ActionEvent(target, ActionEvent.ACTION_PERFORMED,
76                   target.getActionCommand()));
77        }
78    }
79    
80    public void keyReleased(KeyEvent e) {
81        switch(e.getKeyCode()) {
82        case KeyEvent.VK_ENTER : if (butOK != null) pushButton(butOK); break;
83        case KeyEvent.VK_ESCAPE : if (butCancel != null) pushButton(butCancel); break;
84        }
85    }
86      }
87  
88      public final static void placeDialog(Dialog diag) {
89    Dimension sDim = Toolkit.getDefaultToolkit().getScreenSize();
90    Dimension mDim = diag.getSize();
91    int x, y;
92    x = ((sDim.width / 2) - (mDim.width / 2));
93    y = ((sDim.height / 2) - (mDim.height / 2));
94    diag.setLocation(x, y);
95      }
96  
97      public final static void setBackgroundOfChildren(Container container) {
98    Component[] children = container.getComponents();
99    container.setBackground(SystemColor.menu);
100   for(int i = 0; i < children.length; i++) {
101       if(children[i] instanceof Choice)
102     continue;
103       children[i].setBackground(SystemColor.menu);
104 
105       if(children[i] instanceof Container) {
106     setBackgroundOfChildren((Container)children[i]);
107       } else if(children[i] instanceof Choice) {
108     continue;
109       } else if(children[i] instanceof TextField || children[i] instanceof List) {
110     children[i].setBackground(SystemColor.text);
111       } else {
112     children[i].setBackground(SystemColor.menu);
113       }
114   }
115     }
116 
117     public final static void setKeyListenerOfChildren(Container container, KeyListener listener,
118                   Class typeOfChild) {
119   Component[] children = container.getComponents();
120   for(int i = 0; i < children.length; i++) {
121       if(children[i] instanceof Choice)
122     continue;
123       if(children[i] instanceof Container) {
124     setKeyListenerOfChildren((Container)children[i], listener, typeOfChild);
125       } else if(children[i] != null && (typeOfChild == null ||
126                 typeOfChild.isInstance(children[i]))) {
127     children[i].addKeyListener(listener);
128       }
129   }
130     }
131 
132 }