KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bluej > editor > moe > Info


1 // Copyright (c) 2000, 2005 BlueJ Group, Deakin University
2
//
3
// This software is made available under the terms of the "MIT License"
4
// A copy of this license is included with this source distribution
5
// in "license.txt" and is also available at:
6
// http://www.opensource.org/licenses/mit-license.html
7
// Any queries should be directed to Michael Kolling mik@bluej.org
8

9 package bluej.editor.moe;
10
11 import bluej.Config;
12 import bluej.utility.DialogManager;
13 import bluej.utility.BlueJFileReader;
14
15 import java.awt.*; // MenuBar, MenuItem, Menu, Button, etc.
16
import java.awt.event.*; // New Event model
17
import javax.swing.*; // all the GUI components
18
import java.io.*;
19
20 /**
21  *
22  * @author Michael Kolling
23  */

24 public final class Info extends JPanel
25     implements ActionListener
26 {
27     static final ImageIcon helpImage = Config.getImageAsIcon("image.editor.help");
28
29     public static Font infoFont = new Font("SansSerif", Font.BOLD, 10);
30
31     // -------- INSTANCE VARIABLES --------
32

33     private JLabel line1;
34     private JLabel line2;
35     String JavaDoc originalMsg;
36     boolean isClear;
37     JButton helpButton;
38     String JavaDoc helpGroup;
39
40     // ------------- METHODS --------------
41

42     public Info()
43     {
44         super();
45         setLayout(new BorderLayout());
46         setBorder(BorderFactory.createLineBorder(Color.black));
47         setFont(infoFont);
48
49         JPanel body = new JPanel(new GridLayout(0, 1)); // one col, many rows
50
body.setBackground(MoeEditor.infoColor);
51         line1 = new JLabel();
52         line2 = new JLabel();
53         body.add(line1);
54         body.add(line2);
55         add(body, BorderLayout.CENTER);
56
57         helpButton = new JButton(helpImage);
58         helpButton.setMargin(new Insets(0,0,0,0));
59         helpButton.addActionListener(this);
60         helpButton.setRequestFocusEnabled(false); // never get focus
61
add(helpButton, BorderLayout.EAST);
62         helpButton.setVisible(false);
63
64         isClear = true;
65         helpGroup = "";
66     }
67
68     /**
69      * display a one line message
70      */

71     public void message(String JavaDoc msg)
72     {
73         originalMsg = msg;
74         int newline = msg.indexOf('\n');
75         if (newline == -1)
76             if(msg.length() <= 81)
77                 message (msg, "");
78             else
79                 message (msg.substring(0, 80), msg.substring(80));
80         else
81             message (msg.substring(0, newline), msg.substring(newline+1));
82     }
83
84
85     /**
86      * display a two line message
87      */

88     public void message(String JavaDoc msg1, String JavaDoc msg2)
89     {
90         line1.setText(msg1);
91         line2.setText(msg2.replace('\n', ' '));
92
93         isClear = false;
94
95         hideHelp();
96     }
97
98
99     /**
100      * display a one line warning (message with beep)
101      */

102     public void warning(String JavaDoc msg)
103     {
104         message (msg);
105         MoeEditorManager.editorManager.beep();
106     }
107
108
109     /**
110      * display a two line warning (message with beep)
111      */

112     public void warning(String JavaDoc msg1, String JavaDoc msg2)
113     {
114         message (msg1, msg2);
115         MoeEditorManager.editorManager.beep();
116     }
117
118
119     /**
120      * clear the display
121      */

122     public void clear()
123     {
124         if (!isClear) {
125             message (" ", " ");
126             isClear = true;
127         }
128     }
129
130
131     /**
132      *
133      */

134     public void setHelp(String JavaDoc helpGroup)
135     {
136         this.helpGroup = helpGroup;
137         helpButton.setVisible(true);
138     }
139
140     /**
141      *
142      */

143     public void hideHelp()
144     {
145         helpButton.setVisible(false);
146     }
147
148     // ---- ActionListener interface ----
149

150     public void actionPerformed(ActionEvent evt)
151     {
152         displayHelp(helpGroup);
153     }
154
155     private void displayHelp(String JavaDoc helpGroup)
156     {
157         File fileName = Config.getLanguageFile(helpGroup + ".help");
158         int i = originalMsg.indexOf('\n');
159         
160         // fix for newline bug #386 with jdk1.4.0
161
String JavaDoc line;
162         if (i<0) {
163             line = originalMsg;
164         } else {
165             line = originalMsg.substring(0,i);
166         }
167         
168         String JavaDoc helpText = BlueJFileReader.readHelpText(fileName, line.trim(),
169                                                        false);
170 // if(originalMsg.length() > 60) {
171
// int half = originalMsg.length() / 2;
172
// originalMsg = originalMsg.substring(0, half) + "\n" +
173
// originalMsg.substring(half);
174
// }
175

176         if(helpText == null)
177             DialogManager.showMessageWithText(null, "no-help",
178                                               "\n" + originalMsg);
179         else
180             DialogManager.showText(null, originalMsg + "\n\n" + helpText);
181     }
182
183 } // end class Info
184
Popular Tags