001    /////////////////////////////////////////////////
002    //This file is part of Sears project.
003    //Subtitle Editor And Re-Synch
004    //A tool to easily modify and resynch movies subtitles.
005    /////////////////////////////////////////////////
006    //This program is free software; 
007    //you can redistribute it and/or modify it under the terms 
008    //of the GNU General Public License 
009    //as published by the Free Software Foundation; 
010    //either version 2 of the License, or (at your option) any later version.
011    /////////////////////////////////////////////////
012    //Sears project is available under sourceforge
013    //at adress: http://sourceforge.net/projects/sears/
014    //Copyright (C) 2005 Booba Skaya
015    //Mail: booba.skaya@gmail.com
016    /////////////////////////////////////////////////
017    
018    //some suggestions about this class: floriaen@gmail.com
019    
020    package sears.search.core;
021    
022    import java.awt.Graphics2D;
023    import java.awt.Point;
024    import java.awt.Rectangle;
025    
026    import sears.gui.SubtitleCellComponent;
027    import sears.gui.glassPane.element.GPShapeManager;
028    import sears.search.gui.PillShape;
029    
030    /**
031     * the <code>PillManager</code> class provides a way to restrict the access to the <code>Pill</code> class.
032     * <br>The associated word <i>"Manager"</i> in the class name is not a reference to the <i>"Manager"</i> design pattern.
033     * <br>This class is more like a <i>"Factory"</i> ...
034     */
035    public class PillManager implements GPShapeManager {
036            // the pill to manage
037            private PillShape pill;
038    
039            /**
040             * Constructs a new </code>PillManager</code> object
041             */
042            public PillManager() {
043    
044            }
045    
046            /**
047             * Update pill
048             * @param text          a non null and non empty string
049             * @param cell          a cell that represents the row where the occurrence of <tt>text</tt>is
050             * @param index         the index of the first char of the <tt>text</tt> occurrence 
051             * @param location      the pill location
052             */
053            public void firePillDataMustBeUpdated(String text, SubtitleCellComponent cell, 
054                            int index, Point location) {
055    
056                    if( text != null && text.trim().length() > 0 ) {                     
057                            String realSubString = cell.getSubStringBeginAtIndex(index, text);
058                            Rectangle bounds = cell.getBoundsBeginAtIndex(index, text);
059                            // realSubstring != null
060                            // blackBoxBounds != null blackBoxBounds.trim().length() > 0
061                            //pill = null;
062                            if( pill != null ) {
063                                    pill = pill.getPillInstance(cell.getFont(), bounds, realSubString, location);
064                            } else {        
065                                    pill = new PillShape(cell.getFont(), bounds, realSubString);
066                                    pill.translate(location);
067                            }                       
068                    }
069            }
070    
071            /**
072             * Paints a pill in the graphics context given in parameters
073             * @param gr    the graphics context
074             */
075            public void paintPill(Graphics2D gr) {
076                    if( gr != null ) {
077                            if( pill != null ) {
078                                    pill.paint(gr);
079                            }
080                    } // DEVELOPPERS: else...
081            }
082    }