Java tutorial
/** * Software License * * Copyright 2007/2010 National Library of New Zealand. * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * or the file "LICENSE.txt" included with the software. * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or * implied. See the License for the specific language governing * permissions and limitations under the License. * */ package nz.govt.natlib.ndha.manualdeposit.dialogs; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RenderingHints; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.jar.Attributes; import java.util.jar.Manifest; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.Timer; import nz.govt.natlib.ndha.common.guiutilities.FormControl; import nz.govt.natlib.ndha.manualdeposit.FormUtilities; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class About extends javax.swing.JDialog { private static final long serialVersionUID = -8820295609529311822L; private final static Log LOG = LogFactory.getLog(About.class); private final String theLogoFileName = "Indigo_info_screen.jpg"; private String theSettingsPath = "./"; private Image theLogo = null; @SuppressWarnings("unused") private FormControl theFormControl; //NOPMD /** Creates new form About */ public About(java.awt.Frame parent, boolean modal, String settingsPath) { super(parent, modal); theSettingsPath = settingsPath; initComponents(); lblBuildNoLabel.putClientProperty(FormUtilities.TextAttributes.BoldText.description(), "Yes"); lblVersionLabel.putClientProperty(FormUtilities.TextAttributes.BoldText.description(), "Yes"); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ //GEN-BEGIN:initComponents // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { imgIndigo = new org.jdesktop.swingx.JXImagePanel(); lblVersionLabel = new org.jdesktop.swingx.JXLabel(); lblBuildNoLabel = new org.jdesktop.swingx.JXLabel(); lblVersion = new org.jdesktop.swingx.JXLabel(); lblBuildNo = new org.jdesktop.swingx.JXLabel(); setTitle("About NLNZ Indigo"); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); addComponentListener(new java.awt.event.ComponentAdapter() { public void componentResized(java.awt.event.ComponentEvent evt) { formComponentResized(evt); } }); addWindowListener(new java.awt.event.WindowAdapter() { public void windowActivated(java.awt.event.WindowEvent evt) { formWindowActivated(evt); } public void windowOpened(java.awt.event.WindowEvent evt) { formWindowOpened(evt); } }); imgIndigo.setScrollableTracksViewportHeight(true); imgIndigo.setScrollableTracksViewportWidth(true); imgIndigo.addComponentListener(new java.awt.event.ComponentAdapter() { public void componentResized(java.awt.event.ComponentEvent evt) { imgIndigoComponentResized(evt); } }); lblVersionLabel.setLabelFor(lblVersion); lblVersionLabel.setText("Version:"); lblBuildNoLabel.setLabelFor(lblBuildNo); lblBuildNoLabel.setText("Build No:"); lblVersion.setText("Version"); lblBuildNo.setText("Build No"); javax.swing.GroupLayout imgIndigoLayout = new javax.swing.GroupLayout(imgIndigo); imgIndigo.setLayout(imgIndigoLayout); imgIndigoLayout.setHorizontalGroup(imgIndigoLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(imgIndigoLayout.createSequentialGroup().addGap(34, 34, 34) .addGroup(imgIndigoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(imgIndigoLayout.createSequentialGroup() .addComponent(lblBuildNoLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(lblBuildNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(imgIndigoLayout.createSequentialGroup() .addComponent(lblVersionLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(lblVersion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(227, Short.MAX_VALUE))); imgIndigoLayout.setVerticalGroup(imgIndigoLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(imgIndigoLayout.createSequentialGroup().addContainerGap(118, Short.MAX_VALUE) .addGroup(imgIndigoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lblVersionLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(lblVersion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(imgIndigoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lblBuildNoLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(lblBuildNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(188, 188, 188))); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(imgIndigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(imgIndigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); pack(); }// </editor-fold> //GEN-END:initComponents @SuppressWarnings("serial") private void formComponentResized(java.awt.event.ComponentEvent evt) { paintImage(); Action updateDividersAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { Timer t = (Timer) e.getSource(); t.stop(); paintImage(); repaint(); } }; new Timer(200, updateDividersAction).start(); } public void setFormFont(Font theFont) { FormUtilities.setFormFont(this, theFont); } private void imgIndigoComponentResized(java.awt.event.ComponentEvent evt) { paintImage(); } private void paintImage() { if (theLogo != null) { int width = this.getWidth(); int height = this.getHeight(); BufferedImage thumbImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = thumbImage.createGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.drawImage(theLogo, 0, 0, width, height, null); imgIndigo.setImage(thumbImage); imgIndigo.setBounds(0, 0, width, height); repaint(); } } @SuppressWarnings("unchecked") private void startup() { try { try { theFormControl = new FormControl(this, theSettingsPath); } catch (Exception ex) { LOG.error("Error loading form parameters", ex); } ClassLoader cLoader = Thread.currentThread().getContextClassLoader(); Toolkit kit = Toolkit.getDefaultToolkit(); Package p = this.getClass().getPackage(); lblVersion.setText(p.getImplementationVersion()); if (new File(theLogoFileName).exists()) { theLogo = kit.getImage(theLogoFileName); } else { java.net.URL imageURL = cLoader.getResource(theLogoFileName); if (imageURL != null) { theLogo = kit.getImage(imageURL); } } if (theLogo != null) { lblBuildNo.setForeground(Color.white); lblBuildNoLabel.setForeground(Color.white); //lblHeader.setForeground(Color.white); lblVersion.setForeground(Color.white); lblVersionLabel.setForeground(Color.white); paintImage(); } //String manifestFileName = "/META-INF/MANIFEST.MF"; Class cls = this.getClass(); String className = cls.getSimpleName(); String classFileName = className + ".class"; String pathToThisClass = cls.getResource(classFileName).toString(); int mark = pathToThisClass.indexOf("!"); String pathToManifest = pathToThisClass.substring(0, mark + 1); if (!pathToManifest.equals("")) { pathToManifest += "/META-INF/MANIFEST.MF"; LOG.debug("Path to manifest: " + pathToManifest); Manifest mf = new Manifest(new URL(pathToManifest).openStream()); if (mf != null) { Attributes attr = mf.getMainAttributes(); String attrVersion = "Implementation-Version"; String attrBuild = "Implementation-Build"; String version = attr.getValue(attrVersion); String build = attr.getValue(attrBuild); this.lblVersion.setText(version); this.lblBuildNo.setText(build); } } Runtime runtime = Runtime.getRuntime(); long maxMemory = runtime.maxMemory(); long allocatedMemory = runtime.totalMemory(); long freeMemory = runtime.freeMemory(); LOG.debug("free memory: " + freeMemory / 1024); LOG.debug("allocated memory: " + allocatedMemory / 1024); LOG.debug("max memory: " + maxMemory / 1024); LOG.debug("total free memory: " + (freeMemory + (maxMemory - allocatedMemory)) / 1024); } catch (IOException ex) { ex.printStackTrace(); LOG.error(ex.getMessage(), ex); } } private void formWindowActivated(java.awt.event.WindowEvent evt) { startup(); } private void formWindowOpened(java.awt.event.WindowEvent evt) { startup(); } //GEN-BEGIN:variables // Variables declaration - do not modify private org.jdesktop.swingx.JXImagePanel imgIndigo; private org.jdesktop.swingx.JXLabel lblBuildNo; private org.jdesktop.swingx.JXLabel lblBuildNoLabel; private org.jdesktop.swingx.JXLabel lblVersion; private org.jdesktop.swingx.JXLabel lblVersionLabel; // End of variables declaration//GEN-END:variables }