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.screen.device;
042:
043: import org.netbeans.modules.vmd.api.model.DesignComponent;
044: import org.netbeans.modules.vmd.api.model.DesignDocument;
045: import org.netbeans.modules.vmd.api.screen.display.DeviceTheme;
046: import org.netbeans.modules.vmd.api.screen.display.ScreenDeviceInfo;
047: import org.netbeans.modules.vmd.api.screen.display.ScreenDeviceInfoPresenter;
048: import org.netbeans.modules.vmd.api.screen.display.ScreenDisplayPresenter;
049: import org.netbeans.modules.vmd.screen.ScreenAccessController;
050:
051: import javax.swing.*;
052: import java.awt.*;
053:
054: /**
055: * @author David Kaspar
056: */
057: public class DevicePanel extends JPanel {
058:
059: private static final Color BACKGROUND_COLOR = new Color(0xFBF9F3);
060:
061: private final ScreenDisplayPresenter dummyPresenter = new DummyDisplayPresenter();
062:
063: private ScreenAccessController controller;
064: //private ScreenDeviceInfo deviceInfo;
065:
066: private JPanel displayPanel;
067: private TopPanel topPanel;
068:
069: public DevicePanel(ScreenAccessController controller) {
070: this .controller = controller;
071: setBackground(BACKGROUND_COLOR);
072:
073: topPanel = new TopPanel(this );
074:
075: displayPanel = new JPanel();
076: displayPanel.setLayout(new BorderLayout());
077: displayPanel.setBackground(BACKGROUND_COLOR);
078:
079: initializeUI();
080: }
081:
082: private ScreenDeviceInfo getDeviceInfo() {
083: final ScreenDeviceInfo[] screenDevice = new ScreenDeviceInfo[1];
084: final DesignDocument document = controller.getDocument();
085: if (document == null)
086: return null;
087: document.getTransactionManager().readAccess(new Runnable() {
088: public void run() {
089: DesignComponent rootComponent = document
090: .getRootComponent();
091: ScreenDeviceInfoPresenter presenter = rootComponent
092: .getPresenter(ScreenDeviceInfoPresenter.class);
093: assert (presenter != null) : "No ScreenDevice attached to the root component"; //NOI18N
094: screenDevice[0] = presenter.getScreenDeviceInfo();
095: }
096: });
097: return screenDevice[0];
098: }
099:
100: public ScreenAccessController getController() {
101: return controller;
102: }
103:
104: private void initializeUI() {
105: setLayout(new GridBagLayout());
106:
107: GridBagConstraints constraints = new GridBagConstraints();
108: constraints.gridx = 0;
109: constraints.gridy = 0;
110: constraints.gridwidth = 3;
111: constraints.gridheight = 3;
112: constraints.fill = GridBagConstraints.BOTH;
113: add(topPanel, constraints);
114:
115: constraints = new GridBagConstraints();
116: constraints.gridx = 0;
117: constraints.gridy = 0;
118: constraints.fill = GridBagConstraints.NONE;
119: add(getDeviceInfo().getDeviceBorder(
120: ScreenDeviceInfo.Edge.TOP_LEFT), constraints);
121:
122: constraints = new GridBagConstraints();
123: constraints.gridx = 1;
124: constraints.gridy = 0;
125: constraints.fill = GridBagConstraints.HORIZONTAL;
126: add(getDeviceInfo().getDeviceBorder(ScreenDeviceInfo.Edge.TOP),
127: constraints);
128:
129: constraints = new GridBagConstraints();
130: constraints.gridx = 2;
131: constraints.gridy = 0;
132: constraints.fill = GridBagConstraints.NONE;
133: add(getDeviceInfo().getDeviceBorder(
134: ScreenDeviceInfo.Edge.TOP_RIGHT), constraints);
135:
136: constraints = new GridBagConstraints();
137: constraints.gridx = 0;
138: constraints.gridy = 1;
139: constraints.fill = GridBagConstraints.VERTICAL;
140: //constraints.weighty = 1.0;
141: add(
142: getDeviceInfo().getDeviceBorder(
143: ScreenDeviceInfo.Edge.LEFT), constraints);
144:
145: constraints = new GridBagConstraints();
146: constraints.gridx = 1;
147: constraints.gridy = 1;
148: constraints.fill = GridBagConstraints.BOTH;
149: constraints.weightx = 1.0;
150: add(displayPanel, constraints);
151:
152: constraints = new GridBagConstraints();
153: constraints.gridx = 2;
154: constraints.gridy = 1;
155: constraints.fill = GridBagConstraints.VERTICAL;
156: add(getDeviceInfo()
157: .getDeviceBorder(ScreenDeviceInfo.Edge.RIGHT),
158: constraints);
159:
160: constraints = new GridBagConstraints();
161: constraints.gridx = 0;
162: constraints.gridy = 2;
163: constraints.fill = GridBagConstraints.NONE;
164: //constraints.weighty = 0.0;
165: add(getDeviceInfo().getDeviceBorder(
166: ScreenDeviceInfo.Edge.BOTTOM_LEFT), constraints);
167:
168: constraints = new GridBagConstraints();
169: constraints.gridx = 1;
170: constraints.gridy = 2;
171: constraints.fill = GridBagConstraints.HORIZONTAL;
172: add(getDeviceInfo().getDeviceBorder(
173: ScreenDeviceInfo.Edge.BOTTOM), constraints);
174:
175: constraints = new GridBagConstraints();
176: constraints.gridx = 2;
177: constraints.gridy = 2;
178: constraints.fill = GridBagConstraints.NONE;
179: add(getDeviceInfo().getDeviceBorder(
180: ScreenDeviceInfo.Edge.BOTTOM_RIGHT), constraints);
181:
182: JPanel fillPanel = new JPanel();
183: fillPanel.setBackground(BACKGROUND_COLOR);
184: constraints.gridx = 0;
185: constraints.gridy = GridBagConstraints.RELATIVE;
186: constraints.weighty = 1.0;
187: add(fillPanel, constraints);
188: }
189:
190: public void reload() {
191: DesignComponent editedScreen = controller.getEditedScreen();
192: ScreenDisplayPresenter presenter = editedScreen != null ? editedScreen
193: .getPresenter(ScreenDisplayPresenter.class)
194: : null;
195: if (presenter == null)
196: presenter = dummyPresenter;
197: displayPanel.setVisible(false);
198: displayPanel.removeAll();
199: displayPanel.setPreferredSize(null);
200:
201: JComponent comp = presenter.getView();
202: displayPanel.add(comp, BorderLayout.CENTER);
203: displayPanel.setBackground(getDeviceInfo().getDeviceTheme()
204: .getColor(DeviceTheme.Colors.BACKGROUND));
205:
206: presenter.reload(getDeviceInfo());
207:
208: //due to issues in GridBagLayout which ignores minSize, we need to compute necessary height for component
209: int requiredHeight = 0;
210: Component[] content = comp.getComponents();
211: for (Component jComponent : content) {
212: requiredHeight += jComponent.getPreferredSize().getHeight();
213: // GridBagConstraints constrains = ((GridBagLayout)comp.getLayout()).getConstraints(jComponent);
214: // requiredHeight += constrains.insets.top;
215: // requiredHeight += constrains.insets.bottom;
216: }
217: Dimension size = getDeviceInfo().getCurrentScreenSize();
218: displayPanel.setMinimumSize(size);
219: //if the size of component is less than required size, force it. Otherwise force the size to computed one
220: if (size.height >= requiredHeight) {
221: displayPanel.setPreferredSize(size);
222: } else {
223: displayPanel.setPreferredSize(new Dimension(size.width,
224: requiredHeight));
225: }
226: displayPanel.setMaximumSize(new Dimension(size.width,
227: Integer.MAX_VALUE));
228:
229: displayPanel.setVisible(true);
230: displayPanel.validate();
231:
232: topPanel.reload();
233: }
234:
235: public DesignComponent getDesignComponentAt(Point point) {
236: return getDesignComponentAt(controller.getEditedScreen(), this ,
237: point);
238: }
239:
240: private static DesignComponent getDesignComponentAt(
241: DesignComponent component, JComponent parentView,
242: Point point) {
243: if (component == null)
244: return null;
245: ScreenDisplayPresenter presenter = component
246: .getPresenter(ScreenDisplayPresenter.class);
247: if (presenter == null)
248: return null;
249: JComponent view = presenter.getView();
250: Component c = view;
251: Point viewPoint = new Point(point);
252: for (;;) {
253: if (c == null)
254: return null;
255: if (c == parentView)
256: break;
257: Point childPoint = c.getLocation();
258: viewPoint.x -= childPoint.x;
259: viewPoint.y -= childPoint.y;
260: c = c.getParent();
261: }
262: for (DesignComponent child : presenter.getChildren()) {
263: DesignComponent ret = getDesignComponentAt(child, view,
264: viewPoint);
265: if (ret != null)
266: return ret;
267: }
268: return presenter.getSelectionShape().contains(viewPoint) ? presenter
269: .getRelatedComponent()
270: : null;
271: }
272:
273: public Point calculateTranslation(Container view) {
274: Point point = new Point();
275: for (;;) {
276: if (view == null)
277: return null;
278: if (view == this )
279: break;
280: Point childPoint = view.getLocation();
281: point.x += childPoint.x;
282: point.y += childPoint.y;
283: view = view.getParent();
284: }
285: return point;
286: }
287:
288: // /**
289: // * Helper debugging method for inspecting component's hiearchy
290: // */
291: // private void dump(JComponent component){
292: // System.out.println("Type " + component.getClass() + " Layout " + component.getLayout() + " Size: " + component.getSize() + " Preferred size: " + component.getPreferredSize());
293: // Component comps[] = component.getComponents();
294: // for (int i = 0; i < comps.length; i++) {
295: // Component component1 = comps[i];
296: // dump((JComponent)component1);
297: // }
298: // }
299:
300: public void setScreenSize(Dimension deviceScreenSize) {
301: ScreenDeviceInfo deviceInfo = getDeviceInfo();
302: if (deviceScreenSize == null)
303: deviceScreenSize = new Dimension(240, 320);
304: if (deviceScreenSize.equals(deviceInfo.getCurrentScreenSize()))
305: return;
306: deviceInfo.setArbitraryScreenSize(deviceScreenSize);
307: reload();
308: }
309:
310: }
|