001package org.vishia.gral.base;
002
003import java.util.List;
004import java.util.Map;
005import java.util.TreeMap;
006import org.vishia.util.Java4C;
007import org.vishia.gral.ifc.GralVisibleWidgets_ifc;
008
009
010/**This class is the common base class for Tabbed-Panels.
011 * A TabbbedPanel is the container for Tabs. It doesn't contain other widgets than Tabs.
012 * It is a {@link GralPanelContent} because it can be used as one of the areas 
013 * in {@link org.vishia.gral.area9.GralArea9Window}.
014 * It is a {@link GralWidget} because it is used as one member inside another Panel.
015 * The Implementation of this class should create a swt.TabFolder or adequate
016 * A Tab inside the TabPanel is a Panel.
017 * <br><br>
018 * Concepts in base graphic:
019 * <ul>
020 * <li>SWT: TabFolder, TabItem
021 * <li>Swing: TabbedPane
022 * </ul>
023 * @author Hartmut Schorrig
024 *
025 */
026@Java4C.ParseError
027public class GralTabbedPanel extends GralPanelContent /*extends GralWidget*/ implements GralVisibleWidgets_ifc
028{
029  /**The version and history:
030   * <ul>
031   * <li>2013-10-22 Hartmut bugfix: {@link #setMngToTabbedPanel()} necessary because a new Tab should refer to this parent.
032   *   On {@link #addCanvasPanel(String, String)} or {@link #addGridPanel(String, String, int, int, int, int)} the mng
033   *   should refer this as {@link GralMng#pos}.parent. 
034   * <li>2012-01-08 Hartmut new: {@link #removePanel(String)}
035   * <li>2011-11-07 Hartmut chg: a TabbedPanel is a GralWidget too, 
036   * <li>2011-10-01 Hartmut new: abstract method {@link #selectTab(String)}.
037   * <li>2011-09-10 Hartmut chg: move this class from gral/gridPanel/TabPanel to gral/base/GralTabbedPanel
038   *     Reason: it isn't a concept of the grid panel  but a basic concept of gral. It hasn't any dependencies to gridPanel. 
039   * <li>2011-09-08 Hartmut chg: The {@link #widgetsVisible} were stored in the GuiPanelMngBase before.
040   *     But an application can have more as one tabbed panel, and any of them can change the visible widgets while changing the tab.
041   *     The visible widgets are not an concpet of the grid panel manager but a topic of the panel content. 
042   *     It is not a question of one PanelContent but a question of the TabbedPanel which contains some panels.
043   * <li>2011-08-31 Hartmut chg: The {@link #focusedTab} and {@link #getFocusedTab()} is moved from GuiPanelMngBase to here.
044   *     Reason: One application can have more as one tabbed panel.    
045   * <li>2011-05-26 Hartmut chg: {@link #addCanvasPanel(String, String)} now returns {@link GralPanelContent} instead an untyped Object.    
046   * <li>2010-05-00 Hartmut created   
047   * </ul>
048   * 
049   * <b>Copyright/Copyleft</b>:
050   * For this source the LGPL Lesser General Public License,
051   * published by the Free Software Foundation is valid.
052   * It means:
053   * <ol>
054   * <li> You can use this source without any restriction for any desired purpose.
055   * <li> You can redistribute copies of this source to everybody.
056   * <li> Every user of this source, also the user of redistribute copies
057   *    with or without payment, must accept this license for further using.
058   * <li> But the LPGL ist not appropriate for a whole software product,
059   *    if this source is only a part of them. It means, the user
060   *    must publish this part of source,
061   *    but don't need to publish the whole source of the own product.
062   * <li> You can study and modify (improve) this source
063   *    for own using or for redistribution, but you have to license the
064   *    modified sources likewise under this LGPL Lesser General Public License.
065   *    You mustn't delete this Copyright/Copyleft inscription in this source file.
066   * </ol>
067   * If you are indent to use this sources without publishing its usage, you can get
068   * a second license subscribing a special contract with the author. 
069   * 
070   * @author Hartmut Schorrig = hartmut.schorrig@vishia.de
071   * 
072   * 
073   */
074  @SuppressWarnings("hiding")
075  public static final int version = 0x20111001;
076  
077        final public GralPanelActivated_ifc notifyingUserInstanceWhileSelectingTab;
078        
079        final protected Map<String, GralPanelContent> panels = new TreeMap<String, GralPanelContent>();
080
081        /**The currently selected tab. */
082        //public GralPanelContent focusedTab;
083        
084  /**The actual widgets in the visible panel. It may a sub-panel or changed content. The list can be changed. */
085  @Deprecated public List<GralWidget> widgetsVisible;
086
087  /**A new list of actual widgets, set while select another tab etc. The reference may be set 
088   * in the GUI-Thread (GUI-listener). The communication-manager thread reads whether it isn't null,
089   * processes it and sets this reference to null if it is processed. */
090  @Deprecated public List<GralWidget> newWidgetsVisible;
091  
092
093
094        /**The constructor can only be invoked from a implementing class.
095         * @param user
096         */
097        public GralTabbedPanel(String posString, String sName, GralPanelActivated_ifc user, int property)
098        { super(posString, sName);
099          //super(sName, '@', mng);
100          this.notifyingUserInstanceWhileSelectingTab = user;
101        }
102        
103        
104        //protected void setMngToTabbedPanel(){ itsMng.setTabbedPanel(this); }
105        
106        //abstract public GralPanelContent getGuiComponent();
107        
108        public GralPanelContent getFocusedTab(){ return (GralPanelContent)primaryWidget; }
109        
110           /**Adds a grid-panel in the TabPanel. The panel will be registered in the GuiPanelMng,
111     * so the access to the panel can be done with its name.
112     * The position of the widget manager is set to full area of this panel.
113     * 
114     * @param sName The name, used in 
115     * @param sLabel to designate the tab for view. A "&" left from a character determines the hot-key
116     *               to select the tab.
117     * @param yGrid   number of units per grid line vertical. It may be 1. 
118     * @param xGrid   number of units per grid line horizontal. It may be 1. 
119     * @param yGrid2  Number of grid lines vertical per wider ranges for lines
120     * @param xGrid2  Number of grid lines horizontal per wider ranges for lines
121     * @return
122     */
123    public GralPanelContent addGridPanel(String sName, String sLabel, int yGrid, int xGrid, int yGrid2, int xGrid2)
124    {
125      GralMng.PosThreadSafe pos = itsMng.pos();
126      if(pos.pos.panel != this) {
127        pos.pos.setPosition(this, 0, 0, 0, 0, 0, 'd', 0);   //initializes the position of this GralTabbedPanel to have the correct parent for the tab 
128      }
129      GralPanelContent panelg = new GralPanelContent("@", sName);
130      ((ImplAccess)_wdgImpl).addGridPanel(panelg, sLabel, yGrid, xGrid, yGrid2, xGrid2);
131      return panelg;
132    }
133    
134    public GralPanelContent addCanvasPanel(String sName, String sLabel){
135      GralMng.PosThreadSafe pos = itsMng.pos();
136      if(pos.pos.panel != this) {
137        pos.pos.setPosition(this, 0, 0, 0, 0, 0, 'd', 0);   //initializes the position of this GralTabbedPanel to have the correct parent for the tab 
138      }
139      GralPanelContent panelg = new GralPanelContent("@", sName);
140      ((ImplAccess)_wdgImpl).addCanvasPanel(panelg, sLabel);
141      return panelg;
142      
143    }
144 
145        
146    public GralPanelContent selectTab(String name){
147      return ((ImplAccess)_wdgImpl).selectTab(name);
148    }
149    
150  @Override @Deprecated public List<GralWidget> getWidgetsVisible()
151  {
152    if(newWidgetsVisible !=null){
153      //if(panel.widgetList !=null){
154        //remove communication request for actual widgets.
155      //}
156      widgetsVisible = newWidgetsVisible;
157      newWidgetsVisible = null;
158    }
159    
160    return widgetsVisible;
161  }
162  
163
164
165
166 
167
168  /**Sets the visible state to all widgets of the panel, but not to windows (that is only in the primaryWindow)
169   * @see org.vishia.gral.base.GralWidget#setVisibleState(boolean)
170   */
171  @Override public void setVisibleState(boolean visible){
172    setVisibleStateWidget(visible);  //for the own GralWidget
173    //don't touch the tabs, its visible is changed in extra routines.
174    /*
175    for(GralWidget widget: widgetList){
176      if(widget != this && !(widget instanceof GralWindow)) {
177        widget.setVisibleState(visible);
178      }
179    }
180    */
181  }
182
183
184
185
186
187  boolean removePanel(GralPanelContent panel){
188    return removePanel(panel.name);
189  }
190  
191  /**Removes the named panel from this panel container.
192   * @param namePanel The name which was given by {@link #addCanvasPanel(String, String)} or 
193   *   {@link #addGridPanel(String, String, int, int, int, int)} and which is stored in 
194   *   {@link GralPanelContent#namePanel}.
195   * @return true if removed, false if the namePanel isn't ok and therefore nothing is removed.
196   */
197  public boolean removePanel(String namePanel){
198    boolean bOk = true;
199    GralPanelContent panel = panels.get(namePanel);
200    if(panel !=null){
201      panels.remove(namePanel);
202      panel.remove();
203    } else {
204      bOk = false;
205    }
206    return bOk;
207  }
208  
209  
210  public abstract static class ImplAccess extends GralPanelContent.ImplAccess
211  {
212
213    protected final GralTabbedPanel widgg;
214    
215    protected ImplAccess(GralTabbedPanel widgg)
216    {
217      super(widgg);
218      this.widgg = widgg;
219    }
220    
221    protected GralPanelContent getFocusedTab(){ return (GralPanelContent) widgg.primaryWidget; }
222    
223    protected void setFocusedTab(GralPanelContent tab){ widgg.primaryWidget = tab; }
224    
225    /**Adds a grid-panel in the TabPanel. The panel will be registered in the GuiPanelMng,
226     * so the access to the panel can be done with its name.
227     * The position of the widget manager is set to full area of this panel.
228     * 
229     * @param sName The name, used in 
230     * @param sLabel to designate the tab for view. A "&" left from a character determines the hot-key
231     *               to select the tab.
232     * @param yGrid   number of units per grid line vertical. It may be 1. 
233     * @param xGrid   number of units per grid line horizontal. It may be 1. 
234     * @param yGrid2  Number of grid lines vertical per wider ranges for lines
235     * @param xGrid2  Number of grid lines horizontal per wider ranges for lines
236     * @return
237     */
238    abstract public GralPanelContent addGridPanel(GralPanelContent panelg, String sLabel, int yGrid, int xGrid, int yGrid2, int xGrid2);
239    
240    abstract public GralPanelContent addCanvasPanel(GralPanelContent panelg, String sLabel);
241    /**The named tab should be focused.
242     * TODO which widget should be focused? It may be a first widget in any GralPanelContent?
243     * In SWT it works if any widget in a tab is focused. The the tab is focused then automatically.
244     * It means this method is not necessary. If selectTab is called, the whole tab has the focus
245     * which means it content is visible. That is able to use.
246     * @param name
247     * @return
248     * @deprecated use {@link GralWidget#setFocus()}
249     */
250    @Deprecated
251    abstract public GralPanelContent selectTab(String name);
252    
253  }
254
255}