Webber: A Website Construction Application
Lib.java
// General library
package library;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.util.StringTokenizer;
public class Lib {
// Return the index of the next character c after pos in chars
public static int nextChar(char[] chars, int pos, char c) {
while (pos<chars.length) if (chars[pos]!=c) ++pos; else break;
return pos;
}
// Return index of next non-whitespace character after pos in chars
public static int skipWhitespace(char[] chars, int pos) {
for (; pos<chars.length; ++pos)
if (!Character.isWhitespace(chars[pos])) break;
return pos;
}
// Return the index of the first string matching s in array
public static int getStringIndex(String[] array, String s) {
return getStringIndex(array, s, false);
}
public static int getStringIndex(String[] array, String s, boolean ignoreCase) {
for (int i=0; i < array.length; ++i)
if (!ignoreCase) { if (s.equals(array[i])) return i; }
else if (s.equalsIgnoreCase(array[i])) return i;
return -1; // Not found
}
// Return the next string from pos in s delimited by characters start and end
public static String getDelimitedString(String s, int pos, char start, char end) {
int i = pos, len = s.length();
for (; i < len; ++i) if (s.charAt(i)==start) break;
if (i==len) return null; // No start delimiter?
for (pos = ++i; i < len; ++i) if (s.charAt(i)==end) break;
if (i==len) return null; // No end delimiter?
return s.substring(pos, i); // Un-delimited string
}
// Return the next quoted string from pos in s
public static String getQuotedString(String s, int pos) {
return getDelimitedString(s, pos, '"', '"');
}
// Get the name of a file using a FileDialog
public static String getFilename(FileDialog f, String filename) {
if (filename!=null) f.setFile(filename);
f.setVisible(true);
if ((filename = f.getFile())==null) return null;
return f.getDirectory() + filename;
}
// Resolve a relative path using specified base path (fixes mixed separators)
public static String getCanonicalPath(String basepath, String filename) {
File f;
if (!((f = new File(filename)).isAbsolute())) { // Relative path?
f = new File(basepath); // Get path from base
basepath = f.isFile() ? // Base is a filename?
f.getParent() : f.getPath(); // Get directory only
f = new File(basepath, filename); // Combine base & path
}
try { filename = f.getCanonicalPath(); } // Resolve separators
catch (IOException e) { return null; } // File access error
return filename; // Fully resolved path
}
// Load file fname into a character array
public static char[] loadCharFile(String fname) throws IOException {
File f = new File(fname);
FileReader r = new FileReader(f);
int len = (int)f.length();
char[] chars = new char[len];
if (r.read(chars, 0, len)==-1) chars = null;
r.close();
return chars;
}
// Save a character array to file fname
public static void saveCharFile(String fname, char[] chars) throws IOException {
FileWriter w = new FileWriter(new File(fname));
w.write(chars, 0, chars.length);
w.close();
}
// Read a file from an input stream and return it as a character array
public static char[] loadCharStream(InputStream i) throws IOException {
return loadCharStream(i, 0x1000); // Default buffer size
}
public static char[] loadCharStream(InputStream i, int bufsize)
throws IOException {
BufferedReader r = new BufferedReader(new InputStreamReader(i));
int n; // Input block size
char[] buf = new char[bufsize]; // Input buffer
String s = ""; // String buffer
while ((n = r.read(buf, 0, bufsize))!=-1) // Read up to size bytes
s += new String(buf, 0, n); // Append to string
return s.toCharArray(); // Convert to array
}
// Return the filename (excluding directory path) of a URL
public static String getUrlFilename(URL url) {
String name = url.getFile(); // Get specified path
if (name.endsWith("/")) return ""; // No name included?
int i = name.lastIndexOf('/'); // Find last slash
return (i==-1) ? name : name.substring(i + 1); // Return remainder
}
// Return URL filename (excluding path) or defaultName if no name is included
public static String getUrlFilename(URL url, String defaultName) {
String s = getUrlFilename(url); // Get specified name
return (s.length()==0) ? defaultName : s; // Use default if none
}
// Get the containing Frame of a component
public static Frame getFrame(Component c) {
return (c==null) ? null : // No parent Frame?
(c instanceof Frame) ? (Frame)c : // Component is a Frame?
getFrame(c.getParent()); // Try parent component
}
// Set the font size for component c
public static void setFontSize(Component c, int size) {
Font f = c.getFont();
c.setFont(new Font(f.getName(), f.getStyle(), size));
}
// Return a new button with the specified settings
public static Button newButton(String label, String command, ActionListener a) {
Button button = new Button(label);
button.setActionCommand(command);
if (a!=null) button.addActionListener(a);
return button;
}
// Return a new menu item with the specified settings
public static MenuItem newMenuItem(String label, String command, ActionListener a) {
MenuItem item = new MenuItem(label);
item.setActionCommand(command);
if (a!=null) item.addActionListener(a);
return item;
}
// Add an ActionListener to all items on a menu
public static void addMenuListener(Menu menu, ActionListener a) {
for (int i=menu.getItemCount()-1; i>=0; --i)
menu.getItem(i).addActionListener(a);
}
// Remove an ActionListener from all items on a menu
public static void removeMenuListener(Menu menu, ActionListener a) {
for (int i=menu.getItemCount()-1; i>=0; --i)
menu.getItem(i).removeActionListener(a);
}
// Return a menu item in menu with the specified action command
public static MenuItem getMenuItem(Menu menu, String command) {
int i, num = menu.getItemCount();
for (i=num-1; i>=0; --i) {
MenuItem m = menu.getItem(i);
if (m.getActionCommand().equals(command)) return m;
}
return null;
}
// Fill an area with a colour (preserves the current Graphics colour)
public static void fillArea(int x, int y, int w, int h, Color color, Graphics g) {
Color c = g.getColor(); // Store colour
g.setColor(color); // Set colour
g.fillRect(x, y, w, h); // Fill area
g.setColor(c); // Restore colour
}
// Draw a string with word-wrap within a Graphics clip area
public static void drawStringWrap(String s, Graphics g) {
FontMetrics fm = g.getFontMetrics();
int asc = fm.getMaxAscent(), height = asc + fm.getMaxDescent();
Rectangle clip = g.getClipBounds();
StringTokenizer st = new StringTokenizer(s);
String line = "", word;
int width = 0, w;
int y = clip.y + asc;
while (st.hasMoreTokens()) {
word = st.nextToken() + " ";
w = fm.stringWidth(word);
if ((width > 0) && (width+w > clip.width)) {
g.drawString(line, clip.x, y);
y += height;
line = "";
width = 0;
}
line += word;
width += w;
}
if (width>0) g.drawString(line, clip.x, y);
}
}
Go To:
Source Code