001package csheets.ext.comments.ui;
002
003/*
004 * Copyright (c) 2013 Alexandre Braganca, Einar Pehrson
005 *
006 * This file is part of
007 * CleanSheets Extension for Comments
008 *
009 * CleanSheets Extension for Assertions is free software; you can
010 * redistribute it and/or modify it under the terms of the GNU General Public
011 * License as published by the Free Software Foundation; either version 2 of
012 * the License, or (at your option) any later version.
013 *
014 * CleanSheets Extension for Assertions is distributed in the hope that
015 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
016 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
017 * See the GNU General Public License for more details.
018 *
019 * You should have received a copy of the GNU General Public License
020 * along with CleanSheets Extension for Assertions; if not, write to the
021 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
022 * Boston, MA  02111-1307  USA
023 */
024
025import java.awt.*;
026import java.awt.event.*;
027
028import javax.swing.*;
029import javax.swing.border.TitledBorder;
030
031import csheets.core.Cell;
032import csheets.ext.comments.*;
033import csheets.ui.ctrl.*;
034
035/**
036 * A panel for adding or editing a comment for a cell
037 * 
038 * @author Alexandre Braganca
039 * @author Einar Pehrson
040 */
041@SuppressWarnings("serial")
042public class CommentPanel extends JPanel implements SelectionListener,
043        CommentableCellListener {
044
045    /** The assertion controller */
046    private final CommentController controller;
047
048    /** The commentable cell currently being displayed in the panel */
049    private CommentableCell cell;
050
051    /** The text field in which the comment of the cell is displayed. */
052    private final JTextArea commentField = new JTextArea();
053
054    /**
055     * Creates a new comment panel.
056     * 
057     * @param uiController
058     *            the user interface controller
059     */
060    public CommentPanel(UIController uiController) {
061        // Configures panel
062        super(new BorderLayout());
063        setName(CommentsExtension.NAME);
064
065        // Creates controller
066        controller = new CommentController(uiController);
067        uiController.addSelectionListener(this);
068
069        // Creates comment components
070        ApplyAction applyAction = new ApplyAction();
071
072        commentField.setPreferredSize(new Dimension(120, 240)); // width, height
073        commentField.setMaximumSize(new Dimension(Integer.MAX_VALUE,
074                Integer.MAX_VALUE)); // width, height
075        commentField.addFocusListener(applyAction);
076        commentField.setAlignmentX(Component.CENTER_ALIGNMENT);
077
078        // Lays out comment components
079        JPanel commentPanel = new JPanel();
080        commentPanel
081                .setLayout(new BoxLayout(commentPanel, BoxLayout.PAGE_AXIS));
082        commentPanel.setPreferredSize(new Dimension(130, 336));
083        commentPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE,
084                Integer.MAX_VALUE)); // width, height
085        commentPanel.add(commentField);
086
087        // Adds borders
088        TitledBorder border = BorderFactory.createTitledBorder("Comment");
089        border.setTitleJustification(TitledBorder.CENTER);
090        commentPanel.setBorder(border);
091
092        // Adds panels
093        JPanel northPanel = new JPanel(new BorderLayout());
094        northPanel.add(commentPanel, BorderLayout.NORTH);
095        add(northPanel, BorderLayout.NORTH);
096    }
097
098    /**
099     * Updates the comments field
100     * 
101     * @param event
102     *            the selection event that was fired
103     */
104    @Override
105    public void selectionChanged(SelectionEvent event) {
106        Cell cell = event.getCell();
107        if (cell != null) {
108            CommentableCell activeCell = (CommentableCell) cell
109                    .getExtension(CommentsExtension.NAME);
110            activeCell.addCommentableCellListener(this);
111            commentChanged(activeCell);
112        } else {
113            commentField.setText("");
114        }
115
116        // Stops listening to previous active cell
117        if (event.getPreviousCell() != null)
118            ((CommentableCell) event.getPreviousCell().getExtension(
119                    CommentsExtension.NAME))
120                    .removeCommentableCellListener(this);
121    }
122
123    /**
124     * Updates the comment field when the comments of the active cell is
125     * changed.
126     * 
127     * @param cell
128     *            the cell whose comments changed
129     */
130    @Override
131    public void commentChanged(CommentableCell cell) {
132        // Stores the cell for use when applying comments
133        this.cell = cell;
134
135        // Updates the text field and validates the comment, if any
136        if (cell.hasComment()) {
137            commentField.setText(cell.getUserComment());
138        } else {
139            commentField.setText("");
140        }
141    }
142
143    protected class ApplyAction implements FocusListener {
144
145        @Override
146        public void focusGained(FocusEvent e) {
147
148        }
149
150        @Override
151        public void focusLost(FocusEvent e) {
152            if (cell != null) {
153                controller.setComment(cell, commentField.getText().trim());
154            }
155        }
156    }
157}