gt:     protected void initStyleElements() throws DbException {
context: /*************************************************************************
  
  Copyright (C) 2008 Grandite
  
  This file is part of Open ModelSphere.
  
  Open ModelSphere is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 3 of the License, or
  (at your option) any later version.
  
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
  or see http://www.gnu.org/licenses/.
  
  You can reach Grandite at: 
  
  20-1220 Lebourgneuf Blvd.
  Quebec, QC
  Canada  G2K 2G4
  
  or
  
  open-modelsphere@grandite.com
  
   **********************************************************************/
  
  package org.modelsphere.sms.or.graphic;
  
  //Java
  import java.awt.Color;
  import java.awt.Font;
  import java.awt.Image;
  import java.awt.Point;
  import java.awt.Rectangle;
  import java.util.HashSet;
  import java.util.Vector;
  
  import javax.swing.JComponent;
  
  import org.modelsphere.jack.actions.ActionInformation;
  import org.modelsphere.jack.baseDb.db.Db;
  import org.modelsphere.jack.baseDb.db.DbEnumeration;
  import org.modelsphere.jack.baseDb.db.DbException;
  import org.modelsphere.jack.baseDb.db.DbObject;
  import org.modelsphere.jack.baseDb.db.DbRelationN;
  import org.modelsphere.jack.baseDb.db.DbSemanticalObject;
  import org.modelsphere.jack.baseDb.db.event.DbRefreshListener;
  import org.modelsphere.jack.baseDb.db.event.DbRefreshPassListener;
  import org.modelsphere.jack.baseDb.db.event.DbUpdateEvent;
  import org.modelsphere.jack.baseDb.db.srtypes.DbtPrefix;
  import org.modelsphere.jack.baseDb.meta.MetaClass;
  import org.modelsphere.jack.baseDb.meta.MetaField;
  import org.modelsphere.jack.debug.Debug;
  import org.modelsphere.jack.graphic.Diagram;
  import org.modelsphere.jack.graphic.DiagramView;
  import org.modelsphere.jack.graphic.GraphicNode;
  import org.modelsphere.jack.graphic.GraphicUtil;
  import org.modelsphere.jack.graphic.ZoneBox;
  import org.modelsphere.jack.graphic.shape.RectangleShape;
  import org.modelsphere.jack.graphic.zone.CellEditor;
  import org.modelsphere.jack.graphic.zone.CellID;
  import org.modelsphere.jack.graphic.zone.CellRenderer;
  import org.modelsphere.jack.graphic.zone.CellRenderingOption;
  import org.modelsphere.jack.graphic.zone.ColumnCellsOption;
  import org.modelsphere.jack.graphic.zone.CompositeCellRenderer;
  import org.modelsphere.jack.graphic.zone.ImageCellRenderer;
  import org.modelsphere.jack.graphic.zone.MatrixZone;
  import org.modelsphere.jack.graphic.zone.StringCellRenderer;
  import org.modelsphere.jack.graphic.zone.ZoneCell;
  import org.modelsphere.jack.graphic.zone.CompositeCellRenderer.CompositeCellRendererElement;
  import org.modelsphere.jack.srtool.graphic.DbTextFieldCellEditor;
  import org.modelsphere.jack.srtool.graphic.DomainCellEditor;
  import org.modelsphere.jack.text.MessageFormat;
  import org.modelsphere.sms.db.DbSMSCommonItemGo;
  import org.modelsphere.sms.db.DbSMSGraphicalObject;
  import org.modelsphere.sms.db.srtypes.SMSDisplayDescriptor;
  import org.modelsphere.sms.or.db.DbORColumn;
  import org.modelsphere.sms.or.db.DbORCommonItem;
  import org.modelsphere.sms.or.db.DbORCommonItemStyle;
  import org.modelsphere.sms.or.db.DbORTypeClassifier;
  import org.modelsphere.sms.or.international.LocaleMgr;
  
  public class ORCommonItemBox extends GraphicNode implements ActionInformation {
  
      private static final String DESCRIPTOR_FONT = "m_descriptorFont"; //NOT LOCALIZABLE
  
      //    the commonItem prefix
      private static final int C_I_PREFIX_IDX = 0;
      //    the commonItem name
      private static final int C_I_NAME_IDX = 1;
      //    the commonItem type
      private static final int C_I_TYPE_IDX = 2;
      //    the commonItem lenght & decimal
      private static final int C_I_LENGHT_DECIMAL_IDX = 3;
      //    the commonItem null value possible
      private static final int C_I_NULL_VALUE_IDX = 4;
      //    the commonItem duplicate
      private static final int C_I_DUPLICATE_IDX = 5;
  
      private static final String C_I_NAME_ZONE_ID = "Box Name"; //NOT LOCALIZABLE, property key
  
      static {
          BoxRefreshTg commonItemRefreshTg = new BoxRefreshTg();
          Db.addDbRefreshPassListener(commonItemRefreshTg);
          MetaField.addDbRefreshListener(commonItemRefreshTg, null, new MetaField[] {
                  DbObject.fComponents, DbSemanticalObject.fName, DbSemanticalObject.fAlias,
                  DbSemanticalObject.fPhysicalName, DbORCommonItem.fCommonItemGos,
                  DbORCommonItem.fType, DbORCommonItem.fLength, DbORCommonItem.fNbDecimal,
                  DbORCommonItem.fNull });
  
      }
  
      //the db reference
      protected DbSMSCommonItemGo commonItemGO;
      protected DbORCommonItem commonItemSO;
      // the zones
      transient private MatrixZone commonItemZone = new MatrixZone(C_I_NAME_ZONE_ID,
              GraphicUtil.CENTER_ALIGNMENT);
  
      //listeners
      transient private BoxGoRefreshTg commonItemGoRefreshTg = new BoxGoRefreshTg();
  
      //rendering options
      transient private CellRenderingOption commonItemPrefixCRO;
      transient private CellRenderingOption commonItemNameCRO;
      transient private CellRenderingOption commonItemTypeCRO;
      transient private CellRenderingOption commonItemLenghtDecimalCRO;
      transient private CellRenderingOption commonItemNullValueCRO;
      transient private CellRenderingOption duplicateCRO = new CellRenderingOption(
              new StringCellRenderer(), new Font("SansSerif", Font.PLAIN, 8), //NOT LOCALIZABLE
              GraphicUtil.RIGHT_ALIGNMENT);
  
      transient private ColumnCellsOption commonItemNameCellsOption;
  
      public ORCommonItemBox(Diagram diag, DbSMSCommonItemGo newBoxGO) throws DbException {
          super(diag, RectangleShape.singleton);
          commonItemGO = newBoxGO;
          commonItemSO = (DbORCommonItem) newBoxGO.getSO();
          setAutoFit(commonItemGO.isAutoFit());
          setRectangle(commonItemGO.getRectangle());
          objectInit();
      }
  
      protected void objectInit() throws DbException {
          commonItemGO.setGraphicPeer(this);
          commonItemGO.addDbRefreshListener(commonItemGoRefreshTg);
          initCellRenderingOptions();
          setZones();
          initStyleElements();
          populateContents();
  
      }
  
      private void initCellRenderingOptions() throws DbException {
          Color color = (Color) commonItemGO.find(DbSMSCommonItemGo.fTextColor);
          commonItemNameCRO = getCellRenderingOptionForConcept(commonItemNameCRO,
                  getStyleMetaField(DESCRIPTOR_FONT), null, color);
          commonItemNameCRO.setAlignment(GraphicUtil.CENTER_ALIGNMENT);
          duplicateCRO.setColor(color);
  
          commonItemTypeCRO = getCellRenderingOptionForConcept(commonItemTypeCRO,
                  DbORCommonItemStyle.fOr_commonItemTypeFont, null, color);
  
          commonItemLenghtDecimalCRO = getCellRenderingOptionForConcept(commonItemLenghtDecimalCRO,
                  DbORCommonItemStyle.fOr_commonItemLengthDecimalsFont, null, color);
          commonItemNullValueCRO = getCellRenderingOptionForConcept(commonItemNullValueCRO,
                  DbORCommonItemStyle.fOr_commonItemNullValueFont, null, color);
  
          //prefix
          commonItemPrefixCRO = getCellRenderingOptionForConcept(commonItemPrefixCRO,
                  getStyleMetaField(DESCRIPTOR_FONT), new CompositeCellRenderer(), color);
      }
  
      private MetaField getStyleMetaField(String genericName) throws DbException {
          MetaField mf = null;
          String nameMetaField;
  
          MetaClass soMetaClass = commonItemSO.getMetaClass();
          nameMetaField = soMetaClass.getJClass().getName();
          nameMetaField = nameMetaField.substring(nameMetaField.lastIndexOf('.') + 1);
  
          return commonItemGO.findStyle().getMetaField(genericName.concat(nameMetaField));
      }
  
      private CellRenderingOption getCellRenderingOptionForConcept(CellRenderingOption cellRO,
              MetaField metaField, CellRenderer renderer, Color color) throws DbException {
          Font font = (Font) commonItemGO.find(metaField);
          if (cellRO == null) {
              CellRenderingOption cellOption = new CellRenderingOption(renderer != null ? renderer
                      : new StringCellRenderer(), font, GraphicUtil.LEFT_ALIGNMENT);
              cellOption.setColor(color);
              return cellOption;
          } else {
              cellRO.setFont(font);
              cellRO.setColor(color);
              return cellRO;
          }
      }
  
      public MatrixZone getBoxZone() {
          return commonItemZone;
      }
  
      public Db getDb() {
          return commonItemGO.getDb();
      }
  
      public final DbObject getSemanticalObject() {
          return commonItemSO;
      }
  
      public final DbObject getGraphicalObject() {
          return commonItemGO;
      }
  
      public final boolean selectAtCellLevel() {
          return true;
      }
  
      // BEWARE: overriding triggers must call super.delete as last action
      public void delete(boolean all) {
          commonItemGO.setGraphicPeer(null);
          commonItemGO.removeDbRefreshListener(commonItemGoRefreshTg);
          commonItemZone.removeAllRows();
  
          super.delete(all);
      }
  
      private void setZones() throws DbException {
          setZonesVisibility();
  
          commonItemZone.addColumn(new ColumnCellsOption(commonItemPrefixCRO, null));
          commonItemZone.addColumn(new ColumnCellsOption(commonItemNameCRO, null));
          commonItemZone.addColumn(new ColumnCellsOption(commonItemTypeCRO, new TypeCellEditor()));
          commonItemZone.addColumn(new ColumnCellsOption(commonItemLenghtDecimalCRO, null));
          commonItemZone.addColumn(new ColumnCellsOption(commonItemNullValueCRO, null));
          commonItemZone.addColumn(new ColumnCellsOption(duplicateCRO, null));
  
          addZone(commonItemZone);
      }
  
      protected void setZonesVisibility() throws DbException {
          // Modify with zone
      }
  
      protected void setBoxColor() throws DbException {
          setFillColor((Color) commonItemGO.find(DbSMSCommonItemGo.fBackgroundColor));
          setLineColor((Color) commonItemGO.find(DbSMSCommonItemGo.fLineColor));
      }
  
      protected void setLineStyle() throws DbException {
          this.setLineStyle((Boolean) commonItemGO.find(DbSMSCommonItemGo.fHighlight),
                  (Boolean) commonItemGO.find(DbSMSCommonItemGo.fDashStyle));
      }
  
  