KickJava   Java API By Example, From Geeks To Geeks.

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


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  * AbstractSchemaComponentCustomizer.java
22  *
23  * Created on May 8, 2006, 4:43 PM
24  *
25  */

26
27 package org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer;
28
29 import org.netbeans.modules.xml.schema.model.SchemaComponent;
30 import org.netbeans.modules.xml.schema.model.SchemaComponentReference;
31 import org.netbeans.modules.xml.xam.Nameable;
32 import org.netbeans.modules.xml.xam.dom.Utils;
33 import org.netbeans.modules.xml.xam.ui.customizer.AbstractComponentCustomizer;
34 import org.netbeans.modules.xml.xam.ui.customizer.MessageDisplayer;
35 import org.openide.util.NbBundle;
36
37 /**
38  *
39  * @author Ajit Bhate
40  */

41 abstract class AbstractSchemaComponentCustomizer<T extends SchemaComponent>
42         extends AbstractComponentCustomizer<SchemaComponent>
43 {
44     
45     /** Creates a new instance of AbstractSchemaComponentCustomizer */
46     AbstractSchemaComponentCustomizer(SchemaComponentReference<T> reference)
47     {
48         this(reference,null);
49     }
50
51     /** Creates a new instance of AbstractSchemaComponentCustomizer */
52     AbstractSchemaComponentCustomizer(SchemaComponentReference<T> reference,
53             SchemaComponent parent)
54     {
55                 super(reference.get());
56         this.reference = reference;
57         this.parent = parent;
58     }
59     
60     protected SchemaComponentReference<T> getReference()
61     {
62         return reference;
63     }
64
65     protected SchemaComponent getParentComponent()
66     {
67         SchemaComponent sc = getReference().get().getParent();
68         if(sc!=null) return sc;
69         return this.parent;
70     }
71
72     protected boolean hasParent()
73     {
74         return getReference().get().getParent()!=null;
75     }
76
77     protected boolean isNameChanged()
78     {
79         if(!isNameable()) return false;
80         String JavaDoc modelName = _getName();
81         String JavaDoc uiName = getUIName();
82         if(uiName==null || uiName.trim().length()==0)
83             return modelName!=null && modelName.trim().length()!=0;
84         return !uiName.equals(modelName);
85     }
86
87     protected boolean isNameValid()
88     {
89         if(!isNameable()) return true;
90         String JavaDoc uiName = getUIName();
91         if(uiName==null || !Utils.isValidNCName(uiName)) {
92             return false;
93         }
94         for(SchemaComponent child :getParentComponent().getChildren(
95                 getReference().get().getComponentType()))
96         {
97             if(uiName.equals(((Nameable)child).getName())) return false;
98         }
99         return true;
100     }
101
102     protected void saveName()
103     {
104         if(isNameable() && isNameValid() && isNameChanged())
105         {
106             Nameable n = (Nameable)getReference().get();
107             // refactor???
108
n.setName(getUIName());
109         }
110     }
111     
112     protected boolean isNameable()
113     {
114         return getReference().get() instanceof Nameable;
115     }
116
117     protected String JavaDoc _getName()
118     {
119         if(isNameable())
120             return ((Nameable)getReference().get()).getName();
121         return null;
122     }
123
124     protected String JavaDoc getUIName()
125     {
126         return null;
127     }
128
129     private transient SchemaComponentReference<T> reference;
130     /**
131      * parent component for new type
132      */

133     private transient SchemaComponent parent;
134
135     protected void setSaveEnabled(boolean flag)
136     {
137         super.setSaveEnabled(flag);
138         if(isNameable() && isNameChanged())
139         {
140             if(isNameValid())
141             {
142                 if(hasParent())
143                 {
144                     getMessageDisplayer().annotate(NbBundle.getMessage(
145                             AbstractSchemaComponentCustomizer.class,
146                             "MSG_Name_Warning"),
147                             MessageDisplayer.Type.WARNING);
148                 }
149             }
150             else
151             {
152                 getMessageDisplayer().annotate(NbBundle.getMessage(
153                         AbstractSchemaComponentCustomizer.class,
154                         "MSG_Name_Error"),
155                         MessageDisplayer.Type.ERROR);
156             }
157         }
158     }
159
160 }
161
Popular Tags