/** * Message Panel for the Moving Map Navigator * @author Robert J Morton YE572246C * @version 12 December 2007 * @copyright Robert J Morton (all rights reserved) */ import javax.swing.*; // Java Swing GUI utilities import java.awt.*; // Java Abstract Windowing Toolkit import java.awt.event.*; // for the new-fangled 1.1 event handling class msgpanel extends JPanel { // TO DISPLAY ERROR AND PROGRESS MESSAGES private static final long serialVersionUID = 15L; // what the hell this is for, I don't know! private static msgpanel mp; // reference to current instance of this class private boolean progressMsg = true, /* true if message is a progress message, not an error message. */ imageLoad = false, /* true = image load failure, false = route/wpt load failure. */ b = false; private int Lx = 15, Ly = 480, // pos'n of top left corner of panel within applet Lw = 406, Lh = 35, // width and height of panel Bw = 80, // button width Mw = Lw - Bw, // width of message area Ti = 10, // text inset from start of message area q = Bw + Ti, // x-bias for printing directly on panel or on image My, // y-origin of the message line L; // language switch private String S[] = {"Retry","Tentar"}, msg = ""; // to contain the current message to be displayed private movmap mm; // reference to current instance of the movmap applet private loader ld; // reference to the current instance of the data loader private JButton RtryBut; // button for retrying to load // a route names or waypoint list private Font font; // ref for large and small fonts used in this applet private FontMetrics fm; // typeface dimensions for the above fonts private Color fg, fgc; // foreground colour msgpanel(movmap mm, Color fg, int L) { this.mm = mm; this.fg = fg; // foreground colour (for lettering) mp = this; // reference to this instance of this class this.L = L; setLayout(null); // to allow the control panels to be laid out manually fgc = Color.black; // initial foreground colour before images are loaded font = new Font("Serif",Font.BOLD,16); // font for applet canvas fm = getFontMetrics(font); // get letter dimensions for the above font My = (Lh + fm.getAscent()) >> 1; // vertical base line offset /* Create the "Retry" button, set its position and size then create a listener for it. */ RtryBut = new JButton(""); add(RtryBut); RtryBut.setBounds(0,0,Bw,Lh); RtryBut.addActionListener(new rtlisten(rtlisten.RTRYBUT,this)); } void setLoader(loader ld) {this.ld = ld;} void changeColour() { fgc = fg; b = true; } // DISPLAY ON INITIALIZATION OR WHEN APPLET IS UNECLIPSED public void paint(Graphics g) { super.paint(g); // paint the retry button g.setColor(mm.getPanelColour()); // Clear the message field by fill- g.fillRect(Bw,0,Mw,Lh); // ing it with background colour. g.setFont(font); // use the big bold font if(progressMsg) { // if in the process of loading RtryBut.setForeground(Color.lightGray); RtryBut.setText(S[L]); // kill label on retry button g.setColor(fgc); // display route information in black } else { // else it must be an error condition RtryBut.setForeground(Color.black); RtryBut.setText(S[L]); // change label on retry button to "Retry" g.setColor(Color.red); // display error message in red lettering } g.drawString(msg, q, My); // display the message } void showMsg(String msg, boolean progressMsg, boolean imageLoad) { this.msg = msg; this.progressMsg = progressMsg; this.imageLoad = imageLoad; repaint(); // gets graphics reference and calls update(g) } void pressRtryBut() { // RE-TRY BUTTON TO ATTEMPT A RELOAD AFTER AN ERROR if(progressMsg) return; // if it's not an error message, butt out if(imageLoad) { // if error occurred during image loading RtryBut.setText(""); // kill the label of the retry button } else { // must have occurred during routes data loading /* If there was a connecting or loading error, kill the label of the retry button, wipe the error message then re-initiate the loading sequence. */ if(ld.getlp() > 3) { RtryBut.setText(""); showMsg("", true, false); ld.setlp(1); } } } //error conditions tested and found to be working correctly } // LISTENS FOR EVENTS FROM THE BUTTONS class rtlisten implements ActionListener { static final int RTRYBUT = 3; // 'route search button pressed' event int id; // one of the above msgpanel ap; // the application that called: always the above panel // constructor for a new command public rtlisten(int id, msgpanel ap) { this.id = id; this.ap = ap; } // one of the buttons has been clicked public void actionPerformed(ActionEvent e) { switch(id) { // id of this instance of ActionEvent case RTRYBUT: ap.pressRtryBut(); break; // it was the 'find' button } } }