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: */
041:
042: package org.netbeans.modules.vmd.midpnb.screen.display;
043:
044: import org.netbeans.modules.vmd.api.model.DesignComponent;
045: import org.netbeans.modules.vmd.api.screen.display.ScreenDeviceInfo;
046: import org.netbeans.modules.vmd.midp.components.MidpTypes;
047: import org.netbeans.modules.vmd.midp.components.resources.ImageCD;
048: import org.netbeans.modules.vmd.midp.screen.display.DisplayableDisplayPresenter;
049: import org.netbeans.modules.vmd.midp.screen.display.ScreenSupport;
050: import org.netbeans.modules.vmd.midpnb.components.displayables.AbstractInfoScreenCD;
051: import org.openide.util.Utilities;
052: import javax.swing.*;
053: import java.awt.*;
054: import java.util.ArrayList;
055: import java.util.Collection;
056: import org.netbeans.modules.vmd.api.model.PropertyValue;
057: import org.netbeans.modules.vmd.api.screen.display.ScreenPropertyDescriptor;
058: import org.netbeans.modules.vmd.midp.screen.display.ScreenFileObjectListener;
059: import org.netbeans.modules.vmd.midp.screen.display.property.ResourcePropertyEditor;
060: import org.netbeans.modules.vmd.midp.screen.display.property.ScreenStringPropertyEditor;
061: import org.openide.filesystems.FileObject;
062: import org.openide.util.NbBundle;
063:
064: /**
065: *
066: * @author Anton Chechel
067: */
068: public class AbstractInfoDisplayPresenter extends
069: DisplayableDisplayPresenter {
070:
071: private static final String ICON_BROKEN_PATH = "org/netbeans/modules/vmd/midpnb/resources/broken-image.png"; // NOI18N
072: private static final Icon ICON_BROKEN = new ImageIcon(Utilities
073: .loadImage(ICON_BROKEN_PATH));
074: private JLabel imageLabel;
075: private JLabel stringLabel;
076: private ScreenFileObjectListener imageFileListener;
077: private FileObject imageFileObject;
078:
079: public AbstractInfoDisplayPresenter() {
080: imageLabel = new JLabel();
081: imageLabel.setHorizontalAlignment(JLabel.CENTER);
082: stringLabel = new JLabel();
083: stringLabel.setHorizontalAlignment(JLabel.CENTER);
084: JPanel contentPanel = getPanel().getContentPanel();
085: contentPanel.setLayout(new GridBagLayout());
086:
087: GridBagConstraints constraints = new GridBagConstraints();
088: constraints.weightx = 1.0;
089: constraints.weighty = 1.0;
090: constraints.insets = new Insets(2, 2, 2, 2);
091: constraints.fill = GridBagConstraints.HORIZONTAL;
092: constraints.gridx = GridBagConstraints.REMAINDER;
093: constraints.gridy = GridBagConstraints.RELATIVE;
094: constraints.anchor = GridBagConstraints.CENTER;
095: contentPanel.add(imageLabel, constraints);
096:
097: constraints.anchor = GridBagConstraints.NORTHWEST;
098: contentPanel.add(stringLabel, constraints);
099: }
100:
101: @Override
102: public void reload(ScreenDeviceInfo deviceInfo) {
103: super .reload(deviceInfo);
104:
105: PropertyValue value = getComponent().readProperty(
106: AbstractInfoScreenCD.PROP_IMAGE);
107: if (!PropertyValue.Kind.USERCODE.equals(value.getKind())) {
108: DesignComponent imageComponent = value.getComponent();
109: String path = null;
110: if (imageComponent != null) {
111: value = imageComponent
112: .readProperty(ImageCD.PROP_RESOURCE_PATH);
113: if (!PropertyValue.Kind.USERCODE
114: .equals(value.getKind())) {
115: path = MidpTypes.getString(value);
116: }
117: }
118:
119: Icon icon = ScreenSupport
120: .getIconFromImageComponent(imageComponent);
121: imageFileObject = ScreenSupport
122: .getFileObjectFromImageComponent(imageComponent);
123: if (imageFileObject != null) {
124: imageFileObject
125: .removeFileChangeListener(imageFileListener);
126: imageFileListener = new ScreenFileObjectListener(
127: getRelatedComponent(), imageComponent,
128: ImageCD.PROP_RESOURCE_PATH);
129: imageFileObject
130: .addFileChangeListener(imageFileListener);
131: }
132: if (icon != null) {
133: imageLabel.setText(null);
134: imageLabel.setIcon(icon);
135: } else if (path != null) {
136: imageLabel.setText(path);
137: imageLabel.setIcon(ICON_BROKEN);
138: } else {
139: imageLabel.setIcon(null);
140: imageLabel.setText(NbBundle.getMessage(
141: AbstractInfoDisplayPresenter.class,
142: "DISP_image_not_specified")); // NOI18N
143: }
144: } else {
145: imageLabel.setText(NbBundle.getMessage(
146: AbstractInfoDisplayPresenter.class,
147: "DISP_image_is_usercode")); // NOI18N
148: }
149:
150: value = getComponent().readProperty(
151: AbstractInfoScreenCD.PROP_TEXT);
152: if (!PropertyValue.Kind.USERCODE.equals(value.getKind())) {
153: String text = MidpTypes.getString(getComponent()
154: .readProperty(AbstractInfoScreenCD.PROP_TEXT));
155: if (text == null) {
156: stringLabel.setText(NbBundle.getMessage(
157: AbstractInfoDisplayPresenter.class,
158: "DISP_text_not_specified")); // NOI18N
159: } else if (text.length() == 0) {
160: stringLabel.setText(NbBundle.getMessage(
161: AbstractInfoDisplayPresenter.class,
162: "DISP_text_is_empty")); // NOI18N
163: } else {
164: stringLabel.setText(text);
165: value = getComponent().readProperty(
166: AbstractInfoScreenCD.PROP_TEXT_FONT);
167: if (!PropertyValue.Kind.USERCODE
168: .equals(value.getKind())) {
169: DesignComponent font = value.getComponent();
170: stringLabel.setFont(ScreenSupport.getFont(
171: deviceInfo, font));
172: }
173: }
174: } else {
175: stringLabel.setText(NbBundle.getMessage(
176: AbstractInfoDisplayPresenter.class,
177: "DISP_text_is_usercode")); // NOI18N
178: }
179: }
180:
181: @Override
182: public Collection<ScreenPropertyDescriptor> getPropertyDescriptors() {
183: ArrayList<ScreenPropertyDescriptor> descriptors = new ArrayList<ScreenPropertyDescriptor>(
184: super .getPropertyDescriptors());
185: ResourcePropertyEditor imagePropertyEditor = new ResourcePropertyEditor(
186: AbstractInfoScreenCD.PROP_IMAGE, getComponent());
187: descriptors.add(new ScreenPropertyDescriptor(getComponent(),
188: imageLabel, imagePropertyEditor));
189: descriptors.add(new ScreenPropertyDescriptor(getComponent(),
190: stringLabel, new ScreenStringPropertyEditor(
191: AbstractInfoScreenCD.PROP_TEXT)));
192: return descriptors;
193: }
194:
195: @Override
196: protected void notifyDetached(DesignComponent component) {
197: if (imageFileObject != null && imageFileListener != null) {
198: imageFileObject.removeFileChangeListener(imageFileListener);
199: }
200: imageFileObject = null;
201: imageFileListener = null;
202: }
203: }
|