| 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: PreviewPaneUtilities.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.Component; |
| 35 |
import java.awt.FlowLayout; |
| 36 |
import java.awt.Insets; |
| 37 |
import java.awt.event.ActionEvent; |
| 38 |
import java.util.ArrayList; |
| 39 |
import java.util.Arrays; |
| 40 |
import java.util.HashMap; |
| 41 |
import java.util.Iterator; |
| 42 |
import javax.swing.AbstractAction; |
| 43 |
import javax.swing.Action; |
| 44 |
import javax.swing.Icon; |
| 45 |
import javax.swing.JButton; |
| 46 |
import javax.swing.JComboBox; |
| 47 |
import javax.swing.JMenu; |
| 48 |
import javax.swing.JPanel; |
| 49 |
import javax.swing.JToolBar; |
| 50 |
|
| 51 |
import org.jfree.report.modules.gui.common.DefaultIconTheme; |
| 52 |
import org.jfree.report.modules.gui.common.IconTheme; |
| 53 |
import org.jfree.report.modules.gui.swing.common.ActionFactory; |
| 54 |
import org.jfree.report.modules.gui.swing.common.ActionPlugin; |
| 55 |
import org.jfree.report.modules.gui.swing.common.ActionPluginMenuComparator; |
| 56 |
import org.jfree.report.modules.gui.swing.common.DefaultActionFactory; |
| 57 |
import org.jfree.report.modules.gui.swing.common.ExportActionPlugin; |
| 58 |
import org.jfree.report.modules.gui.swing.common.SwingCommonModule; |
| 59 |
import org.jfree.report.modules.gui.swing.common.SwingGuiContext; |
| 60 |
import org.jfree.report.modules.gui.swing.common.KeyedComboBoxModel; |
| 61 |
import org.jfree.report.modules.gui.swing.common.action.ActionMenuItem; |
| 62 |
import org.jfree.report.modules.gui.swing.common.action.ActionButton; |
| 63 |
import org.jfree.report.modules.gui.swing.preview.actions.ControlAction; |
| 64 |
import org.jfree.report.modules.gui.swing.preview.actions.ControlActionPlugin; |
| 65 |
import org.jfree.report.modules.gui.swing.preview.actions.ExportAction; |
| 66 |
import org.jfree.report.modules.gui.swing.preview.actions.ZoomAction; |
| 67 |
import org.jfree.report.modules.gui.swing.preview.actions.ZoomListActionPlugin; |
| 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: 17.11.2006, 15:06:51 |
| 74 |
* |
| 75 |
* @author Thomas Morgner |
| 76 |
*/ |
| 77 |
public class PreviewPaneUtilities |
| 78 |
{ |
| 79 |
/** |
| 80 |
* A zoom select action. |
| 81 |
*/ |
| 82 |
private static class ZoomSelectAction extends AbstractAction |
| 83 |
{ |
| 84 |
private KeyedComboBoxModel source; |
| 85 |
private PreviewPane pane; |
| 86 |
|
| 87 |
/** |
| 88 |
* Creates a new action. |
| 89 |
*/ |
| 90 |
public ZoomSelectAction(KeyedComboBoxModel source, |
| 91 |
PreviewPane pane) |
| 92 |
{ |
| 93 |
this.source = source; |
| 94 |
this.pane = pane; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Invoked when an action occurs. |
| 99 |
* |
| 100 |
* @param e the event. |
| 101 |
*/ |
| 102 |
public void actionPerformed(final ActionEvent e) |
| 103 |
{ |
| 104 |
final Double selected = (Double) source.getSelectedKey(); |
| 105 |
if (selected != null) |
| 106 |
{ |
| 107 |
pane.setZoom(selected.doubleValue()); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
private static final String ICON_THEME_CONFIG_KEY = "org.jfree.report.modules.gui.common.IconTheme"; |
| 113 |
private static final String ACTION_FACTORY_CONFIG_KEY = "org.jfree.report.modules.gui.swing.preview.ActionFactory"; |
| 114 |
private static final String CATEGORY_PREFIX = "org.jfree.report.modules.gui.swing.category."; |
| 115 |
|
| 116 |
private PreviewPaneUtilities() |
| 117 |
{ |
| 118 |
} |
| 119 |
|
| 120 |
public static JMenu createMenu(ActionCategory cat) |
| 121 |
{ |
| 122 |
JMenu menu = new JMenu(); |
| 123 |
menu.setText(cat.getDisplayName()); |
| 124 |
final Integer mnemonicKey = cat.getMnemonicKey(); |
| 125 |
if (mnemonicKey != null) |
| 126 |
{ |
| 127 |
menu.setMnemonic(mnemonicKey.intValue()); |
| 128 |
} |
| 129 |
final String toolTip = cat.getShortDescription(); |
| 130 |
if (toolTip != null && toolTip.length() > 0) |
| 131 |
{ |
| 132 |
menu.setToolTipText(toolTip); |
| 133 |
} |
| 134 |
return menu; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
public static int buildMenu(JMenu menu, |
| 139 |
ActionPlugin[] actions, |
| 140 |
PreviewPane pane) |
| 141 |
{ |
| 142 |
if (actions.length == 0) |
| 143 |
{ |
| 144 |
return 0; |
| 145 |
} |
| 146 |
|
| 147 |
Arrays.sort(actions, new ActionPluginMenuComparator()); |
| 148 |
boolean separatorPending = false; |
| 149 |
int count = 0; |
| 150 |
for (int i = 0; i < actions.length; i++) |
| 151 |
{ |
| 152 |
ActionPlugin actionPlugin = actions[i]; |
| 153 |
if (actionPlugin.isAddToMenu() == false) |
| 154 |
{ |
| 155 |
continue; |
| 156 |
} |
| 157 |
|
| 158 |
if (count > 0 && separatorPending) |
| 159 |
{ |
| 160 |
menu.addSeparator(); |
| 161 |
separatorPending = false; |
| 162 |
} |
| 163 |
|
| 164 |
if (actionPlugin instanceof ExportActionPlugin) |
| 165 |
{ |
| 166 |
final ExportActionPlugin exportPlugin = (ExportActionPlugin) actionPlugin; |
| 167 |
final ExportAction action = new ExportAction(exportPlugin, pane); |
| 168 |
menu.add(new ActionMenuItem(action)); |
| 169 |
count += 1; |
| 170 |
} |
| 171 |
else if (actionPlugin instanceof ControlActionPlugin) |
| 172 |
{ |
| 173 |
final ControlActionPlugin controlPlugin = (ControlActionPlugin) actionPlugin; |
| 174 |
final ControlAction action = new ControlAction(controlPlugin, pane); |
| 175 |
menu.add(new ActionMenuItem(action)); |
| 176 |
count += 1; |
| 177 |
} |
| 178 |
else if (actionPlugin instanceof ZoomListActionPlugin) |
| 179 |
{ |
| 180 |
buildViewMenu(menu, pane); |
| 181 |
} |
| 182 |
|
| 183 |
if (actionPlugin.isSeparated()) |
| 184 |
{ |
| 185 |
separatorPending = true; |
| 186 |
} |
| 187 |
|
| 188 |
} |
| 189 |
return count; |
| 190 |
} |
| 191 |
|
| 192 |
private static void buildViewMenu(JMenu zoom, PreviewPane pane) |
| 193 |
{ |
| 194 |
final double[] zoomFactors = pane.getZoomFactors(); |
| 195 |
for (int i = 0; i < zoomFactors.length; i++) |
| 196 |
{ |
| 197 |
double factor = zoomFactors[i]; |
| 198 |
zoom.add(new ActionMenuItem(new ZoomAction(factor, pane))); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
public static void addActionsToToolBar(final JToolBar toolBar, |
| 203 |
final ActionPlugin[] reportActions, |
| 204 |
final PreviewPane pane) |
| 205 |
{ |
| 206 |
if (reportActions == null) |
| 207 |
{ |
| 208 |
return; |
| 209 |
} |
| 210 |
|
| 211 |
boolean separatorPending = false; |
| 212 |
int count = 0; |
| 213 |
|
| 214 |
for (int i = 0; i < reportActions.length; i++) |
| 215 |
{ |
| 216 |
ActionPlugin actionPlugin = reportActions[i]; |
| 217 |
if (actionPlugin.isAddToToolbar() == false) |
| 218 |
{ |
| 219 |
continue; |
| 220 |
} |
| 221 |
|
| 222 |
if (count > 0 && separatorPending) |
| 223 |
{ |
| 224 |
toolBar.addSeparator(); |
| 225 |
separatorPending = false; |
| 226 |
} |
| 227 |
|
| 228 |
if (actionPlugin instanceof ExportActionPlugin) |
| 229 |
{ |
| 230 |
final ExportActionPlugin exportPlugin = (ExportActionPlugin) actionPlugin; |
| 231 |
final ExportAction action = new ExportAction(exportPlugin, pane); |
| 232 |
toolBar.add(createButton(action, pane.getSwingGuiContext())); |
| 233 |
count += 1; |
| 234 |
} |
| 235 |
else if (actionPlugin instanceof ControlActionPlugin) |
| 236 |
{ |
| 237 |
final ControlActionPlugin controlPlugin = (ControlActionPlugin) actionPlugin; |
| 238 |
final ControlAction action = new ControlAction(controlPlugin, pane); |
| 239 |
toolBar.add(createButton(action, pane.getSwingGuiContext())); |
| 240 |
count += 1; |
| 241 |
} |
| 242 |
else if (actionPlugin instanceof ZoomListActionPlugin) |
| 243 |
{ |
| 244 |
final JPanel zoomPane = new JPanel(); |
| 245 |
zoomPane.setLayout(new FlowLayout(FlowLayout.LEFT)); |
| 246 |
zoomPane.add(createZoomSelector(pane)); |
| 247 |
toolBar.add(zoomPane); |
| 248 |
count += 1; |
| 249 |
} |
| 250 |
|
| 251 |
if (actionPlugin.isSeparated()) |
| 252 |
{ |
| 253 |
separatorPending = true; |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
private static JComboBox createZoomSelector(PreviewPane pane) |
| 260 |
{ |
| 261 |
final JComboBox zoomSelect = new JComboBox(pane.getZoomModel()); |
| 262 |
zoomSelect.addActionListener(new ZoomSelectAction(pane.getZoomModel(), pane)); |
| 263 |
zoomSelect.setAlignmentX(Component.RIGHT_ALIGNMENT); |
| 264 |
return zoomSelect; |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Creates a button using the given action properties for the button's |
| 269 |
* initialisation. |
| 270 |
* |
| 271 |
* @param action the action used to set up the button. |
| 272 |
* @return a button based on the supplied action. |
| 273 |
*/ |
| 274 |
private static JButton createButton(final Action action, |
| 275 |
final SwingGuiContext swingGuiContext) |
| 276 |
{ |
| 277 |
final JButton button = new ActionButton(action); |
| 278 |
boolean needText = true; |
| 279 |
if (isLargeButtonsEnabled(swingGuiContext)) |
| 280 |
{ |
| 281 |
final Icon icon = (Icon) action.getValue(SwingCommonModule.LARGE_ICON_PROPERTY); |
| 282 |
if (icon != null && (icon.getIconHeight() > 1 && icon.getIconHeight() > 1)) |
| 283 |
{ |
| 284 |
button.setIcon(icon); |
| 285 |
needText = false; |
| 286 |
} |
| 287 |
} |
| 288 |
else |
| 289 |
{ |
| 290 |
final Icon icon = (Icon) action.getValue(Action.SMALL_ICON); |
| 291 |
if (icon != null && (icon.getIconHeight() > 1 && icon.getIconHeight() > 1)) |
| 292 |
{ |
| 293 |
button.setIcon(icon); |
| 294 |
needText = false; |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
if (needText) |
| 299 |
{ |
| 300 |
final Object value = action.getValue(Action.NAME); |
| 301 |
if (value != null) |
| 302 |
{ |
| 303 |
button.setText(String.valueOf(value)); |
| 304 |
} |
| 305 |
} |
| 306 |
else |
| 307 |
{ |
| 308 |
button.setText(null); |
| 309 |
button.setMargin(new Insets(0, 0, 0, 0)); |
| 310 |
} |
| 311 |
|
| 312 |
return button; |
| 313 |
} |
| 314 |
|
| 315 |
private static boolean isLargeButtonsEnabled(SwingGuiContext swingGuiContext) |
| 316 |
{ |
| 317 |
final Configuration configuration = swingGuiContext.getConfiguration(); |
| 318 |
if ("true".equals(configuration.getConfigProperty |
| 319 |
("org.jfree.report.modules.gui.swing.preview.LargeIcons"))) |
| 320 |
{ |
| 321 |
return true; |
| 322 |
} |
| 323 |
return false; |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
public static double getNextZoomOut(final double zoom, |
| 328 |
final double[] zoomFactors) |
| 329 |
{ |
| 330 |
if (zoom <= zoomFactors[0]) |
| 331 |
{ |
| 332 |
return (zoom * 2.0) / 3.0; |
| 333 |
} |
| 334 |
|
| 335 |
final double largestZoom = zoomFactors[zoomFactors.length - 1]; |
| 336 |
if (zoom > largestZoom) |
| 337 |
{ |
| 338 |
double linear = (zoom * 2.0) / 3.0; |
| 339 |
if (linear < largestZoom) |
| 340 |
{ |
| 341 |
return largestZoom; |
| 342 |
} |
| 343 |
return linear; |
| 344 |
} |
| 345 |
|
| 346 |
for (int i = zoomFactors.length - 1; i >= 0; i--) |
| 347 |
{ |
| 348 |
double factor = zoomFactors[i]; |
| 349 |
if (factor < zoom) |
| 350 |
{ |
| 351 |
return factor; |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
return (zoom * 2.0) / 3.0; |
| 356 |
} |
| 357 |
|
| 358 |
public static double getNextZoomIn(final double zoom, |
| 359 |
final double[] zoomFactors) |
| 360 |
{ |
| 361 |
final double largestZoom = zoomFactors[zoomFactors.length - 1]; |
| 362 |
if (zoom >= largestZoom) |
| 363 |
{ |
| 364 |
return (zoom * 1.5); |
| 365 |
} |
| 366 |
|
| 367 |
final double smallestZoom = zoomFactors[0]; |
| 368 |
if (zoom < smallestZoom) |
| 369 |
{ |
| 370 |
double linear = (zoom * 1.5); |
| 371 |
if (linear > smallestZoom) |
| 372 |
{ |
| 373 |
return smallestZoom; |
| 374 |
} |
| 375 |
return linear; |
| 376 |
} |
| 377 |
|
| 378 |
for (int i = 0; i < zoomFactors.length; i++) |
| 379 |
{ |
| 380 |
double factor = zoomFactors[i]; |
| 381 |
if (factor > zoom) |
| 382 |
{ |
| 383 |
return factor; |
| 384 |
} |
| 385 |
} |
| 386 |
return (zoom * 1.5); |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
public static IconTheme createIconTheme(Configuration config) |
| 391 |
{ |
| 392 |
String themeClass = config.getConfigProperty(ICON_THEME_CONFIG_KEY); |
| 393 |
Object maybeTheme = ObjectUtilities.loadAndInstantiate(themeClass, PreviewPane.class, IconTheme.class); |
| 394 |
IconTheme iconTheme; |
| 395 |
if (maybeTheme != null) |
| 396 |
{ |
| 397 |
iconTheme = (IconTheme) maybeTheme; |
| 398 |
} |
| 399 |
else |
| 400 |
{ |
| 401 |
iconTheme = new DefaultIconTheme(); |
| 402 |
} |
| 403 |
iconTheme.initialize(config); |
| 404 |
return iconTheme; |
| 405 |
} |
| 406 |
|
| 407 |
public static ActionFactory createActionFactory(Configuration config) |
| 408 |
{ |
| 409 |
final String factoryClass = config.getConfigProperty(ACTION_FACTORY_CONFIG_KEY); |
| 410 |
Object maybeFactory = ObjectUtilities.loadAndInstantiate |
| 411 |
(factoryClass, PreviewPane.class, ActionFactory.class); |
| 412 |
ActionFactory actionFactory; |
| 413 |
if (maybeFactory != null) |
| 414 |
{ |
| 415 |
actionFactory = (ActionFactory) maybeFactory; |
| 416 |
} |
| 417 |
else |
| 418 |
{ |
| 419 |
actionFactory = new DefaultActionFactory(); |
| 420 |
} |
| 421 |
return actionFactory; |
| 422 |
} |
| 423 |
|
| 424 |
public static CategoryTreeItem[] buildMenuTree(final ActionCategory[] categories) |
| 425 |
{ |
| 426 |
final CategoryTreeItem[] tree = new CategoryTreeItem[categories.length]; |
| 427 |
for (int i = 0; i < categories.length; i++) |
| 428 |
{ |
| 429 |
ActionCategory category = categories[i]; |
| 430 |
tree[i] = new CategoryTreeItem(category); |
| 431 |
} |
| 432 |
|
| 433 |
for (int j = 0; j < tree.length; j++) |
| 434 |
{ |
| 435 |
final CategoryTreeItem item = tree[j]; |
| 436 |
final String itemName = item.getName(); |
| 437 |
int parentWeight = 0; |
| 438 |
CategoryTreeItem parent = null; |
| 439 |
// now for each item, find the best parent item. |
| 440 |
for (int k = 0; k < tree.length; k++) |
| 441 |
{ |
| 442 |
if (k == j) |
| 443 |
{ |
| 444 |
// never add yourself .. |
| 445 |
continue; |
| 446 |
} |
| 447 |
CategoryTreeItem treeItem = tree[k]; |
| 448 |
final String parentName = treeItem.getName(); |
| 449 |
if (itemName.startsWith(parentName) == false) |
| 450 |
{ |
| 451 |
continue; |
| 452 |
} |
| 453 |
if (parentName.length() > parentWeight) |
| 454 |
{ |
| 455 |
parent = treeItem; |
| 456 |
parentWeight = parentName.length(); |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
item.setParent(parent); |
| 461 |
} |
| 462 |
|
| 463 |
for (int j = 0; j < tree.length; j++) |
| 464 |
{ |
| 465 |
final CategoryTreeItem item = tree[j]; |
| 466 |
final CategoryTreeItem parent = item.getParent(); |
| 467 |
if (parent != null) |
| 468 |
{ |
| 469 |
parent.add(item); |
| 470 |
} |
| 471 |
} |
| 472 |
return tree; |
| 473 |
} |
| 474 |
|
| 475 |
public static HashMap loadActions(SwingGuiContext swingGuiContext) |
| 476 |
{ |
| 477 |
HashMap actions = new HashMap(); |
| 478 |
|
| 479 |
final Configuration configuration = swingGuiContext.getConfiguration(); |
| 480 |
final ActionCategory[] categories = loadCategories(swingGuiContext); |
| 481 |
final ActionFactory factory = PreviewPaneUtilities.createActionFactory(configuration); |
| 482 |
|
| 483 |
for (int i = 0; i < categories.length; i++) |
| 484 |
{ |
| 485 |
final ActionCategory category = categories[i]; |
| 486 |
actions.put(category, |
| 487 |
factory.getActions(swingGuiContext, category.getName())); |
| 488 |
} |
| 489 |
return actions; |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
public static ActionCategory[] loadCategories(SwingGuiContext swingGuiContext) |
| 494 |
{ |
| 495 |
final ArrayList categories = new ArrayList(); |
| 496 |
final Configuration configuration = swingGuiContext.getConfiguration(); |
| 497 |
final Iterator keys = configuration.findPropertyKeys(CATEGORY_PREFIX); |
| 498 |
while (keys.hasNext()) |
| 499 |
{ |
| 500 |
final String enableKey = (String) keys.next(); |
| 501 |
if (enableKey.endsWith(".enabled") == false) |
| 502 |
{ |
| 503 |
continue; |
| 504 |
} |
| 505 |
|
| 506 |
if ("true".equals(configuration.getConfigProperty(enableKey)) == false) |
| 507 |
{ |
| 508 |
continue; |
| 509 |
} |
| 510 |
|
| 511 |
final String base = enableKey.substring(0, enableKey.length() - ".enabled".length()); |
| 512 |
if (base.length() == 0) |
| 513 |
{ |
| 514 |
continue; |
| 515 |
} |
| 516 |
|
| 517 |
final String categoryKey = base.substring(CATEGORY_PREFIX.length()); |
| 518 |
final String className = configuration.getConfigProperty(base + ".class"); |
| 519 |
ActionCategory actionCategory; |
| 520 |
if (className == null) |
| 521 |
{ |
| 522 |
actionCategory = new ActionCategory(); |
| 523 |
} |
| 524 |
else |
| 525 |
{ |
| 526 |
actionCategory = (ActionCategory) ObjectUtilities.loadAndInstantiate |
| 527 |
(className, PreviewPane.class, ActionCategory.class); |
| 528 |
if (actionCategory == null) |
| 529 |
{ |
| 530 |
actionCategory = new ActionCategory(); |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
final String positionText = configuration.getConfigProperty(base + ".position"); |
| 535 |
actionCategory.setPosition(TextUtilities.parseInt(positionText, 0)); |
| 536 |
actionCategory.setName(categoryKey); |
| 537 |
actionCategory.setResourceBase(configuration.getConfigProperty(base + ".resource-base")); |
| 538 |
actionCategory.setResourcePrefix(configuration.getConfigProperty(base + ".resource-prefix")); |
| 539 |
actionCategory.initialize(swingGuiContext); |
| 540 |
categories.add(actionCategory); |
| 541 |
} |
| 542 |
|
| 543 |
return (ActionCategory[]) categories.toArray |
| 544 |
(new ActionCategory[categories.size()]); |
| 545 |
} |
| 546 |
} |