PropertiesTableModel.java :  » UML » MetaBoss » com » metaboss » applications » designstudio » propertiesview » Java Open Source

Java Open Source » UML » MetaBoss 
MetaBoss » com » metaboss » applications » designstudio » propertiesview » PropertiesTableModel.java
// THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
// CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
// OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// Copyright 2000-2005  Softaris Pty.Ltd. All Rights Reserved.
package com.metaboss.applications.designstudio.propertiesview;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Properties;

import javax.swing.table.AbstractTableModel;

import com.metaboss.applications.designstudio.userobjects.FieldDescriptor;

/*    Properties tree model class    */

public class PropertiesTableModel extends AbstractTableModel
{ 
  protected ArrayList mList = new ArrayList();
  
  /* property descriptor */
  public class PropertyItem 
  {
    public String     mCaption = null;
    public Object     mValue = null;
    public boolean     mReadOnly = false;
    
    public PropertyItem(String pCaption, Object pValue)
    {
      this(pCaption, pValue, true);
    }
    public PropertyItem(String pCaption, Object pValue, boolean pReadOnly)
    {
      mCaption = pCaption;
      mValue = pValue;
      mReadOnly = pReadOnly;
    }
  }
  
  public void AddProperty(String pCaption, Object pValue)
  {
    AddProperty(pCaption, pValue, true);
  }
  public void AddProperty(String pCaption, Object pValue, boolean pReadOnly)
  {
    mList.add(new PropertyItem(pCaption, pValue, pReadOnly));
    fireTableDataChanged();
  }
  
  public void addNewProperty()
  {
    AddProperty(getNewPropertyName(), "");
  }
  
  public void deleteProperty(int pIndex)
  {
    if (pIndex>-1 && pIndex<mList.size())
    {
      mList.remove(pIndex);
      fireTableDataChanged();
    }
  }
    
  public void loadPorperties(Properties pProperties)
  {
    clear();
    if (pProperties!=null)
    {
      for (Enumeration e = pProperties.propertyNames() ; e.hasMoreElements() ;) 
      {
        String lKey = e.nextElement().toString();
        AddProperty(lKey, pProperties.getProperty(lKey));
      }
    }
  }
  
  public void loadProperties(Object[] pFields)
  {
    clear();
    if (pFields!=null)
    {
      for (int i=0; i<pFields.length; i++)
      {
        FieldDescriptor lDescriptor = (FieldDescriptor)pFields[i];
        AddProperty(lDescriptor.getFieldName(), lDescriptor.getFieldType());
      }
    }
  }
  
  public Properties getProperties()
  {
    Properties lResult = new Properties();
    for (int i=0; i<mList.size(); i++)
    {
      PropertyItem lItem = (PropertyItem)mList.get(i); 
      lResult.put(lItem.mCaption, lItem.mValue);
    }
    return lResult;
  }
  
  public void clear()
  {
    mList.clear();
  }
  
  public int getRowCount()
  {
    if (mList!=null)
      return mList.size();
    else
      return 0;    
  }
    
  public int getColumnCount()
  {
    return 2;
  }
    
  public Object getValueAt(int r, int c)
  {
    switch (c)
    {
      case 0:
        return ((PropertyItem)mList.get(r)).mCaption;
      case 1: 
        return ((PropertyItem)mList.get(r)).mValue;
    }
    return null;
  }

  public Object getValue(int pIndex)
  {
    return getValueAt(pIndex, 1);
  }

  public String getColumnName(int c)
  { 
    switch (c)
    {
      case 0:
        return "Property";
      case 1: 
        return "Value";
    }
    return null;
  }
  
  public boolean isCellEditable(int r, int c)
  {
    switch (c)
    {
      case 0:
        return false;
      case 1: 
        return !((PropertyItem)mList.get(r)).mReadOnly;
    }
    return false;
  }
  
  public void setValueAt(Object aValue, int r, int c)
  {
    switch (c)
    {
      case 0:
        ((PropertyItem)mList.get(r)).mCaption = aValue.toString();
        break; 
      case 1: 
        ((PropertyItem)mList.get(r)).mValue = aValue;
        break; 
    }
  }
  
  private String getNewPropertyName()
  {
    String lInitial = "NewProperty";
    String lResult = lInitial;
    int n = 1;
    while (findPropertyIndex(lResult)>-1)
    {
      lResult = lInitial + Integer.toString(n);
      n++;
    }
    return lResult;
  }
  
  private int findPropertyIndex(String pName)
  {
    for (int i=0; i<mList.size(); i++)
    {
      Object lName = getValueAt(i, 0);
      if (lName!=null && lName.equals(pName)) return i;
    }
    return -1;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.