/* * PublicInfoPanel.java * * Created on February 6, 2003, 1:30 AM */ package MDUtils; import java.awt.Color; import java.awt.Dimension; import java.awt.Image; import javax.swing.*; import javax.swing.border.*; /** * * @author dickey */ public class PublicInfoPanel extends JPanel { /** The object from which this panel gets the information. */ IPublicInformation info; private JTextArea description; private JTextArea instructions; private static final int panelWidth = 300; private static final int panelHeight = 300; final int maxIconWidth = (int) (panelWidth/1.5); final int maxIconHeight = panelHeight/4; private JLabel iconArea; private ImageIcon icon; //will be placed in iconArea only if loaded successfully private boolean badImage = false; //set true if image can't be loaded public PublicInfoPanel(final IPublicInformation infoObject) { super(); assert infoObject != null: "null argument to PublicInfoPanel constructor"; this.info = infoObject; this.setLayout(new java.awt.FlowLayout()); //default anyway //this.setMaximumSize(new Dimension(panelWidth+50, panelHeight+50)); String labelinfo = this.info.getObjectName() + " by " + this.info.getAuthor(); //this.add(new JLabel(labelinfo)); Border panelBorder = BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.pink, Color.magenta); Border titledBorder = new javax.swing.border.TitledBorder( panelBorder, labelinfo); this.setBorder(titledBorder); { String imageURL = infoObject.getImageURLString(); if (imageURL == null) { this.iconArea = new JLabel("[No image available]"); } else { this.iconArea = new JLabel("Attempting to load " + imageURL); java.net.URL url; try { url = new java.net.URL(imageURL); icon = new ImageIcon(url); //no malformed error -- start loading it } catch (Exception e) { System.out.println("\t" + imageURL + " is not a URL string."); //not a valid URL -- try is as just a file name icon = new ImageIcon(imageURL); //start loading it icon.setDescription("Image from " + imageURL); } } assert this.iconArea != null; this.add(this.iconArea); } this.description = new JTextArea("", 4, 20); this.description.setLineWrap(true); //needed (at least when scrollbars weren't showing up) this.description.setWrapStyleWord(true); String descriptionText = this.info.getObjectDescription(); if (descriptionText == null || descriptionText.length() <= 0) { descriptionText = "No description available."; description.setRows(2); } else { descriptionText = "Description: " + descriptionText; } this.description.setText(descriptionText); this.description.setFocusable(false); this.add(new JScrollPane(this.description)); String instructionsText = this.info.getInstructions(); if (instructionsText == null || instructionsText.length() <= 0) { //don't even create a text area for instructions. //instructionsText = "No instructions available."; } else { instructionsText = "Instructions: " + instructionsText; this.instructions = new JTextArea(instructionsText, 4, 20); this.instructions.setLineWrap(true); this.instructions.setWrapStyleWord(true); this.instructions.setFocusable(false); this.instructions.setBackground(Color.white); this.add(new JScrollPane(this.instructions)); } Color preferredColor = this.info.getColor(); if (preferredColor == null) { preferredColor = Color.white; } this.setBackground(preferredColor); this.repaint(); //end constructor } /** If an image has been loaded, resize it to fit. */ public void resizeIcon(int maxWidth, int maxHeight) { if (this.badImage) return; if (this.icon == null) return; int imageStatus = icon.getImageLoadStatus(); if (imageStatus != java.awt.MediaTracker.COMPLETE ) { if (imageStatus == java.awt.MediaTracker.LOADING) { this.iconArea.setText("loading image..."); } else { this.iconArea.setText("image error status =" + imageStatus); this.badImage = true; } return; } Image oldImage = icon.getImage(); assert oldImage != null; //The image has been loaded. Rescale it to fit. double iheight = icon.getIconHeight(); double iwidth = icon.getIconWidth(); Dimension desiredSize = new Dimension((int) iwidth, (int) iheight); if (iheight > maxHeight || iwidth > maxWidth) { double aRatio = iwidth/iheight; double scaledWidth = (int) (Math.min(maxWidth, maxHeight*aRatio)); double scaledHeight = Math.min(maxHeight, scaledWidth/aRatio); assert Math.abs(aRatio - scaledWidth/scaledHeight) <= 0.001; // - 1 for height in the following means "preserve aspect ratio" Image scaledImage = oldImage.getScaledInstance( (int) scaledWidth, (int) scaledHeight, 0); icon.setImage(scaledImage); desiredSize = new Dimension((int) scaledWidth, (int) scaledHeight); } this.remove(this.iconArea); //remove the old label this.iconArea = new JLabel(icon); iconArea.setPreferredSize(desiredSize); this.add(this.iconArea); ((JComponent)iconArea.getParent()).revalidate(); //end resize image } public void repaint() { if (this.iconArea != null) { resizeIcon(maxIconWidth, maxIconHeight); } int minHeight = 0; if (this.instructions != null) { minHeight += this.instructions.getPreferredSize().height; } if (this.description != null) { minHeight += this.description.getPreferredSize().height; } if (this.iconArea != null) { minHeight += this.iconArea.getPreferredSize().height; } this.setPreferredSize(new Dimension(panelWidth+6, minHeight+50)); this.validate(); super.repaint(); } //end PublicInfoPanel }