/* dIntProg Browser. A webbrowser written in Java. * Copyright (C) 2001 Martin Geisler <[email protected]> * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the * * Free Software Foundation, Inc., * 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ import java.awt.Graphics; import java.awt.Color; import java.awt.Font; import javax.swing.*; import java.util.*; /** An ordered list, eg. one with numbers before each item. */ public class OrderedList extends VerticalBoxStack { private final int hspace = 6; private int max_width; private Font font; private int nesting; private Graphics pen; /** Creates an ordered list. The list will either use labels like * 1., 2. and so on or labels like (a), (b), depending on the * argument. * @param n the nesting. If the nesting is even, then the lables * will be numbers, but if it's odd the labels will be letters. * * <p>Strange things will happen if there is more than 25 items * in a list that uses letters as the labels... (at first you'll * get brackets as labels, but later you'll get small squares, as * you run out of characters.) * @param f the font used for the labels. */ public OrderedList(int n, Font f) { nesting = n; font = f; } public void doLayout(Graphics g, JComponent c, int w) { pen = g; pen.setFont(font); if (nesting % 2 == 0) { /* We use number like '1.' */ max_width = pen.getFontMetrics().stringWidth(boxes.size() + "."); } else { /* We use letters like '(a)'. We meassure 'm' as it's * probably the widest letter. */ max_width = pen.getFontMetrics().stringWidth("(m)"); } super.doLayout(g, c, w - max_width - 2*hspace); width = w; } public void drawAt(int x, int y, DocumentView v) { int y_offset = 0; int count = 1; int w; String label; pen.setFont(font); ListIterator iterator = boxes.listIterator(); while (iterator.hasNext()) { if (nesting % 2 == 0) { label = "" + count + "."; } else { label = "(" + (char)('a' + count-1) + ")"; } w = pen.getFontMetrics().stringWidth(label); v.drawString(x + hspace + max_width - w, y + y_offset, label, font, null); count = count + 1; Box b = (Box)iterator.next(); b.drawAt(x + max_width + 2*hspace, y+y_offset, v); y_offset = y_offset + b.getHeight(); } } }