001package org.vishia.gral.base; 002 003import java.io.File; 004 005import org.vishia.byteData.VariableAccess_ifc; 006import org.vishia.byteData.VariableContainer_ifc; 007import org.vishia.gral.ifc.GralColor; 008import org.vishia.gral.ifc.GralMngBuild_ifc; 009import org.vishia.gral.ifc.GralUserAction; 010import org.vishia.gral.ifc.GralWidget_ifc; 011import org.vishia.gral.widget.GralInfoBox; 012import org.vishia.util.KeyCode; 013import org.vishia.util.Java4C; 014 015/**This class contains some standard {@link GralUserAction} for widgets. 016 * The show methods can be used by giving its name with {@link GralWidget#setShowMethod(String)} in the configuration 017 * of any widget. The show method may use parameter given with {@link GralWidget#setDataPath(String)} 018 * or alternatively with parameter of the show method, for example: 019 * <pre> 020 * myWidget.setShowMethod("showBool(datapath, green, red)"; 021 * </pre> 022 * The string given parameters should be separated with a colon. They are evaluated by first calling the show method. 023 * The show method can be configured in a script, see {@link org.vishia.gral.cfg.GralCfgZbnf}. 024 * 025 * @author Hartmut Schorrig 026 * 027 */ 028//@Java4C.ParseError 029public class GralShowMethods 030{ 031 032 033 /**Version, history and license. 034 * <ul> 035 * <li>2014-01-15 Hartmut chg: {@link #showBackColor} stores the value which is used in {@link GralWidget#getFloatValue()} (dyda). 036 * <li>2013-03-13 Hartmut new {@link #setBar} for a {@link GralValueBar} 037 * <li>2012-06-00 Hartmut created as common container for methods for different widgets. 038 * </ul> 039 * 040 * <b>Copyright/Copyleft</b>:<br> 041 * For this source the LGPL Lesser General Public License, 042 * published by the Free Software Foundation is valid. 043 * It means: 044 * <ol> 045 * <li> You can use this source without any restriction for any desired purpose. 046 * <li> You can redistribute copies of this source to everybody. 047 * <li> Every user of this source, also the user of redistribute copies 048 * with or without payment, must accept this license for further using. 049 * <li> But the LPGL is not appropriate for a whole software product, 050 * if this source is only a part of them. It means, the user 051 * must publish this part of source, 052 * but doesn't need to publish the whole source of the own product. 053 * <li> You can study and modify (improve) this source 054 * for own using or for redistribution, but you have to license the 055 * modified sources likewise under this LGPL Lesser General Public License. 056 * You mustn't delete this Copyright/Copyleft inscription in this source file. 057 * </ol> 058 * If you intent to use this source without publishing its usage, you can get 059 * a second license subscribing a special contract with the author. 060 * 061 * @author Hartmut Schorrig = hartmut.schorrig@vishia.de 062 */ 063 public static final int version = 20130313; 064 065 066 067 068 private String getParams; 069 070 071 /**Common aggregation to variables. */ 072 protected final VariableContainer_ifc variableContainer; 073 074 075 076 public GralShowMethods(VariableContainer_ifc container){ 077 variableContainer = container; 078 } 079 080 081 /**Shows the back color of the widget depending on the boolean value of the associated variable. 082 * The params[0] of exec should be an {@link VariableAccess_ifc} instance from which the value is read. 083 * It is because the routine is called as show method in {@link GralWidget#refreshFromVariable(VariableContainer_ifc)}. 084 * The colors should be part of the textual arguments of the show method given in {@link GralWidget#setActionShow(GralUserAction, String[])}. 085 * That textual colors are converted and stored in {@link GralWidget.ConfigData#showParam}. 086 * <br><br> 087 * The value which is read from the variable is stored in {@link GralWidget.DynamicData#fValue} for further using, 088 * for example for a change action (on mouse pressed etc. ). 089 * 090 * param of exec should be a VariableAccess_ifc-instance. The int value 0, 1, ... is used to select one of the back colors. 091 */ 092 public final GralUserAction showBackColor = new GralUserAction("showBackColor"){ 093 094 //Note: don't save data here. It is a common instance. 095 096 @Override public boolean exec(int actionCode, GralWidget_ifc widgd, Object... params){ 097 GralWidget widgg = (GralWidget) widgd; 098 if(widgg.cfg.showParam == null){ //not configurated yet: 099 String[] sColors = widgg.getShowParam(); 100 if(sColors == null){ 101 System.err.println("GralShowMethods.showBackColor config error; The show methods should contain \"(color, color)\";" 102 + widgg.getShowMethod()); 103 widgg.cfg.showParam = new GralColor[1]; 104 widgg.cfg.showParam[0] = GralColor.getColor("ma"); 105 } else { 106 widgg.cfg.showParam = new GralColor[sColors.length]; 107 for(int iDst = 0; iDst < sColors.length; ++iDst){ 108 widgg.cfg.showParam[iDst] = GralColor.getColor(sColors[iDst]); 109 } 110 } 111 } 112 int value; 113 if(params[0] instanceof VariableAccess_ifc){ 114 VariableAccess_ifc variable = (VariableAccess_ifc)params[0]; 115 value = variable.getInt(); 116 widgg.dyda.fValue = value; //store the value from variable. 117 if(value>=0 && value < widgg.cfg.showParam.length){ widgd.setBackColor((GralColor)widgg.cfg.showParam[value], 0); } 118 else { widgd.setBackColor((GralColor)widgg.cfg.showParam[0], 0); } 119 } else { 120 String name = widgd.getName(); 121 System.err.println("GralShowMethods.showBackColor parameter error; widget=" + name); 122 } 123 return true; 124 } 125 126 }; 127 128 129 130 /**Shows the back color of the widget depending on the boolean value of a variable. 131 * param of exec should be a VariableAccessWithIdx-instance. The variable value 0, 1, ... is used to select one of the back colors. */ 132 public final GralUserAction setBar = new GralUserAction("setBar"){ 133 134 //Note: don't save data here. It is a common instance. 135 136 @Override public boolean exec(int actionCode, GralWidget_ifc wdgi, Object... params){ 137 if(!(wdgi instanceof GralValueBar)) return false; 138 GralValueBar wdg = (GralValueBar) wdgi; 139 if(wdg.fLevels == null){ //not configurated yet: 140 String[] sShowParam = wdg.getShowParam(); 141 wdg.setBorderAndColors(sShowParam); 142 } 143 if(params[0] instanceof VariableAccess_ifc){ 144 VariableAccess_ifc variable = (VariableAccess_ifc)params[0]; 145 float value = variable.getFloat(); 146 wdg.setValue(value); 147 } else { 148 String name = wdgi.getName(); 149 System.err.println("GralShowMethods.showBackColor parameter error; widget=" + name); 150 } 151 return true; 152 } 153 154 }; 155 156 157 158 /**This userAction can be used by name (calling {@link #addFocusAction(String, GralUserAction, String, String)} 159 * to set a variable when an input field is leaved. 160 */ 161 public final GralUserAction syncVariableOnFocus = new GralUserAction("syncVariableOnFocus") 162 { /**Writes the value to the named variable on leaving the focus. 163 * The name of the variable is contained in the {@link GralWidget}. 164 * @see org.vishia.gral.ifc.GralUserAction#userActionGui(java.lang.String, org.vishia.gral.base.GralWidget, java.lang.Object[]) 165 */ 166 @Override public boolean exec(int actionCode, GralWidget_ifc widgi, Object... params) 167 { 168 GralWidget widg = (GralWidget)widgi; 169 final VariableAccess_ifc variable = widg.getVariable(variableContainer); 170 if(variable !=null){ 171 if(actionCode == KeyCode.focusGained){ 172 widg.setText(variable.getString()); 173 //if(oWidget instanceof Text){ sValue = ((Text)oWidget).getText(); variable.setString(sValue); } 174 //else { sValue = null; } 175 } else if(actionCode == KeyCode.focusLost){ 176 if(widg.isChanged(true)){ 177 String sValue = widg.getValue(); 178 variable.setString(sValue); 179 } 180 //if(oWidget instanceof Text){ sValue = variable.getString(); ((Text)oWidget).setText(sValue == null ? "" : sValue); } 181 //else { sValue = null; } 182 } //else throw new IllegalArgumentException("GralMng.syncVariableOnFocus: unexpected intension on focus: " + actionCode); 183 } else { 184 //throw new IllegalArgumentException("GralMng.syncVariableOnFocus: variable not found: " + widg.getDataPath()); 185 } 186 return true; 187 } 188 }; 189 190 191 192 193 194 /**This userAction can be used by name (calling {@link #addFocusAction(String, GralUserAction, String, String)} 195 * to set a variable when an input field is leaved. 196 */ 197 public final GralUserAction action_openWindow = new GralUserAction("openWindow") 198 { /**Writes the value to the named variable on leaving the focus. 199 * The name of the variable is contained in the {@link GralWidget}. 200 * @see org.vishia.gral.ifc.GralUserAction#userActionGui(java.lang.String, org.vishia.gral.base.GralWidget, java.lang.Object[]) 201 */ 202 @Override public boolean exec(int actionCode, GralWidget_ifc widgi, Object... params) 203 { 204 if(KeyCode.isControlFunctionMouseUpOrMenu(actionCode) || actionCode == KeyCode.mouse1Double) { 205 GralWidget widg = (GralWidget)widgi; 206 String nameWindow = (params.length >=1 && (params[0] instanceof String)) ? (String)params[0] : widg.sCmd; //sCmd for buttons 207 if(nameWindow !=null) { 208 if(nameWindow.endsWith("wind")) { nameWindow = nameWindow.substring(0, nameWindow.length()-4); } 209 GralMng mng = GralMng.get(); 210 GralPanelContent panelWind = mng.getPanel(nameWindow); 211 if(panelWind !=null) { panelWind.setFocus(); } 212 else { System.err.println("action_openWindow: window not found: " + nameWindow); } 213 } 214 else { System.err.println("action_openWindow: name not given: ");} 215 } 216 return true; 217 } 218 }; 219 220 221 222 223 224 /**This userAction can be used by name (calling {@link #addFocusAction(String, GralUserAction, String, String)} 225 * to set a variable when an input field is leaved. 226 */ 227 public final GralUserAction action_openHelp = new GralUserAction("openHtml") 228 { /**Writes the value to the named variable on leaving the focus. 229 * The name of the variable is contained in the {@link GralWidget}. 230 * @see org.vishia.gral.ifc.GralUserAction#userActionGui(java.lang.String, org.vishia.gral.base.GralWidget, java.lang.Object[]) 231 */ 232 @Override public boolean exec(int actionCode, GralWidget_ifc widgi, Object... params) 233 { 234 if(KeyCode.isControlFunctionMouseUpOrMenu(actionCode) || actionCode == KeyCode.mouse1Double) { 235 GralWidget widg = (GralWidget)widgi; 236 String path = (params.length >=1 && (params[0] instanceof String)) ? (String)params[0] : widg.sCmd; //sCmd for buttons 237 File fileCurrdir = new File(".").getAbsoluteFile(); 238 String sDir = System.getProperty("pwd"); 239 String path2 = fileCurrdir.getAbsolutePath() + "/" + path; 240 GralMng mng = GralMng.get(); 241 GralInfoBox windhelp= mng.infoHelp; 242 windhelp.activate(); 243 windhelp.setUrl(path2); 244 //GralPanelContent panelWind = mng.getPanel("help"); 245 //if(panelWind !=null) { panelWind.setFocus(); } 246 //else { System.err.println("action_openWindow: window not found: help"); } 247 } 248 return true; 249 } 250 }; 251 252 253 254 255 256 257 258 259 260 public void registerShowMethods(GralMngBuild_ifc mng){ 261 mng.registerUserAction("showBackColor", showBackColor); 262 mng.registerUserAction("syncVariableOnFocus", this.syncVariableOnFocus); 263 mng.registerUserAction("setBar", this.setBar); 264 mng.registerUserAction("openWindow", action_openWindow); 265 mng.registerUserAction("openHelp", action_openHelp); 266 267 } 268 269 270}