Source code: fzi/injectj/config/MessageDialog.java
1 // This file is part of the Inject/J project
2 // (C) 1999-2001 Forschungszentrum Informatik (FZI) Karlsruhe
3 // Please visit our website at http://injectj.fzi.de
4
5 package fzi.injectj.config;
6
7 import java.awt.*;
8 import javax.swing.*;
9 import java.awt.event.*;
10 import java.io.File;
11 import fzi.injectj.language.*;
12 import fzi.injectj.Main;
13
14 /** A message dialog. This dialog dialog displays icons if the title is
15 * set to the values of LabelCode.ERROR_LABEL or LabelCode.WARNING_LABEL.
16 * The user can choose if the message area should be scrollable or not,
17 * and if an OK-Button should be displayed or not.
18 *
19 * @author Volker Kuttruff
20 */
21 public class MessageDialog extends JDialog
22 {
23 JPanel panel = new JPanel();
24 BorderLayout borderLayout = new BorderLayout();
25 JScrollPane scrollPane = new JScrollPane();
26 JButton okButton = new JButton();
27 JTextArea messageArea = new JTextArea();
28 JTextField messageField = new JTextField();
29 JLabel iconLabel = null;
30 JLabel questionLabel = null;
31 JLabel errorLabel = null;
32 JLabel warningLabel = null;
33 ImageIcon questionIcon;
34 ImageIcon stopIcon;
35 ImageIcon warningIcon;
36
37 boolean noOKButton=false;
38
39 /** Constructs an dialog with a visible OK-Button.
40 *
41 * @param dialog the parent dialog
42 * @param title the window title
43 * @param modal true, if dialog should be modal, false otherwise
44 */
45 public MessageDialog(JFrame parent, String title, boolean modal)
46 {
47 this(parent, title, modal, false);
48 }
49
50 /** @param dialog the parent dialog
51 * @param title the window title
52 * @param modal true, if dialog should be modal, false otherwise
53 * @param noOKButton true, if no OK button should be displayed, false otherwise
54 */
55 public MessageDialog(JFrame parent, String title, boolean modal, boolean noOKButton)
56 {
57 super(parent, modal);
58 this.noOKButton = noOKButton;
59 try
60 {
61 jbInit();
62 pack();
63 this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
64 if (title!=null) setTitle(title);
65 }
66 catch(Exception ex)
67 {
68 ex.printStackTrace();
69 }
70
71 }
72
73 public MessageDialog() {
74 this.noOKButton = false;
75 try
76 {
77 jbInit();
78 pack();
79 this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
80 }
81 catch(Exception ex)
82 {
83 ex.printStackTrace();
84 }
85 }
86
87 void jbInit() throws Exception
88 {
89 this.setSize(new Dimension(500, 100));
90 //Center window
91 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
92 Dimension frameSize = this.getSize();
93 if (frameSize.height > screenSize.height)
94 frameSize.height = screenSize.height;
95 if (frameSize.width > screenSize.width)
96 frameSize.width = screenSize.width;
97 this.setLocation((screenSize.width- frameSize.width) / 2, (screenSize.height - frameSize.height) /2);
98
99 panel.setLayout(borderLayout);
100 okButton.setHorizontalTextPosition(SwingConstants.CENTER);
101 okButton.setText("OK");
102 okButton.setVerticalAlignment(SwingConstants.BOTTOM);
103 okButton.addActionListener(new java.awt.event.ActionListener()
104 {
105
106 public void actionPerformed(ActionEvent e)
107 {
108 okButton_actionPerformed(e);
109 }
110 });
111 messageArea.setLineWrap(true);
112 messageArea.setBorder(null);
113 messageArea.setEditable(false);
114
115 messageField.setBorder(null);
116 messageField.setEditable(false);
117
118 panel.setPreferredSize(new Dimension(500, 100));
119
120 String questionFileName = "/fzi/injectj/images/question.gif";
121 questionIcon = new ImageIcon(this.getClass().getResource(questionFileName));
122 questionLabel = new JLabel(questionIcon);
123 questionLabel.setPreferredSize(new Dimension(60, 40));
124
125 String errorFileName = "/fzi/injectj/images/stop.gif";
126 stopIcon = new ImageIcon(this.getClass().getResource(errorFileName));
127 errorLabel = new JLabel(stopIcon);
128 errorLabel.setPreferredSize(new Dimension(60, 40));
129
130 String warningFileName = "/fzi/injectj/images/warning.gif";
131 warningIcon = new ImageIcon(this.getClass().getResource(warningFileName));
132 warningLabel = new JLabel(warningIcon);
133 warningLabel.setPreferredSize(new Dimension(60, 40));
134
135 getContentPane().add(panel);
136 if (!noOKButton) panel.add(okButton, BorderLayout.SOUTH);
137 panel.add(scrollPane, BorderLayout.CENTER);
138 scrollPane.getViewport().add(messageArea, null);
139 iconLabel = null;
140 }
141
142 void okButton_actionPerformed(ActionEvent e)
143 {
144 this.dispose();
145 }
146
147 public void setOKButtonText(String newOKButtonText) {
148 this.okButton.setText(newOKButtonText);
149 }
150
151 /** Sets the message in the message text field. If message is long,
152 * enableScrolling should be set to true.
153 *
154 * @param message the message to display
155 * @param enableScrolling true, if scrolling of message text should be possible,
156 * false otherwise
157 */
158 public void setMessage(String message, boolean enableScrolling) {
159 if (enableScrolling) {
160 messageArea.setText(message);
161 }
162 else {
163 messageField.setText(message);
164 panel.remove(scrollPane);
165 panel.add(messageField, BorderLayout.CENTER);
166 }
167 }
168
169 /** Sets the message in the message field. Scrolling is enabled.
170 *
171 * @param message the message to display
172 */
173 public void setMessage(String message) {
174 messageArea.setText(message);
175 }
176
177 /** Sets the title of this dialog. If the title is the value of
178 * LabelCode.ERROR_LABEL or LabelCode.WARNING_LABEL, small icons
179 * are displayed at the left end of this dialog.
180 *
181 * @param title the title for this dialog window
182 */
183 public void setTitle(String title) {
184 if (iconLabel!=null) panel.remove(iconLabel);
185 if (title.equals(CodeMapper.getText(LabelCode.ERROR_LABEL))) {
186 iconLabel = errorLabel;
187 }
188 else if (title.equals(CodeMapper.getText(LabelCode.WARNING_LABEL))) {
189 iconLabel = warningLabel;
190 }
191 else iconLabel = null;
192
193 if (iconLabel!=null) {
194 panel.add(iconLabel, BorderLayout.WEST);
195 pack();
196 }
197 super.setTitle(title);
198 }
199 }