KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xam > ui > customizer > AbstractComponentCustomizer


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 package org.netbeans.modules.xml.xam.ui.customizer;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import org.netbeans.modules.xml.xam.Component;
25 import org.netbeans.modules.xml.xam.Model;
26 import org.netbeans.modules.xml.xam.Nameable;
27 import org.netbeans.modules.xml.xam.ui.XAMUtils;
28
29 /**
30  * A customizer of XAM components.
31  *
32  * @author Ajit Bhate
33  * @author Nathan Fiedler
34  */

35 public abstract class AbstractComponentCustomizer<T extends Component>
36         extends AbstractCustomizer {
37     private transient T component;
38     private transient MessageDisplayer messageDisplayer;
39     /**
40      * Creates a new instance of AbstractComponentCustomizer.
41      *
42      * @param component XAM component to customize.
43      */

44     public AbstractComponentCustomizer(T component) {
45         this.component = component;
46         messageDisplayer = new MessagePanel();
47         messageDisplayer.clear();
48     }
49
50     public boolean isEditable() {
51         return XAMUtils.isWritable(component.getModel());
52     }
53
54     /**
55      * Guarantees transaction
56      */

57     public void apply() throws IOException JavaDoc {
58         Model model = component.getModel();
59         // It is possible that current component is not in model,
60
// or a transaction already exists, so don't start transaction.
61
boolean startTransaction = model != null && !model.isIntransaction();
62         if (startTransaction) {
63             model.startTransaction();
64         }
65         try {
66             applyChanges();
67             setSaveEnabled(false);
68             setResetEnabled(false);
69         } finally {
70             if (startTransaction) {
71                 model.endTransaction();
72             }
73         }
74     }
75
76     /**
77      * Returns the XAM component being customized.
78      *
79      * @return customized component.
80      */

81     protected T getModelComponent() {
82         return component;
83     }
84
85     /**
86      * Indicates if the given name (possibly provided by the user) is a
87      * valid name for this component. Ensures that the name is unique
88      * among the siblings of this component, that are of the same type.
89      *
90      * @param name name to validate.
91      * @return true if name is okay, false otherwise.
92      */

93     protected boolean isNameValid(String JavaDoc name) {
94         T comp = getModelComponent();
95         if (!(comp instanceof Nameable)) {
96             return true;
97         }
98         if (name == null || name.trim().length() == 0) {
99             return false;
100         }
101         Iterator JavaDoc iter = comp.getParent().getChildren().iterator();
102         while (iter.hasNext()) {
103             Object JavaDoc child = iter.next();
104             if (child instanceof Nameable) {
105                 Nameable named = (Nameable) child;
106                 if (name.equals(named.getName())) {
107                     return false;
108                 }
109             }
110         }
111         return true;
112     }
113
114     protected abstract void applyChanges() throws IOException JavaDoc;
115
116     protected MessageDisplayer getMessageDisplayer()
117     {
118         return messageDisplayer;
119     }
120 }
121
Popular Tags