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-2006 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: */package org.netbeans.modules.vmd.midp.analyzer;
041:
042: import org.netbeans.modules.vmd.api.analyzer.Analyzer;
043: import org.netbeans.modules.vmd.api.model.*;
044: import org.netbeans.modules.vmd.api.model.presenters.InfoPresenter;
045: import org.netbeans.modules.vmd.midp.components.MidpDocumentSupport;
046: import org.netbeans.modules.vmd.midp.components.MidpVersionDescriptor;
047: import org.netbeans.modules.vmd.midp.components.MidpVersionable;
048: import org.netbeans.modules.vmd.midp.components.MidpTypes;
049: import org.netbeans.modules.vmd.midp.components.items.ItemCD;
050: import org.netbeans.modules.vmd.midp.components.items.ImageItemCD;
051: import org.openide.util.NbBundle;
052:
053: import javax.swing.*;
054: import java.awt.*;
055:
056: /**
057: * @author David Kaspar
058: */
059: public class Midp1CompliantAnalyzer implements Analyzer {
060:
061: public String getProjectType() {
062: return MidpDocumentSupport.PROJECT_TYPE_MIDP;
063: }
064:
065: public String getDisplayName() {
066: return NbBundle.getMessage(Midp1CompliantAnalyzer.class,
067: "Midp1CompliantAnalyzer.displayName"); // NOI18N
068: }
069:
070: public String getToolTip() {
071: return NbBundle.getMessage(Midp1CompliantAnalyzer.class,
072: "Midp1CompliantAnalyzer.toolTip"); // NOI18N
073: }
074:
075: public Image getIcon() {
076: return null;
077: }
078:
079: public JComponent createVisualRepresentation() {
080: JList list = new JList(new DefaultListModel());
081: JScrollPane scrollPane = new JScrollPane(list);
082: scrollPane.setPreferredSize(new java.awt.Dimension(400, 150));
083: return scrollPane;
084: }
085:
086: public void update(JComponent visualRepresentation,
087: final DesignDocument document) {
088: if (visualRepresentation == null || document == null)
089: return;
090: final JList list = (JList) ((JScrollPane) visualRepresentation)
091: .getViewport().getView();
092:
093: document.getTransactionManager().readAccess(new Runnable() {
094: public void run() {
095: DefaultListModel model = (DefaultListModel) list
096: .getModel();
097: model.removeAllElements();
098: DesignComponent rootComponent = document
099: .getRootComponent();
100: if (rootComponent != null)
101: analyze(model, rootComponent);
102: }
103: });
104: }
105:
106: private void analyze(DefaultListModel list,
107: DesignComponent component) {
108: ComponentDescriptor descriptor = component
109: .getComponentDescriptor();
110: if (descriptor == null)
111: return;
112:
113: VersionDescriptor version = descriptor.getVersionDescriptor();
114: if (!version.isCompatibleWith(MidpVersionDescriptor.MIDP_1)) {
115: reportComponent(list, component);
116: return;
117: }
118:
119: for (PropertyDescriptor property : descriptor
120: .getPropertyDescriptors()) {
121: if (!property.getVersionable().isCompatibleWith(
122: MidpVersionable.MIDP_1))
123: if (!component.isDefaultValue(property.getName()))
124: reportComponentProperty(list, component, property
125: .getName());
126: processComponentProperty(list, component, property);
127: }
128:
129: for (DesignComponent child : component.getComponents())
130: analyze(list, child);
131: }
132:
133: private void reportComponent(DefaultListModel list,
134: DesignComponent component) {
135: list.addElement(NbBundle.getMessage(
136: Midp1CompliantAnalyzer.class,
137: "MSG_IncompatibleComponent", InfoPresenter
138: .getHtmlDisplayName(component))); // NOI18N
139: }
140:
141: private void processComponentProperty(DefaultListModel list,
142: DesignComponent component, PropertyDescriptor property) {
143: DescriptorRegistry registry = component.getDocument()
144: .getDescriptorRegistry();
145: if (registry.isInHierarchy(ItemCD.TYPEID, component.getType())
146: && ItemCD.PROP_LAYOUT.equals(property.getName())) {
147: if (!registry.isInHierarchy(ImageItemCD.TYPEID, component
148: .getType())) {
149: list.addElement(NbBundle.getMessage(
150: Midp1CompliantAnalyzer.class,
151: "MSG_IncompatibleItemLayout", InfoPresenter
152: .getHtmlDisplayName(component))); // NOI18N
153: return;
154: }
155: int value = MidpTypes.getInteger(component
156: .readProperty(ItemCD.PROP_LAYOUT));
157: if ((value & (ItemCD.VALUE_LAYOUT_TOP
158: | ItemCD.VALUE_LAYOUT_BOTTOM
159: | ItemCD.VALUE_LAYOUT_VCENTER
160: | ItemCD.VALUE_LAYOUT_SHRINK
161: | ItemCD.VALUE_LAYOUT_VSHRINK
162: | ItemCD.VALUE_LAYOUT_VSHRINK
163: | ItemCD.VALUE_LAYOUT_VEXPAND | ItemCD.VALUE_LAYOUT_2)) != 0) {
164: list.addElement(NbBundle.getMessage(
165: Midp1CompliantAnalyzer.class,
166: "MSG_IncompatibleItemLayout", InfoPresenter
167: .getHtmlDisplayName(component))); // NOI18N
168: return;
169: }
170: }
171: }
172:
173: private void reportComponentProperty(DefaultListModel list,
174: DesignComponent component, String propertyName) {
175: DescriptorRegistry registry = component.getDocument()
176: .getDescriptorRegistry();
177: if (registry.isInHierarchy(ItemCD.TYPEID, component.getType())) {
178: if (ItemCD.PROP_ITEM_COMMAND_LISTENER.equals(propertyName))
179: return;
180: if (ItemCD.PROP_COMMANDS.equals(propertyName)) {
181: list.addElement(NbBundle.getMessage(
182: Midp1CompliantAnalyzer.class,
183: "MSG_ItemsCommandsNotAllowed", InfoPresenter
184: .getHtmlDisplayName(component))); // NOI18N
185: return;
186: }
187: }
188: list.addElement(NbBundle.getMessage(
189: Midp1CompliantAnalyzer.class,
190: "MSG_IncompatiblePropertyValue", propertyName,
191: InfoPresenter.getHtmlDisplayName(component))); // NOI18N
192: }
193:
194: }
|