Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: ledestin/swing/jdom/JDOM2SwingMap.java


1   /*
2   * Copyright (C) 2000-2001 Dmitry Macsema.  All Rights Reserved.  
3   * 
4   * Redistribution and use in source and binary forms, with or without
5   * modification, are permitted under the terms of the following
6   * Open Source license:
7   *
8   * The GNU General Public License, version 2, or any later version, as
9   * published by the Free Software Foundation
10  * (http://www.fsf.org/copyleft/gpl.html);
11  */
12  
13  package ledestin.swing.jdom;
14  
15  import javax.swing.*;
16  import javax.swing.text.JTextComponent;
17  import javax.swing.event.*;
18  import java.awt.GridLayout;
19  import java.util.*;
20  import org.jdom.*;
21  import org.jdom.input.*;
22  
23  abstract class AbstractDocumentListener implements DocumentListener
24  {
25    public void insertUpdate(DocumentEvent e) {};
26    public void removeUpdate(DocumentEvent e) {};
27    public void changedUpdate(DocumentEvent e) {};
28  }
29  
30  /**
31  The class implements a map of data from XML (JDOM) source to a number of 
32  JTextFields.
33  */
34  public class JDOM2SwingMap {
35    protected final int READ_ALL = 0;
36    protected final int RESTORE_CHANGED = 1;
37    
38    /**
39    Contains mapping between <code>org.jdom.Element</code>s and 
40    <code>JDOM2SwingMap.Entry</code>s
41    */
42    private HashMap elements = new HashMap();
43    public void setElements(HashMap m) {
44      elements = m;
45    }
46    public HashMap getElements() {
47      return elements;
48    }
49    public static JPanel generateUI(Element e, JDOM2SwingMap map){
50      List list = e.getChildren();
51      JPanel panel = new JPanel();
52      panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
53      panel.setBorder(BorderFactory.createTitledBorder(e.getName()));
54  
55      for (int i=0; i<list.size(); i++) {
56        Element el = (Element)list.get(i);
57        if (el.getChildren().size() == 0) {
58          panel.add(new JLabel(el.getName()));
59          JTextField fld = new JTextField(el.getText());
60          map.getElements().put(el, new Entry(fld));
61          panel.add(fld);
62        } else {
63          panel.add(generateUI(el, map));
64        }
65      }
66      return panel;
67    }
68    public void saveDataToJDOM() {
69      Iterator it = elements.entrySet().iterator();
70      JDOM2SwingMap.Entry entry;
71      while(it.hasNext()) {
72        Map.Entry e = (Map.Entry)it.next();
73        entry = (JDOM2SwingMap.Entry)e.getValue();
74        if (entry.changed) {
75          Element el = (Element)e.getKey();
76          el.setText(entry.getComponent().getText());
77          entry.changed = false;
78        }
79      }
80    }
81    public void readDataFromJDOM() {
82      readDataFromJDOM(READ_ALL);
83    }
84    public void restoreDataFromJDOM() {
85      readDataFromJDOM(RESTORE_CHANGED);
86    }
87    protected void readDataFromJDOM(int option) {
88      Iterator it = elements.entrySet().iterator();
89      org.jdom.Element el;
90      JDOM2SwingMap.Entry entry;
91      while(it.hasNext()) {
92        Map.Entry e = (Map.Entry)it.next();
93        el = (org.jdom.Element)e.getKey();
94        entry = (JDOM2SwingMap.Entry)e.getValue();
95        if (entry.changed || option == READ_ALL) {
96          entry.getComponent().setText(el.getText());
97          entry.changed = false;
98        }
99      }
100   };
101   
102   public static class Entry implements DocumentListener {
103     /**
104     Variable to hold list of entries
105     */
106     //static HashMap entries = new HashMap();
107 
108     protected JTextField c;
109     boolean changed;
110     public Entry(JTextField c) {
111       setComponent(c);
112     }
113 /*    public static Entry getEntry(JTextField c) {
114       if (entries.size() > 0 && entries.containsKey(c)) {
115         return (Entry)entries.get(c);
116       } else {
117         Entry e = new Entry(c);
118         entries.put(c, e);
119         return e;
120       }
121     }
122 */    
123     public JTextField getComponent() {
124       return c;
125     }
126     public void setComponent(JTextField c) {
127       this.c = c;
128       c.getDocument().addDocumentListener(this);
129       changed = false;
130     }
131 
132     public void changedUpdate(DocumentEvent e) {};
133     public void insertUpdate(DocumentEvent e) {
134       changed = true;
135     };
136     public void removeUpdate(DocumentEvent e) {
137       changed = true;
138     };
139   };
140 };