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.editors.report;
020:
021: import java.awt.*;
022: import java.awt.event.*;
023: import java.text.*;
024: import java.util.*;
025:
026: import javax.swing.*;
027:
028: import org.openharmonise.him.*;
029: import org.openharmonise.him.editors.report.rqom.*;
030: import org.openharmonise.him.editors.report.swing.*;
031: import org.openharmonise.him.harmonise.*;
032: import org.openharmonise.swing.*;
033: import org.openharmonise.swing.datefield.*;
034: import org.openharmonise.vfs.*;
035: import org.openharmonise.vfs.metadata.*;
036: import org.openharmonise.vfs.metadata.range.*;
037: import org.openharmonise.vfs.servers.ServerList;
038:
039: /**
040: * Component to display a metadata item in the report editor.
041: *
042: * @author Matthew Large
043: * @version $Revision: 1.2 $
044: *
045: */
046: public class MetadataPanel extends JPanel implements LayoutManager,
047: ActionListener, KeyListener, DateFieldListener {
048:
049: /**
050: * Report query.
051: */
052: private ReportQuery m_reportQuery = null;
053:
054: /**
055: * Metadata data.
056: */
057: private Metadata m_metadata = null;
058:
059: /**
060: * Property tree.
061: */
062: private JComboTree m_propertyCombo = null;
063:
064: /**
065: * Component to display the value.
066: */
067: private JComponent m_valueComponent = null;
068:
069: private JLabel m_propLabel = null;
070:
071: private JLabel m_valLabel = null;
072:
073: private JComboBox m_operatorBox = null;
074:
075: /**
076: * default operator list
077: */
078: private ArrayList m_operators;
079:
080: /**
081: * Date time format.
082: */
083: private static final String DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss G";
084:
085: /**
086: * Date format.
087: */
088: private static final String DATE_FORMAT = "yyyy-MM-dd G";
089:
090: /**
091: * Constructs a new metadata panel.
092: *
093: * @param query Report query
094: * @param metadata Metadata data
095: */
096: public MetadataPanel(ReportQuery query, Metadata metadata) {
097: super ();
098: this .m_metadata = metadata;
099: this .m_reportQuery = query;
100: this .setup();
101: }
102:
103: /**
104: * Returns the metadata data.
105: *
106: * @return Data
107: */
108: public Metadata getMetadata() {
109: return this .m_metadata;
110: }
111:
112: /**
113: * Configures this component.
114: *
115: */
116: private void setup() {
117: this .setLayout(this );
118:
119: AbstractVirtualFileSystem vfs = ServerList.getInstance()
120: .getHarmoniseServer().getVFS();
121:
122: this .m_propertyCombo = new JComboTree();
123: this .m_propertyCombo
124: .setResourceTreeFilter(new ReportResourceFilter(
125: m_reportQuery));
126: this .m_propertyCombo.setActionCommand("PROPCOMBO");
127: this .m_propertyCombo.addActionListener(this );
128: this .m_propertyCombo.setAllowClear(false);
129: this .m_propertyCombo.setShowLeafNodes(true);
130: this .m_propertyCombo.setSelectedLeafOnly(true);
131: this .m_propertyCombo
132: .addCollectionPath(HarmonisePaths.PATH_PROPERTIES);
133: this .add(m_propertyCombo);
134:
135: if (this .m_metadata.getPath() != null) {
136: this .m_propertyCombo
137: .setPath(vfs, this .m_metadata.getPath());
138: Property prop = PropertyCache.getInstance()
139: .getPropertyByPath(this .m_metadata.getPath());
140: this .m_valueComponent = this .getValueComponent(prop);
141: this .add(this .m_valueComponent);
142: if (this .m_metadata.getValue() != null
143: && !this .m_metadata.getValue().equals("")) {
144: this .setValueForComponent(this .m_metadata.getValue());
145: }
146: }
147:
148: String fontName = "Dialog";
149: int fontSize = 11;
150: Font font = new Font(fontName, Font.PLAIN, fontSize);
151:
152: this .m_propLabel = new JLabel("Select a property");
153: this .m_propLabel.setFont(font);
154: this .add(this .m_propLabel);
155:
156: this .m_valLabel = new JLabel("Select a value");
157: this .m_valLabel.setFont(font);
158: this .add(this .m_valLabel);
159: }
160:
161: /**
162: * Returns a value component that is appropriate for the given
163: * property.
164: *
165: * @param prop Property
166: * @return Value component
167: */
168: private JComponent getValueComponent(Property prop) {
169: JComponent comp = null;
170: Range range = prop.getRange();
171: m_operators = new ArrayList();
172: m_operators.add("=");
173: m_operators.add("NOT");
174: if (range instanceof ResourceRange) {
175: JComboTree tree = new JComboTree();
176: tree.setActionCommand("VALUECOMBO");
177: tree.addActionListener(this );
178: tree.setSelectedLeafOnly(true);
179: tree.setShowLeafNodes(true);
180: tree.setAllowClear(false);
181: Iterator itor = ((ResourceRange) range).getHREFs()
182: .iterator();
183: while (itor.hasNext()) {
184: String sPath = (String) itor.next();
185: tree.addCollectionPath(sPath);
186: }
187: comp = tree;
188: } else if (range instanceof CollectionRange) {
189: JComboTree tree = new JComboTree();
190: tree.setActionCommand("VALUECOMBO");
191: tree.addActionListener(this );
192: tree.setShowLeafNodes(false);
193: tree.setAllowClear(false);
194: Iterator itor = ((CollectionRange) range).getHREFs()
195: .iterator();
196: while (itor.hasNext()) {
197: String sPath = (String) itor.next();
198: tree.addCollectionPath(sPath);
199: }
200: comp = tree;
201: } else if (range instanceof ValueRange) {
202: JComboTree tree = new JComboTree();
203: tree.setActionCommand("VALUECOMBO");
204: tree.addActionListener(this );
205: tree.setSelectedLeafOnly(true);
206: tree.setShowLeafNodes(true);
207: tree.setAllowClear(false);
208: Iterator itor = ((ValueRange) range).getHREFs().iterator();
209: while (itor.hasNext()) {
210: String sPath = (String) itor.next();
211: tree.addCollectionPath(sPath);
212: }
213: comp = tree;
214: } else if (range instanceof DateRange) {
215: JDateField dateSelector = new JDateField(
216: DateRange.XSD_DATE_FORMAT);
217: if (((DateRange) range).getMaximum() != null) {
218: dateSelector.setMaximumDate(((DateRange) range)
219: .getMaximum());
220: }
221: if (((DateRange) range).getMinimum() != null) {
222: dateSelector.setMinimumDate(((DateRange) range)
223: .getMinimum());
224: }
225: dateSelector.addDateFieldListener(this );
226: comp = dateSelector;
227: m_operators.add("<");
228: m_operators.add(">");
229: } else if (range instanceof DateTimeRange) {
230: JDateField dateSelector = new JDateField(
231: DateTimeRange.XSD_DATE_FORMAT);
232: if (((DateTimeRange) range).getMaximum() != null) {
233: dateSelector.setMaximumDate(((DateTimeRange) range)
234: .getMaximum());
235: }
236: if (((DateTimeRange) range).getMinimum() != null) {
237: dateSelector.setMinimumDate(((DateTimeRange) range)
238: .getMinimum());
239: }
240: dateSelector.addDateFieldListener(this );
241: comp = dateSelector;
242: m_operators.add("<");
243: m_operators.add(">");
244: } else {
245: comp = new JTextField();
246: comp.addKeyListener(this );
247: }
248: if (range instanceof IntegerRange
249: || range instanceof FloatRange) {
250: m_operators.add("<");
251: m_operators.add(">");
252: }
253: this .m_operatorBox = new JComboBox(m_operators.toArray());
254: this .m_operatorBox.setBackground(Color.WHITE);
255: this .m_operatorBox.setActionCommand("OPERATOR");
256: this .m_operatorBox.addActionListener(this );
257: this .add(m_operatorBox);
258: if (this .m_metadata.getOperator() != null) {
259: m_operatorBox.setSelectedItem(m_metadata.getOperator());
260: }
261:
262: return comp;
263: }
264:
265: /**
266: * Returns a value from the current value comonent.
267: *
268: * @return Value
269: */
270: private String getValueFromComponent() {
271: String sValue = null;
272:
273: if (this .m_valueComponent instanceof JComboTree) {
274: sValue = ((JComboTree) this .m_valueComponent).getPath();
275: } else if (this .m_valueComponent instanceof JDateField) {
276: Date dtValue = ((JDateField) this .m_valueComponent)
277: .getDate();
278: if (((JDateField) this .m_valueComponent).getDateFormat()
279: .equalsIgnoreCase(MetadataPanel.DATETIME_FORMAT)) {
280: SimpleDateFormat format = new SimpleDateFormat(
281: MetadataPanel.DATETIME_FORMAT);
282: sValue = format.format(dtValue);
283: } else {
284: SimpleDateFormat format = new SimpleDateFormat(
285: MetadataPanel.DATE_FORMAT);
286: sValue = format.format(dtValue);
287: }
288: } else if (this .m_valueComponent instanceof JTextField) {
289: sValue = ((JTextField) this .m_valueComponent).getText();
290: }
291:
292: return sValue;
293: }
294:
295: /**
296: * Sets a value in the current value component.
297: *
298: * @param sValue Value
299: */
300: private void setValueForComponent(String sValue) {
301: if (this .m_valueComponent instanceof JComboTree) {
302: ((JComboTree) this .m_valueComponent).setPath(ServerList
303: .getInstance().getHarmoniseServer().getVFS(),
304: sValue);
305: } else if (this .m_valueComponent instanceof DateSelector) {
306: Date dtValue = new Date();
307: if (((DateSelector) this .m_valueComponent).isShowingTime()) {
308: SimpleDateFormat format = new SimpleDateFormat(
309: MetadataPanel.DATETIME_FORMAT);
310: try {
311: dtValue = format.parse(sValue);
312: } catch (ParseException e) {
313: e.printStackTrace();
314: }
315: } else {
316: SimpleDateFormat format = new SimpleDateFormat(
317: MetadataPanel.DATE_FORMAT);
318: try {
319: dtValue = format.parse(sValue);
320: } catch (ParseException e) {
321: e.printStackTrace();
322: }
323: }
324: ((DateSelector) this .m_valueComponent)
325: .setInitalDate(dtValue);
326: } else if (this .m_valueComponent instanceof JTextField) {
327: ((JTextField) this .m_valueComponent).setText(sValue);
328: }
329: }
330:
331: /* (non-Javadoc)
332: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
333: */
334: public void actionPerformed(ActionEvent ae) {
335: if (ae.getActionCommand().equals("PROPCOMBO")) {
336: this .m_metadata.setPath(this .m_propertyCombo.getPath());
337: Property prop = PropertyCache.getInstance()
338: .getPropertyByPath(this .m_metadata.getPath());
339: if (this .m_valueComponent != null) {
340: this .remove(this .m_valueComponent);
341: }
342: this .m_valueComponent = this .getValueComponent(prop);
343: this .add(this .m_valueComponent);
344: if (m_operatorBox != null) {
345: this .remove(m_operatorBox);
346: }
347: m_operatorBox = new JComboBox(m_operators.toArray());
348: m_operatorBox.setBackground(Color.WHITE);
349: m_operatorBox.setActionCommand("OPERATOR");
350: m_operatorBox.addActionListener(this );
351: this .add(m_operatorBox);
352: this .m_metadata.setOperator("=");
353:
354: this .m_metadata.setValue("");
355: this .layoutContainer(null);
356: this .validateTree();
357: this .repaint();
358: } else if (ae.getActionCommand().equals("VALUECOMBO")) {
359: this .m_metadata
360: .setValue(((JComboTree) this .m_valueComponent)
361: .getPath());
362: } else if (ae.getActionCommand().equals("OPERATOR")) {
363: this .m_metadata.setOperator((String) m_operatorBox
364: .getSelectedItem());
365: }
366: }
367:
368: /* (non-Javadoc)
369: * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
370: */
371: public void keyTyped(KeyEvent ke) {
372: }
373:
374: /* (non-Javadoc)
375: * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
376: */
377: public void layoutContainer(Container arg0) {
378: int x = 0;
379:
380: this .m_propLabel.setSize(this .m_propLabel.getPreferredSize());
381: this .m_propLabel.setLocation(x, 0);
382:
383: this .m_propertyCombo.setSize(this .m_propertyCombo
384: .getPreferredSize());
385: this .m_propertyCombo.setLocation(x, 15);
386:
387: x += this .m_propertyCombo.getSize().width + 10;
388:
389: if (this .m_valueComponent != null) {
390: int nValueWidth = 170;
391:
392: m_operatorBox.setSize(60, 20);
393: m_operatorBox.setLocation(x, 15);
394: x += m_operatorBox.getSize().width + 20;
395:
396: this .m_valLabel.setSize(this .m_valLabel.getPreferredSize());
397: this .m_valLabel.setLocation(x, 0);
398:
399: this .m_valueComponent.setSize(nValueWidth, 20);
400: this .m_valueComponent.setLocation(x, 15);
401: }
402:
403: }
404:
405: /* (non-Javadoc)
406: * @see java.awt.Component#getPreferredSize()
407: */
408: public Dimension getPreferredSize() {
409: return new Dimension(430, 35);
410: }
411:
412: /**
413: *
414: */
415: private MetadataPanel() {
416: super ();
417: }
418:
419: /**
420: * @param arg0
421: */
422: private MetadataPanel(boolean arg0) {
423: super (arg0);
424: }
425:
426: /**
427: * @param arg0
428: */
429: private MetadataPanel(LayoutManager arg0) {
430: super (arg0);
431: }
432:
433: /**
434: * @param arg0
435: * @param arg1
436: */
437: private MetadataPanel(LayoutManager arg0, boolean arg1) {
438: super (arg0, arg1);
439: }
440:
441: /* (non-Javadoc)
442: * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
443: */
444: public void removeLayoutComponent(Component arg0) {
445: }
446:
447: /* (non-Javadoc)
448: * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
449: */
450: public void addLayoutComponent(String arg0, Component arg1) {
451: }
452:
453: /* (non-Javadoc)
454: * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
455: */
456: public Dimension minimumLayoutSize(Container arg0) {
457: return this .getPreferredSize();
458: }
459:
460: /* (non-Javadoc)
461: * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
462: */
463: public Dimension preferredLayoutSize(Container arg0) {
464: return this .getPreferredSize();
465: }
466:
467: /* (non-Javadoc)
468: * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
469: */
470: public void keyPressed(KeyEvent arg0) {
471: }
472:
473: /* (non-Javadoc)
474: * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
475: */
476: public void keyReleased(KeyEvent arg0) {
477: this .m_metadata.setValue(((JTextField) this .m_valueComponent)
478: .getText());
479: }
480:
481: /* (non-Javadoc)
482: * @see com.simulacramedia.swing.datefield.DateFieldListener#validationFailed(com.simulacramedia.swing.datefield.JDateField, int)
483: */
484: public void validationFailed(JDateField source, int nReason) {
485: }
486:
487: /* (non-Javadoc)
488: * @see com.simulacramedia.swing.datefield.DateFieldListener#valueChanged(com.simulacramedia.swing.datefield.JDateField)
489: */
490: public void valueChanged(JDateField source) {
491: this.m_metadata.setValue(this.getValueFromComponent());
492: }
493:
494: }
|