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

Quick Search    Search Deep

Source code: org/bdgp/apps/dagedit/plugin/TypeManagerPlugin.java


1   package org.bdgp.apps.dagedit.plugin;
2   
3   import org.bdgp.apps.dagedit.gui.*;
4   import org.bdgp.apps.dagedit.datamodel.*;
5   import org.bdgp.apps.dagedit.gui.event.*;
6   import org.bdgp.util.*;
7   import org.bdgp.swing.*;
8   import javax.swing.*;
9   import java.awt.*;
10  import java.awt.event.*;
11  import java.util.*;
12  
13  public class TypeManagerPlugin extends DEPlugin {
14  
15      protected ListEditor typeList;
16      protected JLabel noRelationshipLabel =
17    new JLabel("Click a symbol to edit it.");
18      protected JButton commitButton = new JButton("Save Changes");
19      protected JButton revertButton = new JButton("Revert");
20  
21      private class RelationshipTypeWrapper {
22    private String name;
23    private String desc;
24    boolean isDefaultType;
25  
26    public RelationshipTypeWrapper(String name,
27                 String desc,
28                 boolean isDefaultType) {
29        this.name = name;
30        this.desc = desc;
31        this.isDefaultType = isDefaultType;
32    }
33  
34    public TermRelationshipType getTermRelationshipType() {
35        return new TermRelationshipType(name, desc);
36    }
37  
38    public boolean getIsDefault() {
39        return isDefaultType;
40    }
41  
42    public void setIsDefault(boolean isDefaultType) {
43        this.isDefaultType = isDefaultType;
44    }
45  
46    public String toString() {
47        return name;
48    }
49  
50    public String getName() {
51        return name;
52    }
53  
54    public String getDesc() {
55        return desc;
56    }
57  
58    public void setName(String name) {
59        this.name = name;
60    }
61  
62    public void setDesc(String desc) {
63        this.desc = desc;
64    }
65      }
66  
67      private class RelationshipTypeEditor extends JPanel
68    implements GenericEditorComponent {
69    
70    private JTextField charField;
71    private JTextField nameField;
72    private JTextField descField;
73    private JCheckBox defaultBox;
74  
75    protected ListEditor editor;
76  
77    public void setMasterComponent(Component c) {
78        if (c instanceof ListEditor)
79      editor = (ListEditor) c;
80    }
81  
82    public RelationshipTypeEditor() {
83        JLabel nameLabel = new JLabel("Relationship type name");
84        JLabel descLabel = new JLabel("Relationship type description");
85        FocusListener listener = new FocusListener() {
86          public void focusLost(FocusEvent e) {
87        typeList.commit();
88          }
89  
90          public void focusGained(FocusEvent e) {
91          }
92      };
93  
94        nameField = new JTextField(10);
95        descField = new JTextField();
96        defaultBox = new JCheckBox("Is default type");
97  
98        defaultBox.addActionListener(new ActionListener() {
99          public void actionPerformed(ActionEvent e) {
100       Vector symbols = typeList.getData();
101       for(int i=0; i < symbols.size(); i++) {
102           RelationshipTypeWrapper rtw =
103         (RelationshipTypeWrapper) symbols.
104         elementAt(i);
105           rtw.setIsDefault(false);
106       }
107       typeList.commit();
108         }
109     });
110 
111       nameField.addFocusListener(listener);
112       descField.addFocusListener(listener);
113 
114       nameField.setFont(controller.getDefaultFont());
115       nameLabel.setFont(controller.getDefaultFont());
116       descLabel.setFont(controller.getDefaultFont());
117       descField.setFont(controller.getDefaultFont());
118       defaultBox.setFont(controller.getDefaultFont());
119 
120       setLayout(new BoxLayout(RelationshipTypeEditor.this,
121             BoxLayout.Y_AXIS));
122       add(nameLabel);
123       add(nameField);
124       add(Box.createVerticalStrut(10));
125       add(descLabel);
126       add(descField);
127       add(Box.createVerticalStrut(10));
128       add(defaultBox);
129       add(Box.createVerticalGlue());
130   }
131 
132   public void load(Object o) {
133       RelationshipTypeWrapper rtw = (RelationshipTypeWrapper) o;
134       nameField.setText(rtw.getName());
135       descField.setText(rtw.getDesc());
136       defaultBox.setSelected(rtw.getIsDefault());
137   }
138 
139   public void store(Object o) {
140       RelationshipTypeWrapper rtw = (RelationshipTypeWrapper) o;
141       rtw.setName(nameField.getText());
142       rtw.setDesc(descField.getText());
143       rtw.setIsDefault(defaultBox.isSelected());
144   }
145 
146   public Object createNewValue() {
147       return new RelationshipTypeWrapper("NEWTYPE",
148                  "<new relationship type>",
149                  typeList.getData().
150                  size() == 0);
151   }
152     }
153 
154     public TypeManagerPlugin() {
155     }
156 
157     public String getName() {
158   return "Type Manager Plugin";
159     }
160 
161     public void init(MultiProperties props) {
162   setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
163   setPreferredSize(new Dimension(400,300));
164   typeList = new ListEditor(new RelationshipTypeEditor(),
165           noRelationshipLabel,
166           new Vector(),
167           true, true,
168           true, true,
169           false);
170   add(typeList);
171 
172   Box commitBox = new Box(BoxLayout.X_AXIS);
173   commitBox.add(Box.createHorizontalGlue());
174   commitBox.add(commitButton);
175   commitBox.add(Box.createHorizontalStrut(10));
176   commitBox.add(revertButton);
177   commitBox.add(Box.createHorizontalGlue());
178 
179   commitButton.setFont(controller.getDefaultFont());
180   revertButton.setFont(controller.getDefaultFont());
181   noRelationshipLabel.setFont(controller.getDefaultFont());
182 
183   add(commitBox);
184   commitButton.addActionListener(new ActionListener() {
185     public void actionPerformed(ActionEvent e) {
186         saveTypes();
187     }
188       });
189   revertButton.addActionListener(new ActionListener() {
190     public void actionPerformed(ActionEvent e) {
191         loadTypes();
192     }
193       });
194   typeList.setFont(controller.getDefaultFont());
195   loadTypes();
196     }
197 
198     public void setController(Controller controller) {
199   super.setController(controller);
200   controller.addListener(new DETermReloadListener() {
201     public void reload(DETermReloadEvent e) {
202         loadTypes();
203     }
204       });
205   controller.addListener(new RootChangeListener() {
206     public void changeRoot(RootChangeEvent e) {
207         loadTypes();
208     }
209       });
210     }
211 
212     protected void loadTypes() {
213   Vector v = new Vector();
214   Vector oldTypes = controller.getHistory().getRelationshipTypes();
215   TermRelationshipType defaultType = (TermRelationshipType) controller.
216       getHistory().getDefaultRelationshipType();
217   for(int i=0; i < oldTypes.size(); i++) {
218       TermRelationshipType trt = (TermRelationshipType) oldTypes.get(i);
219       v.add(new RelationshipTypeWrapper(trt.getName(), trt.getDesc(),
220                 defaultType != null &&
221                 defaultType.equals(trt)));
222   }
223   typeList.setData(v);
224     }
225 
226     protected void saveTypes() {
227   Vector v = new Vector();
228   TermRelationshipType defaultType = null;
229   Vector data = typeList.getData();
230   for(int i=0; i < data.size(); i++) {
231       RelationshipTypeWrapper rtw = (RelationshipTypeWrapper)
232     data.get(i);
233       v.add(rtw.getTermRelationshipType());
234       if (rtw.getIsDefault())
235     defaultType = rtw.getTermRelationshipType();
236   }
237   HashSet set = new HashSet();
238   Term root = controller.getRoot();
239   Enumeration desc = root.getAllDescendants();
240   while(desc.hasMoreElements()) {
241       Term term = (Term) desc.nextElement();
242       Vector children = term.getChildren();
243       for(int i=0; i < children.size(); i++) {
244     TermRelationship tr = (TermRelationship) children.get(i);
245     if (!v.contains(tr.getType())) {
246         JOptionPane.showMessageDialog(this,
247               "Could not commit new "+
248               "relationship types "+
249               "because "+tr.getType()+" "+
250               "is still in use in the "+
251               "ontology");
252         return;
253     }
254       }
255   }
256   controller.getHistory().setRelationshipTypes(v);
257   controller.getHistory().setDefaultRelationshipType(defaultType);
258     }
259 }
260 
261