001package org.vishia.gral.swt;
002
003import org.eclipse.swt.events.PaintEvent;
004import org.eclipse.swt.events.PaintListener;
005import org.eclipse.swt.graphics.Color;
006import org.eclipse.swt.graphics.GC;
007import org.eclipse.swt.graphics.Rectangle;
008import org.eclipse.swt.widgets.Canvas;
009import org.eclipse.swt.widgets.Composite;
010import org.vishia.gral.base.GralValueBar;
011import org.vishia.gral.ifc.GralColor;
012import org.vishia.gral.ifc.GralRectangle;
013import org.vishia.gral.ifc.GralSetValue_ifc;
014import org.vishia.gral.ifc.GralWidget_ifc;
015
016public class SwtValueBar extends GralValueBar.GraphicImplAccess // implements GralSetValue_ifc, GralWidget_ifc
017{
018
019  /**It contains the association to the swt widget (Control) and the {@link SwtMng}
020   * and implements some methods of {@link GralWidgImpl_ifc} which are delegate from this.
021   */
022  private final SwtWidgetHelper wdgh;
023  
024        //final SwtMng mng;
025
026        protected BarWidget widgetSwt;
027        
028        final Color black;
029        //final Color colorValueOk, colorValueMinLimit, colorValueMaxLimit;
030        //final Color white;
031        
032        private Color[] colorBorder;
033        
034        /**Creates a value bar.
035         * @param mng The Gui-panel-manager contains information about the graphic frame and properties.
036         * @param size The size of text in button, use 'A' or 'B' for small - bold
037         */
038        public SwtValueBar(GralValueBar widgg, SwtMng mng)
039        {
040    widgg.super(mng.mng);
041    //mng.mng.setNextPosition();
042    super.wdgimpl = this.wdgh = new SwtWidgetHelper(widgetSwt, mng);
043    this.widgetSwt = this.new BarWidget();
044                this.widgetSwt.setData(this);
045                this.widgetSwt.setBackground(mng.propertiesGuiSwt.colorBackgroundSwt());
046                //Control xx = mng.currPanel.panelComposite;
047                black = mng.propertiesGuiSwt.colorSwt(0);
048                //white = mng.propertiesGui.color(0xffffff);
049                //colorValueOk = mng.propertiesGui.color(0xff4000);
050                //colorValueMinLimit = mng.propertiesGui.color(0xff4000);
051                //colorValueMaxLimit = mng.propertiesGui.color(0xff4000);
052                colorBorder = new Color[1];  //at least 1 color, if not parametrized
053                colorBorder[0] = mng.propertiesGuiSwt.color("red");
054    //widget.setPanelMng(this);
055  //  widget.setShowMethod(sShowMethod);
056    //widget.widget.setData(widgetInfos);
057    widgetSwt.addMouseListener(mng.mouseClickForInfo);
058    setBounds();
059    
060    mng.mng.registerWidget(widgg);
061        }
062
063  
064        
065        public void setBounds(){
066    pixBounds = wdgh.mng.calcWidgetPosAndSizeSwt(widgg.pos(), widgetSwt.getParent(), 10, 100);
067    //mng.setPosAndSize_(widgetSwt);
068    widgetSwt.setBounds(pixBounds.x, pixBounds.y, pixBounds.dx, pixBounds.dy);
069    horizontal = pixBounds.dx > pixBounds.dy;
070          
071        }
072        
073        
074        
075  @Override public void repaintGthread(){
076    widgetSwt.redraw();
077  }
078
079  
080  @Override
081  public void setBorderAndColorsImpl(String[] sColorLevels)
082  {
083        colorBorder = new Color[sColorLevels.length];
084        int ix = -1;
085        for(String sColor: sColorLevels){
086                colorBorder[++ix] = wdgh.mng.propertiesGuiSwt.color(sColor);
087        }
088  }
089        
090  @Override public Object getWidgetImplementation() { return widgetSwt; }
091  
092  
093  @Override public boolean setFocusGThread(){ return widgetSwt.setFocus(); }
094
095  @Override public void setVisibleGThread(boolean bVisible) { super.setVisibleState(bVisible); wdgh.setVisibleGThread(bVisible); }
096
097  
098  protected void paintRoutine(BarWidget wdgs, PaintEvent e){
099    // TODO Auto-generated method stub
100    GC gc = e.gc;
101    //gc.d
102    Rectangle dim = wdgs.getBounds();
103    int valuePixelMax = horizontal() ? dim.width -2: dim.height -2;
104    if(valuePixelMax != 106)
105      stop();  
106    /*
107    if((SwtValueBar.this.valueMax != valuePixelMax || pixLevel == null)
108      && fLevels !=null && fLevels.length >1) {  //at least one medium border
109      pixLevel = new int[fLevels.length-1];
110      for(int ix = 0; ix < pixLevel.length; ++ix){
111        pixLevel[ix] = dim.height -1 - (int)(valueMax * ((fLevels[ix] - minRange) / (maxRange - minRange)));
112      }
113    }
114    SwtValueBar.this.valueMax = valuePixelMax;
115    */
116    wdgs.drawBackground(e.gc, dim.x, dim.y, dim.width, dim.height);
117    gc.setForeground(black);  //black
118    //FontData fontData = mng.propertiesGui.stdButtonFont.getFontData();
119    //fontData.
120    gc.setLineWidth(1);
121    gc.setForeground(black);  //black
122    gc.drawRectangle(0,0,dim.width-1, dim.height-1);
123    //The bar, colored:
124    gc.setBackground(colorBorder[ixColor()]);  //black
125    //gc.fillRectangle(1,dim.height -1 - value1 ,dim.width-2, value2 - value1);
126    int start, size;
127    if(pix0line() < pixvalue()){
128      start = 1 + pix0line();
129      size = pixvalue() - pix0line();  //difference start bar, end bar
130    } else { //negative value
131      start = pixvalue();
132      size = pix0line() - pixvalue();
133    }
134    if(size > 150 && size < 230)
135      stop();
136    if(horizontal()){
137      gc.fillRectangle(start,1 ,size, dim.height-2);
138    } else {
139      gc.fillRectangle(1,start ,dim.width-2, size);
140    }
141    //division lines for borders.
142    if(pixLevel !=null){
143      if(horizontal()){
144        for(int pixel : pixLevel){
145          gc.drawLine(pixel, 0, pixel, dim.height-1);
146        }
147      } else {
148        for(int pixel : pixLevel){
149          gc.drawLine(0, pixel, dim.width-1, pixel);
150        }
151      }
152    }
153    //gc.drawLine(3,dim.height-2,dim.width, dim.height-2);
154  }
155    
156  
157  
158  
159  public class BarWidget extends Canvas
160        {
161                BarWidget()
162                {
163                        super(wdgh.mng.getCurrentPanel(), 0);  //Canvas
164                        wdgh.widgetSwt = this;
165                        addPaintListener(paintListener);        
166                        
167                }
168                
169                
170          final PaintListener paintListener = new PaintListener(){
171                        @Override
172                        public void paintControl(PaintEvent e) { paintRoutine(BarWidget.this,e); }
173          };
174
175          final Runnable redraw = new Runnable(){
176                        @Override public void run()
177                        { redraw();
178                        }
179                };
180                
181
182        }  
183          
184        void stop(){}
185
186
187  @Override public void setBoundsPixel(int x, int y, int dx, int dy)
188  { widgetSwt.setBounds(x,y,dx,dy);
189  }
190  
191
192  
193  @Override public void removeWidgetImplementation()
194  {
195    widgetSwt.dispose();
196    widgetSwt = null;
197  }
198
199
200  @Override
201  public GralRectangle getPixelPositionSize()
202  {
203    // TODO Auto-generated method stub
204    return null;
205  }
206
207
208}