001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.uml.codegen.ui.customizer;
043:
044: import java.beans.PropertyChangeSupport;
045: import java.util.ArrayList;
046: import java.util.List;
047: import javax.swing.Box;
048: import javax.swing.JCheckBox;
049: import javax.swing.JComponent;
050:
051: import org.netbeans.modules.uml.codegen.ui.customizer.TemplateEntry;
052: import org.netbeans.modules.uml.codegen.ui.customizer.TabbedPanelModel;
053: import org.netbeans.modules.uml.codegen.dataaccess.xmlbeans.DomainObject;
054: import org.netbeans.modules.uml.codegen.dataaccess.xmlbeans.Family;
055: import org.netbeans.modules.uml.codegen.dataaccess.xmlbeans.TemplateFamiliesHandler;
056: import org.netbeans.modules.uml.project.ui.customizer.UMLProjectProperties;
057:
058: /**
059: *
060: * @author treyspiva
061: */
062:
063: public class TemplateModel extends TabbedPanelModel {
064: private TemplateFamiliesHandler handler = TemplateFamiliesHandler
065: .getInstance(true);
066: private UMLProjectProperties umlProjectProperties = null;
067: private List<String> checkedTree = null;
068:
069: public TemplateModel(UMLProjectProperties umlPrjProps) {
070: setUMLProjectProperties(umlPrjProps);
071: propertyChangeSupport = new PropertyChangeSupport(this );
072: }
073:
074: public List<String> getCategories() {
075: List<String> categories = new ArrayList<String>();
076: Family[] families = handler.getTemplateFamilies().getFamily();
077:
078: for (int i = 0; i < families.length; i++) {
079: categories.add(families[i].getName());
080: }
081:
082: return categories;
083: }
084:
085: public String getToolTip(String category) {
086: return ""; // NOI18N
087: }
088:
089: public JComponent getPanel(String category) {
090: DomainObject[] domains = null;
091: String familyName = null;
092: Family[] families = handler.getTemplateFamilies().getFamily();
093:
094: for (int i = 0; i < families.length; i++) {
095: if (families[i].getName().equals(category)) {
096: domains = families[i].getDomainObject();
097: familyName = families[i].getName();
098: break;
099: }
100: }
101:
102: Box templateList = Box.createVerticalBox();
103: for (DomainObject domain : domains) {
104: TemplateEntry entry = new TemplateEntry(familyName, domain,
105: fetchCheckedValue(familyName, domain.getName()));
106:
107: addTemplateEntryCheckEventListener(entry);
108:
109: templateList.add(entry);
110: }
111:
112: return templateList;
113: }
114:
115: private boolean fetchCheckedValue(String familyName,
116: String domainName) {
117: return checkedTree.indexOf(familyName + ':' + domainName) != -1;
118: }
119:
120: private void updateCheckedValue(String familyName,
121: String domainName, boolean isChecked) {
122: if (checkedTree.indexOf(familyName + ':' + domainName) == -1
123: && isChecked) {
124: checkedTree.add(familyName + ':' + domainName);
125: }
126:
127: else if (checkedTree.indexOf(familyName + ':' + domainName) != -1
128: && !isChecked) {
129: checkedTree.remove(familyName + ':' + domainName);
130: }
131: }
132:
133: public void setUMLProjectProperties(UMLProjectProperties val) {
134: umlProjectProperties = val;
135: checkedTree = umlProjectProperties.getCodeGenTemplatesArray();
136: }
137:
138: public UMLProjectProperties getUMLProjectProperties() {
139: umlProjectProperties.setCodeGenTemplates(checkedTree);
140: return umlProjectProperties;
141: }
142:
143: private void addTemplateEntryCheckEventListener(TemplateEntry entry) {
144: entry.getTemplateNameField().addItemListener(
145: new java.awt.event.ItemListener() {
146: public void itemStateChanged(
147: java.awt.event.ItemEvent evt) {
148: JCheckBox checkBox = (JCheckBox) evt
149: .getSource();
150:
151: updateCheckedValue(checkBox.getClientProperty(
152: "familyName").toString(), // NOI18N
153: checkBox.getText(), checkBox
154: .isSelected());
155:
156: getPropertyChangeSupport().firePropertyChange(
157: PROP_TEMPLATE_STATE_CHANGE, null, evt);
158:
159: if (checkBox.isSelected()
160: && checkedTree.size() == 1) {
161: getPropertyChangeSupport()
162: .firePropertyChange(
163: PROP_ONE_TEMPLATE_ENABLED,
164: null, evt);
165: }
166:
167: else if (!checkBox.isSelected()
168: && checkedTree.size() == 0) {
169: getPropertyChangeSupport()
170: .firePropertyChange(
171: PROP_NO_TEMPLATES_ENABLED,
172: null, evt);
173: }
174: }
175: });
176: }
177:
178: public final static String PROP_TEMPLATE_STATE_CHANGE = "TEMPLATE_STATE_CHANGE"; // NOI18N
179: public final static String PROP_NO_TEMPLATES_ENABLED = "NO_TEMPLATES_ENABLED"; // NOI18N
180: public final static String PROP_ONE_TEMPLATE_ENABLED = "ONE_TEMPLATE_ENABLED"; // NOI18N
181:
182: private PropertyChangeSupport propertyChangeSupport = null;
183:
184: public PropertyChangeSupport getPropertyChangeSupport() {
185: return propertyChangeSupport;
186: }
187: }
|