001/*
002 * Copyright (c) 2005 Einar Pehrson <einar@pehrson.nu>.
003 *
004 * This file is part of
005 * CleanSheets - a spreadsheet application for the Java platform.
006 *
007 * CleanSheets is free software; you can redistribute it and/or modify
008 * it under the terms of the GNU General Public License as published by
009 * the Free Software Foundation; either version 2 of the License, or
010 * (at your option) any later version.
011 *
012 * CleanSheets is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015 * GNU General Public License for more details.
016 *
017 * You should have received a copy of the GNU General Public License
018 * along with CleanSheets; if not, write to the Free Software
019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
020 */
021package csheets.ui.sheet;
022
023import java.awt.Dimension;
024
025import javax.swing.JComboBox;
026import javax.swing.JLabel;
027import javax.swing.JTextField;
028import javax.swing.SwingConstants;
029
030import csheets.core.Cell;
031import csheets.ui.ctrl.SelectionEvent;
032import csheets.ui.ctrl.SelectionListener;
033import csheets.ui.ctrl.UIController;
034
035/**
036 * A text field in which cell addresses are displayed, and can be named.
037 * @author Einar Pehrson
038 */
039@SuppressWarnings("serial")
040public class AddressBox extends JComboBox implements SelectionListener {
041
042        /** The user interface controller */
043        // private UIController uiController;
044
045        /**
046         * Creates a cell content field.
047         * @param uiController the user interface controller
048         */
049        public AddressBox(UIController uiController) {
050                // Stores user interface controller
051                // this.uiController = uiController;
052                uiController.addSelectionListener(this);
053
054                // Configures box
055                setPreferredSize(new Dimension(80, 20));
056                setEditable(false); // Change!
057                if (getRenderer() instanceof JLabel)
058                        ((JLabel)getRenderer()).setHorizontalAlignment(SwingConstants.CENTER);
059                if (getEditor().getEditorComponent() instanceof JTextField)
060                        ((JTextField)getEditor().getEditorComponent()).setHorizontalAlignment(SwingConstants.CENTER);
061                // setAction(new RegisterNameAction());
062        }
063
064        /**
065         * Updates the text field with the content of the new active cell.
066         * @param event the selection event that was fired
067         */
068        public void selectionChanged(SelectionEvent event) {
069                Cell cell = event.getCell();
070                if (cell != null) {
071                        if (getItemCount() > 0)
072                                removeItemAt(0);
073                        insertItemAt(cell, 0);
074                        setSelectedIndex(0);
075                }
076        }
077}