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: package org.netbeans.modules.vmd.midp.screen.display;
042:
043: import org.netbeans.modules.vmd.api.model.DesignComponent;
044: import org.netbeans.modules.vmd.api.model.common.AcceptSuggestion;
045: import org.netbeans.modules.vmd.api.model.presenters.actions.ActionsSupport;
046: import org.netbeans.modules.vmd.api.screen.display.ScreenDeviceInfo;
047: import org.netbeans.modules.vmd.api.screen.display.ScreenDisplayDataFlavorSupport;
048: import org.netbeans.modules.vmd.api.screen.display.ScreenDisplayPresenter;
049: import org.netbeans.modules.vmd.api.screen.display.ScreenPropertyDescriptor;
050: import org.netbeans.modules.vmd.midp.components.MidpValueSupport;
051: import org.netbeans.modules.vmd.midp.components.items.ItemCD;
052: import org.netbeans.modules.vmd.midp.screen.display.property.ScreenStringPropertyEditor;
053: import org.openide.util.Utilities;
054: import org.openide.util.Exceptions;
055:
056: import javax.swing.*;
057: import java.awt.*;
058: import java.awt.datatransfer.Transferable;
059: import java.awt.datatransfer.UnsupportedFlavorException;
060: import java.io.IOException;
061: import java.util.Collection;
062: import java.util.Collections;
063:
064: /**
065: * @author David Kaspar
066: */
067: public class ItemDisplayPresenter extends ScreenDisplayPresenter {
068:
069: private JPanel panel;
070: private JLabel label;
071: private JComponent contentComponent;
072:
073: public ItemDisplayPresenter() {
074: panel = new JPanel() {
075: @Override
076: public JPopupMenu getComponentPopupMenu() {
077: return Utilities.actionsToPopup(ActionsSupport
078: .createActionsArray(getRelatedComponent()),
079: this );
080: }
081: };
082: panel.setLayout(new GridBagLayout());
083: panel.setOpaque(false);
084:
085: label = new JLabel();
086: Font bold = label.getFont().deriveFont(Font.BOLD);
087: label.setFont(bold);
088:
089: GridBagConstraints constraints = new GridBagConstraints();
090: constraints.weightx = 1.0;
091: constraints.weighty = 0.0;
092: constraints.insets = new Insets(0, 0, 0, 0);
093: constraints.fill = GridBagConstraints.HORIZONTAL;
094: constraints.gridx = GridBagConstraints.REMAINDER;
095: constraints.gridy = GridBagConstraints.RELATIVE;
096: constraints.anchor = GridBagConstraints.NORTH;
097: panel.add(label, constraints);
098: }
099:
100: public boolean isTopLevelDisplay() {
101: return false;
102: }
103:
104: public Collection<DesignComponent> getChildren() {
105: return Collections.emptyList();
106: }
107:
108: public JComponent getView() {
109: return panel;
110: }
111:
112: protected final JPanel getViewPanel() {
113: return panel;
114: }
115:
116: protected final void setContentComponent(JComponent contentComponent) {
117: panel.setVisible(false);
118: if (this .contentComponent != null) {
119: panel.remove(this .contentComponent);
120: }
121: this .contentComponent = contentComponent;
122: if (contentComponent != null) {
123: GridBagConstraints constraints = new GridBagConstraints();
124: constraints.weightx = 1.0;
125: constraints.weighty = 1.0;
126: constraints.insets = new Insets(0, 0, 0, 0);
127: constraints.fill = GridBagConstraints.BOTH;
128: constraints.gridx = GridBagConstraints.REMAINDER;
129: constraints.gridy = GridBagConstraints.RELATIVE;
130: constraints.anchor = GridBagConstraints.NORTHEAST;
131:
132: panel.add(contentComponent, constraints);
133: }
134: panel.setVisible(true);
135: panel.invalidate();
136: panel.validate();
137: panel.repaint();
138: }
139:
140: public void reload(ScreenDeviceInfo deviceInfo) {
141: String text = MidpValueSupport
142: .getHumanReadableString(getComponent().readProperty(
143: ItemCD.PROP_LABEL));
144: label.setText(text);
145: }
146:
147: public Shape getSelectionShape() {
148: return new Rectangle(panel.getSize());
149: }
150:
151: public Collection<ScreenPropertyDescriptor> getPropertyDescriptors() {
152: return Collections.singleton(new ScreenPropertyDescriptor(
153: getComponent(), label, new ScreenStringPropertyEditor(
154: ItemCD.PROP_LABEL)));
155: }
156:
157: @Override
158: public boolean isDraggable() {
159: return true;
160: }
161:
162: @Override
163: public AcceptSuggestion createSuggestion(Transferable transferable) {
164: if (!(transferable
165: .isDataFlavorSupported(ScreenDisplayDataFlavorSupport.HORIZONTAL_POSITION_DATA_FLAVOR)))
166: return null;
167: if (!(transferable
168: .isDataFlavorSupported(ScreenDisplayDataFlavorSupport.VERTICAL_POSITION_DATA_FLAVOR)))
169: return null;
170:
171: ScreenDeviceInfo.Edge horizontalPosition = null;
172: ScreenDeviceInfo.Edge verticalPosition = null;
173:
174: try {
175: horizontalPosition = (ScreenDeviceInfo.Edge) transferable
176: .getTransferData(ScreenDisplayDataFlavorSupport.HORIZONTAL_POSITION_DATA_FLAVOR);
177: verticalPosition = (ScreenDeviceInfo.Edge) transferable
178: .getTransferData(ScreenDisplayDataFlavorSupport.VERTICAL_POSITION_DATA_FLAVOR);
179: } catch (UnsupportedFlavorException ex) {
180: Exceptions.printStackTrace(ex);
181: } catch (IOException ex) {
182: Exceptions.printStackTrace(ex);
183: }
184: return new ScreenMoveArrayAcceptSuggestion(horizontalPosition,
185: verticalPosition);
186: }
187:
188: }
|