| 1 |
/** |
| 2 |
* ======================================== |
| 3 |
* JFreeReport : a free Java report library |
| 4 |
* ======================================== |
| 5 |
* |
| 6 |
* Project Info: http://reporting.pentaho.org/ |
| 7 |
* |
| 8 |
* (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors. |
| 9 |
* |
| 10 |
* This library is free software; you can redistribute it and/or modify it under the terms |
| 11 |
* of the GNU Lesser General Public License as published by the Free Software Foundation; |
| 12 |
* either version 2.1 of the License, or (at your option) any later version. |
| 13 |
* |
| 14 |
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 15 |
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 16 |
* See the GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License along with this |
| 19 |
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
| 20 |
* Boston, MA 02111-1307, USA. |
| 21 |
* |
| 22 |
* [Java is a trademark or registered trademark of Sun Microsystems, Inc. |
| 23 |
* in the United States and other countries.] |
| 24 |
* |
| 25 |
* ------------ |
| 26 |
* $Id: PreviewPane.java 10756 2009-12-02 15:58:24Z tmorgner $ |
| 27 |
* ------------ |
| 28 |
* (C) Copyright 2000-2005, by Object Refinery Limited. |
| 29 |
* (C) Copyright 2005-2007, by Pentaho Corporation. |
| 30 |
*/ |
| 31 |
|
| 32 |
package org.jfree.report.modules.gui.swing.preview; |
| 33 |
|
| 34 |
import java.awt.BorderLayout; |
| 35 |
import java.awt.Color; |
| 36 |
import java.awt.Window; |
| 37 |
import java.beans.PropertyChangeEvent; |
| 38 |
import java.beans.PropertyChangeListener; |
| 39 |
import java.text.NumberFormat; |
| 40 |
import java.util.ArrayList; |
| 41 |
import java.util.Arrays; |
| 42 |
import java.util.Collections; |
| 43 |
import java.util.HashMap; |
| 44 |
import java.util.Iterator; |
| 45 |
import java.util.Locale; |
| 46 |
import java.util.Map; |
| 47 |
import javax.swing.BorderFactory; |
| 48 |
import javax.swing.JComponent; |
| 49 |
import javax.swing.JMenu; |
| 50 |
import javax.swing.JPanel; |
| 51 |
import javax.swing.JScrollPane; |
| 52 |
import javax.swing.JToolBar; |
| 53 |
import javax.swing.SwingUtilities; |
| 54 |
|
| 55 |
import org.jfree.layouting.modules.output.graphics.PageDrawable; |
| 56 |
import org.jfree.layouting.modules.output.graphics.DrawablePanel; |
| 57 |
import org.jfree.report.JFreeReportBoot; |
| 58 |
import org.jfree.report.flow.ReportJob; |
| 59 |
import org.jfree.report.flow.ReportStructureRoot; |
| 60 |
import org.jfree.report.modules.gui.common.IconTheme; |
| 61 |
import org.jfree.report.modules.gui.swing.common.ActionPlugin; |
| 62 |
import org.jfree.report.modules.gui.swing.common.SwingGuiContext; |
| 63 |
import org.jfree.report.modules.gui.swing.common.SwingUtil; |
| 64 |
import org.jfree.report.modules.gui.swing.common.KeyedComboBoxModel; |
| 65 |
import org.jfree.report.modules.gui.swing.common.CenterLayout; |
| 66 |
import org.jfree.report.modules.gui.swing.printing.PrintReportProcessor; |
| 67 |
import org.jfree.report.util.Worker; |
| 68 |
import org.jfree.report.util.TextUtilities; |
| 69 |
import org.pentaho.reporting.libraries.base.config.Configuration; |
| 70 |
import org.pentaho.reporting.libraries.base.util.ObjectUtilities; |
| 71 |
|
| 72 |
/** |
| 73 |
* Creation-Date: 11.11.2006, 19:36:13 |
| 74 |
* |
| 75 |
* @author Thomas Morgner |
| 76 |
*/ |
| 77 |
public class PreviewPane extends JPanel |
| 78 |
{ |
| 79 |
private class PreviewGuiContext implements SwingGuiContext |
| 80 |
{ |
| 81 |
public PreviewGuiContext() |
| 82 |
{ |
| 83 |
} |
| 84 |
|
| 85 |
public Window getWindow() |
| 86 |
{ |
| 87 |
return SwingUtil.getWindowAncestor(PreviewPane.this); |
| 88 |
} |
| 89 |
|
| 90 |
public Locale getLocale() |
| 91 |
{ |
| 92 |
ReportJob report = getReportJob(); |
| 93 |
if (report != null) |
| 94 |
{ |
| 95 |
return report.getReportStructureRoot().getLocale(); |
| 96 |
} |
| 97 |
return Locale.getDefault(); |
| 98 |
} |
| 99 |
|
| 100 |
public IconTheme getIconTheme() |
| 101 |
{ |
| 102 |
return PreviewPane.this.getIconTheme(); |
| 103 |
} |
| 104 |
|
| 105 |
public Configuration getConfiguration() |
| 106 |
{ |
| 107 |
ReportJob report = getReportJob(); |
| 108 |
if (report != null) |
| 109 |
{ |
| 110 |
return report.getConfiguration(); |
| 111 |
} |
| 112 |
return JFreeReportBoot.getInstance().getGlobalConfig(); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
private class RepaginationRunnable implements Runnable |
| 117 |
{ |
| 118 |
private PrintReportProcessor processor; |
| 119 |
|
| 120 |
public RepaginationRunnable(PrintReportProcessor processor) |
| 121 |
{ |
| 122 |
this.processor = processor; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* When an object implementing interface <code>Runnable</code> is used to |
| 127 |
* create a thread, starting the thread causes the object's <code>run</code> |
| 128 |
* method to be called in that separately executing thread. |
| 129 |
* <p/> |
| 130 |
* The general contract of the method <code>run</code> is that it may take |
| 131 |
* any action whatsoever. |
| 132 |
* |
| 133 |
* @see Thread#run() |
| 134 |
*/ |
| 135 |
public void run() |
| 136 |
{ |
| 137 |
final UpdatePaginatingPropertyHandler startPaginationNotify = |
| 138 |
new UpdatePaginatingPropertyHandler(processor, true, 0); |
| 139 |
if (SwingUtilities.isEventDispatchThread()) |
| 140 |
{ |
| 141 |
startPaginationNotify.run(); |
| 142 |
} |
| 143 |
else |
| 144 |
{ |
| 145 |
SwingUtilities.invokeLater(startPaginationNotify); |
| 146 |
} |
| 147 |
|
| 148 |
// Perform the pagination .. |
| 149 |
final int pageCount = processor.getNumberOfPages(); |
| 150 |
|
| 151 |
final UpdatePaginatingPropertyHandler endPaginationNotify = |
| 152 |
new UpdatePaginatingPropertyHandler(processor, false, pageCount); |
| 153 |
if (SwingUtilities.isEventDispatchThread()) |
| 154 |
{ |
| 155 |
endPaginationNotify.run(); |
| 156 |
} |
| 157 |
else |
| 158 |
{ |
| 159 |
SwingUtilities.invokeLater(endPaginationNotify); |
| 160 |
} |
| 161 |
|
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
private class UpdatePaginatingPropertyHandler implements Runnable |
| 166 |
{ |
| 167 |
private boolean paginating; |
| 168 |
private int pageCount; |
| 169 |
private PrintReportProcessor processor; |
| 170 |
|
| 171 |
public UpdatePaginatingPropertyHandler(final PrintReportProcessor processor, |
| 172 |
final boolean paginating, |
| 173 |
final int pageCount) |
| 174 |
{ |
| 175 |
this.processor = processor; |
| 176 |
this.paginating = paginating; |
| 177 |
this.pageCount = pageCount; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* When an object implementing interface <code>Runnable</code> is used to |
| 182 |
* create a thread, starting the thread causes the object's <code>run</code> |
| 183 |
* method to be called in that separately executing thread. |
| 184 |
* <p/> |
| 185 |
* The general contract of the method <code>run</code> is that it may take |
| 186 |
* any action whatsoever. |
| 187 |
* |
| 188 |
* @see Thread#run() |
| 189 |
*/ |
| 190 |
public void run() |
| 191 |
{ |
| 192 |
if (processor != getPrintReportProcessor()) |
| 193 |
{ |
| 194 |
return; |
| 195 |
} |
| 196 |
|
| 197 |
if (paginating == false) |
| 198 |
{ |
| 199 |
setNumberOfPages(pageCount); |
| 200 |
if (getPageNumber() < 1) |
| 201 |
{ |
| 202 |
setPageNumber(1); |
| 203 |
} |
| 204 |
else if (getPageNumber() > pageCount) |
| 205 |
{ |
| 206 |
setPageNumber(pageCount); |
| 207 |
} |
| 208 |
} |
| 209 |
setPaginating(paginating); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
private class PreviewUpdateHandler implements PropertyChangeListener |
| 214 |
{ |
| 215 |
public PreviewUpdateHandler() |
| 216 |
{ |
| 217 |
} |
| 218 |
|
| 219 |
public void propertyChange(PropertyChangeEvent evt) |
| 220 |
{ |
| 221 |
final String propertyName = evt.getPropertyName(); |
| 222 |
if (PAGINATING_PROPERTY.equals(propertyName)) |
| 223 |
{ |
| 224 |
if (isPaginating()) |
| 225 |
{ |
| 226 |
drawablePanel.setDrawableAsRawObject(getPaginatingDrawable()); |
| 227 |
} |
| 228 |
else |
| 229 |
{ |
| 230 |
updateVisiblePage(getPageNumber()); |
| 231 |
} |
| 232 |
} |
| 233 |
else if (REPORT_JOB_PROPERTY.equals(propertyName)) |
| 234 |
{ |
| 235 |
if (getReportJob() == null) |
| 236 |
{ |
| 237 |
drawablePanel.setDrawableAsRawObject(getNoReportDrawable()); |
| 238 |
} |
| 239 |
// else the paginating property will be fired anyway .. |
| 240 |
} |
| 241 |
else if (PAGE_NUMBER_PROPERTY.equals(propertyName)) |
| 242 |
{ |
| 243 |
if (isPaginating()) |
| 244 |
{ |
| 245 |
return; |
| 246 |
} |
| 247 |
|
| 248 |
updateVisiblePage(getPageNumber()); |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
private class UpdateZoomHandler implements PropertyChangeListener |
| 254 |
{ |
| 255 |
public UpdateZoomHandler() |
| 256 |
{ |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* This method gets called when a bound property is changed. |
| 261 |
* |
| 262 |
* @param evt A PropertyChangeEvent object describing the event source and |
| 263 |
* the property that has changed. |
| 264 |
*/ |
| 265 |
|
| 266 |
public void propertyChange(PropertyChangeEvent evt) |
| 267 |
{ |
| 268 |
if ("zoom".equals(evt.getPropertyName()) == false) |
| 269 |
{ |
| 270 |
return; |
| 271 |
} |
| 272 |
|
| 273 |
final double zoom = getZoom(); |
| 274 |
pageDrawable.setZoom(zoom); |
| 275 |
zoomModel.setSelectedKey(new Double(zoom)); |
| 276 |
if (zoomModel.getSelectedKey() == null) |
| 277 |
{ |
| 278 |
zoomModel.setSelectedItem(formatZoomText(zoom)); |
| 279 |
} |
| 280 |
drawablePanel.revalidate(); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
private static final double ZOOM_FACTORS[] = { |
| 285 |
0.5, 0.75, 1, 1.20, 1.50, 2.00 |
| 286 |
}; |
| 287 |
private static final int DEFAULT_ZOOM_INDEX = 2; |
| 288 |
public static final String PAGE_NUMBER_PROPERTY = "pageNumber"; |
| 289 |
public static final String NUMBER_OF_PAGES_PROPERTY = "numberOfPages"; |
| 290 |
public static final String STATUS_TEXT_PROPERTY = "statusText"; |
| 291 |
public static final String STATUS_TYPE_PROPERTY = "statusType"; |
| 292 |
public static final String REPORT_CONTROLLER_PROPERTY = "reportController"; |
| 293 |
public static final String REPORT_JOB_PROPERTY = "reportJob"; |
| 294 |
public static final String ZOOM_PROPERTY = "zoom"; |
| 295 |
public static final String CLOSED_PROPERTY = "closed"; |
| 296 |
public static final String PAGINATING_PROPERTY = "paginating"; |
| 297 |
public static final String ICON_THEME_PROPERTY = "iconTheme"; |
| 298 |
public static final String TITLE_PROPERTY = "title"; |
| 299 |
public static final String MENU_PROPERTY = "menu"; |
| 300 |
|
| 301 |
private Object paginatingDrawable; |
| 302 |
private Object noReportDrawable; |
| 303 |
private PageBackgroundDrawable pageDrawable; |
| 304 |
|
| 305 |
private DrawablePanel drawablePanel; |
| 306 |
private ReportController reportController; |
| 307 |
private JMenu[] menus; |
| 308 |
private JToolBar toolBar; |
| 309 |
private String statusText; |
| 310 |
private String title; |
| 311 |
private int statusType; |
| 312 |
private boolean closed; |
| 313 |
private ReportJob reportJob; |
| 314 |
|
| 315 |
private int numberOfPages; |
| 316 |
private int pageNumber; |
| 317 |
private SwingGuiContext swingGuiContext; |
| 318 |
private IconTheme iconTheme; |
| 319 |
private double zoom; |
| 320 |
private boolean paginating; |
| 321 |
|
| 322 |
private PrintReportProcessor printReportProcessor; |
| 323 |
|
| 324 |
|
| 325 |
private Worker paginationWorker; |
| 326 |
private JPanel innerReportControllerHolder; |
| 327 |
private JPanel toolbarHolder; |
| 328 |
private JPanel outerReportControllerHolder; |
| 329 |
private boolean reportControllerInner; |
| 330 |
private String reportControllerLocation; |
| 331 |
private JComponent reportControllerComponent; |
| 332 |
private KeyedComboBoxModel zoomModel; |
| 333 |
|
| 334 |
|
| 335 |
/** |
| 336 |
* Creates a new <code>JPanel</code> with a double buffer and a flow layout. |
| 337 |
*/ |
| 338 |
public PreviewPane() |
| 339 |
{ |
| 340 |
this.menus = new JMenu[0]; |
| 341 |
setLayout(new BorderLayout()); |
| 342 |
|
| 343 |
zoomModel = new KeyedComboBoxModel(); |
| 344 |
zoomModel.setAllowOtherValue(true); |
| 345 |
zoom = ZOOM_FACTORS[DEFAULT_ZOOM_INDEX]; |
| 346 |
|
| 347 |
pageDrawable = new PageBackgroundDrawable(); |
| 348 |
|
| 349 |
drawablePanel = new DrawablePanel(); |
| 350 |
drawablePanel.setOpaque(false); |
| 351 |
drawablePanel.setBackground(Color.green); |
| 352 |
|
| 353 |
swingGuiContext = new PreviewGuiContext(); |
| 354 |
|
| 355 |
final JPanel reportPaneHolder = new JPanel(new CenterLayout()); |
| 356 |
reportPaneHolder.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); |
| 357 |
reportPaneHolder.add(drawablePanel); |
| 358 |
|
| 359 |
final JScrollPane s1 = new JScrollPane(reportPaneHolder); |
| 360 |
s1.getVerticalScrollBar().setUnitIncrement(20); |
| 361 |
|
| 362 |
innerReportControllerHolder = new JPanel(); |
| 363 |
innerReportControllerHolder.setLayout(new BorderLayout()); |
| 364 |
innerReportControllerHolder.add(s1, BorderLayout.CENTER); |
| 365 |
|
| 366 |
toolbarHolder = new JPanel(); |
| 367 |
toolbarHolder.setLayout(new BorderLayout()); |
| 368 |
toolbarHolder.add(innerReportControllerHolder, BorderLayout.CENTER); |
| 369 |
|
| 370 |
outerReportControllerHolder = new JPanel(); |
| 371 |
outerReportControllerHolder.setLayout(new BorderLayout()); |
| 372 |
outerReportControllerHolder.add(toolbarHolder, BorderLayout.CENTER); |
| 373 |
|
| 374 |
add(outerReportControllerHolder, BorderLayout.CENTER); |
| 375 |
|
| 376 |
addPropertyChangeListener(new PreviewUpdateHandler()); |
| 377 |
addPropertyChangeListener("zoom", new UpdateZoomHandler()); |
| 378 |
} |
| 379 |
|
| 380 |
public synchronized PrintReportProcessor getPrintReportProcessor() |
| 381 |
{ |
| 382 |
return printReportProcessor; |
| 383 |
} |
| 384 |
|
| 385 |
protected synchronized void setPrintReportProcessor(final PrintReportProcessor printReportProcessor) |
| 386 |
{ |
| 387 |
this.printReportProcessor = printReportProcessor; |
| 388 |
} |
| 389 |
|
| 390 |
public JMenu[] getMenu() |
| 391 |
{ |
| 392 |
return menus; |
| 393 |
} |
| 394 |
|
| 395 |
protected void setMenu(final JMenu[] menus) |
| 396 |
{ |
| 397 |
if (menus == null) |
| 398 |
{ |
| 399 |
throw new NullPointerException(); |
| 400 |
} |
| 401 |
final JMenu[] oldmenu = this.menus; |
| 402 |
this.menus = (JMenu[]) menus.clone(); |
| 403 |
firePropertyChange(MENU_PROPERTY, oldmenu, this.menus); |
| 404 |
} |
| 405 |
|
| 406 |
public JToolBar getToolBar() |
| 407 |
{ |
| 408 |
return toolBar; |
| 409 |
} |
| 410 |
|
| 411 |
public String getStatusText() |
| 412 |
{ |
| 413 |
return statusText; |
| 414 |
} |
| 415 |
|
| 416 |
public void setStatusText(final String statusText) |
| 417 |
{ |
| 418 |
String oldStatus = this.statusText; |
| 419 |
this.statusText = statusText; |
| 420 |
|
| 421 |
firePropertyChange(STATUS_TEXT_PROPERTY, oldStatus, statusText); |
| 422 |
} |
| 423 |
|
| 424 |
public int getStatusType() |
| 425 |
{ |
| 426 |
return statusType; |
| 427 |
} |
| 428 |
|
| 429 |
public void setStatusType(final int statusType) |
| 430 |
{ |
| 431 |
int oldType = this.statusType; |
| 432 |
this.statusType = statusType; |
| 433 |
|
| 434 |
firePropertyChange(STATUS_TYPE_PROPERTY, oldType, statusType); |
| 435 |
} |
| 436 |
|
| 437 |
public ReportController getReportController() |
| 438 |
{ |
| 439 |
return reportController; |
| 440 |
} |
| 441 |
|
| 442 |
public void setReportController(final ReportController reportController) |
| 443 |
{ |
| 444 |
ReportController oldController = this.reportController; |
| 445 |
this.reportController = reportController; |
| 446 |
firePropertyChange(REPORT_CONTROLLER_PROPERTY, oldController, reportController); |
| 447 |
|
| 448 |
// Now add the controller to the GUI .. |
| 449 |
refreshReportController(reportController); |
| 450 |
} |
| 451 |
|
| 452 |
public void refreshReportController(final ReportController newReportController) |
| 453 |
{ |
| 454 |
if (newReportController != null) |
| 455 |
{ |
| 456 |
final JComponent rcp = newReportController.getControlPanel(); |
| 457 |
// if either the controller component or its position (inner vs outer) |
| 458 |
// and border-position has changed, then refresh .. |
| 459 |
if (reportControllerComponent != rcp || |
| 460 |
reportControllerInner != newReportController.isInnerComponent() || |
| 461 |
ObjectUtilities.equal(reportControllerLocation, |
| 462 |
newReportController.getControllerLocation())) |
| 463 |
{ |
| 464 |
if (reportControllerComponent != null) |
| 465 |
{ |
| 466 |
outerReportControllerHolder.remove(reportControllerComponent); |
| 467 |
innerReportControllerHolder.remove(reportControllerComponent); |
| 468 |
} |
| 469 |
final String sanLocation = sanitizeLocation( |
| 470 |
newReportController.getControllerLocation()); |
| 471 |
final boolean innerComponent = newReportController.isInnerComponent(); |
| 472 |
if (rcp != null) |
| 473 |
{ |
| 474 |
if (innerComponent) |
| 475 |
{ |
| 476 |
innerReportControllerHolder.add(rcp, sanLocation); |
| 477 |
} |
| 478 |
else |
| 479 |
{ |
| 480 |
outerReportControllerHolder.add(rcp, sanLocation); |
| 481 |
} |
| 482 |
} |
| 483 |
reportControllerComponent = rcp; |
| 484 |
reportControllerLocation = sanLocation; |
| 485 |
reportControllerInner = innerComponent; |
| 486 |
} |
| 487 |
} |
| 488 |
else |
| 489 |
{ |
| 490 |
if (reportControllerComponent != null) |
| 491 |
{ |
| 492 |
outerReportControllerHolder.remove(reportControllerComponent); |
| 493 |
innerReportControllerHolder.remove(reportControllerComponent); |
| 494 |
} |
| 495 |
reportControllerComponent = null; |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
private String sanitizeLocation(final String location) |
| 501 |
{ |
| 502 |
if (BorderLayout.NORTH.equals(location)) |
| 503 |
{ |
| 504 |
return BorderLayout.NORTH; |
| 505 |
} |
| 506 |
if (BorderLayout.SOUTH.equals(location)) |
| 507 |
{ |
| 508 |
return BorderLayout.SOUTH; |
| 509 |
} |
| 510 |
if (BorderLayout.WEST.equals(location)) |
| 511 |
{ |
| 512 |
return BorderLayout.WEST; |
| 513 |
} |
| 514 |
if (BorderLayout.EAST.equals(location)) |
| 515 |
{ |
| 516 |
return BorderLayout.EAST; |
| 517 |
} |
| 518 |
return BorderLayout.NORTH; |
| 519 |
} |
| 520 |
|
| 521 |
public ReportJob getReportJob() |
| 522 |
{ |
| 523 |
return reportJob; |
| 524 |
} |
| 525 |
|
| 526 |
public void setReportJob(final ReportJob reportJob) |
| 527 |
{ |
| 528 |
ReportJob oldJob = this.reportJob; |
| 529 |
this.reportJob = reportJob; |
| 530 |
|
| 531 |
firePropertyChange(REPORT_JOB_PROPERTY, oldJob, reportJob); |
| 532 |
if (reportJob == null) |
| 533 |
{ |
| 534 |
initializeWithoutJob(); |
| 535 |
} |
| 536 |
else |
| 537 |
{ |
| 538 |
initializeFromReport(); |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
public double getZoom() |
| 543 |
{ |
| 544 |
return zoom; |
| 545 |
} |
| 546 |
|
| 547 |
public void setZoom(final double zoom) |
| 548 |
{ |
| 549 |
double oldZoom = this.zoom; |
| 550 |
this.zoom = zoom; |
| 551 |
firePropertyChange(ZOOM_PROPERTY, oldZoom, zoom); |
| 552 |
} |
| 553 |
|
| 554 |
public boolean isClosed() |
| 555 |
{ |
| 556 |
return closed; |
| 557 |
} |
| 558 |
|
| 559 |
public void setClosed(final boolean closed) |
| 560 |
{ |
| 561 |
boolean oldClosed = this.closed; |
| 562 |
this.closed = closed; |
| 563 |
firePropertyChange(CLOSED_PROPERTY, oldClosed, closed); |
| 564 |
if (closed) |
| 565 |
{ |
| 566 |
prepareShutdown(); |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
private void prepareShutdown() |
| 571 |
{ |
| 572 |
synchronized (this) |
| 573 |
{ |
| 574 |
if (paginationWorker != null) |
| 575 |
{ |
| 576 |
synchronized (paginationWorker) |
| 577 |
{ |
| 578 |
paginationWorker.finish(); |
| 579 |
} |
| 580 |
paginationWorker = null; |
| 581 |
} |
| 582 |
if (printReportProcessor != null) |
| 583 |
{ |
| 584 |
printReportProcessor.close(); |
| 585 |
printReportProcessor = null; |
| 586 |
} |
| 587 |
closeToolbar(); |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
private int getUserDefinedCategoryPosition() |
| 592 |
{ |
| 593 |
return TextUtilities.parseInt |
| 594 |
(swingGuiContext.getConfiguration().getConfigProperty |
| 595 |
("org.jfree.report.modules.gui.swing.user-defined-category.position"), 15000); |
| 596 |
} |
| 597 |
|
| 598 |
|
| 599 |
public Locale getLocale() |
| 600 |
{ |
| 601 |
ReportStructureRoot report = getReportJob().getReportStructureRoot(); |
| 602 |
if (report != null) |
| 603 |
{ |
| 604 |
return report.getLocale(); |
| 605 |
} |
| 606 |
return super.getLocale(); |
| 607 |
} |
| 608 |
|
| 609 |
public int getNumberOfPages() |
| 610 |
{ |
| 611 |
return numberOfPages; |
| 612 |
} |
| 613 |
|
| 614 |
public void setNumberOfPages(final int numberOfPages) |
| 615 |
{ |
| 616 |
final int oldPageNumber = this.numberOfPages; |
| 617 |
this.numberOfPages = numberOfPages; |
| 618 |
firePropertyChange(NUMBER_OF_PAGES_PROPERTY, oldPageNumber, numberOfPages); |
| 619 |
} |
| 620 |
|
| 621 |
public int getPageNumber() |
| 622 |
{ |
| 623 |
return pageNumber; |
| 624 |
} |
| 625 |
|
| 626 |
public void setPageNumber(final int pageNumber) |
| 627 |
{ |
| 628 |
final int oldPageNumber = this.pageNumber; |
| 629 |
this.pageNumber = pageNumber; |
| 630 |
firePropertyChange(PAGE_NUMBER_PROPERTY, oldPageNumber, pageNumber); |
| 631 |
} |
| 632 |
|
| 633 |
public IconTheme getIconTheme() |
| 634 |
{ |
| 635 |
return iconTheme; |
| 636 |
} |
| 637 |
|
| 638 |
protected void setIconTheme(IconTheme theme) |
| 639 |
{ |
| 640 |
IconTheme oldTheme = this.iconTheme; |
| 641 |
this.iconTheme = theme; |
| 642 |
firePropertyChange(ICON_THEME_PROPERTY, oldTheme, theme); |
| 643 |
} |
| 644 |
|
| 645 |
protected void initializeFromReport() |
| 646 |
{ |
| 647 |
setIconTheme(PreviewPaneUtilities.createIconTheme(reportJob.getConfiguration())); |
| 648 |
|
| 649 |
zoomModel.clear(); |
| 650 |
for (int i = 0; i < ZOOM_FACTORS.length; i++) |
| 651 |
{ |
| 652 |
zoomModel.add(new Double(ZOOM_FACTORS[i]), formatZoomText(ZOOM_FACTORS[i])); |
| 653 |
} |
| 654 |
zoom = ZOOM_FACTORS[DEFAULT_ZOOM_INDEX]; |
| 655 |
zoomModel.setSelectedKey(new Double(ZOOM_FACTORS[DEFAULT_ZOOM_INDEX])); |
| 656 |
|
| 657 |
HashMap actions = PreviewPaneUtilities.loadActions(swingGuiContext); |
| 658 |
buildMenu(actions); |
| 659 |
|
| 660 |
|
| 661 |
if (toolBar != null) |
| 662 |
{ |
| 663 |
toolbarHolder.remove(toolBar); |
| 664 |
} |
| 665 |
toolBar = buildToolbar(actions); |
| 666 |
if (toolBar != null) |
| 667 |
{ |
| 668 |
toolbarHolder.add(toolBar, BorderLayout.NORTH); |
| 669 |
} |
| 670 |
|
| 671 |
startPagination(); |
| 672 |
} |
| 673 |
|
| 674 |
private JToolBar buildToolbar(final HashMap actions) |
| 675 |
{ |
| 676 |
JToolBar toolBar = new JToolBar(); |
| 677 |
toolBar.setFloatable(false); |
| 678 |
|
| 679 |
final ActionCategory[] cats = (ActionCategory[]) |
| 680 |
actions.keySet().toArray(new ActionCategory[actions.size()]); |
| 681 |
Arrays.sort(cats); |
| 682 |
|
| 683 |
for (int i = 0; i < cats.length; i++) |
| 684 |
{ |
| 685 |
final ActionCategory cat = cats[i]; |
| 686 |
final ActionPlugin[] plugins = (ActionPlugin[]) actions.get(cat); |
| 687 |
PreviewPaneUtilities.addActionsToToolBar(toolBar, plugins, this); |
| 688 |
} |
| 689 |
|
| 690 |
return toolBar; |
| 691 |
} |
| 692 |
|
| 693 |
private void closeToolbar() |
| 694 |
{ |
| 695 |
if (toolBar.getParent() != toolbarHolder) |
| 696 |
{ |
| 697 |
// ha!, we detected that the toolbar is floating ... |
| 698 |
// Log.debug (currentToolbar.getParent()); |
| 699 |
final Window w = SwingUtilities.windowForComponent(toolBar); |
| 700 |
if (w != null) |
| 701 |
{ |
| 702 |
w.setVisible(false); |
| 703 |
w.dispose(); |
| 704 |
} |
| 705 |
} |
| 706 |
toolBar.setVisible(false); |
| 707 |
} |
| 708 |
|
| 709 |
public SwingGuiContext getSwingGuiContext() |
| 710 |
{ |
| 711 |
return swingGuiContext; |
| 712 |
} |
| 713 |
|
| 714 |
public KeyedComboBoxModel getZoomModel() |
| 715 |
{ |
| 716 |
return zoomModel; |
| 717 |
} |
| 718 |
|
| 719 |
private String formatZoomText(final double zoom) |
| 720 |
{ |
| 721 |
final NumberFormat numberFormat = |
| 722 |
NumberFormat.getPercentInstance(swingGuiContext.getLocale()); |
| 723 |
return (numberFormat.format(zoom)); |
| 724 |
} |
| 725 |
|
| 726 |
|
| 727 |
private void buildMenu(final HashMap actions) |
| 728 |
{ |
| 729 |
final HashMap menus = new HashMap(); |
| 730 |
final int userPos = getUserDefinedCategoryPosition(); |
| 731 |
|
| 732 |
final ActionCategory[] categories = new ActionCategory[actions.size()]; |
| 733 |
boolean insertedUserDefinedActions = false; |
| 734 |
int catCount = 0; |
| 735 |
final Iterator iterator = actions.entrySet().iterator(); |
| 736 |
while (iterator.hasNext()) |
| 737 |
{ |
| 738 |
final Map.Entry entry = (Map.Entry) iterator.next(); |
| 739 |
final ActionCategory cat = (ActionCategory) entry.getKey(); |
| 740 |
categories[catCount] = cat; |
| 741 |
catCount += 1; |
| 742 |
final ActionPlugin[] plugins = (ActionPlugin[]) entry.getValue(); |
| 743 |
|
| 744 |
if (insertedUserDefinedActions == false && cat.getPosition() > userPos) |
| 745 |
{ |
| 746 |
ReportController controller = getReportController(); |
| 747 |
if (controller != null) |
| 748 |
{ |
| 749 |
controller.initialize(this); |
| 750 |
final JMenu[] controlerMenus = controller.getMenus(); |
| 751 |
for (int i = 0; i < controlerMenus.length; i++) |
| 752 |
{ |
| 753 |
final ActionCategory userCategory = new ActionCategory(); |
| 754 |
userCategory.setName("X-User-Category-" + i); |
| 755 |
userCategory.setPosition(userPos + i); |
| 756 |
menus.put(userCategory, controlerMenus[i]); |
| 757 |
} |
| 758 |
} |
| 759 |
|
| 760 |
insertedUserDefinedActions = true; |
| 761 |
} |
| 762 |
|
| 763 |
final JMenu menu = PreviewPaneUtilities.createMenu(cat); |
| 764 |
int count = PreviewPaneUtilities.buildMenu(menu, plugins, this); |
| 765 |
menus.put(cat, menu); |
| 766 |
} |
| 767 |
|
| 768 |
final CategoryTreeItem[] categoryTreeItems = |
| 769 |
PreviewPaneUtilities.buildMenuTree(categories); |
| 770 |
|
| 771 |
ArrayList menuList = new ArrayList(); |
| 772 |
for (int i = 0; i < categoryTreeItems.length; i++) |
| 773 |
{ |
| 774 |
final CategoryTreeItem item = categoryTreeItems[i]; |
| 775 |
final JMenu menu = (JMenu) menus.get(item.getCategory()); |
| 776 |
// now connect all menus .. |
| 777 |
final CategoryTreeItem[] childs = item.getChilds(); |
| 778 |
Arrays.sort(childs); |
| 779 |
for (int j = 0; j < childs.length; j++) |
| 780 |
{ |
| 781 |
CategoryTreeItem child = childs[j]; |
| 782 |
final JMenu childMenu = (JMenu) menus.get(child.getCategory()); |
| 783 |
if (childMenu != null) |
| 784 |
{ |
| 785 |
menu.add(childMenu); |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
if (item.getParent() == null) |
| 790 |
{ |
| 791 |
menuList.add(item); |
| 792 |
} |
| 793 |
} |
| 794 |
|
| 795 |
Collections.sort(menuList); |
| 796 |
ArrayList retval = new ArrayList(); |
| 797 |
for (int i = 0; i < menuList.size(); i++) |
| 798 |
{ |
| 799 |
final CategoryTreeItem item = (CategoryTreeItem) menuList.get(i); |
| 800 |
JMenu menu = (JMenu) menus.get(item.getCategory()); |
| 801 |
if (menu.getItemCount() > 0) |
| 802 |
{ |
| 803 |
retval.add(menu); |
| 804 |
} |
| 805 |
} |
| 806 |
|
| 807 |
setMenu((JMenu[]) retval.toArray(new JMenu[retval.size()])); |
| 808 |
} |
| 809 |
|
| 810 |
// private JMenu createViewMenu(ActionCategory cat) |
| 811 |
// { |
| 812 |
// JMenu zoom = new JMenu("Zoom"); |
| 813 |
// zoom.add(new ActionMenuItem(new ZoomOutAction(this))); |
| 814 |
// zoom.add(new ActionMenuItem(new ZoomInAction(this))); |
| 815 |
// zoom.addSeparator(); |
| 816 |
// |
| 817 |
// for (int i = 0; i < ZOOM_FACTORS.length; i++) |
| 818 |
// { |
| 819 |
// double factor = ZOOM_FACTORS[i]; |
| 820 |
// zoom.add(new ActionMenuItem(new ZoomAction(factor, this))); |
| 821 |
// } |
| 822 |
// |
| 823 |
// zoom.addSeparator(); |
| 824 |
// zoom.add(new ActionMenuItem(new ZoomCustomAction(this))); |
| 825 |
// |
| 826 |
// JMenu menu = new JMenu("View"); |
| 827 |
// menu.add(zoom); |
| 828 |
// menu.addSeparator(); |
| 829 |
// menu.add(new ActionMenuItem("Paginated")); |
| 830 |
// menu.add(new ActionMenuItem("Flow")); |
| 831 |
// return menu; |
| 832 |
// } |
| 833 |
|
| 834 |
protected void initializeWithoutJob() |
| 835 |
{ |
| 836 |
final Configuration globalConfig = |
| 837 |
JFreeReportBoot.getInstance().getGlobalConfig(); |
| 838 |
setIconTheme(PreviewPaneUtilities.createIconTheme(globalConfig)); |
| 839 |
|
| 840 |
zoomModel.clear(); |
| 841 |
for (int i = 0; i < ZOOM_FACTORS.length; i++) |
| 842 |
{ |
| 843 |
zoomModel.add(new Double(ZOOM_FACTORS[i]), formatZoomText(ZOOM_FACTORS[i])); |
| 844 |
} |
| 845 |
zoom = ZOOM_FACTORS[DEFAULT_ZOOM_INDEX]; |
| 846 |
zoomModel.setSelectedKey(new Double(ZOOM_FACTORS[DEFAULT_ZOOM_INDEX])); |
| 847 |
|
| 848 |
HashMap actions = PreviewPaneUtilities.loadActions(swingGuiContext); |
| 849 |
buildMenu(actions); |
| 850 |
if (toolBar != null) |
| 851 |
{ |
| 852 |
toolbarHolder.remove(toolBar); |
| 853 |
} |
| 854 |
toolBar = buildToolbar(actions); |
| 855 |
if (toolBar != null) |
| 856 |
{ |
| 857 |
toolbarHolder.add(toolBar, BorderLayout.NORTH); |
| 858 |
} |
| 859 |
|
| 860 |
} |
| 861 |
|
| 862 |
public String getTitle() |
| 863 |
{ |
| 864 |
return title; |
| 865 |
} |
| 866 |
|
| 867 |
public void setTitle(final String title) |
| 868 |
{ |
| 869 |
String oldTitle = this.title; |
| 870 |
this.title = title; |
| 871 |
firePropertyChange(TITLE_PROPERTY, oldTitle, title); |
| 872 |
} |
| 873 |
|
| 874 |
public double[] getZoomFactors() |
| 875 |
{ |
| 876 |
return (double[]) ZOOM_FACTORS.clone(); |
| 877 |
} |
| 878 |
|
| 879 |
public boolean isPaginating() |
| 880 |
{ |
| 881 |
return paginating; |
| 882 |
} |
| 883 |
|
| 884 |
public void setPaginating(final boolean paginating) |
| 885 |
{ |
| 886 |
boolean oldPaginating = this.paginating; |
| 887 |
this.paginating = paginating; |
| 888 |
firePropertyChange(PAGINATING_PROPERTY, oldPaginating, paginating); |
| 889 |
} |
| 890 |
|
| 891 |
private synchronized void startPagination() |
| 892 |
{ |
| 893 |
if (paginationWorker != null) |
| 894 |
{ |
| 895 |
// make sure that old pagination handler does not run longer than |
| 896 |
// necessary.. |
| 897 |
synchronized(paginationWorker) |
| 898 |
{ |
| 899 |
paginationWorker.finish(); |
| 900 |
} |
| 901 |
paginationWorker = null; |
| 902 |
} |
| 903 |
|
| 904 |
if (printReportProcessor != null) |
| 905 |
{ |
| 906 |
printReportProcessor.close(); |
| 907 |
printReportProcessor = null; |
| 908 |
} |
| 909 |
|
| 910 |
final ReportJob reportJob = getReportJob(); |
| 911 |
printReportProcessor = new PrintReportProcessor(reportJob.derive()); |
| 912 |
|
| 913 |
paginationWorker = new Worker(); |
| 914 |
paginationWorker.setWorkload |
| 915 |
(new RepaginationRunnable(printReportProcessor)); |
| 916 |
} |
| 917 |
|
| 918 |
public Object getNoReportDrawable() |
| 919 |
{ |
| 920 |
return noReportDrawable; |
| 921 |
} |
| 922 |
|
| 923 |
public void setNoReportDrawable(final Object noReportDrawable) |
| 924 |
{ |
| 925 |
this.noReportDrawable = noReportDrawable; |
| 926 |
} |
| 927 |
|
| 928 |
public Object getPaginatingDrawable() |
| 929 |
{ |
| 930 |
return paginatingDrawable; |
| 931 |
} |
| 932 |
|
| 933 |
public void setPaginatingDrawable(final Object paginatingDrawable) |
| 934 |
{ |
| 935 |
this.paginatingDrawable = paginatingDrawable; |
| 936 |
} |
| 937 |
|
| 938 |
protected void updateVisiblePage(int pageNumber) |
| 939 |
{ |
| 940 |
// |
| 941 |
if (printReportProcessor == null) |
| 942 |
{ |
| 943 |
throw new IllegalStateException(); |
| 944 |
} |
| 945 |
|
| 946 |
// todo: This can be very expensive - so we better move this off the event-dispatcher |
| 947 |
final int pageIndex = getPageNumber() - 1; |
| 948 |
if (pageIndex < 0 || pageIndex >= printReportProcessor.getNumberOfPages()) |
| 949 |
{ |
| 950 |
drawablePanel.setDrawable(null); |
| 951 |
pageDrawable.setBackend(null); |
| 952 |
} |
| 953 |
else |
| 954 |
{ |
| 955 |
final PageDrawable drawable = printReportProcessor.getPageDrawable(pageIndex); |
| 956 |
this.pageDrawable.setBackend(drawable); |
| 957 |
this.drawablePanel.setDrawableAsRawObject(pageDrawable); |
| 958 |
} |
| 959 |
} |
| 960 |
} |