001package org.vishia.gral.awt;
002
003import java.awt.Canvas;
004import java.awt.Color;
005import java.awt.Container;
006import java.awt.Font;
007import java.awt.FontMetrics;
008import java.awt.Graphics;
009import java.awt.Rectangle;
010
011import org.vishia.gral.base.GralButton;
012import org.vishia.gral.ifc.GralColor;
013import org.vishia.gral.ifc.GralRectangle;
014
015public class AwtButton extends GralButton.GraphicImplAccess
016{
017  /**Version, history and license.
018   * <ul>
019   * <li>2013-12-22 Hartmut chg: Now {@link GralButton} uses the new concept of instantiation: It is not
020   *   the super class of the implementation class. But it provides {@link GralButton.GraphicImplAccess}
021   *   as the super class. 
022   * <li>2012-03-09 Hartmut new setFocus on press button
023   * <li>2012-03-09 Hartmut new 3-state-Button.
024   * <li>2012-03-09 Hartmut improved: appearance of the buttons.
025   * <li>All other changes in 2010, 2011
026   * </ul>
027   * 
028   * <b>Copyright/Copyleft</b>:<br>
029   * For this source the LGPL Lesser General Public License,
030   * published by the Free Software Foundation is valid.
031   * It means:
032   * <ol>
033   * <li> You can use this source without any restriction for any desired purpose.
034   * <li> You can redistribute copies of this source to everybody.
035   * <li> Every user of this source, also the user of redistribute copies
036   *    with or without payment, must accept this license for further using.
037   * <li> But the LPGL is not appropriate for a whole software product,
038   *    if this source is only a part of them. It means, the user
039   *    must publish this part of source,
040   *    but doesn't need to publish the whole source of the own product.
041   * <li> You can study and modify (improve) this source
042   *    for own using or for redistribution, but you have to license the
043   *    modified sources likewise under this LGPL Lesser General Public License.
044   *    You mustn't delete this Copyright/Copyleft inscription in this source file.
045   * </ol>
046   * If you intent to use this source without publishing its usage, you can get
047   * a second license subscribing a special contract with the author. 
048   * 
049   * @author Hartmut Schorrig = hartmut.schorrig@vishia.de
050   */
051  //@SuppressWarnings("hiding")
052  public static final int version = 20130524;
053
054  AwtWidgetHelper helper;
055  
056  AwtButtonImpl widgetSwt;
057  
058  final Color black;
059  final Color white;
060  
061  final Font fontText;
062  
063  
064  final AwtGralMouseListener.MouseListenerGralAction mouseListener;
065  
066
067  
068  //public AwtButton(String sName, AwtWidgetMng mng, Container parent, int styleSwt, char size)
069  AwtButton(GralButton widgg, AwtWidgetMng mng)
070  {
071    widgg.super(widgg, mng.mng);
072    mouseListener = new AwtGralMouseListener.MouseListenerGralAction(mouseWidgetAction);
073    float ySize = widgg.pos().height();
074    char size1 = ySize > 3? 'B' : 'A';
075    switch(size1){ 
076      case 'A': fontText = mng.propertiesGuiAwt.stdInputFont; break;
077      case 'B': fontText = mng.propertiesGuiAwt.stdButtonFont; break;
078      default: throw new IllegalArgumentException("param size must be A or B");
079      }
080      //Control xx = mng.pos.panel.panelComposite;
081      black = mng.propertiesGuiAwt.colorAwt(GralColor.getColor("bk"));
082      white = mng.propertiesGuiAwt.colorAwt(GralColor.getColor("wh"));
083      Container panelSwt = mng.getCurrentPanel();
084      int styleSwt = 0;
085      widgetSwt = new AwtButtonImpl(panelSwt, styleSwt);
086      widgetSwt.setData(widgg);
087      widgetSwt.setBackground(mng.propertiesGuiAwt.colorBackground);
088      widgetSwt.addMouseListener(mouseListener);
089      setBoundsGraphic(mng);
090      helper = new AwtWidgetHelper(widgetSwt, mng);
091  }
092
093  
094  
095  @Override public GralRectangle getPixelPositionSize(){ return helper.getPixelPositionSize(); }
096
097  
098  void setBoundsGraphic(AwtWidgetMng mng)
099  {
100    //widgetSwt.setSize(mng.propertiesGui.xPixelUnit() * xSize -2, mng.propertiesGui.yPixelUnit() * ySize -2);
101    mng.setBounds_(widgg.pos(), widgetSwt);
102    
103  }
104  
105  
106
107  
108  @Override public boolean setFocusGThread()
109  { return AwtWidgetHelper.setFocusOfTabSwt(widgetSwt);
110  }
111
112
113  
114  
115  @Override
116  public void removeWidgetImplementation()
117  {
118    // TODO Auto-generated method stub
119    
120  }
121
122  @Override
123  public Object getWidgetImplementation()
124  { return widgetSwt; }
125
126  
127  @Override public void setBoundsPixel(int x, int y, int dx, int dy)
128  { widgetSwt.setBounds(x,y,dx,dy);
129  }
130  
131  protected void paintRoutine(Graphics gc, Canvas canvas){
132    Rectangle dim = canvas.getBounds();
133    AwtButton.this.paint1();
134    
135    Color colorBack = helper.mng.getColorImpl(colorgback);
136    canvas.setBackground(colorBack);
137    //drawBackground(gc, dim.x+1, dim.y+1, dim.width-1, dim.height-1);
138    Color color = canvas.getForeground(); //of the widget.
139    canvas.setForeground(color);  //black
140    gc.setFont(fontText);
141    //FontData fontData = mng.propertiesGui.stdButtonFont.getFontData();
142    //fontData.
143    if(isPressed()){
144      //gc.setLineWidth(3);
145      gc.drawRect(1,1,dim.width-2, dim.height-2);
146      //gc.setLineStyle(SWT.LINE_DOT);
147      gc.drawRect(3,3,dim.width-6, dim.height-6);
148    } else {
149      //gc.setLineWidth(1);
150      //setForeground(colorBack);
151      gc.setColor(colorBack);
152      gc.fillRect(2,2,dim.width-6, dim.height-6);
153      canvas.setForeground(black);
154      gc.drawRect(1,1,dim.width-5, dim.height-5);
155      //setForeground(white); 
156      gc.setColor(black);
157      //gc.setLineWidth(3);
158      gc.drawLine(0, dim.height-2,dim.width, dim.height-2);
159      gc.drawLine(dim.width-1, 0, dim.width-1, dim.height);
160    }
161    if(sButtonText !=null){
162      FontMetrics fontMetrics = gc.getFontMetrics();
163      int charWidth = 8; //fontMetrics.getAverageCharWidth();
164      int halfWidthButtonText = charWidth * sButtonText.length() /2;
165      int xText = dim.width / 2 - halfWidthButtonText;
166      if(xText < 2){ xText = 2; }
167      int halfHeightButtonText = fontMetrics.getHeight() /2;
168      int yText = dim.height / 2 + halfHeightButtonText;
169      canvas.setForeground(black);
170      gc.drawString(sButtonText, xText, yText);
171    }
172     System.out.println("Button") ;
173  }
174  
175  
176  private class AwtButtonImpl extends Canvas implements AwtWidget
177  {
178    
179    private Object data;
180    
181
182    
183    AwtButtonImpl(Container parent, int style){
184      super();
185      setForeground(black);
186      parent.add(this);      
187      //addPaintListener(paintListener);  
188      
189    }
190    
191    public Object getData(){ return data; }
192    
193    public void setData(Object dataP){ data = dataP; }
194    
195    //PaintListener paintListener = new PaintListener(){
196      @Override public void paint(Graphics gc) {
197        paintRoutine(gc, this);
198     }
199   }
200
201
202
203  @Override
204  public void repaintGthread() {
205    // TODO Auto-generated method stub
206    
207  };
208    
209
210    
211  /**Sets the implementation widget vible or not.
212   * @see org.vishia.gral.base.GralWidgImpl_ifc#setVisibleGThread(boolean)
213   */
214  @Override public void setVisibleGThread(boolean bVisible){ super.setVisibleState(bVisible); helper.setVisibleGThread(bVisible); }
215  
216  
217
218  
219}