PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
PrintRequestAttributeSet printRequestAttributeSet = Page.getPrintRequestAttributeSet();
		public void run()		{			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();			FileOutputStream fileOutputStream = null;							DocFlavor pageableFlavor = DocFlavor.SERVICE_FORMATTED.PAGEABLE;		    String postScriptMime = new String("application/postscript");	        PrintRequestAttributeSet printRequestAttributeSet =	        	new HashPrintRequestAttributeSet();			StreamPrintServiceFactory[] streamPrintServiceFactory =				StreamPrintServiceFactory.lookupStreamPrintServiceFactories(pageableFlavor,				postScriptMime);			if(streamPrintServiceFactory.length == 0)			{				JOptionPane.showMessageDialog(VisualDCT.this, "Printing error", "No suitable PostScript factory found.", JOptionPane.ERROR_MESSAGE); 				//System.err.println("Printing error: no suitable PostScript factory found.");				return;			}			StreamPrintService streamPrintService =				streamPrintServiceFactory[0].getPrintService(byteArrayOutputStream);							PrinterJob printerJob = PrinterJob.getPrinterJob();					printerJob.setPageable(pi.getPageable());			ViewState view = ViewState.getInstance();			int rx = view.getRx();			int ry = view.getRy();			double scale = view.getScale();			try			{				printerJob.setPrintService(streamPrintService);								PageFormat pageFormat = printerJob.pageDialog(printRequestAttributeSet);				if(pageFormat == null)					return;				JFileChooser jFileChooser = getfileChooser();				UniversalFileFilter universalFileFilter =					new UniversalFileFilter(new String("ps"), "PostScript file");				jFileChooser.resetChoosableFileFilters();				jFileChooser.addChoosableFileFilter(universalFileFilter);				jFileChooser.setDialogTitle("Save as...");				jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);				boolean jFileChooserConfirmed = false;			    File outputFile = null;								while(!jFileChooserConfirmed)				{					int jFileChooserOption = jFileChooser.showSaveDialog(ownerComponent);					if(jFileChooserOption == JFileChooser.CANCEL_OPTION)						return;				    outputFile = jFileChooser.getSelectedFile();						    							// fix ending					if (jFileChooser.getFileFilter().getDescription().startsWith("PostScript") &&						!outputFile.getName().endsWith(".ps"))						outputFile = new java.io.File(outputFile.getAbsoluteFile()+".ps");					if(outputFile.exists())					{   						int jOptionPaneOption =   							JOptionPane.showConfirmDialog(ownerComponent, "The file '"   							+ outputFile.getName()   							+ "' already exists! Overwrite?", "Confirmation",   							JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);    					if(jOptionPaneOption == JOptionPane.YES_OPTION)   							jFileChooserConfirmed = true;					}					else						jFileChooserConfirmed = true;				}				setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.WAIT_CURSOR));				Thread.yield();				pi.getPageable().getPageFormat(0).setOrientation(pageFormat.getOrientation());				pi.getPageable().getPageFormat(0).setPaper(pageFormat.getPaper());				boolean showGrid = Settings.getInstance().getShowGrid();				Settings.getInstance().setShowGrid(false);				printerJob.print();				Settings.getInstance().setShowGrid(showGrid);				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 " + (new Integer(sx)).toString() + " def\n"							+ "/posY " + (new Integer(y0)).toString() + " def\n"							+ "{\n{\ngridshape\n"													+ "/posX posX " + (new Integer(gridSize)).toString() + " add def\n"													+ "posX " + (new Integer(imageWidth)).toString()							+ " ge {exit} if\n"													+ "} loop\n"							+ "/posX " + (new Integer(sx)).toString() + " def\n"													+ "/posY posY " + (new Integer(gridSize)).toString() + " add def\n"													+ "posY " + (new Integer(imageHeight)).toString()							+ " 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);				}				fileOutputStream = new FileOutputStream(outputFile);				fileOutputStream.write(stringBuffer.toString().getBytes());				fileOutputStream.close();			}			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));						CommandManager.getInstance().execute("RepaintWorkspace");			}		}