/** * Message Panel for the Moving Map Navigator * @author Robert J Morton * @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 // what the hell this is for, I don't private static final long serialVersionUID = 37L; private static msgpanel mp; // reference to current instance of this class private movmap mm; // Reference to current instance of the movmap applet private loader ld; // Reference to the current instance of the data loader private boolean // TRUE = progressMsg = true, // progress message, not an error message imageLoad = false, // image load failure, false: route/wpt load failure b = false; // text colour has been changed private int Lx = 15, Ly = 480, // position of top left corner of panel within applet Lw = 406, Lh = 35, // width and height of panel Bw = 72, // 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 private String msg = ""; // to contain the current message to be displayed // button for retrying loading of a route names or waypoints list private JButton RtryBut; /* References for large and small fonts used in this applet and the typeface dimensions for this font. */ private Font font; private FontMetrics fm; private Color fg, fgc; // foreground colour msgpanel(movmap mm, Color fg) { this.mm = mm; this.fg = fg; // foreground colour (for lettering) mp = this; // reference to this instance of this class setLayout(null); // to allow the control panels to be laid out manually fgc = Color.black; // initial foreground colour before images are loaded /* Create font for applet canvas, get its letter dimensions etc vertical base line offset, */ font = new Font("Serif",Font.BOLD,16); fm = getFontMetrics(font); My = (Lh + fm.getAscent()) >> 1; /* Create the "Retry" button, set position and size and 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 /* If the text colour has been changed, draw the panel's background photograph; otherwise, clear the text field by filling it with the background colour. */ if(b) g.drawImage(movmap.IMG4,Bw,0,this); else { g.setColor(getBackground()); g.fillRect(Bw,0,Mw,Lh); } g.setFont(font); // use the big bold font /* if loading is in progress, set button lettering colour to light grey and display the text 'Retry' then display the route information in black lettering. */ if(progressMsg) { RtryBut.setForeground(Color.lightGray); RtryBut.setText("Retry"); g.setColor(fgc); } /* Otherwise it must be an error condition, so set button lettering colour to black and display the text 'Retry' then display the error message in red lettering. */ else { RtryBut.setForeground(Color.black); RtryBut.setText("Retry"); g.setColor(Color.red); } 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) } // RE-TRY BUTTON TO ATTEMPT A RELOAD AFTER AN ERROR void pressRtryBut() { if(progressMsg) return; // exit if it's not an error message /* if the error occurred during image loading, kill the label of the retry button and request a reload of all images. */ if(imageLoad) { RtryBut.setText(""); mm.imgReload(); } /* Otherwise, the error must have occurred during the loading of the routes data, so if there was a connecting or loading error, kill the label of the retry button, wipe the error message and re-initiate the loading sequence. */ else if(ld.getlp() > 3) { RtryBut.setText(""); showMsg("",true,false); ld.setlp(1); } } } // 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; } // The 'find' button is the only one we are listening for here. public void actionPerformed(ActionEvent e) { switch(id) { case RTRYBUT: ap.pressRtryBut(); break; } } }