001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.applications.designstudio.propertiesview;
016:
017: import java.util.ArrayList;
018: import java.util.Enumeration;
019: import java.util.Properties;
020:
021: import javax.swing.table.AbstractTableModel;
022:
023: import com.metaboss.applications.designstudio.userobjects.FieldDescriptor;
024:
025: /* Properties tree model class */
026:
027: public class PropertiesTableModel extends AbstractTableModel {
028: protected ArrayList mList = new ArrayList();
029:
030: /* property descriptor */
031: public class PropertyItem {
032: public String mCaption = null;
033: public Object mValue = null;
034: public boolean mReadOnly = false;
035:
036: public PropertyItem(String pCaption, Object pValue) {
037: this (pCaption, pValue, true);
038: }
039:
040: public PropertyItem(String pCaption, Object pValue,
041: boolean pReadOnly) {
042: mCaption = pCaption;
043: mValue = pValue;
044: mReadOnly = pReadOnly;
045: }
046: }
047:
048: public void AddProperty(String pCaption, Object pValue) {
049: AddProperty(pCaption, pValue, true);
050: }
051:
052: public void AddProperty(String pCaption, Object pValue,
053: boolean pReadOnly) {
054: mList.add(new PropertyItem(pCaption, pValue, pReadOnly));
055: fireTableDataChanged();
056: }
057:
058: public void addNewProperty() {
059: AddProperty(getNewPropertyName(), "");
060: }
061:
062: public void deleteProperty(int pIndex) {
063: if (pIndex > -1 && pIndex < mList.size()) {
064: mList.remove(pIndex);
065: fireTableDataChanged();
066: }
067: }
068:
069: public void loadPorperties(Properties pProperties) {
070: clear();
071: if (pProperties != null) {
072: for (Enumeration e = pProperties.propertyNames(); e
073: .hasMoreElements();) {
074: String lKey = e.nextElement().toString();
075: AddProperty(lKey, pProperties.getProperty(lKey));
076: }
077: }
078: }
079:
080: public void loadProperties(Object[] pFields) {
081: clear();
082: if (pFields != null) {
083: for (int i = 0; i < pFields.length; i++) {
084: FieldDescriptor lDescriptor = (FieldDescriptor) pFields[i];
085: AddProperty(lDescriptor.getFieldName(), lDescriptor
086: .getFieldType());
087: }
088: }
089: }
090:
091: public Properties getProperties() {
092: Properties lResult = new Properties();
093: for (int i = 0; i < mList.size(); i++) {
094: PropertyItem lItem = (PropertyItem) mList.get(i);
095: lResult.put(lItem.mCaption, lItem.mValue);
096: }
097: return lResult;
098: }
099:
100: public void clear() {
101: mList.clear();
102: }
103:
104: public int getRowCount() {
105: if (mList != null)
106: return mList.size();
107: else
108: return 0;
109: }
110:
111: public int getColumnCount() {
112: return 2;
113: }
114:
115: public Object getValueAt(int r, int c) {
116: switch (c) {
117: case 0:
118: return ((PropertyItem) mList.get(r)).mCaption;
119: case 1:
120: return ((PropertyItem) mList.get(r)).mValue;
121: }
122: return null;
123: }
124:
125: public Object getValue(int pIndex) {
126: return getValueAt(pIndex, 1);
127: }
128:
129: public String getColumnName(int c) {
130: switch (c) {
131: case 0:
132: return "Property";
133: case 1:
134: return "Value";
135: }
136: return null;
137: }
138:
139: public boolean isCellEditable(int r, int c) {
140: switch (c) {
141: case 0:
142: return false;
143: case 1:
144: return !((PropertyItem) mList.get(r)).mReadOnly;
145: }
146: return false;
147: }
148:
149: public void setValueAt(Object aValue, int r, int c) {
150: switch (c) {
151: case 0:
152: ((PropertyItem) mList.get(r)).mCaption = aValue.toString();
153: break;
154: case 1:
155: ((PropertyItem) mList.get(r)).mValue = aValue;
156: break;
157: }
158: }
159:
160: private String getNewPropertyName() {
161: String lInitial = "NewProperty";
162: String lResult = lInitial;
163: int n = 1;
164: while (findPropertyIndex(lResult) > -1) {
165: lResult = lInitial + Integer.toString(n);
166: n++;
167: }
168: return lResult;
169: }
170:
171: private int findPropertyIndex(String pName) {
172: for (int i = 0; i < mList.size(); i++) {
173: Object lName = getValueAt(i, 0);
174: if (lName != null && lName.equals(pName))
175: return i;
176: }
177: return -1;
178: }
179: }
|