This page is intended to provide information on small chunks of code that are of general use, but are not necessarily fully functional blocks. If you are stuck on a coding problem then search this page for the topic/function/keyword of interest, an index is not provided as the list of contents is expected to grow unwieldy.
Note, if some of the code is not visible in the examples below just copy the code and paste it into a text editor to see it all.
FileChooser Filter, DeferredChooser, FileFilter, FileNameExtensionFilter By monkeyboy
How to limit the file types that can be selected in a file chooser
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import se.datadosen.jalbum.DeferredChooser;
DeferredChooser fc = new DeferredChooser(JFileChooser.class);
FileFilter filter = new FileNameExtensionFilter(
"JPEG file", new String[] {"jpg", "jpeg"}
);
fc.setFileFilter(filter);
ui.fc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int fcReturn = fc.showOpenDialog(window);
if (fcReturn == JFileChooser.APPROVE_OPTION) {
//Your code
}
}
});
Engine, UI, UI2Bean, get settings by David Ekholm
Settings in the user interface don’t get transferred to the JAlbum engine until album generation starts. To update the engine, so all gui settings are correctly reflected use UI2Bean before accessing the engine, e.g.
window.UI2Bean(); engine.getDirectory();
Or query the UI components directly, e.g.
window.directory.getText()
window.bean2UI(), UI, by David Ekholm to refresh the interface from a script
window.bean2UI()
It will copy the settings from the engine object to the user interface.
Rows, cols, override, reset by David Ekholm
To reset the number of rows and cols during album generation, for example to have the root index with 6 rows and 2 cols, but sub-directories indexes with 5 cols and 4 rows. Use hints.jap to set the rows and cols to 6 and 2 then in index.htt use
If(level == 0) {
engine.setRows(4);
engine.setCols(5);
}
ProcessTemplateFile, templates, vars, custom variables by David Ekholm
To pass skin variables from one template to another for processing, e.g. pass variableA from index.htt to MyTemplate.htt:
Map vars = new HashMap();
vars.put("variableA", variableA);
vars.put("variableB", variableB);
vars.put("variableC", variableC);
engine.processTemplateFile(
new File(skinDirectory, "MyTemplate.htt"),
new File(outputDirectory, "MyTemplate.html"),
vars);
jAlbumListener, event, engine, onload, UI, get, set by David Ekholm
To monitor and change settings in onload.bsh when album creation starts and stops. In the example below the page extentension, normally ‘.html’ is changed to ‘.php’ if a skin setting is selected.
import se.datadosen.jalbum.event.*;
window.addJAlbumListener(new JAlbumAdapter() {
String pageExtension;
public void albumCreationStarted(JAlbumEvent e) {
pageExtension = engine.getPageExtension();
if (ui.skinSettingName.isSelected())
engine.setPageExtension(".php");
}
public void albumCreationFinished(JAlbumEvent e) {
engine.setPageExtension(pageExtension);
engine.bean2UI();
}
});
AlbumObject, createInstance, file to object by David Ekholm
If you want to convert a file to an album object then this example shows how to do it, with for example a project's root's folder parent folder
File masterProjectFolder = new File(rootFolder.getFile().getParentFile().getParentFile(), rootFolder.getFile().getParentFile().getName()); // Make the master project's folder an object AlbumObject masterProjectObject = rootFolder.getFactory().createInstance(masterProjectFolder);
Read, Write, File, FileInputStream, DataInputStream, BufferedReader, BufferedWriter based on code by David Ekholm
If you want to read a text file and either do something with or based on its content, and write a changed content back to the file, then one way is:
//Where f is a text file
FileInputStream fStream = new FileInputStream(f);
// make an empty string ready to hold the text of the file
fContent = "";
// Get the object of DataInputStream
DataInputStream fIn = new DataInputStream(fStream);
BufferedReader fBR = new BufferedReader(new InputStreamReader(fIn));
// read the text file line by line
String strLine;
while ((strLine = fBR.readLine()) != null) {
//Read the whole file (or do something with each strLine)
fContent = fContent + strLine + "\n";
}
//Close the input stream
fIn.close();
// If the file is to be changed then write the modified file content back out
BufferedWriter newf = new BufferedWriter(new FileWriter(f));
newf.write(fContent);
newf.close();