/*
Copyright 2008-2010 Gephi
Authors : Eduardo Ramos <eduramiba@gmail.com>
Website : http://www.gephi.org
This file is part of Gephi.
Gephi is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
Gephi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Gephi. If not, see <http://www.gnu.org/licenses/>.
*/
package org.gephi.ui.components;
import java.awt.Dimension;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
/**
* UI for showing a JFreeChart with the advantages of ChartPanel and also allows to resize the chart up to a maximum dimension.
* @author Eduardo Ramos <eduramiba@gmail.com>
*/
public class JFreeChartDialog extends javax.swing.JDialog {
private ChartPanel chartPanel;
private static final int MAX_DIMENSION=3000;
/**
* Chart can't be null.
*/
public JFreeChartDialog(java.awt.Frame parent, String title, JFreeChart chart, int initialWidth, int initialHeight) {
super(parent, false);
setTitle(title);
initialize(chart);
setSize(initialWidth, initialHeight);
setLocationRelativeTo(parent);
}
/**
* Chart can't be null.
*/
public JFreeChartDialog(java.awt.Frame parent, JFreeChart chart) {
super(parent, false);
initialize(chart);
setLocationRelativeTo(parent);
}
private void initialize(JFreeChart chart) {
initComponents();
if (chart == null) {
throw new IllegalArgumentException("Chart panel can't be null");
}
this.chartPanel = new ChartPanel(chart, true);
scroll.setViewportView(chartPanel);
adaptChartPanelSizeToScrollSize();
scroll.revalidate();
scroll.repaint();
setVisible(true);
}
private void adaptChartPanelSizeToScrollSize() {
chartPanel.setPreferredSize(new Dimension(0, 0));//Let the chartPanel adjust to scroll pane current size.
chartPanel.revalidate();
chartPanel.repaint();
}
private void applyZoom(float multiplier) {
int width = (int) (chartPanel.getSize().width * multiplier);
int heigth = (int) (chartPanel.getSize().height * multiplier);
width= width > MAX_DIMENSION ? MAX_DIMENSION : width;
heigth= heigth > MAX_DIMENSION ? MAX_DIMENSION : heigth;
chartPanel.setPreferredSize(new Dimension(width,heigth));
chartPanel.revalidate();
chartPanel.repaint();
}
public JFreeChart getChart(){
return chartPanel.getChart();
}
public void setChart(JFreeChart chart){
if (chart == null) {
throw new IllegalArgumentException("Chart panel can't be null");
}
chartPanel.setChart(chart);
}
public Dimension getChartSize(){
return chartPanel.getSize();
}
/** 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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
scroll = new javax.swing.JScrollPane();
closeButton = new javax.swing.JButton();
resetButton = new javax.swing.JButton();
zoomOutButton = new javax.swing.JButton();
zoomInButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setMinimumSize(new java.awt.Dimension(200, 200));
scroll.setMinimumSize(new java.awt.Dimension(10, 10));
scroll.setPreferredSize(new java.awt.Dimension(10, 10));
closeButton.setText(org.openide.util.NbBundle.getMessage(JFreeChartDialog.class, "JFreeChartDialog.closeButton.text")); // NOI18N
closeButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
closeButtonActionPerformed(evt);
}
});
resetButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/gephi/ui/components/resources/magnifier-history.png"))); // NOI18N
resetButton.setText(org.openide.util.NbBundle.getMessage(JFreeChartDialog.class, "JFreeChartDialog.resetButton.text")); // NOI18N
resetButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
resetButtonActionPerformed(evt);
}
});
zoomOutButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/gephi/ui/components/resources/magnifier--minus.png"))); // NOI18N
zoomOutButton.setText(org.openide.util.NbBundle.getMessage(JFreeChartDialog.class, "JFreeChartDialog.zoomOutButton.text")); // NOI18N
zoomOutButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
zoomOutButtonActionPerformed(evt);
}
});
zoomInButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/gephi/ui/components/resources/magnifier--plus.png"))); // NOI18N
zoomInButton.setText(org.openide.util.NbBundle.getMessage(JFreeChartDialog.class, "JFreeChartDialog.zoomInButton.text")); // NOI18N
zoomInButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
zoomInButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(scroll, javax.swing.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(zoomOutButton, javax.swing.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE)
.addComponent(zoomInButton, javax.swing.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(closeButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(resetButton))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(scroll, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(resetButton)
.addComponent(zoomInButton))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(zoomOutButton)
.addComponent(closeButton))
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
dispose();
}//GEN-LAST:event_closeButtonActionPerformed
private void resetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetButtonActionPerformed
adaptChartPanelSizeToScrollSize();
}//GEN-LAST:event_resetButtonActionPerformed
private void zoomInButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_zoomInButtonActionPerformed
applyZoom(1.5f);
}//GEN-LAST:event_zoomInButtonActionPerformed
private void zoomOutButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_zoomOutButtonActionPerformed
applyZoom(0.5f);
}//GEN-LAST:event_zoomOutButtonActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton closeButton;
private javax.swing.JPanel jPanel1;
private javax.swing.JButton resetButton;
private javax.swing.JScrollPane scroll;
private javax.swing.JButton zoomInButton;
private javax.swing.JButton zoomOutButton;
// End of variables declaration//GEN-END:variables
}