KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > ui > nodes > categorized > customizer > ReferenceCustomizer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * ReferenceCustomizer.java
22  *
23  * Created on January 17, 2006, 10:26 PM
24  */

25
26 package org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer;
27
28 import java.io.IOException JavaDoc;
29 import java.beans.PropertyChangeEvent JavaDoc;
30 import java.beans.PropertyChangeListener JavaDoc;
31 import org.netbeans.modules.xml.schema.model.AttributeGroupReference;
32 import org.netbeans.modules.xml.schema.model.AttributeReference;
33 import org.netbeans.modules.xml.schema.model.ElementReference;
34 import org.netbeans.modules.xml.schema.model.GlobalAttribute;
35 import org.netbeans.modules.xml.schema.model.GlobalAttributeGroup;
36 import org.netbeans.modules.xml.schema.model.GlobalElement;
37 import org.netbeans.modules.xml.schema.model.GlobalGroup;
38 import org.netbeans.modules.xml.schema.model.GroupReference;
39 import org.netbeans.modules.xml.schema.model.ReferenceableSchemaComponent;
40 import org.netbeans.modules.xml.schema.model.SchemaComponent;
41 import org.netbeans.modules.xml.schema.model.SchemaComponentReference;
42 import org.netbeans.modules.xml.schema.model.visitor.DefaultSchemaVisitor;
43 import org.netbeans.modules.xml.schema.ui.basic.editors.SchemaComponentSelectionPanel;
44 import org.netbeans.modules.xml.xam.ui.customizer.MessageDisplayer;
45 import org.openide.util.HelpCtx;
46
47 /**
48  * Attribute customizer
49  *
50  * @author Ajit Bhate
51  */

52 public class ReferenceCustomizer<T extends SchemaComponent>
53         extends AbstractSchemaComponentCustomizer<T>
54         implements PropertyChangeListener JavaDoc {
55     
56     static final long serialVersionUID = 1L;
57     
58     /**
59      * Creates new form ReferenceCustomizer
60      */

61     public ReferenceCustomizer(SchemaComponentReference<T> reference,
62             SchemaComponent parent) {
63         super(reference, parent);
64         this.refVisitor = new ReferenceVisitor(reference.get());
65         initComponents();
66         reset();
67     }
68     
69     public void applyChanges() throws IOException JavaDoc {
70         if(canApply()) {
71             setRef();
72         }
73     }
74     
75     public void reset() {
76         initializeModel();
77         initializeUISelection();
78         if(hasParent()||getUIRef()==null) {
79             setSaveEnabled(false);
80         } else {
81             setSaveEnabled(true);
82         }
83         setResetEnabled(false);
84     }
85     
86     /**
87      * Returns current type of the element
88      */

89     protected ReferenceableSchemaComponent getRef() {
90         return refVisitor.getRef();
91     }
92     
93     /**
94      * initializes non ui elements
95      */

96     protected void initializeModel(){};
97     
98     /**
99      * Changes the type of element
100      *
101      */

102     protected void setRef(){
103         refVisitor.setRef(getUIRef());
104     };
105     
106     /**
107      * selects model node on ui
108      */

109     private void selectModelNode() {
110         refVisitor.getComponentSelectionPanel().removePropertyChangeListener(this);
111         refVisitor.reset();
112         refVisitor.getComponentSelectionPanel().addPropertyChangeListener(this);
113     }
114     
115     /**
116      *
117      *
118      */

119     private void initializeTypeView() {
120         refVisitor.getComponentSelectionPanel().addPropertyChangeListener(this);
121         typePanel.add(refVisitor.getComponentSelectionPanel().getTypeSelectionPanel(),
122                 java.awt.BorderLayout.CENTER);
123         refVisitor.getComponentSelectionPanel().getTypeSelectionPanel().
124                 getAccessibleContext().setAccessibleParent(typePanel);
125     }
126     
127     private void initializeUISelection() {
128         getMessageDisplayer().clear();
129         selectModelNode();
130     }
131     
132     /**
133      * Retrieve the selected ref from the UI.
134      *
135      * @return global reference from UI.
136      */

137     protected ReferenceableSchemaComponent getUIRef() {
138         return refVisitor.getComponentSelectionPanel().getCurrentSelection();
139     }
140     
141     /**
142      * This method is called from within the constructor to
143      * initializeTypeView the form.
144      * WARNING: Do NOT modify this code. The content of this method is
145      * always regenerated by the Form Editor.
146      */

147     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
148
private void initComponents() {
149         typePanel = new javax.swing.JPanel JavaDoc();
150         mPanel = new javax.swing.JPanel JavaDoc();
151
152         typePanel.setLayout(new java.awt.BorderLayout JavaDoc());
153
154         initializeTypeView();
155
156         mPanel.setLayout(new java.awt.BorderLayout JavaDoc());
157
158         mPanel.add(getMessageDisplayer().getComponent(),java.awt.BorderLayout.CENTER);
159
160         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
161         this.setLayout(layout);
162         layout.setHorizontalGroup(
163             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
164             .add(org.jdesktop.layout.GroupLayout.TRAILING, mPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
165             .add(layout.createSequentialGroup()
166                 .add(10, 10, 10)
167                 .add(typePanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
168                 .addContainerGap())
169         );
170         layout.setVerticalGroup(
171             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
172             .add(layout.createSequentialGroup()
173                 .addContainerGap()
174                 .add(typePanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 407, Short.MAX_VALUE)
175                 .add(0, 0, 0)
176                 .add(mPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE))
177         );
178     }// </editor-fold>//GEN-END:initComponents
179

180     ////////////////////////
181
// event handling
182
////////////////////////
183
/**
184      * Since it implements PCL.
185      */

186     public void propertyChange(PropertyChangeEvent JavaDoc event) {
187         if (event.getPropertyName().equals(SchemaComponentSelectionPanel.PROPERTY_SELECTION)) {
188             determineValidity();
189         }
190     }
191     
192     /**
193      * Based on the current radio button status and node selections, decide
194      * if we are in a valid state for accepting the user's input.
195      */

196     private void determineValidity() {
197         getMessageDisplayer().clear();
198         ReferenceableSchemaComponent ref = getUIRef();
199         if(ref!=null) {
200             if(getRef()==ref) {
201                 setResetEnabled(false);
202                 setSaveEnabled(!hasParent());
203             } else {
204                 setResetEnabled(true);
205                 setSaveEnabled(true);
206             }
207         } else {
208             setResetEnabled(getRef()!=null);
209             setSaveEnabled(false);
210             getMessageDisplayer().annotate(org.openide.util.NbBundle.
211                     getMessage(ReferenceCustomizer.class,
212                     "MSG_Reference_Error"),
213                     MessageDisplayer.Type.ERROR);
214         }
215     }
216     
217     public HelpCtx getHelpCtx() {
218         return new HelpCtx(ReferenceCustomizer.class);
219     }
220     
221     
222     // Variables declaration - do not modify//GEN-BEGIN:variables
223
public javax.swing.JPanel JavaDoc mPanel;
224     public javax.swing.JPanel JavaDoc typePanel;
225     // End of variables declaration//GEN-END:variables
226

227     private transient ReferenceVisitor refVisitor;
228     
229     private static class ReferenceVisitor extends DefaultSchemaVisitor {
230         private SchemaComponent component;
231         private OPERATION operation;
232         private static enum OPERATION {GET,SET};
233         private ReferenceableSchemaComponent ref;
234         private SchemaComponentSelectionPanel<? extends ReferenceableSchemaComponent>
235                 componentSelectionPanel;
236         public ReferenceVisitor(SchemaComponent component) {
237             this.component = component;
238             this.operation = null;
239             component.accept(this);
240         }
241         public void visit(ElementReference reference) {
242             if(operation==null) {
243                 if(componentSelectionPanel==null) {
244                     componentSelectionPanel = new SchemaComponentSelectionPanel
245                             <GlobalElement>(reference.getModel(), GlobalElement.class,
246                             (GlobalElement)getRef(), null, false);
247                 }
248             } else if (operation==OPERATION.GET) {
249                 if(reference.getRef()!=null) {
250                     ref = reference.getRef().get();
251                 }
252             } else if (operation==OPERATION.SET) {
253                 reference.setRef(reference.getModel().getFactory().
254                         createGlobalReference((GlobalElement)ref,
255                         GlobalElement.class,reference));
256             }
257         }
258         
259         public void visit(AttributeReference reference) {
260             if(operation==null) {
261                 componentSelectionPanel = new SchemaComponentSelectionPanel<GlobalAttribute>(
262                         reference.getModel(), GlobalAttribute.class,
263                         (GlobalAttribute)getRef(), null, false);
264             } else if (operation==OPERATION.GET) {
265                 if(reference.getRef()!=null) {
266                     ref = reference.getRef().get();
267                 }
268             } else if (operation==OPERATION.SET) {
269                 reference.setRef(reference.getModel().getFactory().
270                         createGlobalReference((GlobalAttribute)ref,
271                         GlobalAttribute.class,reference));
272             }
273         }
274         
275         public void visit(GroupReference reference) {
276             if(operation==null) {
277                 componentSelectionPanel = new SchemaComponentSelectionPanel<GlobalGroup>(
278                         reference.getModel(), GlobalGroup.class,
279                         (GlobalGroup)getRef(), null, false);
280             } else if (operation==OPERATION.GET) {
281                 if(reference.getRef()!=null) {
282                     ref = reference.getRef().get();
283                 }
284             } else if (operation==OPERATION.SET) {
285                 reference.setRef(reference.getModel().getFactory().
286                         createGlobalReference((GlobalGroup)ref,
287                         GlobalGroup.class,reference));
288             }
289         }
290         
291         public void visit(AttributeGroupReference reference) {
292             if(operation==null) {
293                 componentSelectionPanel = new SchemaComponentSelectionPanel<GlobalAttributeGroup>(
294                         reference.getModel(), GlobalAttributeGroup.class,
295                         (GlobalAttributeGroup)getRef(), null, false);
296             } else if (operation==OPERATION.GET) {
297                 if(reference.getGroup()!=null) {
298                     ref = reference.getGroup().get();
299                 }
300             } else if (operation==OPERATION.SET) {
301                 reference.setGroup(reference.getModel().getFactory().
302                         createGlobalReference((GlobalAttributeGroup)ref,
303                         GlobalAttributeGroup.class,reference));
304             }
305         }
306         
307         public SchemaComponentSelectionPanel<? extends ReferenceableSchemaComponent>
308                 getComponentSelectionPanel() {
309             return componentSelectionPanel;
310         }
311         
312         public ReferenceableSchemaComponent getRef() {
313             operation=OPERATION.GET;
314             ref = null;
315             component.accept(this);
316             return ref;
317         }
318         
319         public void setRef(ReferenceableSchemaComponent ref) {
320             operation=OPERATION.SET;
321             this.ref = ref;
322             component.accept(this);
323         }
324         
325         public void reset() {
326         }
327     }
328 }
329
Popular Tags