Major cleanup of Utils class.
[trakem2.git] / ini / trakem2 / display / YesNoDialog.java
blobfecf4f1a8ad9cf9ca112852d2d4f945407ad8015
1 /**
3 TrakEM2 plugin for ImageJ(C).
4 Copyright (C) 2005, 2006, 2007 Albert Cardona and Rodney Douglas.
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation (http://www.gnu.org/licenses/gpl.txt )
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 You may contact Albert Cardona at acardona at ini.phys.ethz.ch
20 Institute of Neuroinformatics, University of Zurich / ETH, Switzerland.
21 **/
22 package ini.trakem2.display;
23 import ij.IJ;
24 import ij.gui.*;
25 import java.awt.*;
26 import java.awt.event.*;
28 /** A modal dialog box with a one line message and
29 "Yes" and "No" buttons.
30 Almost literally copied from ij.gui.YesNoCancelDialog class
32 public class YesNoDialog extends Dialog implements ActionListener, KeyListener {
33 private Button yesB, noB;
34 private boolean yesPressed;
35 private boolean firstPaint = true;
37 public YesNoDialog(String title, String msg) {
38 this(IJ.getInstance(), title, msg);
41 public YesNoDialog(Frame parent, String title, String msg) {
42 super(parent, title, true);
43 setLayout(new BorderLayout());
44 Panel panel = new Panel();
45 panel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));
46 MultiLineLabel message = new MultiLineLabel(msg);
47 message.setFont(new Font("Dialog", Font.PLAIN, 12));
48 panel.add(message);
49 add("North", panel);
51 panel = new Panel();
52 panel.setLayout(new FlowLayout(FlowLayout.RIGHT, 15, 8));
53 if (IJ.isMacintosh() && msg.startsWith("Save")) {
54 yesB = new Button(" Save ");
55 noB = new Button("Don't Save");
56 } else {
57 yesB = new Button(" Yes ");
58 noB = new Button(" No ");
60 yesB.addActionListener(this);
61 noB.addActionListener(this);
62 yesB.addKeyListener(this);
63 noB.addKeyListener(this);
64 if (IJ.isMacintosh()) {
65 panel.add(noB);
66 panel.add(yesB);
67 setResizable(false);
68 } else {
69 panel.add(yesB);
70 panel.add(noB);
72 add("South", panel);
73 pack();
74 GUI.center(this);
75 show();
78 public void actionPerformed(ActionEvent e) {
79 if (e.getSource()==yesB)
80 yesPressed = true;
81 closeDialog();
84 /** Returns true if the user dismissed dialog by pressing "Yes". */
85 public boolean yesPressed() {
86 return yesPressed;
89 void closeDialog() {
90 setVisible(false);
91 dispose();
94 public void keyPressed(KeyEvent e) {
95 int keyCode = e.getKeyCode();
96 IJ.setKeyDown(keyCode);
97 if (keyCode==KeyEvent.VK_ENTER||keyCode==KeyEvent.VK_Y||keyCode==KeyEvent.VK_S) {
99 yesPressed = true;
100 closeDialog();
102 // PREVENTING unintentional saving of projects. TODO setup an auto save just like Blender, a .quit file with the contents of the last XML, without exporting images (but with their correct paths) (maybe as an .xml1, similar to the .blend1)
103 } else if (keyCode==KeyEvent.VK_N || keyCode==KeyEvent.VK_D) {
104 closeDialog();
108 public void keyReleased(KeyEvent e) {
109 int keyCode = e.getKeyCode();
110 IJ.setKeyUp(keyCode);
113 public void keyTyped(KeyEvent e) {}
115 public void paint(Graphics g) {
116 super.paint(g);
117 if (firstPaint) {
118 yesB.requestFocus();
119 firstPaint = false;