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

Quick Search    Search Deep

Source code: ledestin/swing/JEditableDialog.java


1   /*
2   * Copyright (C) 2000-2001 Dmitry Macsema.  All Rights Reserved.  
3   * 
4   * Redistribution and use in source and binary forms, with or without
5   * modification, are permitted under the terms of the following
6   * Open Source license:
7   *
8   * The GNU General Public License, version 2, or any later version, as
9   * published by the Free Software Foundation
10  * (http://www.fsf.org/copyleft/gpl.html);
11  */
12  
13  package ledestin.swing;
14  
15  import javax.swing.*;
16  import javax.swing.text.*;
17  import java.awt.*;
18  import java.awt.event.*;
19  
20  public abstract class JEditableDialog extends JDialog implements EditableDialog
21  {
22    protected int returnValue;
23  
24    public JEditableDialog(Dialog owner, String title, boolean modal) {
25      super(owner, title, modal);
26      initWindowListener();
27    }
28    public JEditableDialog(Frame owner, String title, boolean modal) {
29      super(owner, title, modal);
30      initWindowListener();
31    }
32  
33    protected void initWindowListener() {
34      addWindowListener(new WindowAdapter() {
35        public void windowClosing(WindowEvent e) {
36          returnValue = JOptionPane.CLOSED_OPTION;
37          hide();
38        }
39  /*      public void windowActivated(WindowEvent e) {
40          //move focus to the first non-panel element
41          JTextComponent c = findTextComponent(getComponents());
42          if (c != null) {
43            c.requestFocus();
44          }
45        }
46        private JTextComponent findTextComponent(Component[] c) {
47          JTextComponent tc = null;
48          for (int i=0; i<c.length; i++) {
49            if (c[i] instanceof Container) {
50              tc = findTextComponent(((Container)c[i]).getComponents());
51            } else {
52              if (c[i] instanceof JTextComponent) {
53                tc = (JTextComponent)c[i];
54              }
55            }
56            if (tc != null) {
57              break;
58            }
59          }
60          return tc;
61        }
62  */      
63      });
64    }
65    public int getReturnValue() {
66      return returnValue;
67    }
68    /**
69    When pressed dialog's "Ok" button (added with addOkCancelPanel()) this 
70    method will be called. So if you wish to do checking of input data,
71    override this method. If returns true, the dialog will be hidden else
72    nothing will happen.
73    @return by default returns true
74    */
75    public boolean isValidInput() {
76      return true;
77    }
78    
79    /**
80    Adds to content pane (at BorderLayout.SOUTH) ok and cancel buttons,
81    the result of calling dialog can be obtained with @see getReturnValue().
82    Content pane's layout should be BorderLayout and SOUTH region should be left
83    for the buttons.
84    */
85    public static void addOkCancelPanel(final JEditableDialog dlg) {
86      JButton okBtn = new JButton("Ok");
87      JButton cancelBtn = new JButton("Cancel");
88      JPanel btnPanel = new JPanel();
89  
90      okBtn.addActionListener(new ActionListener() {
91        public void actionPerformed(ActionEvent evt) {
92          if (dlg.isValidInput()) {
93            dlg.returnValue = JOptionPane.OK_OPTION;
94            dlg.hide();
95          }
96        }
97      });
98  
99      ActionListener cancelAL = new ActionListener() {
100       public void actionPerformed(ActionEvent e) {
101         dlg.returnValue = JOptionPane.CANCEL_OPTION;
102         dlg.hide();
103       }
104     };
105     cancelBtn.addActionListener(cancelAL);
106     cancelBtn.registerKeyboardAction(cancelAL, 
107       KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), 
108       JComponent.WHEN_IN_FOCUSED_WINDOW);
109       
110     btnPanel.add(okBtn);
111     btnPanel.add(cancelBtn);
112     dlg.getRootPane().setDefaultButton(okBtn);
113     dlg.getContentPane().add(btnPanel, BorderLayout.SOUTH);
114   }
115 }