001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.him.metadata.range.swing.relatedevents;
020:
021: import java.awt.Color;
022: import java.awt.event.*;
023: import java.awt.event.KeyListener;
024: import java.util.List;
025:
026: import javax.swing.JTextArea;
027:
028: import org.openharmonise.vfs.metadata.*;
029: import org.openharmonise.vfs.metadata.range.*;
030: import org.openharmonise.vfs.metadata.value.*;
031:
032: /**
033: * @author Michael Bell
034: * @version $Revision: 1.1 $
035: *
036: */
037: public class RelatedEventText extends JTextArea implements KeyListener {
038:
039: private PropertyInstance m_propInst = null;
040:
041: private static final String INVALID_TEXT = "--Invalid--";
042:
043: /**
044: *
045: */
046: public RelatedEventText(PropertyInstance propInst) {
047: super ();
048: m_propInst = propInst;
049: setup();
050: }
051:
052: /**
053: *
054: */
055: private void setup() {
056: this .setRows(3);
057: this .setColumns(30);
058:
059: List vals = m_propInst.getValues();
060:
061: if (vals.size() > 0) {
062: StringValue val = (StringValue) vals.get(0);
063: if (val.getValue().length() > 0) {
064: this .setText(val.getValue());
065: }
066: } else {
067: setForeground(Color.RED);
068: setText(INVALID_TEXT);
069: }
070:
071: this .addKeyListener(this );
072: }
073:
074: /* (non-Javadoc)
075: * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
076: */
077: public void keyPressed(KeyEvent e) {
078: }
079:
080: /* (non-Javadoc)
081: * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
082: */
083: public void keyReleased(KeyEvent e) {
084: String sValue = getText();
085:
086: List vals = m_propInst.getValues();
087: StringValue val = null;
088: if (vals.size() > 0) {
089: val = (StringValue) vals.get(0);
090: } else {
091: val = (StringValue) m_propInst.getNewValueInstance();
092: }
093: if (sValue != null && sValue.length() > 0) {
094: val.setValue(sValue);
095: m_propInst.setValue(val);
096: } else {
097: m_propInst.removeValue(val);
098: }
099:
100: firePropertyChange(
101: RelatedEventValueDisplay.PROPERTY_METADATA_CHANGE,
102: null, null);
103: }
104:
105: /* (non-Javadoc)
106: * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
107: */
108: public void keyTyped(KeyEvent e) {
109: }
110:
111: /**
112: * @return
113: */
114: public boolean isMetadataValid() {
115: boolean bIsValid = false;
116: Property prop = m_propInst.getDefinition();
117:
118: StringRange range = (StringRange) prop.getRange();
119:
120: List vals = m_propInst.getValues();
121:
122: if (vals.size() > 0) {
123: bIsValid = range.validate(vals).isValid();
124: } else {
125: bIsValid = false;
126: this .setText(INVALID_TEXT);
127: }
128:
129: if (bIsValid == false) {
130: setForeground(Color.RED);
131: } else {
132: setForeground(Color.BLACK);
133: }
134:
135: revalidate();
136:
137: return bIsValid;
138: }
139:
140: }
|