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.floathandling;
020:
021: import java.awt.*;
022: import java.awt.event.*;
023: import java.util.*;
024: import java.util.List;
025:
026: import javax.swing.*;
027: import javax.swing.text.*;
028:
029: import org.openharmonise.him.metadata.range.swing.*;
030: import org.openharmonise.vfs.*;
031: import org.openharmonise.vfs.gui.*;
032: import org.openharmonise.vfs.metadata.*;
033: import org.openharmonise.vfs.metadata.range.*;
034:
035: /**
036: * @author Matthew Large
037: *
038: */
039: public class FloatValuePanel extends AbstractRangeDisplay implements
040: RangeDisplay, LayoutManager, KeyListener {
041:
042: protected int m_nHeight = 10;
043:
044: private int m_nMinOccurs = 0;
045: private int m_nMaxOccurs = 1000;
046:
047: protected JTextComponent m_textComp = null;
048:
049: private String m_sPreviousText = "";
050:
051: protected WarningsLabel m_warnings = null;
052:
053: protected JLabel m_errorLabel = null;
054:
055: private String m_sValue = "";
056:
057: private FloatRangeDisplay m_display = null;
058:
059: /**
060: * @param propInstance
061: */
062: public FloatValuePanel(FloatRangeDisplay display,
063: PropertyInstance propInstance, String sValue) {
064: super (propInstance);
065: this .m_display = display;
066: this .m_sValue = sValue;
067: this .setup();
068: }
069:
070: /* (non-Javadoc)
071: * @see com.simulacramedia.contentmanager.metadata.range.swing.RangeDisplay#getPanel()
072: */
073: public JPanel getPanel() {
074: return null;
075: }
076:
077: public void setup() {
078: this .setLayout(this );
079: FloatRange range = (FloatRange) this .getPropertyInstance()
080: .getDefinition().getRange();
081:
082: List domains = this .getPropertyInstance().getDefinition()
083: .getDomains();
084: Iterator itor = domains.iterator();
085:
086: while (itor.hasNext()) {
087: Domain domain = (Domain) itor.next();
088: boolean bApplicableDomain = false;
089: String sFilePath = ((VersionedVirtualFile) this
090: .getPropertyInstance().getVirtualFile())
091: .getLogicalPath();
092: Iterator itor2 = domain.getPaths().iterator();
093: while (itor2.hasNext()) {
094: String sDomainPath = (String) itor2.next();
095: if (sFilePath.startsWith(sDomainPath)) {
096: bApplicableDomain = true;
097: }
098: }
099:
100: if (bApplicableDomain) {
101: if (this .m_nMinOccurs < domain.getMinOccurs()) {
102: this .m_nMinOccurs = domain.getMinOccurs();
103: }
104: if (this .m_nMaxOccurs > domain.getMaxOccurs()) {
105: this .m_nMaxOccurs = domain.getMaxOccurs();
106: }
107: }
108:
109: }
110:
111: String sWarning = "";
112:
113: this .m_errorLabel = new JLabel();
114: this .m_errorLabel.setIcon(IconManager.getInstance().getIcon(
115: "16-message-confirm.gif"));
116: this .add(this .m_errorLabel);
117:
118: if (range.getMinimum() != null && range.getMaximum() != null) {
119: sWarning = "Please type a number between {"
120: + range.getMinimum() + "} and {"
121: + range.getMaximum() + "}.";
122: } else if (range.getMaximum() != null) {
123: sWarning = "Please type a number less than {"
124: + range.getMaximum() + "}.";
125: } else if (range.getMinimum() != null) {
126: sWarning = "Please type a number greater than {"
127: + range.getMinimum() + "}.";
128: } else {
129: sWarning = "Please type a number.";
130: }
131:
132: m_warnings = new WarningsLabel(sWarning);
133: this .add(m_warnings);
134:
135: m_textComp = new JTextField(this .m_sValue);
136: this .m_sPreviousText = this .m_sValue;
137: m_textComp.addKeyListener(this );
138: m_textComp.setBorder(BorderFactory.createEtchedBorder());
139: int nHeight = 30;
140: m_nHeight = nHeight + 20;
141: m_textComp.setPreferredSize(new Dimension(200, 20));
142: this .add(m_textComp);
143:
144: this .keyReleased(null);
145: }
146:
147: /* (non-Javadoc)
148: * @see java.awt.Component#getPreferredSize()
149: */
150: public Dimension getPreferredSize() {
151: this .layoutContainer(null);
152: int nWidth = this .getParent().getWidth();
153: int nHeight = 10;
154: nHeight = nHeight + this .m_textComp.getSize().height;
155: return new Dimension(nWidth, nHeight);
156: }
157:
158: /* (non-Javadoc)
159: * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
160: */
161: public void removeLayoutComponent(Component arg0) {
162: // NO-OP
163: }
164:
165: /* (non-Javadoc)
166: * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
167: */
168: public void layoutContainer(Container arg0) {
169: int nWidth = this .getParent().getWidth() - 18;
170: Dimension dim = m_textComp.getPreferredSize();
171: m_textComp.setPreferredSize(new Dimension(nWidth - 85,
172: dim.height));
173: m_textComp.setSize(m_textComp.getPreferredSize());
174: m_textComp.setLocation(20, 0);
175:
176: this .m_errorLabel.setLocation(0, 0);
177: this .m_errorLabel.setSize(15, 15);
178: this .repaint();
179: }
180:
181: /* (non-Javadoc)
182: * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
183: */
184: public void addLayoutComponent(String arg0, Component arg1) {
185: // NO-OP
186: }
187:
188: /* (non-Javadoc)
189: * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
190: */
191: public Dimension minimumLayoutSize(Container arg0) {
192: return this .getPreferredSize();
193: }
194:
195: /* (non-Javadoc)
196: * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
197: */
198: public Dimension preferredLayoutSize(Container arg0) {
199: return this .getPreferredSize();
200: }
201:
202: /* (non-Javadoc)
203: * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
204: */
205: public void keyPressed(KeyEvent arg0) {
206: // NO-OP
207: }
208:
209: /* (non-Javadoc)
210: * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
211: */
212: public void keyReleased(KeyEvent ke) {
213: if (ke != null) {
214: char key = ke.getKeyChar();
215:
216: this .m_errorLabel.setIcon(IconManager.getInstance()
217: .getIcon("16-message-confirm.gif"));
218: m_textComp.setBorder(BorderFactory.createEtchedBorder());
219:
220: if (!(key == '0' || key == '1' || key == '2' || key == '3'
221: || key == '4' || key == '5' || key == '6'
222: || key == '7' || key == '8' || key == '9'
223: || key == '.' || key == '-'
224: || ke.getKeyCode() == KeyEvent.VK_RIGHT
225: || ke.getKeyCode() == KeyEvent.VK_LEFT
226: || ke.getKeyCode() == KeyEvent.VK_DELETE || ke
227: .getKeyCode() == KeyEvent.VK_BACK_SPACE)
228: || ke.getKeyCode() == KeyEvent.VK_SPACE) {
229: this .m_textComp.setText(this .m_sPreviousText);
230: } else if (key == '-') {
231: String sTemp = this .m_textComp.getText();
232: if (sTemp.lastIndexOf("-") > 0) {
233: this .m_textComp.setText(this .m_sPreviousText);
234: } else {
235: this .m_sPreviousText = this .m_textComp.getText();
236: }
237: } else if (key == '.') {
238: char[] cTemp = this .m_textComp.getText().toCharArray();
239: int nCount = 0;
240: for (int i = 0; i < cTemp.length; i++) {
241: if (cTemp[i] == '.') {
242: nCount++;
243: }
244: if (nCount > 1) {
245: this .m_textComp.setText(this .m_sPreviousText);
246: break;
247: }
248: }
249: if (nCount < 2) {
250: this .m_sPreviousText = this .m_textComp.getText();
251: }
252: } else {
253: this .m_sPreviousText = this .m_textComp.getText();
254: }
255: }
256:
257: FloatRange range = (FloatRange) this .getPropertyInstance()
258: .getDefinition().getRange();
259:
260: float fVal = 0;
261: try {
262: fVal = Float.parseFloat(this .m_textComp.getText());
263: } catch (NumberFormatException e) {
264: if (range.getMinimum() != null) {
265: this .m_warnings.setHighlight(range.getMinimum()
266: .toString(), true);
267: }
268: if (range.getMaximum() != null) {
269: this .m_warnings.setHighlight(range.getMaximum()
270: .toString(), true);
271: }
272: this .m_errorLabel.setIcon(IconManager.getInstance()
273: .getIcon("16-message-error.gif"));
274: m_textComp.setBorder(BorderFactory
275: .createLineBorder(Color.RED));
276: }
277:
278: if (range.getMinimum() != null && range.getMaximum() != null) {
279: if (fVal < range.getMinimum().floatValue()) {
280: this .m_warnings.setHighlight(range.getMinimum()
281: .toString(), true);
282: this .m_errorLabel.setIcon(IconManager.getInstance()
283: .getIcon("16-message-error.gif"));
284: m_textComp.setBorder(BorderFactory
285: .createLineBorder(Color.RED));
286: } else {
287: this .m_warnings.setHighlight(range.getMinimum()
288: .toString(), false);
289: }
290: if (fVal > range.getMaximum().floatValue()) {
291: this .m_warnings.setHighlight(range.getMaximum()
292: .toString(), true);
293: this .m_errorLabel.setIcon(IconManager.getInstance()
294: .getIcon("16-message-error.gif"));
295: m_textComp.setBorder(BorderFactory
296: .createLineBorder(Color.RED));
297: } else {
298: this .m_warnings.setHighlight(range.getMaximum()
299: .toString(), false);
300: }
301: } else if (range.getMinimum() != null) {
302: if (fVal < range.getMinimum().floatValue()) {
303: this .m_warnings.setHighlight(range.getMinimum()
304: .toString(), true);
305: this .m_errorLabel.setIcon(IconManager.getInstance()
306: .getIcon("16-message-error.gif"));
307: m_textComp.setBorder(BorderFactory
308: .createLineBorder(Color.RED));
309: } else {
310: this .m_warnings.setHighlight(range.getMinimum()
311: .toString(), false);
312: }
313: } else if (range.getMaximum() != null) {
314: if (fVal > range.getMaximum().floatValue()) {
315: this .m_warnings.setHighlight(range.getMaximum()
316: .toString(), true);
317: this .m_errorLabel.setIcon(IconManager.getInstance()
318: .getIcon("16-message-error.gif"));
319: m_textComp.setBorder(BorderFactory
320: .createLineBorder(Color.RED));
321: } else {
322: this .m_warnings.setHighlight(range.getMaximum()
323: .toString(), false);
324: }
325: }
326:
327: if (!this .m_errorLabel.getIcon().toString().equals(
328: IconManager.getInstance().getIcon(
329: "16-message-error.gif").toString())
330: && this .getParent() != null) {
331: this .m_display.valueChanged(this .m_textComp.getText()
332: .trim());
333: }
334: super .validateTab();
335: }
336:
337: /* (non-Javadoc)
338: * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
339: */
340: public void keyTyped(KeyEvent arg0) {
341: // NO-OP
342: }
343:
344: protected String getValue() {
345: return this .m_textComp.getText().trim();
346: }
347:
348: /* (non-Javadoc)
349: * @see com.simulacramedia.contentmanager.metadata.range.swing.AbstractRangeDisplay#isValid()
350: */
351: public boolean isMetadataValid() {
352: return !this .m_errorLabel.getIcon().toString().equals(
353: IconManager.getInstance().getIcon(
354: "16-message-error.gif").toString());
355: }
356:
357: }
|