view.setScale(scale); view.setRx(rx); view.setRy(ry);
		public void run()		{			ViewState view = ViewState.getInstance();			int rx = view.getRx();			int ry = view.getRy();			double scale = view.getScale();			boolean showGrid = Settings.getInstance().getShowGrid();			try			{				PrintRequestAttributeSet printRequestAttributeSet = Page.getPrintRequestAttributeSet();				PrinterJob printerJob = PrinterJob.getPrinterJob();				printerJob.setJobName(getTitle());				printerJob.setPageable(pi.getPageable());				if(!printerJob.printDialog(printRequestAttributeSet))					return;				int pageFormatOrientation = PageFormat.PORTRAIT;						OrientationRequested orientationRequested = (OrientationRequested)					printRequestAttributeSet.get(OrientationRequested.class);									if(orientationRequested != null)				{					if(orientationRequested.equals(OrientationRequested.LANDSCAPE))						pageFormatOrientation = PageFormat.LANDSCAPE;					else if(orientationRequested.equals(OrientationRequested.REVERSE_LANDSCAPE))						pageFormatOrientation = PageFormat.REVERSE_LANDSCAPE;				}								pi.getPageable().getPageFormat(0).setOrientation(pageFormatOrientation);				MediaPrintableArea mediaPrintableArea = (MediaPrintableArea)					printRequestAttributeSet.get(MediaPrintableArea.class);									Paper paper = pi.getPageable().getPageFormat(0).getPaper();				paper.setImageableArea(mediaPrintableArea.getX(MediaPrintableArea.INCH) * 72.0,					mediaPrintableArea.getY(MediaPrintableArea.INCH) * 72.0,					mediaPrintableArea.getWidth(MediaPrintableArea.INCH) * 72,					mediaPrintableArea.getHeight(MediaPrintableArea.INCH) * 72);					pi.getPageable().getPageFormat(0).setPaper(paper);				ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();								DocFlavor pageableFlavor = DocFlavor.SERVICE_FORMATTED.PAGEABLE;			    String postScriptMime = new String("application/postscript");				StreamPrintServiceFactory[] streamPrintServiceFactory =					StreamPrintServiceFactory.lookupStreamPrintServiceFactories(pageableFlavor,					postScriptMime);				if(streamPrintServiceFactory.length == 0)				{					System.err.println("Printing error: no suitable PostScript factory found.");					return;				}					StreamPrintService streamPrintService =					streamPrintServiceFactory[0].getPrintService(byteArrayOutputStream);								PrinterJob postScriptPrinterJob = PrinterJob.getPrinterJob();				postScriptPrinterJob.setPrintService(streamPrintService);				postScriptPrinterJob.setJobName(getTitle());				postScriptPrinterJob.setPageable(pi.getPageable());				setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.WAIT_CURSOR));				Thread.yield();				Settings.getInstance().setShowGrid(false);				postScriptPrinterJob.print(printRequestAttributeSet);				Settings.getInstance().setShowGrid(showGrid);		        DocFlavor postScriptFlavor = DocFlavor.INPUT_STREAM.POSTSCRIPT;			    if(!printerJob.getPrintService().isDocFlavorSupported(postScriptFlavor))			    {			    	JOptionPane.showMessageDialog(VisualDCT.this, "Printing error", "PostScript not supported on selected printer.", JOptionPane.ERROR_MESSAGE); 					//System.err.println("Printing error: PostScript not supported on selected printer.");					return;			    }				PageFormat pageFormat = pi.getPageable().getPageFormat(0);				int pageWidth = (int)pageFormat.getImageableWidth();				int pageHeight = (int)pageFormat.getImageableHeight();					double screen2printer = 0;				NullCommand pm =					(NullCommand)CommandManager.getInstance().getCommand("NullCommand");				Component component = pm.getComponent();							switch(Page.getPrintMode())				{					case Page.TRUE_SCALE:						// 1:1 ratio						screen2printer =							72.0 / component.getToolkit().getScreenResolution();						break;					case Page.USER_SCALE:						screen2printer =							72.0 / component.getToolkit().getScreenResolution();						screen2printer *= Page.getUserScale();						break;								case Page.FIT_SCALE:						// fit to paper						double xscale = pageWidth / (double)view.getViewWidth();						double yscale = pageHeight / (double)view.getViewHeight();						screen2printer = Math.min(xscale, yscale) * view.getScale();						break;				}					double converter = screen2printer / view.getScale();				int w = (int)(view.getViewWidth() * converter);				int h = (int)(view.getViewHeight() * converter);				int nCol = Math.max((int)Math.ceil((double)w / pageWidth), 1);				int nRow = Math.max((int)Math.ceil((double)h / pageHeight), 1);				int maxNumPage = nCol * nRow;				String numberOfPages = String.valueOf(maxNumPage);				StringBuffer stringBuffer = new StringBuffer(byteArrayOutputStream.toString());				byteArrayOutputStream = null;				int stringOffset = stringBuffer.indexOf("%%BeginProlog");				stringBuffer.insert(stringOffset, "%%Pages: " + numberOfPages + "\n");				for(int pageIndex = 0; pageIndex < maxNumPage; pageIndex++)				{					int iCol = pageIndex % nCol;					int iRow = pageIndex / nCol;					int x = iCol * pageWidth;					int y = iRow * pageHeight;					int imageWidth = Math.min(pageWidth, w-x);					int imageHeight = Math.min(pageHeight, h-y);					view.setScale(screen2printer);						view.setRx((int)(rx*converter+x));					view.setRy((int)(ry*converter+y));					String postScriptGrid = new String();						if (Settings.getInstance().getShowGrid())					{						postScriptGrid += "\n"							+ (new Double(Constants.GRID_COLOR.getRed() / 255.0)).toString()							+ " " + (new Double(Constants.GRID_COLOR.getGreen()							/ 255.0)).toString() + " "							+ (new Double(Constants.GRID_COLOR.getBlue() / 255.0)).toString()							+ " setrgbcolor\n";						int gridSize = view.getGridSize();						int sx = view.getGridSize() - view.getRx() % gridSize;						int y0 = view.getGridSize() - view.getRy() % gridSize;						int xsteps = (int)(view.getViewWidth() / gridSize + 1);						int ysteps = (int)(view.getViewHeight() / gridSize + 1);						imageWidth = Math.min(imageWidth, gridSize * xsteps + sx);						imageHeight = Math.min(imageHeight, gridSize * ysteps + y0);						if(gridSize >= 15)							postScriptGrid += "/gridshape {posX posY moveto -1 -1 rmoveto "								+ "2 2 rlineto 0 -2 rmoveto -2 2 rlineto stroke} def\n";						else							postScriptGrid += "/gridshape {newpath posX posY moveto "								+ "-0.5 -0.5 rmoveto 1 0 rlineto 0 1 rlineto -1 0 rlineto "								+ "0 -1 rlineto closepath fill} def\n";						postScriptGrid += "/posX " + String.valueOf(sx) + " def\n"							+ "/posY " + String.valueOf(y0) + " def\n"							+ "{\n{\ngridshape\n"													+ "/posX posX " + String.valueOf(gridSize) + " add def\n"													+ "posX " + String.valueOf(imageWidth)							+ " ge {exit} if\n"													+ "} loop\n"							+ "/posX " + String.valueOf(sx) + " def\n"													+ "/posY posY " + String.valueOf(gridSize) + " add def\n"													+ "posY " + String.valueOf(imageHeight)							+ " ge {exit} if\n} loop\n";					}					stringOffset = stringBuffer.indexOf("%%Page:", stringOffset);					int selectionStart = stringBuffer.indexOf("concat", stringOffset) + 6;					int selectionEnd = stringBuffer.indexOf("gsave", selectionStart);					stringOffset = selectionEnd;					stringBuffer.insert(stringOffset, "\n");					String startingColour = stringBuffer.substring(selectionStart,						selectionEnd);					stringBuffer.delete(selectionStart, selectionEnd);										stringOffset = stringBuffer.indexOf("concat", stringOffset) + 6;					if((startingColour.indexOf("setgray") == -1)						&& (startingColour.indexOf("setrgbcolor") == -1))					{						startingColour = "0.0 setgray\n";						}					stringBuffer.insert(stringOffset, postScriptGrid + startingColour);				}				ByteArrayInputStream byteArrayInputStream =					new ByteArrayInputStream(stringBuffer.toString().getBytes());				stringBuffer = null;				DocPrintJob docPrintJob = printerJob.getPrintService().createPrintJob();   		 	  	DocAttributeSet docAttributeSet = new HashDocAttributeSet();         	  	       	 	    Doc doc = new SimpleDoc(byteArrayInputStream, postScriptFlavor,       	 	    	docAttributeSet);       	 	    docPrintJob.print(doc, printRequestAttributeSet);			}			catch (Exception exception)			{				exception.printStackTrace();				com.cosylab.vdct.Console.getInstance().println("Printing error: "					+ exception);			}			finally			{				view.setScale(scale);				view.setRx(rx);				view.setRy(ry);				setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.DEFAULT_CURSOR));				Settings.getInstance().setShowGrid(showGrid);				CommandManager.getInstance().execute("RepaintWorkspace");			}		}