1 17 18 19 20 package org.apache.fop.render.awt.viewer; 21 22 import java.awt.Color ; 23 import java.awt.Dimension ; 24 import java.awt.GridLayout ; 25 import java.awt.Point ; 26 import java.awt.event.MouseEvent ; 27 import java.awt.event.MouseListener ; 28 import java.awt.event.MouseMotionListener ; 29 import java.awt.geom.Rectangle2D ; 30 31 import javax.swing.JLabel ; 32 import javax.swing.JOptionPane ; 33 import javax.swing.JPanel ; 34 import javax.swing.JScrollPane ; 35 import javax.swing.JViewport ; 36 import javax.swing.SwingUtilities ; 37 import javax.swing.border.EmptyBorder ; 38 39 import org.apache.fop.apps.FOPException; 40 import org.apache.fop.apps.FOUserAgent; 41 import org.apache.fop.apps.MimeConstants; 42 import org.apache.fop.area.PageViewport; 43 44 import org.apache.fop.render.awt.AWTRenderer; 45 46 47 91 public class PreviewPanel extends JPanel { 92 93 94 public static final int SINGLE = 1; 95 96 public static final int CONTINUOUS = 2; 97 98 public static final int CONT_FACING = 3; 99 100 101 private static final int BORDER_SPACING = 10; 102 103 104 private JScrollPane previewArea; 105 106 107 private AWTRenderer renderer; 108 109 110 protected FOUserAgent foUserAgent; 111 115 protected Renderable renderable; 116 117 private int currentPage = 0; 118 119 120 private int firstPage = 0; 121 122 123 private int pageRange = 1; 124 125 126 private int displayMode = SINGLE; 127 128 129 private ImageProxyPanel[] pagePanels = null; 130 131 135 private JPanel gridPanel = null; 136 137 138 private Reloader reloader; 139 140 143 private ViewportScroller scroller; 144 145 146 153 public PreviewPanel(FOUserAgent foUserAgent, Renderable renderable, AWTRenderer renderer) { 154 super(new GridLayout (1, 1)); 155 this.renderable = renderable; 156 this.renderer = renderer; 157 this.foUserAgent = foUserAgent; 158 159 gridPanel = new JPanel (); 160 gridPanel.setLayout(new GridLayout (0, 1)); 162 previewArea = new JScrollPane (gridPanel); 163 previewArea.getViewport().setBackground(Color.gray); 164 165 scroller = new ViewportScroller(previewArea.getViewport()); 167 previewArea.addMouseListener(scroller); 168 previewArea.addMouseMotionListener(scroller); 169 170 previewArea.setMinimumSize(new Dimension (50, 50)); 171 add(previewArea); 172 } 173 174 177 public int getPage() { 178 return currentPage; 179 } 180 181 186 public void setPage(int number) { 187 if (displayMode == CONTINUOUS || displayMode == CONT_FACING) { 188 currentPage = number; 189 gridPanel.scrollRectToVisible(pagePanels[currentPage].getBounds()); 190 } else { currentPage = number; 192 firstPage = currentPage; 193 } 194 showPage(); 195 } 196 197 201 public void setDisplayMode(int mode) { 202 if (mode != displayMode) { 203 displayMode = mode; 204 gridPanel.setLayout(new GridLayout (0, displayMode == CONT_FACING ? 2 : 1)); 205 reload(); 206 } 207 } 208 209 213 public int getDisplayMode() { 214 return displayMode; 215 } 216 217 220 public synchronized void reload() { 221 if (reloader == null || !reloader.isAlive()) { 222 reloader = new Reloader(); 223 reloader.start(); 224 } 225 } 226 227 230 void debug() { 231 renderer.debug = !renderer.debug; 232 reload(); 233 } 234 235 238 private class ViewportScroller implements MouseListener , MouseMotionListener { 239 240 private final JViewport viewport; 241 242 private int startPosX = 0; 243 244 private int startPosY = 0; 245 246 ViewportScroller(JViewport vp) { 247 viewport = vp; 248 } 249 250 252 public synchronized void mouseDragged(MouseEvent e) { 253 if (viewport == null) { 254 return; 255 } 256 int x = e.getX(); 257 int y = e.getY(); 258 int xmove = x - startPosX; 259 int ymove = y - startPosY; 260 int viewWidth = viewport.getExtentSize().width; 261 int viewHeight = viewport.getExtentSize().height; 262 int imageWidth = viewport.getViewSize().width; 263 int imageHeight = viewport.getViewSize().height; 264 265 Point viewPoint = viewport.getViewPosition(); 266 int viewX = Math.max(0, Math.min(imageWidth - viewWidth, viewPoint.x - xmove)); 267 int viewY = Math.max(0, Math.min(imageHeight - viewHeight, viewPoint.y - ymove)); 268 269 viewport.setViewPosition(new Point (viewX, viewY)); 270 271 startPosX = x; 272 startPosY = y; 273 } 274 275 public void mouseMoved(MouseEvent e) { } 276 277 279 public void mousePressed(MouseEvent e) { 280 startPosX = e.getX(); 281 startPosY = e.getY(); 282 } 283 284 public void mouseExited(MouseEvent e) { } 285 public void mouseEntered(MouseEvent e) { } 286 public void mouseClicked(MouseEvent e) { } 287 public void mouseReleased(MouseEvent e) { } 288 } 289 290 293 private class Reloader extends Thread { 294 295 public void run() { 296 if (!renderer.isRenderingDone()) { 297 JOptionPane.showMessageDialog(previewArea, 299 "Cannot perform the requested operation until " 300 + "all page are rendered. Please wait", 301 "Please wait ", 1 ); 302 return; 303 } 304 305 pagePanels = null; 306 307 int savedCurrentPage = currentPage; 308 currentPage = 0; 309 310 gridPanel.removeAll(); 311 switch(displayMode) { 312 case CONT_FACING: 313 gridPanel.add(new JLabel ("")); 316 case CONTINUOUS: 317 currentPage = 0; 318 firstPage = 0; 319 pageRange = renderer.getNumberOfPages(); 320 break; 321 case SINGLE: 322 default: 323 currentPage = 0; 324 firstPage = 0; 325 pageRange = 1; 326 break; 327 } 328 329 pagePanels = new ImageProxyPanel[pageRange]; 330 for (int pg = 0; pg < pageRange; pg++) { 331 pagePanels[pg] = new ImageProxyPanel(renderer, pg + firstPage); 332 pagePanels[pg].setBorder(new EmptyBorder ( 333 BORDER_SPACING, BORDER_SPACING, BORDER_SPACING, BORDER_SPACING)); 334 gridPanel.add(pagePanels[pg]); 335 } 336 337 try { 338 if (renderable != null) { 339 renderer.clearViewportList(); 340 renderable.renderTo(foUserAgent, MimeConstants.MIME_FOP_AWT_PREVIEW); 341 } 342 } catch (FOPException e) { 343 e.printStackTrace(); 344 } 346 347 setPage(savedCurrentPage); 348 } 349 } 350 351 355 public void setScaleFactor(double scale) { 356 renderer.setScaleFactor(scale); 357 reload(); 358 } 359 360 367 public double getScaleToFitWindow() throws FOPException { 368 Dimension extents = previewArea.getViewport().getExtentSize(); 369 return getScaleToFit(extents.getWidth() - 2 * BORDER_SPACING, 370 extents.getHeight() - 2 * BORDER_SPACING); 371 } 372 373 378 public double getScaleToFitWidth() throws FOPException { 379 Dimension extents = previewArea.getViewport().getExtentSize(); 380 return getScaleToFit(extents.getWidth() - 2 * BORDER_SPACING, Double.MAX_VALUE); 381 } 382 383 393 public double getScaleToFit(double viewWidth, double viewHeight) throws FOPException { 394 PageViewport pageViewport = renderer.getPageViewport(currentPage); 395 Rectangle2D pageSize = pageViewport.getViewArea(); 396 double widthScale = viewWidth / (pageSize.getWidth() / 1000f); 397 double heightScale = viewHeight / (pageSize.getHeight() / 1000f); 398 return Math.min(displayMode == CONT_FACING ? widthScale / 2 : widthScale, heightScale); 399 } 400 401 402 public synchronized void showPage() { 403 ShowPageImage viewer = new ShowPageImage(); 404 405 if (SwingUtilities.isEventDispatchThread()) { 406 viewer.run(); 407 } else { 408 SwingUtilities.invokeLater(viewer); 409 } 410 } 411 412 413 private class ShowPageImage implements Runnable { 414 415 418 public void run() { 419 for (int pg = firstPage; pg < firstPage + pageRange; pg++) { 420 pagePanels[pg - firstPage].setPage(pg); 421 } 422 revalidate(); 423 } 424 } 425 } 426 | Popular Tags |