/* * Copyright (C) 2013 by MHC SoftWare GmbH, All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package Klassen; import xdev.ui.*; import xdev.ui.Validator; import xdev.vt.VirtualTable; import xdev.vt.VirtualTables; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.util.ArrayList; import java.util.List; public class WindowModifier { private static Color defaultColor = new Color(250,240,230); private static Class[] defaultFieldTypes = {XdevTextField.class,XdevTextArea.class, XdevListBox.class,XdevComboBox.class,XdevFormattedTextField.class, XdevDateTextField.class,XdevPasswordField.class,XdevCheckBox.class, XdevRadioButton.class,XdevPicture.class }; /** * Durchsucht Container rekursiv und liefert eine Liste der Objekte die den Typen entsprechen * * @param c Container der druchsucht werden soll * @param fieldTypes Array der Feldtypen * @return */ private static List getAllComponents(Container c, Class[] fieldTypes) { List compList = new ArrayList(); for(Component comp : c.getComponents()) { for(Object target : fieldTypes) { if(((Class)target).isInstance(comp)) { compList.add(comp); } } if(comp instanceof Container) compList.addAll(getAllComponents((Container)comp,fieldTypes)); } return compList; } /** * Setzt den Hintergund auf die gewünschte Farbe und fügt ggf. einen RequiredFieldValidator * an alle Objekte eines Containers, die vom Typ der angegeben Klassen sind, vorausgesetzt * in der virtuellen Tabelle ist für die Spalte, die in dem Feld als Datenfeld hinterlegt ist, * ist auf "not null" gesetzt. *

* Der Container kann z.B. ein Window sein. Beispiel: *

*

	 * {@code
	 * WindowModifier.setMandBackgoundColor(this,
	 *    new Color(255, 174, 185), XdevTextField.class, XdevTextArea.class);
	 * }
	 * 
* @param c Container * @param color Farbe * @param fieldTypes Array von Typen */ @SuppressWarnings("rawtypes") public static void setMandBackgoundColor(Container c, Color color, Class... fieldTypes) { // Liste der Objekte laden List list = getAllComponents(c,fieldTypes); for(Component comp : list) { // Datenfeld auslsen String df = ((FormularComponent)comp).getDataField(); // Wenn ein Datenfeld gesetzt ist if(df != null) { // virtuelle Tabelle ermitteln VirtualTable vt = VirtualTables .getVirtualTable(df.substring(0,df.lastIndexOf("."))); if(vt != null) { String colName = df.substring(df.lastIndexOf(".") + 1,df.length()); // Prüfen, ob die ermittelte Spalte auf "not null" gesetzt ist if(!vt.getColumn(colName).isNullable()) { // Farbe setzen comp.setBackground(color); // Validatoren durchsuchen und ggf. neuen hinzufügen Boolean RFV = false; for(Validator v : ((FormularComponent)comp).getValidators()) { if(v instanceof RequiredFieldValidator) { RFV = true; break; } } if(!RFV) { String fieldName = vt.getColumn(colName).getCaption(); if(fieldName.isEmpty()) { fieldName = vt.getColumn(colName).getName(); } ((FormularComponent)comp).addValidator(new RequiredFieldValidator( "Das Feld >" + fieldName + "< darf nicht leer sein!","Fehler")); } } } } } } /** * Setzt den Hintergund auf die standard Farbe für Mussfelder und fügt ggf. einen RequiredFieldValidator * an alle Objekte eines Containers, die vom Typ der angegeben Klassen sind, vorausgesetzt * in der virtuellen Tabelle ist für die Spalte, die in dem Feld als Datenfeld hinterlegt ist, * ist auf "not null" gesetzt. *

* Der Container kann z.B. ein Window sein. Beispiel: *

*

	 * {@code
	 * WindowModifier.setMandBackgoundColor(this, XdevTextField.class, XdevTextArea.class); 
	 * }
	 * 
* @param c Container * @param fieldTypes Array von Typen */ public static void setMandBackgoundColor(Container c, Class... fieldTypes) { setMandBackgoundColor(c,defaultColor,fieldTypes); } /** * Setzt den Hintergund auf die standard Farbe für Mussfelder und fügt ggf. einen RequiredFieldValidator * an alle Objekte eines Containers, die in der Defaul-Liste stehen, vorausgesetzt * in der virtuellen Tabelle ist für die Spalte, die in dem Feld als Datenfeld hinterlegt ist, * ist auf "not null" gesetzt. *

* Der Container kann z.B. ein Window sein. Beispiel: *

*

	 * {@code
	 * WindowModifier.setMandBackgoundColor(this);
	 * }
	 * 
* @param c Container */ public static void setMandBackgoundColor(Container c) { setMandBackgoundColor(c,defaultColor,defaultFieldTypes); } }