April 10, 2006
Incrementally Updating SoftwareSimpleMortgageCalculator() {
super();
this.setUndecorated(true);
//this is where the settings from the last use are retrieved
try{
applicationProperties.load(new FileInputStream(CALCULATOR_CONFIG_FILE));
}
catch(Exception ex){
System.out.println("problem loading properties
file on startup "+ex.toString());
}
// Add the window listener
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
dispose();
System.exit(0);
}});
Listing One: SimpleMortgageCalculator.
public void actionPerformed(ActionEvent e){
if(e.getSource()==buttons[CALCULATE]){//calculate button
double dResult=Calculate.crunch(fields[AMOUNT].getText(),
fields[INTEREST].getText(), fields[TERM].getText());
if(dResult>0.0){
fields[RESULTS].setText(""+(int)dResult);
}
else{
fields[RESULTS].setText(LanguageLoader.
getInstance().lookUpString("badresult"));
}
}
else if(e.getSource()==buttons[SKINS]){//skins button
SkinLoader.getInstance().chooseSkin(this);
Listing Two: actionPerformed method.
public void loadComponent(String skinFile){
try{
URL u = new URL("jar:file:skins/"+skinFile+"!/");
int index=skinFile.indexOf(".jar");
String thisClass=skinFile.substring(0, index);
String strClass="skins."+thisClass;//jar and main class files match
ucl = new URLClassLoader(new URL[] { u });
c= (Class) Class.forName(strClass, true, ucl);
m = c.getMethod(SKIN_METHOD, SKIN_METHOD_ARGS);
currentSkin=skinFile;
image=null;
}
catch(Exception ex){
System.out.println("exception in SkinLoader.loadComponent()
"+ex.toString());
}
}
Listing Three: loadComponent method.
//bundling a dimension for a call to the skin to get the dimension
Object oa[]=new Object[1];
oa[0]=new Dimension();
Integer IReturn=(Integer)m.invoke(c, new Object[] {oa, new Integer(1), new Integer(1) });
d=(Dimension)oa[0];
Listing Four: Bundling in SkinsLoader.
//fragment from the skinMethod in the skin where arguments are unbundled
public static Integer skinMethod(Object [] args, Integer callType, Integer callerVersion){
int type=callType.intValue();
Listing Five: Unbundling in the skin.
//code called in LanguageLoader to load the current language
public static void loadNewLanguage(String languageFile){
languageChangedFlag=true;
properties.clear();
try{
properties.load(new FileInputStream("languages/"+languageFile));
}
catch(IOException ioe){
System.out.println("problem loading language file "+ioe.toString());
}
frame.paint(frame.getGraphics());
}
Listing Six: loadNewLanguage method.
if (e.getSource()==list) {
// When the user double-clicks on a component, try to load it
if (e.getID() == Event.ACTION_EVENT) {
String strFile=list.getSelectedItem();
ComponentInfo ci=(ComponentInfo)hashComponents.get(strFile);
//if the component is not yet on the file system, download it
if(!ci.getDownloaded()){
this.setTitle("Downloading "+ci.getFileName());
RemoteComponentLoader.requestComponent(ci.getLocation(),
directory+"/"+ci.getFileName());
}
loader.loadComponent(strFile);
this.dispose();
}
}
Listing Seven: Loading a component.
|
|
||||||||||||||||||||||||||||||
|
|
|
|