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 General
007: * Public License Version 2 only ("GPL") or the Common Development and Distribution
008: * License("CDDL") (collectively, the "License"). You may not use this file except in
009: * compliance with the License. You can obtain a copy of the License at
010: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
011: * License for the specific language governing permissions and limitations under the
012: * License. When distributing the software, include this License Header Notice in
013: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
014: * designates this particular file as subject to the "Classpath" exception as
015: * provided by Sun in the GPL Version 2 section of the License file that
016: * accompanied this code. If applicable, add the following below the License Header,
017: * with the fields enclosed by brackets [] replaced by your own identifying
018: * information: "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Contributor(s):
021: *
022: * The Original Software is NetBeans. The Initial Developer of the Original Software
023: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
024: * Rights Reserved.
025: *
026: * If you wish your version of this file to be governed by only the CDDL or only the
027: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
028: * this software in this distribution under the [CDDL or GPL Version 2] license." If
029: * you do not indicate a single choice of license, a recipient has the option to
030: * distribute your version of this file under either the CDDL, the GPL Version 2 or
031: * to extend the choice of license to its licensees as provided above. However, if
032: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
033: * the option applies only if the new code is made subject to such option by the
034: * copyright holder.
035: */
036:
037: package org.netbeans.installer.utils.helper.swing;
038:
039: import java.awt.Graphics;
040: import java.awt.GraphicsConfiguration;
041: import java.awt.GraphicsDevice;
042: import java.awt.GraphicsEnvironment;
043: import java.awt.Image;
044: import java.awt.event.ComponentAdapter;
045: import java.awt.event.ComponentEvent;
046: import java.io.File;
047: import java.net.MalformedURLException;
048: import java.net.URL;
049: import javax.swing.ImageIcon;
050: import javax.swing.JFrame;
051: import org.netbeans.installer.utils.ErrorManager;
052: import org.netbeans.installer.utils.FileProxy;
053: import org.netbeans.installer.utils.ResourceUtils;
054: import org.netbeans.installer.utils.UiUtils;
055: import org.netbeans.installer.utils.exceptions.DownloadException;
056: import org.netbeans.installer.utils.helper.ExtendedUri;
057:
058: /**
059: *
060: * @author Kirill Sorokin
061: */
062: public class NbiFrame extends JFrame {
063: /////////////////////////////////////////////////////////////////////////////////
064: // Instance
065: /**
066: * Initial width of the frame.
067: */
068: protected int frameWidth;
069:
070: /**
071: * Minimum width of the frame.
072: */
073: protected int frameMinimumWidth;
074:
075: /**
076: * Maximum width of the frame.
077: */
078: protected int frameMaximumWidth;
079:
080: /**
081: * Initial height of the frame.
082: */
083: protected int frameHeight;
084:
085: /**
086: * Minimum height of the frame.
087: */
088: protected int frameMinimumHeight;
089:
090: /**
091: * Maximum height of the frame.
092: */
093: protected int frameMaximumHeight;
094:
095: /**
096: * Frame's icon.
097: */
098: protected File frameIcon;
099:
100: private NbiFrameContentPane contentPane;
101:
102: public NbiFrame() {
103: super ();
104:
105: frameWidth = UiUtils.getDimension(System.getProperties(),
106: FRAME_WIDTH_PROPERTY, DEFAULT_FRAME_WIDTH);
107: frameHeight = UiUtils.getDimension(System.getProperties(),
108: FRAME_HEIGHT_PROPERTY, DEFAULT_FRAME_HEIGHT);
109:
110: frameMinimumWidth = UiUtils.getDimension(
111: System.getProperties(), FRAME_MINIMUM_WIDTH_PROPERTY,
112: DEFAULT_FRAME_MINIMUM_WIDTH);
113: frameMinimumHeight = UiUtils.getDimension(System
114: .getProperties(), FRAME_MINIMUM_HEIGHT_PROPERTY,
115: DEFAULT_FRAME_MINIMUM_HEIGHT);
116:
117: frameMaximumWidth = UiUtils.getDimension(
118: System.getProperties(), FRAME_MAXIMUM_WIDTH_PROPERTY,
119: DEFAULT_FRAME_MAXIMUM_WIDTH);
120: frameMaximumHeight = UiUtils.getDimension(System
121: .getProperties(), FRAME_MAXIMUM_HEIGHT_PROPERTY,
122: DEFAULT_FRAME_MAXIMUM_HEIGHT);
123:
124: boolean customIconLoaded = false;
125: if (System.getProperty(FRAME_ICON_URI_PROPERTY) != null) {
126: final String frameIconUri = System
127: .getProperty(FRAME_ICON_URI_PROPERTY);
128:
129: try {
130: frameIcon = FileProxy.getInstance().getFile(
131: frameIconUri, true);
132: customIconLoaded = true;
133: } catch (DownloadException e) {
134: ErrorManager.notifyWarning(ResourceUtils.getString(
135: NbiFrame.class,
136: RESOURCE_FAILED_TO_DOWNLOAD_WIZARD_ICON,
137: frameIconUri), e);
138: }
139: }
140:
141: if (!customIconLoaded) {
142: final String frameIconUri = DEFAULT_FRAME_ICON_URI;
143:
144: try {
145: frameIcon = FileProxy.getInstance().getFile(
146: frameIconUri, true);
147: customIconLoaded = true;
148: } catch (DownloadException e) {
149: ErrorManager.notifyWarning(ResourceUtils.getString(
150: NbiFrame.class,
151: RESOURCE_FAILED_TO_DOWNLOAD_WIZARD_ICON,
152: frameIconUri), e);
153: }
154: }
155:
156: initComponents();
157: }
158:
159: public void setVisible(boolean visible) {
160: final GraphicsDevice screen = GraphicsEnvironment
161: .getLocalGraphicsEnvironment().getScreenDevices()[0];
162: final GraphicsConfiguration config = screen
163: .getDefaultConfiguration();
164:
165: final int screenWidth = config.getBounds().width;
166: final int screenHeight = config.getBounds().height;
167:
168: setLocation((screenWidth - getSize().width) / 2,
169: (screenHeight - getSize().height) / 2);
170:
171: super .setVisible(visible);
172: }
173:
174: public Image getBackgroundImage() {
175: return contentPane.getBackgroundImage();
176: }
177:
178: public void setBackgroundImage(URL url) {
179: contentPane.setBackgroundImage(url);
180: }
181:
182: // protected ////////////////////////////////////////////////////////////////////
183: private void initComponents() {
184: // the frame itself
185: try {
186: setDefaultCloseOperation(EXIT_ON_CLOSE);
187: } catch (SecurityException e) {
188: // we might fail here with a custom security manager (e.g. the netbeans
189: // one); in this case just log the exception and "let it be" (c)
190: ErrorManager.notifyDebug(
191: "Cannot set the default close operation", e);
192: }
193:
194: setSize(frameWidth, frameHeight);
195:
196: try {
197: setIconImage(new ImageIcon(frameIcon.toURI().toURL())
198: .getImage());
199: } catch (MalformedURLException e) {
200: ErrorManager.notifyWarning(ResourceUtils.getString(
201: NbiFrame.class, RESOURCE_FAILED_TO_SET_FRAME_ICON),
202: e);
203: }
204:
205: addComponentListener(new ComponentAdapter() {
206: @Override
207: public void componentResized(ComponentEvent event) {
208: if ((frameMinimumWidth != -1)
209: && (getSize().width < frameMinimumWidth)) {
210: setSize(frameMinimumWidth, getSize().height);
211: }
212: if ((frameMinimumHeight != -1)
213: && (getSize().height < frameMinimumHeight)) {
214: setSize(getSize().width, frameMinimumHeight);
215: }
216:
217: if ((frameMaximumWidth != -1)
218: && (getSize().width > frameMaximumWidth)) {
219: setSize(frameMaximumWidth, getSize().height);
220: }
221: if ((frameMaximumHeight != -1)
222: && (getSize().height > frameMaximumHeight)) {
223: setSize(getSize().width, frameMaximumHeight);
224: }
225: }
226: });
227:
228: // content pane
229: contentPane = new NbiFrameContentPane();
230: setContentPane(contentPane);
231: }
232:
233: /////////////////////////////////////////////////////////////////////////////////
234: // Inner Classes
235: public static class NbiFrameContentPane extends NbiPanel {
236: private Image backgroundImage;
237:
238: @Override
239: protected void paintComponent(Graphics graphics) {
240: super .paintComponent(graphics);
241:
242: if (backgroundImage != null) {
243: graphics.drawImage(backgroundImage, 0, 0, this );
244: }
245: }
246:
247: public Image getBackgroundImage() {
248: return backgroundImage;
249: }
250:
251: public void setBackgroundImage(URL url) {
252: if (url != null) {
253: backgroundImage = new ImageIcon(url).getImage();
254: } else {
255: backgroundImage = null;
256: }
257: }
258:
259: public void setBackgroundImage(Image image) {
260: if (image != null) {
261: backgroundImage = image;
262: } else {
263: backgroundImage = null;
264: }
265: }
266: }
267:
268: /////////////////////////////////////////////////////////////////////////////////
269: // Constants
270: public static final String FRAME_WIDTH_PROPERTY = "nbi.ui.swing.frame.width";
271:
272: public static final String FRAME_MINIMUM_WIDTH_PROPERTY = "nbi.ui.swing.frame.minimum.width";
273:
274: public static final String FRAME_MAXIMUM_WIDTH_PROPERTY = "nbi.ui.swing.frame.maximum.width";
275:
276: public static final String FRAME_HEIGHT_PROPERTY = "nbi.ui.swing.frame.height";
277:
278: public static final String FRAME_MINIMUM_HEIGHT_PROPERTY = "nbi.ui.swing.frame.minimum.height";
279:
280: public static final String FRAME_MAXIMUM_HEIGHT_PROPERTY = "nbi.ui.swing.frame.maximum.height";
281:
282: public static final String FRAME_ICON_URI_PROPERTY = "nbi.ui.swing.frame.icon.uri";
283:
284: public static final int DEFAULT_FRAME_WIDTH = 650;
285:
286: public static final int DEFAULT_FRAME_MINIMUM_WIDTH = 650;
287:
288: public static final int DEFAULT_FRAME_MAXIMUM_WIDTH = -1;
289:
290: public static final int DEFAULT_FRAME_HEIGHT = 600;
291:
292: public static final int DEFAULT_FRAME_MINIMUM_HEIGHT = 600;
293:
294: public static final int DEFAULT_FRAME_MAXIMUM_HEIGHT = -1;
295:
296: public static final String DEFAULT_FRAME_ICON_URI = ExtendedUri.RESOURCE_SCHEME
297: + ":org/netbeans/installer/utils/helper/swing/frame-icon.png";
298:
299: /**
300: * Name of a resource bundle entry.
301: */
302: private static final String RESOURCE_FAILED_TO_PARSE_SYSTEM_PROPERTY = "NF.error.failed.to.parse.property"; // NOI18N
303:
304: /**
305: * Name of a resource bundle entry.
306: */
307: private static final String RESOURCE_FAILED_TO_DOWNLOAD_WIZARD_ICON = "NF.error.failed.to.download.icon"; // NOI18N
308:
309: /**
310: * Name of a resource bundle entry.
311: */
312: private static final String RESOURCE_FAILED_TO_SET_FRAME_ICON = "NF.error.failed.to.set.frame.icon"; // NOI18N
313: }
|