// --------------------------------------- // Restricted Focus Viewer (RFV) Program // Version 2.1 // Language: Java 2 // October, 2000 // Copyright (C) 2000 Anthony R. Jansen // --------------------------------------- // 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. // --------------------------------------- // RFV_Display_Row.java // // Class for specifying display elements // to be set out in a horizontal row. // --------------------------------------- import java.util.*; import java.awt.Graphics; public class RFV_Display_Row extends RFV_Display_Element { // Variables for row elements // -------------------------- private LinkedList row_elements; private int num_flexible_spaces; private boolean calculated; // Constructor // ----------- public RFV_Display_Row() { super(false, false); calculated = false; num_flexible_spaces = 0; row_elements = new LinkedList(); } // Add an element to the row // ------------------------- public void addElement(RFV_Display_Element element) { row_elements.add(element); element.setParent(this); if (element.isFlexibleSpace()) num_flexible_spaces++; } // Returns the number of elements // ------------------------------ public int getNumberOfElements() { return row_elements.size(); } // Returns the indexed element // --------------------------- public RFV_Display_Element getElement(int index) { RFV_Display_Element element = null; try { element = (RFV_Display_Element) row_elements.get(index); } catch(IndexOutOfBoundsException e) { RFV_Error.die(49); } return element; } // Adds flexible space to ends of the row // if it is needed, otherwise does nothing // --------------------------------------- public void addFlexibleSpace() { if (num_flexible_spaces == 0) { row_elements.add(new RFV_H_Space()); row_elements.addFirst(new RFV_H_Space()); num_flexible_spaces = 2; } } // Draws row // --------- public void draw(Graphics g) { for (int i = 0; i < getNumberOfElements(); i++) getElement(i).draw(g); } // Calculates the row dimensions if not previously done // ---------------------------------------------------- public void calculateDimensions() { RFV_Display_Element element; int maximum_height = 0; if (calculated) return; width = 0; for (int i = 0; i < getNumberOfElements(); i++) { element = getElement(i); if (!element.isSingleElement()) ((RFV_Display_Column) element).calculateDimensions(); if (element.getHeight() > maximum_height) maximum_height= element.getHeight(); width += element.getWidth(); } height = maximum_height; calculated = true; } // Allocates extra width to the flexible spaces // so that the row has the new specified width // -------------------------------------------- public void extendWidthTo(int w) { RFV_Display_Element element; int modulus; int division; if (!calculated) return; addFlexibleSpace(); modulus = (w - width) % num_flexible_spaces; division = (w - width - modulus) / num_flexible_spaces; for (int i = 0; i < getNumberOfElements(); i++) { element = getElement(i); if (element.isFlexibleSpace()) { if (modulus > 0) { ((RFV_H_Space) element).setWidth(division + 1); modulus--; } else ((RFV_H_Space) element).setWidth(division); } else if (!element.isSingleElement()) ((RFV_Display_Column) element).extendHeightTo(height); } width = w; } // Calculates the top left co-ordinates (x_offset, y_offset) // of all of the elements in the row // --------------------------------------------------------- public void calculateOffsets() { RFV_Display_Element element; int x_loc; int y_loc; if (!calculated) return; x_loc = x_offset; for (int i = 0; i < getNumberOfElements(); i++) { element = getElement(i); y_loc = ((height - element.getHeight()) / 2) + y_offset; element.setLocation(x_loc, y_loc); if (!element.isSingleElement()) ((RFV_Display_Column) element).calculateOffsets(); x_loc += element.getWidth(); } } }