001//==JZcmd==
002//JZcmd Obj a = java org.vishia.gral.widget.GralColorSelector.todo();
003//==endJZcmd==
004
005package org.vishia.gral.widget;
006
007import org.vishia.gral.base.GralButton;
008import org.vishia.gral.base.GralPos;
009import org.vishia.gral.ifc.GralColor;
010import org.vishia.gral.ifc.GralMngBuild_ifc;
011import org.vishia.gral.ifc.GralUserAction;
012import org.vishia.gral.ifc.GralWidget_ifc;
013import org.vishia.gral.ifc.GralWindow_ifc;
014import org.vishia.util.KeyCode;
015
016/**This class creates a window with buttons for selecting colors.
017 * The window may be created permanently but hided. On calling {@link #openDialog(String, GralUserAction)}
018 * the window is placed in foreground in a concurrently mode. 
019 * The user have to implement a instance of {@link GralColorSelector.SetColorIfc}. This interface instance
020 * is used as parameter for {@link #openDialog(String, SetColorIfc)} to receive the color. The user should show
021 * its graphic with the selected color immediately to see the effect in the users context.
022 * @author Hartmut Schorrig
023 *
024 */
025public class GralColorSelector
026{
027
028  /**Version, history and license. The version number is a date written as yyyymmdd as decimal number.
029   * Changes:
030   * <ul>
031   * <li>2013-04-15 Hartmut create new
032   * </ul>
033   * <br><br> 
034   * 
035   * <b>Copyright/Copyleft</b>:
036   * For this source the LGPL Lesser General Public License,
037   * published by the Free Software Foundation is valid.
038   * It means:
039   * <ol>
040   * <li> You can use this source without any restriction for any desired purpose.
041   * <li> You can redistribute copies of this source to everybody.
042   * <li> Every user of this source, also the user of redistribute copies
043   *    with or without payment, must accept this license for further using.
044   * <li> But the LPGL ist not appropriate for a whole software product,
045   *    if this source is only a part of them. It means, the user
046   *    must publish this part of source,
047   *    but don't need to publish the whole source of the own product.
048   * <li> You can study and modify (improve) this source
049   *    for own using or for redistribution, but you have to license the
050   *    modified sources likewise under this LGPL Lesser General Public License.
051   *    You mustn't delete this Copyright/Copyleft inscription in this source file.
052   * </ol>
053   * If you are intent to use this sources without publishing its usage, you can get
054   * a second license subscribing a special contract with the author. 
055   * 
056   * @author Hartmut Schorrig = hartmut.schorrig@vishia.de
057   */
058  //@SuppressWarnings("hiding")
059  public final static int version = 20130415;
060
061  
062  
063  
064  String[][] colors = {
065      { "pma", "prd", "por", "pye", "pam",  "pgn", "pcy", "pbl", "pmv", "pgr", "lgr"}
066    , { "lma", "lrd", "lor", "lye", "lam",  "lgn", "lcy", "lbl", "lmv", "gr", "dgr"}
067    , { "Ma1", "Ma2", "Rd1", "Rd2", "Rd3", "Ye1", "Ye2", "Ye3", "Ye4", "Gn1", "Gn2", "Gn3", "Gn4", "Cy1", "Cy2", "Cy3", "Bl1", "Bl2", "Bl3"}     
068    , { "sMa1", "sMa2", "sRd1", "sRd2", "sRd3", "sYe1", "sYe2", "sYe3", "sYe4", "sGn1", "sGn2", "sGn3", "sGn4", "sCy1", "sCy2", "sCy3", "sBl1", "sBl2", "sBl3"}     
069    , { "ma", "rp", "rd", "or", "am", "ye", "ygn", "lm", "gn", "sgn", "cy", "bl", "vi", "gr", "pbk", "lbk"}
070    , { "dma", "pu", "pk", "or", "am", "sye", "fgn", "sg2", "scy", "dbl", "vi", "dgr", "lbk"}
071    , { "dma", "pu", "pk", "dye", "wh", "dgn", "dcy", "dbl", "vi", "dgr", "lbk"}
072    , { "wh", "drd", "bn", "bk"}
073  };
074  
075  
076  private final GralWindow_ifc wind;
077
078  SetColorIfc callback;
079  
080  /**Creates the dialog window to select colors. This window may be created only one time
081   * for some functionality, if the application is attempt to open only one color selection dialog.
082   * Use {@link #openDialog(String, SetColorIfc)} to open the window and specify the destination for selecting colors. 
083   * @return The created window.
084   */
085   public GralColorSelector(String name, GralMngBuild_ifc mng){
086     mng.selectPanel("primaryWindow");
087     mng.setPosition(-24, 0, -67, 0, 1, 'r'); //right bottom, about half less display width and hight.
088     wind = mng.createWindow("windSelectColor", "select file", GralWindow_ifc.windConcurrently | GralWindow_ifc.windResizeable );
089     for(int irow=0; irow <= 5 /*colors.length*/; ++irow){
090       //String[] colorRow = colors[irow];
091       mng.setPosition(0.5f + 4*irow, GralPos.size + 2.5f, 0, GralPos.size + 3.5f, 0, 'r', 0.5f);
092       for(int ix=0; ix < 19 /*colorRow.length*/; ++ix){
093         //String sColor = colorRow[ix];
094         GralColor color = GralColor.getStdColor(irow, ix);
095         String sColor = color.getColorName();
096         GralButton btn = mng.addButton("", actionSelect_, sColor);
097         btn.setBackColor(color, 0);
098         if(irow >=3){
099           btn.setLineColor(GralColor.getColor("wh"), 0);
100         }
101       }
102       mng.setPosition(0.5f + 4*irow + 2.6f, GralPos.size + 1.2f, 0, GralPos.size + 3.5f, 0, 'r', 0.5f);
103       for(int ix=0; ix < 19 /*colorRow.length*/; ++ix){
104         GralColor color = GralColor.getStdColor(irow, ix);
105         int colorValue = color.getColorValue();
106         String sColorValue = String.format("%06x", new Integer(colorValue));
107         mng.addText(sColorValue);
108       }
109     }
110    // mng.setPosition(0.5f + 3*irow, 3, 0, GralPos.size + 2.5f, 0, 'r', 0.5f);
111     
112   }
113   
114
115   /**Opens the window with the given title. The window is non-exclusive.
116   * @param sTitle The title of the window
117   * @param actionSelect called method on press any color button.
118   */
119  public void openDialog(String sTitle, SetColorIfc actionSelect){
120     wind.setTitle(sTitle);
121     callback = actionSelect;
122     wind.setFocus(); //setWindowVisible(true);
123  }
124
125   
126  /**Action for a color button. It calls {@link #callback}*/
127  private final GralUserAction actionSelect_ = new GralUserAction("GralColorSelector-select"){
128    @Override public boolean exec(int actionCode, GralWidget_ifc widgd, Object... params){
129      if(KeyCode.isControlFunctionMouseUpOrMenu(actionCode)){
130        GralButton widg = (GralButton)widgd;
131        if(callback !=null){
132          GralColor color = widg.getBackColor(0);
133          callback.setColor(color, SetColorFor.line);
134        }
135        return true;
136      } else return false;
137    }
138  };
139  
140   
141  /**Enumeration to specify what's color is changed.  */
142  public enum SetColorFor{
143     back, line, text
144   }
145   
146  /**The user can build some instances with this interface.
147   * A anonymous inner class is recommended in form:
148   * <pre>
149  public GralColorSelector.SetColorIfc actionColorSet = new GralColorSelector.SetColorIfc(){
150    @Override public void setColor(GralColor color, SetColorFor what) { 
151      switch(what){
152        case SetColorFor.back: {
153          //do something for show the line
154        } break;
155        case SetColorFor.line: {
156          //do something for show the line
157        } break;
158        case SetColorFor.text: {
159          //do something for show the line
160        } break;
161      }//switch
162  } };
163   * </pre>
164   * In that routine the widget which should be changed may be chosen by the users algorithm maybe depending
165   * by selecting any widget or other mode. So the GralColorSelector-window may remain open to change 
166   * the colors of some parts one after another in a simple way.
167   */
168  public interface SetColorIfc{
169     void setColor(GralColor color, SetColorFor what);
170   }
171   
172  
173}