/** * 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 a progress message, not an error message imageLoad = false, /* true = image load failure, false = route/waypoints load failure. */ b = false; // background photo not yet loaded private int Lx = 15, // x-coord of top left corner of panel within applet Ly = 480, // y-coord of top left corner of panel within applet Lw = 406, // width of panel Lh = 35, // 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 // button for retrying to load a route names or waypoint list private JButton RtryBut; 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 etc for this 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 if(b) // if background photo available, draw it on the applet panel g.drawImage(movmap.IMG4,Bw,0,this); else { g.setColor(getBackground()); // clear the field by g.fillRect(Bw, 0, Mw, Lh); // filling 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 lettering } 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) // if it's not an error message, return; // then exit now if(imageLoad) { // if error occurred during image loading RtryBut.setText(""); // kill the label of the retry button mm.imgReload(); // command to reload all images } /* Else, the error must have occurred during the loading of the routes data. So, if it were a connecting or loading error, kill the label of the retry button, wipe the error message then re-initiate the loading sequence. */ else { 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; // 're-try 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: // it was the 're-try' button ap.pressRtryBut(); } } }