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.stringhandling;
020:
021: import java.awt.Color;
022: import java.awt.Component;
023: import java.awt.Container;
024: import java.awt.Dimension;
025: import java.awt.Font;
026: import java.awt.LayoutManager;
027: import java.awt.event.ActionEvent;
028: import java.awt.event.KeyEvent;
029: import java.awt.event.KeyListener;
030: import java.util.Iterator;
031: import java.util.List;
032:
033: import javax.swing.AbstractAction;
034: import javax.swing.Action;
035: import javax.swing.BorderFactory;
036: import javax.swing.JComponent;
037: import javax.swing.JLabel;
038: import javax.swing.JPanel;
039: import javax.swing.JScrollPane;
040: import javax.swing.JTextArea;
041: import javax.swing.JTextField;
042: import javax.swing.KeyStroke;
043: import javax.swing.border.EtchedBorder;
044: import javax.swing.text.JTextComponent;
045:
046: import org.openharmonise.him.metadata.range.swing.*;
047: import org.openharmonise.vfs.*;
048: import org.openharmonise.vfs.gui.*;
049: import org.openharmonise.vfs.metadata.*;
050: import org.openharmonise.vfs.metadata.range.*;
051:
052: /**
053: * @author Matthew Large
054: *
055: */
056: public class StringValuePanel extends AbstractRangeDisplay implements
057: RangeDisplay, KeyListener, LayoutManager {
058:
059: private int m_nHeight = 30;
060:
061: private int m_nMinOccurs = 0;
062: private int m_nMaxOccurs = 1000;
063:
064: private JTextComponent m_textComp = null;
065: private Counter m_counter = null;
066:
067: private WarningsLabel m_warnings = null;
068:
069: private JLabel m_errorLabel = null;
070:
071: private String m_sValue = "";
072:
073: private StringRangeDisplay m_display = null;
074:
075: private JScrollPane m_textScroller = null;
076:
077: private int m_nValueHeight = 20;
078:
079: /**
080: * @param propInstance
081: */
082: public StringValuePanel(StringRangeDisplay display,
083: PropertyInstance propInstance, String sValue) {
084: super (propInstance);
085: this .m_display = display;
086: this .m_sValue = sValue;
087: this .setup();
088: }
089:
090: /* (non-Javadoc)
091: * @see com.simulacramedia.contentmanager.metadata.range.swing.RangeDisplay#getPanel()
092: */
093: private void setup() {
094: this .setLayout(this );
095:
096: StringRange range = (StringRange) this .getPropertyInstance()
097: .getDefinition().getRange();
098:
099: List domains = this .getPropertyInstance().getDefinition()
100: .getDomains();
101: Iterator itor = domains.iterator();
102:
103: while (itor.hasNext()) {
104: Domain domain = (Domain) itor.next();
105: boolean bApplicableDomain = false;
106: String sFilePath = ((VersionedVirtualFile) this
107: .getPropertyInstance().getVirtualFile())
108: .getLogicalPath();
109: Iterator itor2 = domain.getPaths().iterator();
110: while (itor2.hasNext()) {
111: String sDomainPath = (String) itor2.next();
112: if (sFilePath.startsWith(sDomainPath)) {
113: bApplicableDomain = true;
114: }
115: }
116:
117: if (bApplicableDomain) {
118: if (this .m_nMinOccurs < domain.getMinOccurs()) {
119: this .m_nMinOccurs = domain.getMinOccurs();
120: }
121: if (this .m_nMaxOccurs > domain.getMaxOccurs()) {
122: this .m_nMaxOccurs = domain.getMaxOccurs();
123: }
124: }
125:
126: }
127:
128: this .m_errorLabel = new JLabel();
129: this .m_errorLabel.setIcon(IconManager.getInstance().getIcon(
130: "16-message-confirm.gif"));
131: this .add(this .m_errorLabel);
132:
133: String sWarning = "";
134: if (range.getMinLength() != -1 && range.getMaxLength() != -1) {
135: sWarning = "Please type text between {"
136: + range.getMinLength() + "} and {"
137: + range.getMaxLength() + "} characters.";
138: } else if (range.getMinLength() != -1) {
139: sWarning = "Please type text with more than {"
140: + range.getMinLength() + "} characters.";
141: } else if (range.getMaxLength() != -1) {
142: sWarning = "Please type text with less than {"
143: + range.getMaxLength() + "} characters.";
144: } else {
145: sWarning = "Please type some text.";
146: }
147:
148: m_warnings = new WarningsLabel(sWarning);
149: this .add(m_warnings);
150:
151: if (range.getMaxLength() <= StringRange.SIZE_BORDER) {
152: m_textComp = new JTextField(this .m_sValue);
153: m_textComp.setBorder(BorderFactory.createEtchedBorder());
154: m_textComp.setPreferredSize(new Dimension(200, 20));
155: this .m_textComp.addKeyListener(this );
156: this .add(m_textComp);
157: m_counter = new Counter(m_textComp, (StringRange) this
158: .getPropertyInstance().getDefinition().getRange());
159: } else {
160: JTextArea textComp = new JTextArea(this .m_sValue);
161: this .m_textComp = textComp;
162: textComp.setWrapStyleWord(true);
163: textComp.setLineWrap(true);
164:
165: m_textScroller = new JScrollPane(textComp,
166: JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
167: JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
168: m_textScroller.setPreferredSize(new Dimension(400, 100));
169: m_textScroller.setFocusable(false);
170: m_nValueHeight = 100;
171: textComp.addKeyListener(this );
172: this .add(m_textScroller);
173: m_counter = new Counter(textComp, (StringRange) this
174: .getPropertyInstance().getDefinition().getRange());
175: textComp.getInputMap(JComponent.WHEN_FOCUSED).put(
176: KeyStroke.getKeyStroke("TAB"),
177: nextFocusAction.getValue(Action.NAME));
178: textComp.getInputMap(JComponent.WHEN_FOCUSED).put(
179: KeyStroke.getKeyStroke("shift TAB"),
180: prevFocusAction.getValue(Action.NAME));
181: textComp.getActionMap().put(
182: nextFocusAction.getValue(Action.NAME),
183: nextFocusAction);
184: textComp.getActionMap().put(
185: prevFocusAction.getValue(Action.NAME),
186: prevFocusAction);
187: }
188:
189: this .m_textComp.setFont(new Font("Arial Unicode MS",
190: Font.PLAIN, 11));
191:
192: this .add(m_counter);
193:
194: this .keyReleased(null);
195:
196: if (this .m_display.isReadOnly()) {
197: m_textComp.setFocusable(false);
198: }
199: }
200:
201: // The actions
202: public Action nextFocusAction = new AbstractAction(
203: "Move Focus Forwards") {
204: public void actionPerformed(ActionEvent evt) {
205: ((Component) evt.getSource()).transferFocus();
206: }
207: };
208: public Action prevFocusAction = new AbstractAction(
209: "Move Focus Backwards") {
210: public void actionPerformed(ActionEvent evt) {
211: ((Component) evt.getSource()).transferFocusBackward();
212: }
213: };
214:
215: /* (non-Javadoc)
216: * @see java.awt.Component#getPreferredSize()
217: */
218: public Dimension getPreferredSize() {
219: this .layoutContainer(null);
220: int nWidth = this .getParent().getWidth();
221: int nHeight = 10;
222: nHeight = nHeight + m_nValueHeight;
223: return new Dimension(nWidth, nHeight);
224: }
225:
226: /* (non-Javadoc)
227: * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
228: */
229: public void keyPressed(KeyEvent arg0) {
230: // NO-OP-generated method stub
231: }
232:
233: /* (non-Javadoc)
234: * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
235: */
236: public void keyReleased(KeyEvent arg0) {
237:
238: this .m_errorLabel.setIcon(IconManager.getInstance().getIcon(
239: "16-message-confirm.gif"));
240: if (this .m_textScroller != null) {
241: m_textScroller.setBorder(BorderFactory
242: .createEtchedBorder(EtchedBorder.LOWERED));
243: } else {
244: m_textComp.setBorder(BorderFactory
245: .createEtchedBorder(EtchedBorder.LOWERED));
246: }
247:
248: StringRange range = (StringRange) this .getPropertyInstance()
249: .getDefinition().getRange();
250: int nLength = this .m_textComp.getText().length();
251: if (range.getMinLength() != -1 && range.getMaxLength() != -1) {
252: if (nLength < range.getMinLength()) {
253: this .m_warnings.setHighlight(Integer.toString(range
254: .getMinLength()), true);
255: this .m_errorLabel.setIcon(IconManager.getInstance()
256: .getIcon("16-message-error.gif"));
257: if (this .m_textScroller != null) {
258: m_textScroller.setBorder(BorderFactory
259: .createLineBorder(Color.RED));
260: } else {
261: m_textComp.setBorder(BorderFactory
262: .createLineBorder(Color.RED));
263: }
264: } else {
265: this .m_warnings.setHighlight(Integer.toString(range
266: .getMinLength()), false);
267: }
268: if (nLength > range.getMaxLength()) {
269: this .m_warnings.setHighlight(Integer.toString(range
270: .getMaxLength()), true);
271: this .m_errorLabel.setIcon(IconManager.getInstance()
272: .getIcon("16-message-error.gif"));
273: if (this .m_textScroller != null) {
274: m_textScroller.setBorder(BorderFactory
275: .createLineBorder(Color.RED));
276: } else {
277: m_textComp.setBorder(BorderFactory
278: .createLineBorder(Color.RED));
279: }
280: } else {
281: this .m_warnings.setHighlight(Integer.toString(range
282: .getMaxLength()), false);
283: }
284: } else if (range.getMinLength() != -1) {
285: if (nLength < range.getMinLength()) {
286: this .m_warnings.setHighlight(Integer.toString(range
287: .getMinLength()), true);
288: this .m_errorLabel.setIcon(IconManager.getInstance()
289: .getIcon("16-message-error.gif"));
290: if (this .m_textScroller != null) {
291: m_textScroller.setBorder(BorderFactory
292: .createLineBorder(Color.RED));
293: } else {
294: m_textComp.setBorder(BorderFactory
295: .createLineBorder(Color.RED));
296: }
297: } else {
298: this .m_warnings.setHighlight(Integer.toString(range
299: .getMinLength()), false);
300: }
301: } else if (range.getMaxLength() != -1) {
302: if (nLength > range.getMaxLength()) {
303: this .m_warnings.setHighlight(Integer.toString(range
304: .getMaxLength()), true);
305: this .m_errorLabel.setIcon(IconManager.getInstance()
306: .getIcon("16-message-error.gif"));
307: if (this .m_textScroller != null) {
308: m_textScroller.setBorder(BorderFactory
309: .createLineBorder(Color.RED));
310: } else {
311: m_textComp.setBorder(BorderFactory
312: .createLineBorder(Color.RED));
313: }
314: } else {
315: this .m_warnings.setHighlight(Integer.toString(range
316: .getMaxLength()), false);
317: }
318: }
319: if (this .m_nMinOccurs > 0
320: && this .m_textComp.getText().trim().length() < 1) {
321: this .m_errorLabel.setIcon(IconManager.getInstance()
322: .getIcon("16-message-error.gif"));
323: if (this .m_textScroller != null) {
324: m_textScroller.setBorder(BorderFactory
325: .createLineBorder(Color.RED));
326: } else {
327: m_textComp.setBorder(BorderFactory
328: .createLineBorder(Color.RED));
329: }
330: }
331:
332: if (!this .m_errorLabel.getIcon().toString().equals(
333: IconManager.getInstance().getIcon(
334: "16-message-error.gif").toString())
335: && this .getParent() != null) {
336: this .m_display.valueChanged(this .m_textComp.getText()
337: .trim());
338: }
339: super .validateTab();
340: }
341:
342: /* (non-Javadoc)
343: * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
344: */
345: public void keyTyped(KeyEvent arg0) {
346: // NO-OP
347: }
348:
349: /* (non-Javadoc)
350: * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
351: */
352: public void removeLayoutComponent(Component arg0) {
353: }
354:
355: /* (non-Javadoc)
356: * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
357: */
358: public void layoutContainer(Container arg0) {
359: m_nHeight = 30;
360: int nWidth = this .getParent().getWidth() - 18;
361: Dimension dim = null;
362: if (this .m_textScroller != null) {
363: dim = this .m_textScroller.getPreferredSize();
364: this .m_textScroller.setPreferredSize(new Dimension(
365: nWidth - 105, dim.height));
366: } else {
367: dim = this .m_textComp.getPreferredSize();
368: this .m_textComp.setPreferredSize(new Dimension(
369: nWidth - 105, dim.height));
370: }
371:
372: this .m_counter.setSize(this .m_counter.getPreferredSize());
373:
374: if (this .m_textScroller == null) {
375: this .m_textComp.setSize(this .m_textComp.getPreferredSize());
376: this .m_textComp.setLocation(20, 0);
377: this .m_nHeight = this .m_nHeight + m_nValueHeight;
378: this .m_counter.setLocation(this .m_textComp.getLocation().x
379: + this .m_textComp.getSize().width + 5,
380: this .m_textComp.getLocation().y);
381: } else {
382: this .m_textScroller.setSize(this .m_textScroller
383: .getPreferredSize());
384: this .m_textScroller.setLocation(20, 0);
385: this .m_nHeight = this .m_nHeight + m_nValueHeight;
386: this .m_counter.setLocation(this .m_textScroller
387: .getLocation().x
388: + this .m_textScroller.getSize().width + 5,
389: this .m_textScroller.getLocation().y);
390: }
391:
392: this .m_errorLabel.setLocation(0, 0);
393: this .m_errorLabel.setSize(15, 15);
394: this .repaint();
395: }
396:
397: /* (non-Javadoc)
398: * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
399: */
400: public void addLayoutComponent(String arg0, Component arg1) {
401: }
402:
403: /* (non-Javadoc)
404: * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
405: */
406: public Dimension minimumLayoutSize(Container arg0) {
407: return this .getPreferredSize();
408: }
409:
410: /* (non-Javadoc)
411: * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
412: */
413: public Dimension preferredLayoutSize(Container arg0) {
414: return this .getPreferredSize();
415: }
416:
417: /* (non-Javadoc)
418: * @see com.simulacramedia.contentmanager.metadata.range.swing.RangeDisplay#getPanel()
419: */
420: public JPanel getPanel() {
421: return null;
422: }
423:
424: protected String getValue() {
425: return this .m_textComp.getText().trim();
426: }
427:
428: /* (non-Javadoc)
429: * @see com.simulacramedia.contentmanager.metadata.range.swing.AbstractRangeDisplay#isValid()
430: */
431: public boolean isMetadataValid() {
432: return !this .m_errorLabel.getIcon().toString().equals(
433: IconManager.getInstance().getIcon(
434: "16-message-error.gif").toString());
435: }
436:
437: /* (non-Javadoc)
438: * @see java.awt.Component#setEnabled(boolean)
439: */
440: public void setEnabled(boolean bEnabled) {
441: super .setEnabled(bEnabled);
442: if (this .m_textComp instanceof JTextArea) {
443: ((JTextArea) this.m_textComp).setEditable(bEnabled);
444: }
445: }
446:
447: }
|