PrintPreview.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
net.sourceforge.ganttproject.print |
![]() |
![]() |
GanttProject |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
package net.sourceforge.ganttproject.print;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.print.*;
import java.util.Date;
import javax.swing.*;
import javax.swing.border.*;
import net.sourceforge.ganttproject.*;
import net.sourceforge.ganttproject.gui.*;
import net.sourceforge.ganttproject.gui.options.*;
import net.sourceforge.ganttproject.gui.options.model.*;
import net.sourceforge.ganttproject.language.GanttLanguage;
/**
* PintPreview dialog
*
* @author jseiler <juliens@actimage.net>
* @version 0.1
* @since 2.0pre2
*/
public class PrintPreview extends JFrame
{
static final long serialVersionUID=0;
protected int page_width;
protected int page_height;
protected int scale;
protected PrintableChart target;
protected JComboBox cmb_scale;
protected PreviewContainer preview;
protected PrinterJob printer_job;
protected GanttLanguage language;
protected DateOption date_start;
protected DateOption date_end;
protected GanttOptions gantt_options;
/**
* Construct a new print preview dialog based on a
* new PrintableChart object
*/
public PrintPreview(ChartComponentBase area)
{
this(new PrintableChart(area));
}
/**
* Construct a new print preview dialog displaying the
* printable object target
*
* @param target an instance of a class implementing the Printable interface
*/
public PrintPreview(PrintableChart target)
{
super();
this.gantt_options = Mediator.getGanttProjectSingleton().getOptions();
this.language = GanttLanguage.getInstance();
this.setTitle(this.language.correctLabel(GanttLanguage.getInstance().getText("preview")));
this.target = target;
this.printer_job = PrinterJob.getPrinterJob();
JButton btn_print = new TestGanttRolloverButton(new ImageIcon(getClass().getResource("/icons/print_16.gif")));
JButton btn_pagesetup = new TestGanttRolloverButton(new ImageIcon(getClass().getResource("/icons/settings_16.gif")));
JLabel lbl_scale = new JLabel(this.language.getText("zoom")+" : ");
String[] scales = { "10 %", "25 %", "50 %", "75 %", "100 %" };
this.cmb_scale = new JComboBox(scales);
this.date_start = new DefaultDateOption("generic.startDate")
{
public void setValue(Date value)
{
super.setValue(value);
this.commit();
PrintPreview.this.onStartDateChanged();
this.lock();
}
};
this.date_end = new DefaultDateOption("generic.endDate")
{
public void setValue(Date value)
{
super.setValue(value);
this.commit();
PrintPreview.this.onEndDateChanged();
this.lock();
}
};
JButton btn_wholeproject = new JButton(this.language.getText("wholeProject"));
this.date_start.lock();
this.date_end.lock();
OptionsPageBuilder builder = new OptionsPageBuilder();
builder.setOptionKeyPrefix("");
JScrollPane scp_preview = new JScrollPane();
this.preview = new PreviewContainer();
JToolBar tlb_settings = new JToolBar();
tlb_settings.add(btn_pagesetup);
tlb_settings.add(btn_print);
tlb_settings.addSeparator();
tlb_settings.add(lbl_scale);
tlb_settings.add(this.cmb_scale);
JToolBar tlb_chart = new JToolBar();
tlb_chart.add(builder.createStandaloneOptionPanel(date_start));
tlb_chart.add(builder.createStandaloneOptionPanel(date_end));
tlb_chart.add(btn_wholeproject);
this.scale = 50;
this.cmb_scale.setSelectedIndex(2);
this.loadPreview();
scp_preview.setViewportView(this.preview);
this.getContentPane().add(tlb_settings, BorderLayout.NORTH);
this.getContentPane().add(scp_preview, BorderLayout.CENTER);
this.getContentPane().add(tlb_chart, BorderLayout.SOUTH);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
btn_wholeproject.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
PrintPreview.this.viewWholeProject();
}
});
btn_print.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
// Use default printer, no dialog
PrintPreview.this.printer_job.setPrintable(PrintPreview.this.target, gantt_options.getPageFormat());
PrintPreview.this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
PrintPreview.this.printer_job.print();
PrintPreview.this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
PrintPreview.this.dispose();
}
catch (PrinterException ex)
{
ex.printStackTrace();
System.err.println("Printing error: "+ex.toString());
}
}
});
this.cmb_scale.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Thread runner = new Thread()
{
public void run()
{
String str = PrintPreview.this.cmb_scale.getSelectedItem().toString();
if (str.endsWith("%"))
str = str.substring(0, str.length()-1);
str = str.trim();
try
{
int newscale = Integer.parseInt(str);
PrintPreview.this.scale = newscale;
}
catch (NumberFormatException ex)
{
return;
}
int w = (int)(PrintPreview.this.page_width*scale/100);
int h = (int)(PrintPreview.this.page_height*scale/100);
Component[] comps = PrintPreview.this.preview.getComponents();
for (int k=0; k<comps.length; k++)
{
if (!(comps[k] instanceof PagePreview))
continue;
PagePreview pp = (PagePreview)comps[k];
pp.setScaledSize(w, h);
}
PrintPreview.this.preview.doLayout();
PrintPreview.this.preview.getParent().getParent().validate();
}
};
runner.start();
}
});
btn_pagesetup.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
PageFormat format = PrintPreview.this.gantt_options.getPageFormat();
format = PrintPreview.this.printer_job.pageDialog(format);
PrintPreview.this.gantt_options.setPageFormat(format);
PrintPreview.this.loadPreview();
}
});
this.setSize(800, 400);
this.date_start.setValue(Mediator.getGanttProjectSingleton().getArea().getStartDate());
this.date_end.setValue(Mediator.getGanttProjectSingleton().getArea().getEndDate());
}
/**
* Load all pages preview in the PreviewContainer
*/
private void loadPreview()
{
if (this.gantt_options.getPageFormat().getHeight()==0 || this.gantt_options.getPageFormat().getWidth()==0)
{
System.err.println("Unable to determine default page size");
return;
}
this.preview.clear();
this.page_width = (int)(this.gantt_options.getPageFormat().getWidth());
this.page_height = (int)(this.gantt_options.getPageFormat().getHeight());
int w = (int)(this.page_width*this.scale/100);
int h = (int)(this.page_height*this.scale/100);
int page_index = 0;
try
{
while (true)
{
BufferedImage img = new BufferedImage(this.page_width, this.page_height, BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, this.page_width, this.page_height);
if (this.target.print(g, this.gantt_options.getPageFormat(), page_index) != Printable.PAGE_EXISTS)
break;
PagePreview pp = new PagePreview(w, h, img);
this.preview.add(pp);
page_index++;
}
}
catch (PrinterException e)
{
e.printStackTrace();
System.err.println("Printing error: "+e.toString());
}
this.preview.validate();
this.preview.repaint();
}
private void viewWholeProject()
{
this.date_start.setValue(Mediator.getGanttProjectSingleton().getArea().getStartDate());
this.date_end.setValue(Mediator.getGanttProjectSingleton().getArea().getEndDate());
this.target.setStartDate(this.date_start.getValue());
this.target.setEndDate(this.date_end.getValue());
this.loadPreview();
}
private void onStartDateChanged()
{
System.out.println("Start date changed");
this.target.setStartDate(this.date_start.getValue());
this.loadPreview();
}
private void onEndDateChanged()
{
System.out.println("End date changed");
this.target.setEndDate(this.date_end.getValue());
this.loadPreview();
}
/**
* JPanel containing the pages of the PrintPreview
*/
class PreviewContainer extends JPanel
{
static final long serialVersionUID=0;
protected int H_GAP = 16;
protected int V_GAP = 10;
public Dimension getPreferredSize()
{
int n = getComponentCount();
if (n == 0)
return new Dimension(this.H_GAP, this.V_GAP);
Component comp = getComponent(0);
Dimension dc = comp.getPreferredSize();
int w = dc.width;
int h = dc.height;
Dimension dp = getParent().getSize();
int nCol = Math.max((dp.width-this.H_GAP)/(w+this.H_GAP), 1);
int nRow = n/nCol;
if (nRow*nCol < n)
nRow++;
int ww = nCol*(w+this.H_GAP) + this.H_GAP;
int hh = nRow*(h+this.V_GAP) + this.V_GAP;
Insets ins = getInsets();
return new Dimension(ww+ins.left+ins.right, hh+ins.top+ins.bottom);
}
public Dimension getMaximumSize()
{
return getPreferredSize();
}
public Dimension getMinimumSize()
{
return getPreferredSize();
}
public void doLayout()
{
Insets ins = getInsets();
int x = ins.left + this.H_GAP;
int y = ins.top + this.V_GAP;
int n = this.getComponentCount();
if (n == 0)
return;
Component comp = this.getComponent(0);
Dimension dc = comp.getPreferredSize();
int w = dc.width;
int h = dc.height;
Dimension dp = this.getParent().getSize();
int nCol = Math.max((dp.width-this.H_GAP)/(w+this.H_GAP), 1);
int nRow = n/nCol;
if (nRow*nCol < n)
nRow++;
int index = 0;
for (int k = 0; k<nRow; k++)
{
for (int m = 0; m<nCol; m++)
{
if (index >= n)
return;
comp = getComponent(index++);
comp.setBounds(x, y, w, h);
x += w + this.H_GAP;
}
y += h+V_GAP;
x = ins.left + this.H_GAP;
}
}
public void clear()
{
this.removeAll();
this.repaint();
}
}
/**
* JPanel corresponding to one page preview
*/
class PagePreview extends JPanel
{
static final long serialVersionUID=0;
protected int width;
protected int height;
protected Image source;
protected Image image;
public PagePreview(int w, int h, Image source)
{
this.width = w;
this.height = h;
this.source = source;
this.image = this.source.getScaledInstance(this.width, this.height, Image.SCALE_SMOOTH);
this.image.flush();
this.setBackground(Color.white);
this.setBorder(new MatteBorder(1, 1, 2, 2, Color.black));
}
public void setScaledSize(int w, int h)
{
this.width = w;
this.height = h;
this.image = this.source.getScaledInstance(this.width, this.height, Image.SCALE_SMOOTH);
this.repaint();
}
public Dimension getPreferredSize()
{
Insets ins = getInsets();
return new Dimension(this.width+ins.left+ins.right, this.height+ins.top+ins.bottom);
}
public Dimension getMaximumSize()
{
return getPreferredSize();
}
public Dimension getMinimumSize()
{
return getPreferredSize();
}
public void paint(Graphics g)
{
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(this.image, 0, 0, this);
this.paintBorder(g);
}
}
}
The table below shows all metrics for PrintPreview.java.




