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.integerhandling;
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: * <p>Title: IntegerValuePanel</p>
037: * <p>Description: </p>
038: * <p>Copyright: SimulacraMedia 2003</p>
039: * <p>Company: Simulacra</p>
040: * @author Matthew Large
041: *
042: */
043: public class IntegerValuePanel extends AbstractRangeDisplay implements
044: RangeDisplay, LayoutManager, KeyListener {
045:
046: protected int m_nHeight = 10;
047:
048: private int m_nMinOccurs = 0;
049: private int m_nMaxOccurs = 1000;
050:
051: protected JTextComponent m_textComp = null;
052:
053: private String m_sPreviousText = "";
054:
055: protected WarningsLabel m_warnings = null;
056:
057: protected JLabel m_errorLabel = null;
058:
059: private String m_sValue = "";
060:
061: private IntegerRangeDisplay m_display = null;
062:
063: /**
064: * @param propInstance
065: */
066: public IntegerValuePanel(IntegerRangeDisplay display,
067: PropertyInstance propInstance, String sValue) {
068: super (propInstance);
069: this .m_display = display;
070: this .m_sValue = sValue;
071: this .setup();
072: }
073:
074: /* (non-Javadoc)
075: * @see com.simulacramedia.contentmanager.metadata.range.swing.RangeDisplay#getPanel()
076: */
077: public void setup() {
078: this .setLayout(this );
079: IntegerRange range = (IntegerRange) 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: this .m_errorLabel = new JLabel();
112: this .m_errorLabel.setIcon(IconManager.getInstance().getIcon(
113: "16-message-confirm.gif"));
114: this .add(this .m_errorLabel);
115:
116: String sWarning = "";
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:
171: Dimension dim = m_textComp.getPreferredSize();
172: m_textComp.setPreferredSize(new Dimension(nWidth - 85,
173: dim.height));
174: m_textComp.setSize(m_textComp.getPreferredSize());
175: m_textComp.setLocation(20, 0);
176:
177: this .m_errorLabel.setLocation(0, 0);
178: this .m_errorLabel.setSize(15, 15);
179: this .repaint();
180: }
181:
182: /* (non-Javadoc)
183: * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
184: */
185: public void addLayoutComponent(String arg0, Component arg1) {
186: // NO-OP
187: }
188:
189: /* (non-Javadoc)
190: * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
191: */
192: public Dimension minimumLayoutSize(Container arg0) {
193: return this .getPreferredSize();
194: }
195:
196: /* (non-Javadoc)
197: * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
198: */
199: public Dimension preferredLayoutSize(Container arg0) {
200: return this .getPreferredSize();
201: }
202:
203: /* (non-Javadoc)
204: * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
205: */
206: public void keyPressed(KeyEvent arg0) {
207: // NO-OP
208: }
209:
210: /* (non-Javadoc)
211: * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
212: */
213: public void keyReleased(KeyEvent ke) {
214: if (ke != null) {
215: char key = ke.getKeyChar();
216:
217: this .m_errorLabel.setIcon(IconManager.getInstance()
218: .getIcon("16-message-confirm.gif"));
219: m_textComp.setBorder(BorderFactory.createEtchedBorder());
220:
221: if (!(key == '0' || key == '1' || key == '2' || key == '3'
222: || key == '4' || key == '5' || key == '6'
223: || key == '7' || key == '8' || key == '9'
224: || key == '-'
225: || ke.getKeyCode() == KeyEvent.VK_RIGHT
226: || ke.getKeyCode() == KeyEvent.VK_LEFT
227: || ke.getKeyCode() == KeyEvent.VK_DELETE || ke
228: .getKeyCode() == KeyEvent.VK_BACK_SPACE)
229: || ke.getKeyCode() == KeyEvent.VK_SPACE) {
230: this .m_textComp.setText(this .m_sPreviousText);
231: } else if (key == '-') {
232: String sTemp = this .m_textComp.getText();
233: if (sTemp.lastIndexOf("-") > 0) {
234: this .m_textComp.setText(this .m_sPreviousText);
235: } else {
236: this .m_sPreviousText = this .m_textComp.getText();
237: }
238: } else {
239: this .m_sPreviousText = this .m_textComp.getText();
240: }
241: }
242:
243: if (!this .m_textComp.getText().equals("")) {
244: int fVal = Integer.parseInt(this .m_textComp.getText());
245: IntegerRange range = (IntegerRange) this
246: .getPropertyInstance().getDefinition().getRange();
247:
248: if (range.getMinimum() != null
249: && range.getMaximum() != null) {
250: if (fVal < range.getMinimum().intValue()) {
251: this .m_warnings.setHighlight(range.getMinimum()
252: .toString(), true);
253: this .m_errorLabel.setIcon(IconManager.getInstance()
254: .getIcon("16-message-error.gif"));
255: m_textComp.setBorder(BorderFactory
256: .createLineBorder(Color.RED));
257: } else {
258: this .m_warnings.setHighlight(range.getMinimum()
259: .toString(), false);
260: }
261: if (fVal > range.getMaximum().intValue()) {
262: this .m_warnings.setHighlight(range.getMaximum()
263: .toString(), true);
264: this .m_errorLabel.setIcon(IconManager.getInstance()
265: .getIcon("16-message-error.gif"));
266: m_textComp.setBorder(BorderFactory
267: .createLineBorder(Color.RED));
268: } else {
269: this .m_warnings.setHighlight(range.getMaximum()
270: .toString(), false);
271: }
272: } else if (range.getMinimum() != null) {
273: if (fVal < range.getMinimum().intValue()) {
274: this .m_warnings.setHighlight(range.getMinimum()
275: .toString(), true);
276: this .m_errorLabel.setIcon(IconManager.getInstance()
277: .getIcon("16-message-error.gif"));
278: m_textComp.setBorder(BorderFactory
279: .createLineBorder(Color.RED));
280: } else {
281: this .m_warnings.setHighlight(range.getMinimum()
282: .toString(), false);
283: }
284: } else if (range.getMaximum() != null) {
285: if (fVal > range.getMaximum().intValue()) {
286: this .m_warnings.setHighlight(range.getMaximum()
287: .toString(), true);
288: this .m_errorLabel.setIcon(IconManager.getInstance()
289: .getIcon("16-message-error.gif"));
290: m_textComp.setBorder(BorderFactory
291: .createLineBorder(Color.RED));
292: } else {
293: this .m_warnings.setHighlight(range.getMaximum()
294: .toString(), false);
295: }
296: }
297: } else if (this .m_nMinOccurs > 0) {
298: this .m_errorLabel.setIcon(IconManager.getInstance()
299: .getIcon("16-message-error.gif"));
300: m_textComp.setBorder(BorderFactory
301: .createLineBorder(Color.RED));
302: }
303:
304: if (!this .m_errorLabel.getIcon().toString().equals(
305: IconManager.getInstance().getIcon(
306: "16-message-error.gif").toString())
307: && this .getParent() != null) {
308: this .m_display.valueChanged(this .m_textComp.getText()
309: .trim());
310: }
311: super .validateTab();
312:
313: }
314:
315: /* (non-Javadoc)
316: * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
317: */
318: public void keyTyped(KeyEvent arg0) {
319: // NO-OP
320: }
321:
322: /* (non-Javadoc)
323: * @see com.simulacramedia.contentmanager.metadata.range.swing.RangeDisplay#getPanel()
324: */
325: public JPanel getPanel() {
326: return null;
327: }
328:
329: protected String getValue() {
330: return this .m_textComp.getText().trim();
331: }
332:
333: /* (non-Javadoc)
334: * @see com.simulacramedia.contentmanager.metadata.range.swing.AbstractRangeDisplay#isValid()
335: */
336: public boolean isMetadataValid() {
337: return !this .m_errorLabel.getIcon().toString().equals(
338: IconManager.getInstance().getIcon(
339: "16-message-error.gif").toString());
340: }
341:
342: }
|