    001package org.vishia.gral.swt;
    002
    003import java.util.List;
    004import java.util.Queue;
    005
    006import org.eclipse.swt.SWT;
    007import org.eclipse.swt.events.ControlEvent;
    008import org.eclipse.swt.events.ControlListener;
    009import org.eclipse.swt.events.FocusEvent;
    010import org.eclipse.swt.events.FocusListener;
    011import org.eclipse.swt.events.SelectionEvent;
    012import org.eclipse.swt.events.SelectionListener;
    013import org.eclipse.swt.graphics.Color;
    014import org.eclipse.swt.graphics.Rectangle;
    015import org.eclipse.swt.widgets.Composite;
    016import org.eclipse.swt.widgets.Control;
    017import org.eclipse.swt.widgets.TabFolder;
    018import org.eclipse.swt.widgets.TabItem;
    019import org.eclipse.swt.widgets.Widget;
    020import org.vishia.gral.base.GralPanelContent;
    021import org.vishia.gral.base.GralTabbedPanel;
    022import org.vishia.gral.base.GralPanelActivated_ifc;
    023import org.vishia.gral.base.GralWidget;
    024import org.vishia.gral.ifc.GralColor;
    025import org.vishia.gral.ifc.GralRectangle;
    026
    027public class SwtTabbedPanel extends GralTabbedPanel.ImplAccess
    028{
    029
    030  /**Version, history and license.
    031   * <ul>
    032   * <li>2012-03-31 Hartmut new: supports {@link GralPanelContent.MethodsCalledbackFromImplementation#setVisibleState(boolean)}
    033   * <li>2011-06-00 Hartmut created
    034   * </ul>
    035   *
    036   * <b>Copyright/Copyleft</b>:<br>
    037   * For this source the LGPL Lesser General Public License,
    038   * published by the Free Software Foundation is valid.
    039   * It means:
    040   * <ol>
    041   * <li> You can use this source without any restriction for any desired purpose.
    042   * <li> You can redistribute copies of this source to everybody.
    043   * <li> Every user of this source, also the user of redistribute copies
    044   *    with or without payment, must accept this license for further using.
    045   * <li> But the LPGL is not appropriate for a whole software product,
    046   *    if this source is only a part of them. It means, the user
    047   *    must publish this part of source,
    048   *    but doesn't need to publish the whole source of the own product.
    049   * <li> You can study and modify (improve) this source
    050   *    for own using or for redistribution, but you have to license the
    051   *    modified sources likewise under this LGPL Lesser General Public License.
    052   *    You mustn't delete this Copyright/Copyleft inscription in this source file.
    053   * </ol>
    054   * If you intent to use this source without publishing its usage, you can get
    055   * a second license subscribing a special contract with the author.
    056   *
    057   * @author Hartmut Schorrig = hartmut.schorrig@vishia.de
    058   */
    059  public static final int version = 20120331;
    060
    061
    062  /**The Swt TabFolder implementation. */
    063  /*pkgprivate*/ TabFolder widgetSwt;
    064
    065        final SwtMng mng;
    066
    067        SwtTabbedPanel(GralTabbedPanel panelg, SwtMng mng, GralPanelActivated_ifc user, int property)
    068        { super(panelg);  //initializes as GralWidget and as GralPanel
    069                this.mng = mng;
    070                Object oParent = widgg.pos().panel._wdgImpl.getWidgetImplementation(); //this.pos().panel.getPanelImpl();
    071    if(oParent == null || !(oParent instanceof Composite) ){
    072      throw new IllegalArgumentException("Software error. You must select a panel before.");
    073    }
    074                Composite parent = (Composite)oParent;
    075
    076                //this.panelComposite = parent;
    077    widgetSwt = new TabFolder(parent, SWT.TOP);
    078
    079
    080                widgetSwt.addSelectionListener(tabItemSelectListener);
    081                widgetSwt.addControlListener(resizeListener);
    082
    083        }
    084
    085
    086
    087
    088        @Override public GralPanelContent addGridPanel(GralPanelContent panelg, String sLabel, int yGrid, int xGrid, int yGrid2, int xGrid2)
    089        { ///
    090          widgg.gralMng().setTabbedPanel(widgg);  //setMngToTabbedPanel();
    091          Rectangle sizeTabFolder = widgetSwt.getBounds();
    092          TabItem tabItem = new TabItem(widgetSwt, SWT.None);
    093          tabItem.setText(sLabel);
    094          //tabItem.addFocusListener(SWT.FocusIn, focusTabListener);
    095          SwtCanvasStorePanel panel;
    096          Color colorBackground = mng.propertiesGuiSwt.colorSwt(0xeeeeee);
    097          if(yGrid <0 || xGrid <0){
    098            panel = new SwtCanvasStorePanel(panelg, widgetSwt, 0, colorBackground, mng.mng);
    099          } else {
    100            panel = new SwtGridPanel(panelg, widgetSwt, 0, colorBackground, mng.mng.propertiesGui.xPixelUnit(), mng.mng.propertiesGui.yPixelUnit(), 5, 5, mng.mng);
    101          }
    102          panel.swtCanvas.setBounds(sizeTabFolder);
    103          panel.itsTabSwt = tabItem;
    104          tabItem.setControl(panel.swtCanvas);
    105          panel.swtCanvas.addFocusListener(focusTabListener); //unused...
    106          GralPanelContent gralPanel = panel.gralPanel();
    107          //mng.mng.registerPanel(gralPanel);   //register the panel in the mng.
    108          //mng.mng.registerWidget(gralPanel);
    109          //panels.put(sName, gralPanel);   //register the tab panel in the TabbedPanel
    110          mng.mng.setPosition(0, 0, 0, 0, 0, '.');
    111          return gralPanel;
    112        }
    113
    114
    115        @Override public GralPanelContent addCanvasPanel(GralPanelContent panelg, String sLabel)
    116        {
    117           widgg.gralMng().setTabbedPanel(widgg);  //setMngToTabbedPanel();
    118           TabItem tabItemOperation = new TabItem(widgetSwt, SWT.None);
    119           tabItemOperation.setText(sLabel);
    120           Color colorBackground = mng.propertiesGuiSwt.colorSwt(0xeeeeee);
    121           SwtCanvasStorePanel swtPanel = (new SwtCanvasStorePanel(panelg, widgetSwt, 0, colorBackground, mng.mng));
    122           GralPanelContent panel = swtPanel.gralPanel();
    123           //mng.mng.registerPanel(panel);
    124           tabItemOperation.setControl(swtPanel.swtCanvas);
    125           return panel;
    126        }
    127
    128
    129
    130        /**See {@link GralWidget#setFocusGThread()}
    131         * @see org.vishia.gral.base.GralTabbedPanel#selectTab(java.lang.String)
    132         */
    133        @Override public GralPanelContent selectTab(String name)
    134        { //assert(false);
    135
    136          GralPanelContent panel = mng.mng.getPanel(name);
    137          Object oSwtPanel = panel.getImpl();  //getWidgetImplementation();
    138          SwtPanel swtPanel = (SwtPanel)oSwtPanel;
    139          if(swtPanel.itsTabSwt !=null){
    140                  widgetSwt.setSelection(swtPanel.itsTabSwt);
    141          }
    142          return panel;
    143        }
    144
    145
    146  public SelectionListener tabItemSelectListener = new SelectionListener(){
    147
    148                @Override
    149                public void widgetDefaultSelected(SelectionEvent event)
    150                {
    151                        widgetSelected(event);
    152                }
    153
    154
    155                /**It is the selected method of the TabFolder.
    156                 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
    157                 */
    158                @Override public void widgetSelected(SelectionEvent event)
    159                {
    160                        try{
    161                  TabItem tab = (TabItem)event.item;    //The tab
    162                        Control container = tab.getControl(); //Its container
    163                        if(container != null){
    164                        //TabFolder tabFolder = tab.getParent();
    165                                Object data = container.getData();
    166                                if(data != null){
    167                                        @SuppressWarnings("unchecked")
    168                                        SwtPanel swtPanel = (SwtPanel)data;
    169                                        GralPanelContent panelContent = (GralPanelContent)(swtPanel.widgg);
    170                                        List<GralWidget> widgetInfos = panelContent.getWidgetList();
    171                                        widgg.newWidgetsVisible = widgetInfos;  //the next call of getWidgetsVisible will be move this reference to widgetsVisible.
    172                                        if(getFocusedTab() !=null){
    173                                          widgg.getFocusedTab().setVisibleState(false);  //the last focused tab.
    174                                        }
    175                                        setFocusedTab( panelContent );
    176                                        //done with setFocus: widgg.focusedTab.setVisibleState(true);   //the currently focused tab.
    177                                        panelContent.setFocus();
    178                                        //System.out.printf("Fcmd-selectTab; %s", panelContent.toString());
    179            //mng.log.sendMsg(0, "Fcmd-selectTab %s", panelContent.toString());
    180                                        if(widgg.notifyingUserInstanceWhileSelectingTab !=null){
    181                                        widgg.notifyingUserInstanceWhileSelectingTab.panelActivatedGui(widgetInfos);
    182            }
    183                }
    184                        }
    185                }
    186                        catch(Exception exc){
    187                          String sMsg = exc.getMessage();
    188                          if(sMsg == null){ sMsg = "nullPointer"; }
    189                          System.err.println(sMsg);
    190                          exc.printStackTrace(System.err);
    191                        }
    192                }
    193  };
    194
    195
    196  ControlListener resizeListener = new ControlListener()
    197  { @Override public void controlMoved(ControlEvent e)
    198    { //do nothing if moved.
    199      stop();
    200    }
    201
    202    @Override public void controlResized(ControlEvent e)
    203    {
    204      stop();
    205      //validateFrameAreas();  //calculates the size of the areas newly and redraw.
    206    }
    207
    208  };
    209
    210
    211  ControlListener resizeItemListener = new ControlListener()
    212  { @Override public void controlMoved(ControlEvent e)
    213    { //do nothing if moved.
    214      stop();
    215    }
    216
    217    @Override public void controlResized(ControlEvent e)
    218    {
    219      stop();
    220      //validateFrameAreas();  //calculates the size of the areas newly and redraw.
    221    }
    222
    223  };
    224
    225
    226  FocusListener focusTabListener = new FocusListener()
    227  {
    228
    229    @Override
    230    public void focusGained(FocusEvent e)
    231    {
    232      // TODO Auto-generated method stub
    233      stop();
    234    }
    235
    236    @Override
    237    public void focusLost(FocusEvent e)
    238    {
    239      // TODO Auto-generated method stub
    240
    241    }
    242
    243  };
    244
    245  @Override public Widget getWidgetImplementation(){ return widgetSwt; }
    246
    247
    248
    249  @Override public GralRectangle getPixelPositionSize(){ return SwtWidgetHelper.getPixelPositionSize(widgetSwt); }
    250
    251
    252  /*
    253  @Override public GralRectangle getPixelSize(){
    254    Rectangle r = ((Composite)panelComposite).getClientArea();
    255    GralRectangle posSize = new GralRectangle(0, 0, r.width, r.height);
    256    return posSize;
    257  }
    258  */
    259
    260
    261  @Override public boolean setFocusGThread(){
    262    super.widgg.setVisibleState(true);
    263    return widgetSwt.setFocus();
    264  }
    265
    266  @Override public void setVisibleGThread(boolean bVisible) { super.setVisibleState(bVisible); widgetSwt.setVisible(bVisible); }
    267
    268
    269  @Override public void setBoundsPixel(int x, int y, int dx, int dy)
    270  { widgetSwt.setBounds(x,y,dx,dy);
    271  }
    272
    273
    274
    275  @Override public void repaintGthread(){  widgetSwt.redraw(); widgetSwt.update(); }
    276
    277  //@Override public Composite getPanelImpl() { return widgetSwt; }
    278
    279  @Override public void removeWidgetImplementation()
    280  { widgetSwt.dispose();
    281    widgetSwt = null;
    282  }
    283
    284
    285
    286  void stop(){}
    287
    288
    289
    290
    291  @Override public GralRectangle getPixelSize()
    292  {
    293    // TODO Auto-generated method stub
    294    return null;
    295  }
    296
    297
    298}