1 19 package jcckit; 20 21 import jcckit.data.DataPlot; 22 import jcckit.util.AppletBasedConfigData; 23 import jcckit.util.ConfigParameters; 24 import jcckit.util.Factory; 25 import jcckit.util.PropertiesBasedConfigData; 26 27 import java.applet.Applet ; 28 import java.awt.CardLayout ; 29 import java.awt.Component ; 30 import java.awt.Color ; 31 import java.awt.Label ; 32 import java.awt.TextArea ; 33 import java.io.InputStream ; 34 import java.net.URL ; 35 import java.util.Properties ; 36 37 76 public class PlotApplet extends Applet implements Runnable { 77 public static final String DEFAULT_WAITING_MESSAGE 78 = "Please wait, applet data are loading..."; 79 public static final String WAITING_MESSAGE_KEY = "waitingMessage", 80 DATA_PROPERTIES_KEY = "dataProperties", 81 DATA_KEY = "data", 82 RENDERER_KEY = "renderer"; 83 private CardLayout _layout = new CardLayout (); 84 85 86 public void init() { 87 setLayout(_layout); 88 String label = getParameter(WAITING_MESSAGE_KEY); 89 if (label == null) { 90 label = DEFAULT_WAITING_MESSAGE; 91 } 92 add(new Label (label, Label.CENTER), ""); 93 } 94 95 96 public void start() { 97 new Thread (this).start(); 98 } 99 100 104 public void run() { 105 try { 106 ConfigParameters config 107 = new ConfigParameters(new AppletBasedConfigData(this)); 108 DataPlot dPlot = new DataPlot(getDataConfig(config)); 109 GraphicsPlotCanvas canvas 110 = config.get(Factory.CLASS_NAME_KEY, null) == null ? 111 new GraphicsPlotCanvas(config) 112 : (GraphicsPlotCanvas) Factory.create(config); 113 canvas.setRenderer(config.get(RENDERER_KEY, 114 "jcckit.renderer.GraphicsRenderer")); 115 canvas.connect(dPlot); 116 show(canvas.getGraphicsCanvas()); 117 } catch (Throwable t) { 118 show(t); 119 } 120 } 121 122 private void show(Throwable throwable) { 123 TextArea error = new TextArea (throwable.toString()); 124 error.setForeground(Color.red); 125 show(error); 126 } 127 128 private void show(Component component) { 129 removeAll(); 130 add(component, ""); 131 _layout.last(this); 132 } 133 134 138 private ConfigParameters getDataConfig(ConfigParameters config) 139 throws Throwable { 140 ConfigParameters result = config.getNode(DATA_KEY); 141 String dataProperties = config.get(DATA_PROPERTIES_KEY, null); 142 if (dataProperties != null) { 143 Properties properties = new Properties (); 144 InputStream is 145 = new URL (getDocumentBase(), dataProperties).openStream(); 146 properties.load(is); 147 is.close(); 148 result = new ConfigParameters(new PropertiesBasedConfigData(properties)); 149 } 150 return result; 151 } 152 } 153 | Popular Tags |