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.model.PropertyValue;
046: import org.netbeans.modules.vmd.api.model.common.AcceptSuggestion;
047: import org.netbeans.modules.vmd.api.screen.display.ScreenDeviceInfo;
048: import org.netbeans.modules.vmd.api.screen.display.ScreenDisplayDataFlavorSupport;
049: import org.netbeans.modules.vmd.api.screen.display.ScreenDisplayPresenter;
050: import org.netbeans.modules.vmd.api.screen.display.ScreenPropertyDescriptor;
051: import org.netbeans.modules.vmd.midp.components.MidpTypes;
052: import org.netbeans.modules.vmd.midp.components.MidpValueSupport;
053: import org.netbeans.modules.vmd.midp.components.elements.ChoiceElementCD;
054: import org.netbeans.modules.vmd.midp.components.items.ChoiceGroupCD;
055: import org.netbeans.modules.vmd.midp.components.items.ChoiceSupport;
056: import org.netbeans.modules.vmd.midp.components.resources.ImageCD;
057: import org.netbeans.modules.vmd.midp.screen.display.property.ScreenBooleanPropertyEditor;
058: import org.netbeans.modules.vmd.midp.screen.display.property.ScreenStringPropertyEditor;
059: import org.openide.util.Exceptions;
060: import org.openide.util.Utilities;
061: import javax.swing.*;
062: import java.awt.*;
063: import java.awt.datatransfer.Transferable;
064: import java.awt.datatransfer.UnsupportedFlavorException;
065: import java.io.IOException;
066: import java.util.Arrays;
067: import java.util.Collection;
068: import java.util.Collections;
069: import org.openide.filesystems.FileObject;
070:
071: /**
072: *
073: * @author Anton Chechel
074: * @version 1.0
075: */
076: public class ChoiceElementDisplayPresenter extends
077: ScreenDisplayPresenter {
078:
079: public static final String ICON_EMPTY_CHECKBOX_PATH = "org/netbeans/modules/vmd/midp/resources/screen/unchecked.png"; // NOI18N
080: public static final String ICON_CHECKBOX_PATH = "org/netbeans/modules/vmd/midp/resources/screen/checked.png"; // NOI18N
081: public static final String ICON_EMPTY_RADIOBUTTON_PATH = "org/netbeans/modules/vmd/midp/resources/screen/radio-unchecked.png"; // NOI18N
082: public static final String ICON_RADIOBUTTON_PATH = "org/netbeans/modules/vmd/midp/resources/screen/radio-checked.png"; // NOI18N
083: public static final String ICON_POPUP_PATH = "org/netbeans/modules/vmd/midp/resources/screen/drop-down.png"; // NOI18N
084: public static final String ICON_BROKEN_PATH = "org/netbeans/modules/vmd/midp/resources/screen/broken-image.png"; // NOI18N
085:
086: public static final Icon ICON_EMPTY_CHECKBOX = new ImageIcon(
087: Utilities.loadImage(ICON_EMPTY_CHECKBOX_PATH));
088: public static final Icon ICON_CHECKBOX = new ImageIcon(Utilities
089: .loadImage(ICON_CHECKBOX_PATH));
090: public static final Icon ICON_EMPTY_RADIOBUTTON = new ImageIcon(
091: Utilities.loadImage(ICON_EMPTY_RADIOBUTTON_PATH));
092: public static final Icon ICON_RADIOBUTTON = new ImageIcon(Utilities
093: .loadImage(ICON_RADIOBUTTON_PATH));
094: public static final Icon ICON_POPUP = new ImageIcon(Utilities
095: .loadImage(ICON_POPUP_PATH));
096: public static final Icon ICON_BROKEN = new ImageIcon(Utilities
097: .loadImage(ICON_BROKEN_PATH));
098:
099: private JPanel view;
100: private JLabel state;
101: private JLabel image;
102: private JLabel label;
103: private ScreenFileObjectListener imageFileListener;
104: private FileObject imageFileObject;
105:
106: public ChoiceElementDisplayPresenter() {
107: view = new JPanel();
108: view.setLayout(new BoxLayout(view, BoxLayout.X_AXIS));
109: view.setOpaque(false);
110:
111: state = new JLabel();
112: view.add(state);
113: image = new JLabel();
114: view.add(image);
115: label = new JLabel();
116: view.add(label);
117:
118: view.add(Box.createHorizontalGlue());
119: }
120:
121: public boolean isTopLevelDisplay() {
122: return false;
123: }
124:
125: public Collection<DesignComponent> getChildren() {
126: return Collections.emptyList();
127: }
128:
129: public JComponent getView() {
130: return view;
131: }
132:
133: public void reload(ScreenDeviceInfo deviceInfo) {
134: PropertyValue value = getComponent().getParentComponent()
135: .readProperty(ChoiceGroupCD.PROP_CHOICE_TYPE);
136: int type;
137: if (!PropertyValue.Kind.USERCODE.equals(value.getKind())) {
138: type = MidpTypes.getInteger(value);
139: } else {
140: type = ChoiceSupport.VALUE_EXCLUSIVE;
141: }
142:
143: PropertyValue selectedValue = getComponent().readProperty(
144: ChoiceElementCD.PROP_SELECTED);
145: boolean selected = selectedValue.getKind() == PropertyValue.Kind.VALUE
146: && MidpTypes.getBoolean(selectedValue);
147: switch (type) {
148: case ChoiceSupport.VALUE_EXCLUSIVE:
149: state
150: .setIcon(selected ? ChoiceElementDisplayPresenter.ICON_RADIOBUTTON
151: : ChoiceElementDisplayPresenter.ICON_EMPTY_RADIOBUTTON);
152: break;
153: case ChoiceSupport.VALUE_MULTIPLE:
154: state
155: .setIcon(selected ? ChoiceElementDisplayPresenter.ICON_CHECKBOX
156: : ChoiceElementDisplayPresenter.ICON_EMPTY_CHECKBOX);
157: break;
158: case ChoiceSupport.VALUE_POPUP:
159: state.setIcon(ChoiceElementDisplayPresenter.ICON_POPUP);
160: break;
161: default:
162: state.setIcon(null);
163: break;
164: }
165:
166: Icon icon = null;
167: String path = null;
168: value = getComponent().readProperty(ChoiceElementCD.PROP_IMAGE);
169: if (!PropertyValue.Kind.USERCODE.equals(value.getKind())) {
170: DesignComponent imageComponent = value.getComponent();
171: if (imageComponent != null) {
172: path = (String) imageComponent.readProperty(
173: ImageCD.PROP_RESOURCE_PATH).getPrimitiveValue();
174: }
175: icon = ScreenSupport
176: .getIconFromImageComponent(imageComponent);
177: imageFileObject = ScreenSupport
178: .getFileObjectFromImageComponent(imageComponent);
179: if (imageFileObject != null) {
180: imageFileObject
181: .removeFileChangeListener(imageFileListener);
182: imageFileListener = new ScreenFileObjectListener(
183: getRelatedComponent(), imageComponent,
184: ImageCD.PROP_RESOURCE_PATH);
185: imageFileObject
186: .addFileChangeListener(imageFileListener);
187: }
188: }
189: if (icon != null) {
190: image.setIcon(icon);
191: } else if (path != null) {
192: image.setIcon(ICON_BROKEN);
193: } else {
194: image.setIcon(null);
195: }
196:
197: String text = MidpValueSupport
198: .getHumanReadableString(getComponent().readProperty(
199: ChoiceElementCD.PROP_STRING));
200: label.setText(text);
201:
202: value = getComponent().readProperty(ChoiceElementCD.PROP_FONT);
203: if (!PropertyValue.Kind.USERCODE.equals(value.getKind())) {
204: DesignComponent font = value.getComponent();
205: label.setFont(ScreenSupport.getFont(deviceInfo, font));
206: }
207: }
208:
209: public Shape getSelectionShape() {
210: return new Rectangle(view.getSize());
211: }
212:
213: public Collection<ScreenPropertyDescriptor> getPropertyDescriptors() {
214: return Arrays.asList(new ScreenPropertyDescriptor(
215: getComponent(), state, new ScreenBooleanPropertyEditor(
216: ChoiceElementCD.PROP_SELECTED)),
217: new ScreenPropertyDescriptor(getComponent(), label,
218: new ScreenStringPropertyEditor(
219: ChoiceElementCD.PROP_STRING)));
220: }
221:
222: @Override
223: public boolean isDraggable() {
224: return true;
225: }
226:
227: @Override
228: public AcceptSuggestion createSuggestion(Transferable transferable) {
229: if (!(transferable
230: .isDataFlavorSupported(ScreenDisplayDataFlavorSupport.HORIZONTAL_POSITION_DATA_FLAVOR))) {
231: return null;
232: }
233: if (!(transferable
234: .isDataFlavorSupported(ScreenDisplayDataFlavorSupport.VERTICAL_POSITION_DATA_FLAVOR))) {
235: return null;
236: }
237: ScreenDeviceInfo.Edge horizontalPosition = null;
238: ScreenDeviceInfo.Edge verticalPosition = null;
239:
240: try {
241: horizontalPosition = (ScreenDeviceInfo.Edge) transferable
242: .getTransferData(ScreenDisplayDataFlavorSupport.HORIZONTAL_POSITION_DATA_FLAVOR);
243: verticalPosition = (ScreenDeviceInfo.Edge) transferable
244: .getTransferData(ScreenDisplayDataFlavorSupport.VERTICAL_POSITION_DATA_FLAVOR);
245: } catch (UnsupportedFlavorException ex) {
246: Exceptions.printStackTrace(ex);
247: } catch (IOException ex) {
248: Exceptions.printStackTrace(ex);
249: }
250: return new ScreenMoveArrayAcceptSuggestion(horizontalPosition,
251: verticalPosition);
252: }
253:
254: @Override
255: protected void notifyDetached(DesignComponent component) {
256: if (imageFileObject != null && imageFileListener != null) {
257: imageFileObject.removeFileChangeListener(imageFileListener);
258: }
259: imageFileObject = null;
260: imageFileListener = null;
261: }
262: }
|