View Javadoc
1 /* 2 * Created on Oct 29, 2003 3 * 4 * To change the template for this generated file go to 5 * Window>Preferences>Java>Code Generation>Code and Comments 6 */ 7 package portaview.view; 8 9 import java.awt.Image; 10 import java.util.Observable; 11 import java.util.Observer; 12 13 import javax.swing.AbstractButton; 14 import javax.swing.ImageIcon; 15 import javax.swing.JToggleButton; 16 import javax.swing.SwingConstants; 17 18 import portaview.Log; 19 20 /*** 21 * Icon view that serve as the parent of things that can have an icon. 22 * @author William Lee 23 */ 24 public abstract class IconView extends JToggleButton implements Observer 25 { 26 public static final int DEFAULT_WIDTH = 100; 27 public static final int DEFAULT_HEIGHT = 100; 28 29 private ImageIcon icon = null; 30 private IconPanel iconPanel = null; 31 32 public IconView(IconPanel ipanel) 33 { 34 super(); 35 iconPanel = ipanel; 36 setHorizontalAlignment(SwingConstants.CENTER); 37 38 } 39 /*** 40 * Update to call when the PortaView has changed. 41 * @see java.util.Observer#update(java.util.Observable, java.lang.Object) 42 */ 43 public void update(Observable o, Object arg) 44 { 45 refresh(); 46 } 47 48 /*** 49 * Children should implement this in order to get the icon image. 50 * @return 51 */ 52 protected abstract Image getIconImage(); 53 54 /*** 55 * Children should implement this in order to return the text label. 56 * @return 57 */ 58 protected abstract String getIconLabel(); 59 60 /*** 61 * Children can override this method in order to do its specific refresh. 62 * 63 */ 64 protected void doRefresh() 65 { 66 } 67 68 /*** 69 * Returns true if the view need refreshing. 70 * 71 */ 72 protected abstract boolean needRefresh(); 73 74 /*** 75 * Children class can override this class to process the icon. 76 * @param img the image to process (can apply the effects for the icon). 77 */ 78 protected Image postProcessIcon(Image img) 79 { 80 return img; 81 } 82 83 /*** 84 * Refreshes the view. 85 */ 86 public void refresh() 87 { 88 if (!needRefresh()) 89 return; 90 // should get the icon image from the cache, but oh well. 91 try 92 { 93 Image img = getIconImage(); 94 if (img != null) 95 { 96 icon = new ImageIcon(postProcessIcon(img)); 97 setIcon(icon); 98 } 99 } 100 catch (Exception e) 101 { 102 Log.debug(e.toString()); 103 } 104 setText(getIconLabel()); 105 setVerticalTextPosition(AbstractButton.BOTTOM); 106 setHorizontalTextPosition(AbstractButton.CENTER); 107 doRefresh(); 108 repaint(); 109 iconPanel.repaint(); 110 } 111 112 /*** 113 * Utility method to get a multi-line label string. 114 * @param lines lines of text 115 * @return an HTML encoded label text that you can use in a multi-line label. 116 */ 117 protected String getMultiLineLabel(String[] lines) 118 { 119 StringBuffer rtn = new StringBuffer(); 120 rtn.append("<html>"); 121 // rtn.append("<center>"); 122 for (int i = 0; i < lines.length; i++) 123 { 124 rtn.append(lines[i]); 125 if (i != lines.length - 1) 126 rtn.append("<br>"); 127 } 128 // rtn.append("</center>"); 129 rtn.append("</html>"); 130 return rtn.toString(); 131 } 132 133 }

This page was automatically generated by Maven