001 /* 002 * Copyright (C) 2003 by Christian Lauer. 003 * 004 * This library is free software; you can redistribute it and/or 005 * modify it under the terms of the GNU Library General Public 006 * License as published by the Free Software Foundation; either 007 * version 2 of the License, or (at your option) any later version. 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Library General Public License for more details. 013 * 014 * You should have received a copy of the GNU Library General Public 015 * License along with this library; if not, write to the Free 016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 017 * 018 * If you didn't download this code from the following link, you should check if 019 * you aren't using an obsolete version: 020 * http://sourceforge.net/projects/ujac 021 */ 022 023 package nl.cwi.sen1.gui.plugin.editor; 024 025 import java.awt.Container; 026 import java.awt.Dialog; 027 import java.awt.Dimension; 028 import java.awt.Frame; 029 import java.awt.Point; 030 import java.awt.event.ActionEvent; 031 import java.awt.event.KeyEvent; 032 033 import javax.swing.AbstractAction; 034 import javax.swing.ActionMap; 035 import javax.swing.InputMap; 036 import javax.swing.JComponent; 037 import javax.swing.JDialog; 038 import javax.swing.KeyStroke; 039 040 public class BaseDialog extends JDialog { 041 042 /** Constant for the dialog state OK. */ 043 public static final int OK = 1; 044 /** Constant for the dialog state CANCEL. */ 045 public static final int CANCEL = 0; 046 047 /** The dialog state. */ 048 private int state = CANCEL; 049 050 /** 051 * Gets the dialog state. 052 * @return The dialog state. 053 */ 054 public int getState() { 055 return state; 056 } 057 058 /** 059 * Constructs a BaseDialog instance with specific attributes. 060 * @throws java.awt.HeadlessException 061 */ 062 public BaseDialog() throws UnsupportedOperationException { 063 super(); 064 } 065 066 /** 067 * Constructs a BaseDialog instance with specific attributes. 068 * @param frame The parent frame. 069 * @throws java.awt.HeadlessException 070 */ 071 public BaseDialog(Frame frame) throws UnsupportedOperationException { 072 super(frame); 073 } 074 075 /** 076 * Constructs a BaseDialog instance with specific attributes. 077 * @param frame The parent frame. 078 * @param modal The modal flag. 079 * @throws java.awt.HeadlessException 080 */ 081 public BaseDialog(Frame frame, boolean modal) throws UnsupportedOperationException { 082 super(frame, modal); 083 } 084 085 /** 086 * Constructs a BaseDialog instance with specific attributes. 087 * @param frame The parent frame. 088 * @param title The dialog title. 089 * @throws java.awt.HeadlessException if GraphicsEnvironment.isHeadless() returns true. 090 */ 091 public BaseDialog(Frame frame, String title) throws UnsupportedOperationException { 092 super(frame, title); 093 } 094 095 /** 096 * Constructs a BaseDialog instance with specific attributes. 097 * @param frame The parent frame. 098 * @param title The dialog title. 099 * @param modal The modal flag. 100 * @throws java.awt.HeadlessException 101 */ 102 public BaseDialog(Frame frame, String title, boolean modal) throws UnsupportedOperationException { 103 super(frame, title, modal); 104 } 105 106 /** 107 * Constructs a BaseDialog instance with specific attributes. 108 * @param parent The parent dialog. 109 * @throws java.awt.HeadlessException 110 */ 111 public BaseDialog(Dialog parent) throws UnsupportedOperationException { 112 super(parent); 113 } 114 115 /** 116 * Constructs a BaseDialog instance with specific attributes. 117 * @param parent The parent dialog. 118 * @param modal The modal flag. 119 * @throws java.awt.HeadlessException 120 */ 121 public BaseDialog(Dialog parent, boolean modal) throws UnsupportedOperationException { 122 super(parent, modal); 123 } 124 125 /** 126 * Constructs a BaseDialog instance with specific attributes. 127 * @param parent The parent dialog. 128 * @param title The dialog title. 129 * @throws java.awt.HeadlessException 130 */ 131 public BaseDialog(Dialog parent, String title) throws UnsupportedOperationException { 132 super(parent, title); 133 } 134 135 /** 136 * Constructs a BaseDialog instance with specific attributes. 137 * @param parent The parent dialog. 138 * @param title The dialog title. 139 * @param modal The modal flag. 140 * @throws java.awt.HeadlessException 141 */ 142 public BaseDialog(Dialog parent, String title, boolean modal) throws UnsupportedOperationException { 143 super(parent, title, modal); 144 } 145 146 /** 147 * Registers standard key bindings. 148 */ 149 protected void registerStandardKeyBindings() { 150 // defining key bindings 151 InputMap inputMap = this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); 152 inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "escape"); 153 inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter"); 154 155 ActionMap actionMap = this.getRootPane().getActionMap(); 156 actionMap.put("enter", new AbstractAction() { 157 /** 158 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) 159 */ 160 public void actionPerformed(ActionEvent evt) { 161 okButtonActionPerformed(); 162 } 163 }); 164 actionMap.put("escape", new AbstractAction() { 165 /** 166 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) 167 */ 168 public void actionPerformed(ActionEvent evt) { 169 cancelButtonActionPerformed(); 170 } 171 }); 172 } 173 174 /** 175 * Processes the action event, raised by the OK button. 176 * @param evt The raised action event. 177 */ 178 private void okButtonActionPerformed() { 179 this.state = OK; 180 closeDialog(); 181 } 182 183 /** 184 * Processes the action event, raised by the Cancel button. 185 * @param evt The raised action event. 186 */ 187 private void cancelButtonActionPerformed() { 188 this.state = CANCEL; 189 closeDialog(); 190 } 191 192 193 /** 194 * Closes the dialog 195 * @param evt The window event. 196 */ 197 protected void closeDialog() { 198 setVisible(false); 199 dispose(); 200 } 201 202 /** 203 * Centers the dialog over its parent component. 204 */ 205 protected void centerDialog(Container parent) { 206 Dimension parentSize = parent.getSize(); 207 Dimension dialogSize = this.getSize(); 208 Point parentLocn = parent.getLocationOnScreen(); 209 210 int locnX = parentLocn.x + (parentSize.width - dialogSize.width) / 2; 211 int locnY = parentLocn.y + (parentSize.height - dialogSize.height) / 2; 212 213 setLocation(locnX, locnY); 214 } 215 }