KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > editors > report > MetadataPanel


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.him.editors.report;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import java.text.*;
24 import java.util.*;
25
26 import javax.swing.*;
27
28 import org.openharmonise.him.*;
29 import org.openharmonise.him.editors.report.rqom.*;
30 import org.openharmonise.him.editors.report.swing.*;
31 import org.openharmonise.him.harmonise.*;
32 import org.openharmonise.swing.*;
33 import org.openharmonise.swing.datefield.*;
34 import org.openharmonise.vfs.*;
35 import org.openharmonise.vfs.metadata.*;
36 import org.openharmonise.vfs.metadata.range.*;
37 import org.openharmonise.vfs.servers.ServerList;
38
39
40 /**
41  * Component to display a metadata item in the report editor.
42  *
43  * @author Matthew Large
44  * @version $Revision: 1.2 $
45  *
46  */

47 public class MetadataPanel extends JPanel implements LayoutManager, ActionListener, KeyListener, DateFieldListener {
48
49     /**
50      * Report query.
51      */

52     private ReportQuery m_reportQuery = null;
53     
54     /**
55      * Metadata data.
56      */

57     private Metadata m_metadata = null;
58     
59     /**
60      * Property tree.
61      */

62     private JComboTree m_propertyCombo = null;
63
64     /**
65      * Component to display the value.
66      */

67     private JComponent m_valueComponent = null;
68     
69     private JLabel m_propLabel = null;
70     
71     private JLabel m_valLabel = null;
72
73     private JComboBox m_operatorBox = null;
74     
75     /**
76      * default operator list
77      */

78     private ArrayList m_operators;
79     
80     /**
81      * Date time format.
82      */

83     private static final String JavaDoc DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss G";
84     
85     /**
86      * Date format.
87      */

88     private static final String JavaDoc DATE_FORMAT = "yyyy-MM-dd G";
89
90     /**
91      * Constructs a new metadata panel.
92      *
93      * @param query Report query
94      * @param metadata Metadata data
95      */

96     public MetadataPanel(ReportQuery query, Metadata metadata) {
97         super();
98         this.m_metadata = metadata;
99         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().getHarmoniseServer().getVFS();
120         
121         this.m_propertyCombo = new JComboTree();
122         this.m_propertyCombo.setResourceTreeFilter(new ReportResourceFilter(m_reportQuery));
123         this.m_propertyCombo.setActionCommand("PROPCOMBO");
124         this.m_propertyCombo.addActionListener(this);
125         this.m_propertyCombo.setAllowClear(false);
126         this.m_propertyCombo.setShowLeafNodes(true);
127         this.m_propertyCombo.setSelectedLeafOnly(true);
128         this.m_propertyCombo.addCollectionPath(HarmonisePaths.PATH_PROPERTIES);
129         this.add(m_propertyCombo);
130         
131         if(this.m_metadata.getPath()!=null) {
132             this.m_propertyCombo.setPath(vfs, this.m_metadata.getPath());
133             Property prop = PropertyCache.getInstance().getPropertyByPath(this.m_metadata.getPath());
134             this.m_valueComponent = this.getValueComponent(prop);
135             this.add(this.m_valueComponent);
136             if(this.m_metadata.getValue()!=null && !this.m_metadata.getValue().equals("")) {
137                 this.setValueForComponent(this.m_metadata.getValue());
138             }
139         }
140
141         String JavaDoc fontName = "Dialog";
142         int fontSize = 11;
143         Font font = new Font(fontName, Font.PLAIN, fontSize);
144         
145         this.m_propLabel = new JLabel("Select a property");
146         this.m_propLabel.setFont(font);
147         this.add(this.m_propLabel);
148         
149         this.m_valLabel = new JLabel("Select a value");
150         this.m_valLabel.setFont(font);
151         this.add(this.m_valLabel);
152     }
153     
154     /**
155      * Returns a value component that is appropriate for the given
156      * property.
157      *
158      * @param prop Property
159      * @return Value component
160      */

161     private JComponent getValueComponent(Property prop) {
162         JComponent comp = null;
163         Range range = prop.getRange();
164         m_operators = new ArrayList();
165         m_operators.add("=");
166         m_operators.add("NOT");
167         if(range instanceof ResourceRange) {
168             JComboTree tree = new JComboTree();
169             tree.setActionCommand("VALUECOMBO");
170             tree.addActionListener(this);
171             tree.setSelectedLeafOnly(true);
172             tree.setShowLeafNodes(true);
173             tree.setAllowClear(false);
174             Iterator itor = ((ResourceRange)range).getHREFs().iterator();
175             while (itor.hasNext()) {
176                 String JavaDoc sPath = (String JavaDoc) itor.next();
177                 tree.addCollectionPath( sPath );
178             }
179             comp = tree;
180         } else if(range instanceof CollectionRange) {
181             JComboTree tree = new JComboTree();
182             tree.setActionCommand("VALUECOMBO");
183             tree.addActionListener(this);
184             tree.setShowLeafNodes(false);
185             tree.setAllowClear(false);
186             Iterator itor = ((CollectionRange)range).getHREFs().iterator();
187             while (itor.hasNext()) {
188                 String JavaDoc sPath = (String JavaDoc) itor.next();
189                 tree.addCollectionPath( sPath );
190             }
191             comp = tree;
192         } else if(range instanceof ValueRange) {
193             JComboTree tree = new JComboTree();
194             tree.setActionCommand("VALUECOMBO");
195             tree.addActionListener(this);
196             tree.setSelectedLeafOnly(true);
197             tree.setShowLeafNodes(true);
198             tree.setAllowClear(false);
199             Iterator itor = ((ValueRange)range).getHREFs().iterator();
200             while (itor.hasNext()) {
201                 String JavaDoc sPath = (String JavaDoc) itor.next();
202                 tree.addCollectionPath( sPath );
203             }
204             comp = tree;
205         } else if(range instanceof DateRange) {
206             JDateField dateSelector = new JDateField(DateRange.XSD_DATE_FORMAT);
207             if(((DateRange)range).getMaximum()!=null) {
208                 dateSelector.setMaximumDate(((DateRange)range).getMaximum());
209             }
210             if(((DateRange)range).getMinimum()!=null) {
211                 dateSelector.setMinimumDate(((DateRange)range).getMinimum());
212             }
213             dateSelector.addDateFieldListener(this);
214             comp = dateSelector;
215             m_operators.add("<");
216             m_operators.add(">");
217         } else if(range instanceof DateTimeRange) {
218             JDateField dateSelector = new JDateField(DateTimeRange.XSD_DATE_FORMAT);
219             if(((DateTimeRange)range).getMaximum()!=null) {
220                 dateSelector.setMaximumDate(((DateTimeRange)range).getMaximum());
221             }
222             if(((DateTimeRange)range).getMinimum()!=null) {
223                 dateSelector.setMinimumDate(((DateTimeRange)range).getMinimum());
224             }
225             dateSelector.addDateFieldListener(this);
226             comp = dateSelector;
227             m_operators.add("<");
228             m_operators.add(">");
229         } else {
230             comp = new JTextField();
231             comp.addKeyListener(this);
232         }
233         if(range instanceof IntegerRange || range instanceof FloatRange){
234             m_operators.add("<");
235             m_operators.add(">");
236         }
237         this.m_operatorBox = new JComboBox(m_operators.toArray());
238         this.m_operatorBox.setBackground(Color.WHITE);
239         this.m_operatorBox.setActionCommand("OPERATOR");
240         this.m_operatorBox.addActionListener(this);
241         this.add(m_operatorBox);
242         if(this.m_metadata.getOperator()!=null){
243             m_operatorBox.setSelectedItem(m_metadata.getOperator());
244         }
245         
246         return comp;
247     }
248     
249     /**
250      * Returns a value from the current value comonent.
251      *
252      * @return Value
253      */

254     private String JavaDoc getValueFromComponent() {
255         String JavaDoc sValue = null;
256         
257         if(this.m_valueComponent instanceof JComboTree) {
258             sValue = ((JComboTree)this.m_valueComponent).getPath();
259         } else if(this.m_valueComponent instanceof JDateField) {
260             Date dtValue = ((JDateField)this.m_valueComponent).getDate();
261             if(((JDateField)this.m_valueComponent).getDateFormat().equalsIgnoreCase(MetadataPanel.DATETIME_FORMAT)) {
262                 SimpleDateFormat format = new SimpleDateFormat(MetadataPanel.DATETIME_FORMAT);
263                 sValue = format.format(dtValue);
264             } else {
265                 SimpleDateFormat format = new SimpleDateFormat(MetadataPanel.DATE_FORMAT);
266                 sValue = format.format(dtValue);
267             }
268         } else if(this.m_valueComponent instanceof JTextField) {
269             sValue = ((JTextField)this.m_valueComponent).getText();
270         }
271         
272         return sValue;
273     }
274     
275     /**
276      * Sets a value in the current value component.
277      *
278      * @param sValue Value
279      */

280     private void setValueForComponent(String JavaDoc sValue) {
281         if(this.m_valueComponent instanceof JComboTree) {
282             ((JComboTree)this.m_valueComponent).setPath(ServerList.getInstance().getHarmoniseServer().getVFS(), sValue);
283         } else if(this.m_valueComponent instanceof DateSelector) {
284             Date dtValue = new Date();
285             if(((DateSelector)this.m_valueComponent).isShowingTime()) {
286                 SimpleDateFormat format = new SimpleDateFormat(MetadataPanel.DATETIME_FORMAT);
287                 try {
288                     dtValue = format.parse(sValue);
289                 } catch (ParseException e) {
290                     e.printStackTrace();
291                 }
292             } else {
293                 SimpleDateFormat format = new SimpleDateFormat(MetadataPanel.DATE_FORMAT);
294                 try {
295                     dtValue = format.parse(sValue);
296                 } catch (ParseException e) {
297                     e.printStackTrace();
298                 }
299             }
300             ((DateSelector)this.m_valueComponent).setInitalDate(dtValue);
301         } else if(this.m_valueComponent instanceof JTextField) {
302             ((JTextField)this.m_valueComponent).setText(sValue);
303         }
304     }
305
306     /* (non-Javadoc)
307      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
308      */

309     public void actionPerformed(ActionEvent ae) {
310         if(ae.getActionCommand().equals("PROPCOMBO")) {
311             this.m_metadata.setPath(this.m_propertyCombo.getPath());
312             Property prop = PropertyCache.getInstance().getPropertyByPath(this.m_metadata.getPath());
313             if(this.m_valueComponent!=null) {
314                 this.remove(this.m_valueComponent);
315             }
316             this.m_valueComponent = this.getValueComponent(prop);
317             this.add(this.m_valueComponent);
318             if(m_operatorBox!=null){
319                 this.remove(m_operatorBox);
320             }
321             m_operatorBox = new JComboBox(m_operators.toArray());
322             m_operatorBox.setBackground(Color.WHITE);
323             m_operatorBox.setActionCommand("OPERATOR");
324             m_operatorBox.addActionListener(this);
325             this.add(m_operatorBox);
326             this.m_metadata.setOperator("=");
327             
328             this.m_metadata.setValue("");
329             this.layoutContainer(null);
330             this.validateTree();
331             this.repaint();
332         } else if(ae.getActionCommand().equals("VALUECOMBO")) {
333             this.m_metadata.setValue( ((JComboTree)this.m_valueComponent).getPath() );
334         } else if(ae.getActionCommand().equals("OPERATOR")) {
335             this.m_metadata.setOperator((String JavaDoc)m_operatorBox.getSelectedItem());
336         }
337     }
338
339     /* (non-Javadoc)
340      * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
341      */

342     public void keyTyped(KeyEvent ke) {
343     }
344
345     /* (non-Javadoc)
346      * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
347      */

348     public void layoutContainer(Container arg0) {
349         int x = 0;
350         
351         this.m_propLabel.setSize(this.m_propLabel.getPreferredSize());
352         this.m_propLabel.setLocation(x, 0);
353         
354         this.m_propertyCombo.setSize(this.m_propertyCombo.getPreferredSize());
355         this.m_propertyCombo.setLocation(x, 15);
356         
357         x += this.m_propertyCombo.getSize().width + 10;
358         
359         
360         if(this.m_valueComponent!=null) {
361             int nValueWidth = 170;
362
363             m_operatorBox.setSize(60,20);
364             m_operatorBox.setLocation(x, 15);
365             x += m_operatorBox.getSize().width + 20;
366
367             this.m_valLabel.setSize(this.m_valLabel.getPreferredSize());
368             this.m_valLabel.setLocation(x, 0);
369             
370             this.m_valueComponent.setSize(nValueWidth, 20);
371             this.m_valueComponent.setLocation(x, 15);
372         }
373         
374     }
375
376     /* (non-Javadoc)
377      * @see java.awt.Component#getPreferredSize()
378      */

379     public Dimension getPreferredSize() {
380         return new Dimension(430, 35);
381     }
382
383     /**
384      *
385      */

386     private MetadataPanel() {
387         super();
388     }
389
390     /**
391      * @param arg0
392      */

393     private MetadataPanel(boolean arg0) {
394         super(arg0);
395     }
396
397     /**
398      * @param arg0
399      */

400     private MetadataPanel(LayoutManager arg0) {
401         super(arg0);
402     }
403
404     /**
405      * @param arg0
406      * @param arg1
407      */

408     private MetadataPanel(LayoutManager arg0, boolean arg1) {
409         super(arg0, arg1);
410     }
411
412     /* (non-Javadoc)
413      * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
414      */

415     public void removeLayoutComponent(Component arg0) {
416     }
417
418     /* (non-Javadoc)
419      * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
420      */

421     public void addLayoutComponent(String JavaDoc arg0, Component arg1) {
422     }
423
424     /* (non-Javadoc)
425      * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
426      */

427     public Dimension minimumLayoutSize(Container arg0) {
428         return this.getPreferredSize();
429     }
430
431     /* (non-Javadoc)
432      * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
433      */

434     public Dimension preferredLayoutSize(Container arg0) {
435         return this.getPreferredSize();
436     }
437
438     /* (non-Javadoc)
439      * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
440      */

441     public void keyPressed(KeyEvent arg0) {
442     }
443
444     /* (non-Javadoc)
445      * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
446      */

447     public void keyReleased(KeyEvent arg0) {
448         this.m_metadata.setValue(((JTextField)this.m_valueComponent).getText());
449     }
450
451     /* (non-Javadoc)
452      * @see com.simulacramedia.swing.datefield.DateFieldListener#validationFailed(com.simulacramedia.swing.datefield.JDateField, int)
453      */

454     public void validationFailed(JDateField source, int nReason) {
455     }
456
457     /* (non-Javadoc)
458      * @see com.simulacramedia.swing.datefield.DateFieldListener#valueChanged(com.simulacramedia.swing.datefield.JDateField)
459      */

460     public void valueChanged(JDateField source) {
461         this.m_metadata.setValue(this.getValueFromComponent());
462     }
463
464 }
465
Popular Tags