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.midp.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.displayables.AlertCD;
048: import org.netbeans.modules.vmd.midp.components.resources.ImageCD;
049: import org.openide.util.Utilities;
050: import javax.swing.*;
051: import java.awt.*;
052: import java.util.ArrayList;
053: import java.util.Collection;
054: import org.netbeans.modules.vmd.api.model.PropertyValue;
055: import org.netbeans.modules.vmd.api.screen.display.ScreenPropertyDescriptor;
056: import org.netbeans.modules.vmd.midp.components.items.GaugeCD;
057: import org.netbeans.modules.vmd.midp.screen.display.property.ResourcePropertyEditor;
058: import org.netbeans.modules.vmd.midp.screen.display.property.ScreenStringPropertyEditor;
059: import org.openide.filesystems.FileObject;
060: import org.openide.util.NbBundle;
061:
062: /**
063: *
064: * @author Anton Chechel
065: */
066: public class AlertDisplayPresenter extends DisplayableDisplayPresenter {
067:
068: private static final String ICON_BROKEN_PATH = "org/netbeans/modules/vmd/midp/resources/screen/broken-image.png"; // NOI18N
069: private static final Icon ICON_BROKEN = new ImageIcon(Utilities
070: .loadImage(ICON_BROKEN_PATH));
071: private JLabel imageLabel;
072: private JLabel stringLabel;
073: private ScreenFileObjectListener imageFileListener;
074: private FileObject imageFileObject;
075: private GaugeDisplayPresenterElement gauge;
076: private JPanel panel;
077: private GridBagConstraints constraints;
078:
079: public AlertDisplayPresenter() {
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: 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: AlertCD.PROP_STRING);
107: if (!PropertyValue.Kind.USERCODE.equals(value.getKind())) {
108: String text = MidpTypes.getString(value);
109: if (text == null) {
110: stringLabel.setText(NbBundle.getMessage(
111: AlertDisplayPresenter.class,
112: "DISP_text_not_specified")); // NOI18N
113: } else if (text.length() == 0) {
114: stringLabel.setText(NbBundle.getMessage(
115: AlertDisplayPresenter.class,
116: "DISP_text_is_empty")); // NOI18N
117: } else {
118: stringLabel.setText(text);
119: }
120: } else {
121: stringLabel.setText(NbBundle.getMessage(
122: AlertDisplayPresenter.class,
123: "DISP_text_is_usercode")); // NOI18N
124: }
125:
126: value = getComponent().readProperty(AlertCD.PROP_IMAGE);
127: if (!PropertyValue.Kind.USERCODE.equals(value.getKind())) {
128: DesignComponent imageComponent = value.getComponent();
129: String path = null;
130: if (imageComponent != null) {
131: path = (String) imageComponent.readProperty(
132: ImageCD.PROP_RESOURCE_PATH).getPrimitiveValue();
133: }
134: Icon icon = ScreenSupport
135: .getIconFromImageComponent(imageComponent);
136: imageFileObject = ScreenSupport
137: .getFileObjectFromImageComponent(imageComponent);
138: if (imageFileObject != null) {
139: imageLabel.setText(null);
140: imageFileObject
141: .removeFileChangeListener(imageFileListener);
142: imageFileListener = new ScreenFileObjectListener(
143: getRelatedComponent(), imageComponent,
144: ImageCD.PROP_RESOURCE_PATH);
145: imageFileObject
146: .addFileChangeListener(imageFileListener);
147: }
148: if (icon != null) {
149: imageLabel.setIcon(icon);
150: } else if (path != null) {
151: imageLabel.setIcon(ICON_BROKEN);
152: } else {
153: imageLabel.setIcon(null);
154: imageLabel.setText(NbBundle.getMessage(
155: AlertDisplayPresenter.class,
156: "DISP_image_not_specified")); // NOI18N
157: }
158: } else {
159: imageLabel.setText(NbBundle.getMessage(
160: AlertDisplayPresenter.class,
161: "DISP_image_is_usercode")); // NOI18N
162: }
163:
164: DesignComponent indicator = getComponent().readProperty(
165: AlertCD.PROP_INDICATOR).getComponent();
166: if (indicator != null) {
167: gauge = new GaugeDisplayPresenterElement();
168: if (panel == null) {
169: panel = new JPanel() {
170:
171: @Override
172: public void paint(Graphics g) {
173: super .paint(g);
174: gauge.setPanel(this );
175: gauge.paintGauge(g);
176: }
177: };
178: panel.setOpaque(false);
179: panel.setPreferredSize(new Dimension(200, 40)); // TODO compute it from fontSize
180: panel.repaint();
181: panel.revalidate();
182:
183: constraints.anchor = GridBagConstraints.CENTER;
184: getPanel().getContentPanel().add(panel, constraints);
185: }
186: gauge.setSize(panel.getSize());
187: gauge.setInteractive(MidpTypes.getBoolean(indicator
188: .readProperty(GaugeCD.PROP_INTERACTIVE)));
189: int maxValue = MidpTypes.getInteger(indicator
190: .readProperty(GaugeCD.PROP_MAX_VALUE));
191: if (maxValue < 0) {
192: maxValue = 1;
193: }
194: gauge.setMaxValue(maxValue);
195: int intValue = MidpTypes.getInteger(indicator
196: .readProperty(GaugeCD.PROP_VALUE));
197: if (intValue < 0) {
198: intValue = 0;
199: } else if (intValue > maxValue) {
200: intValue = maxValue;
201: }
202: gauge.setValue(intValue);
203: panel.repaint();
204: } else if (panel != null) {
205: getPanel().getContentPanel().remove(panel);
206: panel = null;
207: gauge = null;
208: }
209: }
210:
211: @Override
212: public Collection<ScreenPropertyDescriptor> getPropertyDescriptors() {
213: ArrayList<ScreenPropertyDescriptor> descriptors = new ArrayList<ScreenPropertyDescriptor>(
214: super .getPropertyDescriptors());
215: ResourcePropertyEditor imagePropertyEditor = new ResourcePropertyEditor(
216: AlertCD.PROP_IMAGE, getComponent());
217: descriptors.add(new ScreenPropertyDescriptor(getComponent(),
218: imageLabel, imagePropertyEditor));
219: descriptors.add(new ScreenPropertyDescriptor(getComponent(),
220: stringLabel, new ScreenStringPropertyEditor(
221: AlertCD.PROP_STRING)));
222: DesignComponent indicator = getComponent().readProperty(
223: AlertCD.PROP_INDICATOR).getComponent();
224: ResourcePropertyEditor gaugePropertyEditor = new ResourcePropertyEditor(
225: GaugeCD.PROP_VALUE, indicator);
226: if (indicator != null) {
227: descriptors.add(new ScreenPropertyDescriptor(indicator,
228: panel, gaugePropertyEditor));
229: }
230: return descriptors;
231: }
232:
233: @Override
234: protected void notifyDetached(DesignComponent component) {
235: if (imageFileObject != null && imageFileListener != null) {
236: imageFileObject.removeFileChangeListener(imageFileListener);
237: }
238: imageFileObject = null;
239: imageFileListener = null;
240: }
241: }
|