package utility; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.LayoutManager; import java.awt.Panel; import java.awt.Window; import java.awt.event.ComponentEvent; import java.awt.event.ComponentListener; import java.awt.print.PageFormat; import java.awt.print.PrinterException; import java.awt.print.PrinterJob; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.print.attribute.HashPrintRequestAttributeSet; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JLayeredPane; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JRootPane; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextArea; import javax.swing.JToggleButton; import javax.swing.JTree; import javax.swing.table.JTableHeader; import javax.swing.text.JTextComponent; import utility.printing.ContainerPageRenderer; /** * Operations on a container - a utility class. * @author Konrad Borowiecki */ public class ContainerOps { //to ensure its un-instantiability, as it is a utility class private ContainerOps() { } /** Changes text on a button that is within a given container. Useful when there * is no reference to the button, when we only know its current text. * !!! NOTICE it will change texts on all buttons which text corresponds to currentText. * @param c the container which has the button. * @param currentText the current text of the button we look for. * @param newText the text to be set for the button. * @param newToolTip the tool tip text to be set for the button. */ public void changeButtonText(Container c, String currentText, String newText, String newToolTip) { int len = c.getComponentCount(); for(int i = 0; i < len; i++) { Component comp = c.getComponent(i); if(comp instanceof JButton) { JButton b = (JButton) comp; if(b != null && b.getText() != null && b.getText().equals(currentText)) { System.out.println("Text changed from:" + currentText + "; to:" + newText); b.setText(newText); b.setToolTipText(newToolTip); } } else if(comp instanceof Container) changeButtonText((Container) comp, currentText, newText, newToolTip); } } /** * Finds highest components in the given container in each row * (i.e. going from top to bottom finding components which position plus height extends * furthest then that of other components in the row). * @param container the container where to look for the components. * @return list of highest components in the given container in each row. */ public static List findFurthestReachingInRowChildComponents(Container container) { List result = new ArrayList(); List fccList = findChildComponents(container); List ccList = orderComponentsForY(fccList); int containerEndPoint = container.getLocationOnScreen().y + container.getHeight(); System.out.println("containerEndPoint=" + containerEndPoint + "; containerY=" + container.getLocationOnScreen().y + ";containerH=" + container.getHeight()); //current Y int y = 0; Component pcc = null; for(int i = 0; i < ccList.size(); i++) { Component cc = ccList.get(i); int ccY = cc.getLocationOnScreen().y; if(ccY >= y) { //take the first component as the potential child if(pcc == null) { //System.out.println(" pcc == null Oi; now is="+cc.getName()); pcc = cc; if(i == ccList.size() - 1) result.add(pcc); } else // pcc != null { int pccY = pcc.getLocationOnScreen().y; int pccEndY = pccY + pcc.getHeight(); int ccEndY = ccY + cc.getHeight(); if(pccY >= ccEndY || (pccY < ccY && pccEndY > ccY && pccEndY < ccEndY) || (pccY > ccY && pccEndY < ccEndY)) { // System.out.println(" pcc != null Oi pcc was="+pcc.getName()+"; now is="+cc.getName()); pcc = cc; if(i == ccList.size() - 1) result.add(pcc); } else /* Do not add null element. If in an iteration an element is not added * it will cause the while loop to break since the list size will not change. */ { result.add(pcc); y = pcc.getLocationOnScreen().y + pcc.getHeight(); pcc = null; } } } } // int counter = 0; // int prevSize = -1; // //curent index of ccList pointing to a latest selected child component // int ccListIndex = -1; // while(y < containerEndPoint && result.size() != prevSize) { // prevSize = result.size(); // //potential child component // Component pcc = null; // for(int i = ccListIndex +1; i < ccList.size(); i++) { // Component cc = ccList.get(i); // int ccY = cc.getLocationOnScreen().y; // if(ccY >= y) { // // System.out.println(" if(ccY >= y); cc.name="+cc.getName()); // //take the first component as the potential child // if(pcc == null) { // //System.out.println(" pcc == null Oi; now is="+cc.getName()); // pcc = cc; // ccListIndex = i; // } // else // pcc != null // { // int pccY = pcc.getLocationOnScreen().y; // int pccEndY = pccY + pcc.getHeight(); // int ccEndY = ccY + cc.getHeight(); // if(pccY >= ccEndY // || (pccY < ccY && pccEndY > ccY && pccEndY < ccEndY) // || (pccY > ccY && pccEndY < ccEndY)) { // // System.out.println(" pcc != null Oi pcc was="+pcc.getName()+"; now is="+cc.getName()); // pcc = cc; // ccListIndex = i; // } // } // } // } // /* Do not add null element. If in an iteration an element is not added // * it will cause the while loop to break since the list size will not change. */ // if(pcc != null) { // System.out.println("y=" + y // + "; pcc.getLocationOnScreen().y=" + pcc.getLocationOnScreen().y // + "; pcc.getHeight()=" + pcc.getHeight() // + "; pcc=" + pcc); // result.add(pcc); // y = pcc.getLocationOnScreen().y + pcc.getHeight(); // } // System.out.println("y=" + y + "; counter=" + (++counter)); // } return result; } /** * Orders a list of components for their left top most point's Y coordinate on screen. * @param components list of components to order. * @return new list created by ordering the given list of components in ascending * order for their Y coordinate of the left top most point. */ public static List orderComponentsForY(List components) { List result = new ArrayList(); //order the list of children for y coordinate in ascending order for(Component fcc : components) { if(result.isEmpty()) { result.add(fcc); // System.out.println("ccList.isEmpty() fccY="+ fcc.getLocationOnScreen().y+"; fcc="+fcc); } else for(int j = 0; j < result.size(); j++) { Component cc = result.get(j); int fccY = fcc.getLocationOnScreen().y; int ccY = cc.getLocationOnScreen().y; if(fccY <= ccY) { // System.out.println("asassa fccY="+fccY+"; ccY="+ccY+"; j="+j+"; fcc="+fcc); result.add(j, fcc); break; } else if(j == result.size() - 1) { // System.out.println("if(j == ccList.size()-1) fccY="+fccY+"; ccY="+ccY+"; j="+j+"; fcc="+fcc); result.add(fcc); break; } } } // System.out.println("ccList.size()="+result.size()); // for(int i = 0; i < result.size(); i++) // { // Component c = result.get(i); // System.out.println("ccList i="+i+"; c.pOnS.y="+c.getLocationOnScreen().y+"; c="+c); // } return result; } /** * Finds a list of only child components without their container. * @param container where to look for the child components. * @return list of child components. */ public static List findChildComponents(Container container) { List result = new ArrayList(); for(Component c : container.getComponents()) if(c instanceof JComboBox || c instanceof JButton //direct super calss of JRadionButton and JCheckBox || c instanceof JToggleButton //|| c instanceof JRadioButton || c instanceof JCheckBox || c instanceof JList || c instanceof JScrollPane || c instanceof JTable || c instanceof JTableHeader || c instanceof JLabel || c instanceof JTextComponent || c instanceof JTree) { // if(c instanceof JTextComponent)//direct subclasses are: JEditorPane, JTextArea, JTextField //find row size similarly for the table // if(c instanceof JScrollPane) // result.add(((JScrollPane)c).getViewport()); // else // if(c.getParent() instanceof JTable) // System.out.println("KOAOAKOKASOKOSAASKO"); result.add(c); // System.out.println("else c="+c+"\nc.getBounds="+c.getBounds()); } else if(c instanceof Container && (c instanceof JPanel || c instanceof JRootPane || c instanceof Window || c instanceof Panel || c instanceof JLayeredPane)) { result.addAll(findChildComponents((Container) c)); // System.out.println("if(c instanceof Container) c="+c); } else System.err.println("!!!Do not know how to process with this class of component =" + c.getClass()); // System.out.println("findChildComponents result="+result.size()); return result; } public static void main(String[] args) throws IOException { JPanel p = new JPanel(); LayoutManager pLayout = new BoxLayout(p, BoxLayout.Y_AXIS); p.setLayout(pLayout); JButton aB = new JButton("ButtonA"); aB.setName("aB"); p.add(aB); JButton bB = new JButton("ButtonB"); bB.setName("bB"); p.add(bB); JLabel aL = new JLabel("LabelA"); aL.setName("aL"); p.add(aL); JPanel aPanel = new JPanel(); JLabel bL = new JLabel("LabelB"); bL.setName("bL"); aPanel.add(bL); JComboBox aCB = new JComboBox(); aCB.setName("aCB"); aPanel.add(aCB); p.add(aPanel); JPanel bPanel = new JPanel(); JPanel aBPanel = new JPanel(); LayoutManager aBPanelLayout = new BoxLayout(aBPanel, BoxLayout.Y_AXIS); aBPanel.setLayout(aBPanelLayout); JLabel bPanelL = new JLabel("bPanel Label1"); bPanelL.setName("bPanelL"); aBPanel.add(bPanelL); JRadioButton bPanelRB = new JRadioButton("bPanel RadioButton1"); bPanelRB.setName("bPanelRB"); aBPanel.add(bPanelRB); bPanel.add(aBPanel); JButton bPanelB = new JButton("bPanelB Button1"); bPanelB.setName("bPanelB"); bPanel.add(bPanelB); JButton bBPanelB = new JButton("bBPanelB"); bBPanelB.setName("bBPanelB"); bBPanelB.setPreferredSize(new Dimension(100, 55)); bBPanelB.setBackground(Color.RED); JLabel bBPanelL2 = new JLabel("bBPanelL2"); // bBPanelL2.setPreferredSize(new Dimension(700, 1900)); bBPanelL2.setName("bBPanelL2"); bBPanelL2.setOpaque(true); bBPanelL2.setBackground(Color.YELLOW); JPanel bBPanel = new JPanel(); LayoutManager bBPanelLayout = new BorderLayout(); bBPanel.setLayout(bBPanelLayout); bBPanel.add(bBPanelB, BorderLayout.NORTH); bBPanel.add(bBPanelL2, BorderLayout.SOUTH); bBPanel.setBackground(Color.BLUE); bPanel.add(bBPanel); JPanel emptyPanel = new JPanel(); emptyPanel.setPreferredSize(new Dimension(800, 2222)); p.add(emptyPanel); p.add(bPanel); int rows = 214; int columns = 5; Object[][] data = new Object[rows][columns]; for(int i = 0; i < rows; i++) for(int j = 0; j < columns; j++) { data[i][j] = "i=" + i + "; j=" + j; } System.out.println("data.length=" + data.length); Object[] colNames = { "A", "B", "C" }; JTable table = new JTable(data, colNames); table.setName("table"); JTableHeader th = table.getTableHeader(); th.setName("tableHeader"); p.add(th); p.add(table); JTextArea ta = new JTextArea("asddasasd\nAKSLAKSL\nAKSLAKSL\nAKSLAKSL" + "\nAasddasasd\nAKSLAKSL\nAKSLAKSL\nAKSLAKSL\nAasddasasd" + "\nAKSLAKSL\nAKSLAKSL\nAKSLAKSL\nAasddasasd\nAKSLAKSL" + "\nAKSLAKSL\nAKSLAKSL\nAasddasasd\nAKSLAKSL\nAKSLAKSL" + "\nAKSLAKSL\nAasddasasd\nAKSLAKSL\nAKSLAKSL\nAKSLAKSL\nAasddasasd" + "\nAKSLAKSL\nAKSLAKSL\nAKSLAKSL\nAasddasasd\nAKSLAKSL\nAKSLAKSL" + "\nAKSLAKSL\nAasddasasd\nAKSLAKSL\nAKSLAKSL\nAKSLAKSL\nAasddasasd" + "\nAKSLAKSL\nAKSLAKSL\nAKSLAKSL\nAasddasasd\nAKSLAKSL\nAKSLAKSL" + "\nAKSLAKSL\nAasddasasd\nAKSLAKSL\nAKSLAKSL\nAKSLAKSL\nAasddasasd" + "\nAKSLAKSL\nAKSLAKSL\nAKSLAKSL\nAasddasasd\nAKSLAKSL\nAKSLAKSL" + "\nAKSLAKSL\nAasddasasd\nAKSLAKSL\nAKSLAKSL\nAKSLAKSL\nAasddasasd" + "\nAKSLAKSL\nAKSLAKSL\nAKSLAKSL\nAasddasasd\nAKSLAKSL\nAKSLAKSL" + "\nAKSLAKSL\nAasddasasd\nAKSLAKSL\nAKSLAKSL\nAKSLAKSL\nAasddasasd" + "\nAKSLAKSL\nAKSLAKSL\nAKSLAKSL\nAasddasasd\nAKSLAKSL\nAKSLAKSL" + "\nAKSLAKSL\nAasddasasd\nAKSLAKSL\nAKSLAKSL\nAKSLAKSL\nAasddasasd" + "\nAKSLAKSL\nAKSLAKSL\nAKSLAKSL\nAasddasasd\nAKSLAKSL\nAKSLAKSL" + "\nAKSLAKSL\nAasddasasd\nAKSLAKSL\nAKSLAKSL\nAKSLAKSL\nAasddasasd" + "\nAKSLAKSL\nAKSLAKSL\nAKSLAKSL\nAasddasasd\nAKSLAKSL\nAKSLAKSL" + "\nAKSLAKSL\nAasddasasd\nAKSLAKSL\nAKSLAKSL\nAKSLAKSL\nAasddasasd" + "\nAKSLAKSL\nAKSLAKSL\nAKSLAKSL\nAasddasasd\nAKSLAKSL\nAKSLAKSL\nAKSLAKSL" + "\nAasddasasd\nAKSLAKSL\nAKSLAKSL\nAKSLAKSL\nAasddasasd\nAKSLAKSL\nAKSLAKSL" + "\nAKSLAKSL\nAasddasasd\nAKSLAKSL\nAKSLAKSL\nAKSLAKSL\nAasddasasd\nAKSLAKSL" + "\nAKSLAKSL\nAKSLAKSL\nAasddasasd\nAKSLAKSL\nAKSLAKSL\nAKSLAKSL" + "\nAKSLAKSLAKSLAKSLasddasasd\nAKSLAKSL\nAKSLAKSL\nAKSLAKSL" + "\nAKSLAKSLAKSLAKSLasddasasd\nAKSLAKSL\nAKSLAKSL\nAKSLAKSL" + "\nAKSLAKSLAKSLAKSLasddasasd\nAKSLAKSL\nAKSLAKSL\nAKSLAKSL" + "\nAKSLAKSLAKSLAKSLasddasasd\nAKSLAKSL\nAKSLAKSL\nAKSLAKSL" + "\nAKSLAKSLAKSLAKSLasddasasd\nAKSLAKSL\nAKSLAKSL\nAKSLAKSL" + "\nAKSLAKSLAKSLAKSLasddasasd\nAKSLAKSL\nAKSLAKSL\nAKSLAKSL" + "\nAKSLAKSLAKSLAKSL\nAKSLAKSL\nAKSLAKSL\nAKSLAKSL\nAKSLAKSL"); ta.setName("Text Area ta"); // p.add(ta); System.out.println("ta.getRows()=" + ta.getRows() + "; ta.getPreferredSize()=" + ta.getPreferredSize()); final JFrame f = new JFrame(); p.setSize(p.getPreferredSize()); // JPanel contentPane = new JPanel(); // contentPane.setLayout(new BorderLayout()); // contentPane.setBackground(Color.BLUE); PrinterJob pj = PrinterJob.getPrinterJob(); PageFormat pageFormat = pj.getPageFormat(new HashPrintRequestAttributeSet()); // System.out.println("pageFormatW=" + pageFormat.getImageableWidth() + "; pageFormatH=" + pageFormat.getImageableHeight()); ContainerPageRenderer cpr = new ContainerPageRenderer(p, pageFormat); cpr.setSize(cpr.getPreferredSize()); f.getLayeredPane().add(p, JLayeredPane.PALETTE_LAYER); f.getLayeredPane().add(cpr, JLayeredPane.MODAL_LAYER); // f.setContentPane(contentPane); f.setSize(600, 300); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.addComponentListener(new ComponentListener() { @Override public void componentResized(ComponentEvent e) { f.repaint(); } @Override public void componentMoved(ComponentEvent e) { } @Override public void componentShown(ComponentEvent e) { } @Override public void componentHidden(ComponentEvent e) { } }); f.setVisible(true); cpr.initPages(); //System.out.println("table.getHeight()="+table.getHeight() + "; table.pOnS.y="+table.getLocationOnScreen().y); //System.out.println("aB.getHeight()="+aB.getHeight() + "; aB.pOnS.y="+aB.getLocationOnScreen().y); //System.out.println("bB.getHeight()="+bB.getHeight() + "; bB.pOnS.y="+bB.getLocationOnScreen().y); //System.out.println("aL.getHeight()="+aL.getHeight() + "; aL.pOnS.y="+aL.getLocationOnScreen().y); //System.out.println("bL.getHeight()="+bL.getHeight() + "; bL.pOnS.y="+bL.getLocationOnScreen().y); //System.out.println("cb1.getHeight()="+aCB.getHeight() + "; cb1.pOnS.y="+aCB.getLocationOnScreen().y); //System.out.println("bPanelRB.getHeight()="+bPanelRB.getHeight()+ "; bPanelRB.pOnS.y="+bPanelRB.getLocationOnScreen().y); //System.out.println("bPanelL.getHeight()="+bPanelL.getHeight()+ "; bPanelL.pOnS.y="+bPanelL.getLocationOnScreen().y); //System.out.println("bPanelB.getHeight()="+bPanelB.getHeight()+ "; bPanelB.pOnS.y="+bPanelB.getLocationOnScreen().y); // List cc = findChildComponents(p); // System.out.println("cc.size()=" + cc.size()); // for(int i = 0; i < cc.size(); i++) // { // Component c = cc.get(i); // System.out.println("i=" + i + "; c=" + c); // } // System.out.println("p.getHeight()=" + p.getHeight()+"; p.pOnS.y="+p.getLocationOnScreen().y); // List result = findFurthestReachingInRowChildComponents(p); // System.out.println("final result.size()=" + result.size()); // for(int i = 0; i < result.size(); i++) // { // Component c = result.get(i); // System.out.println("i=" + i + "; c=" + c); // if(i > 0) // { // Component pc = result.get(i - 1); // int spaceSize = c.getLocationOnScreen().y - pc.getLocationOnScreen().y - pc.getHeight(); // System.out.println("i=" + i + "; spaceSize=" + spaceSize); // } // } // for(Component c : result) // { // if(c instanceof JLabel) // ((JLabel) c).setOpaque(true); // c.setBackground(Color.GREEN); // } List pageHeights = cpr.getPageHeights(); System.out.println("******pageHeights.size()=" + pageHeights.size()); for(int i = 0; i < pageHeights.size(); i++) { Double pageHeight = pageHeights.get(i); System.out.println("i=" + i + "; pageHeight=" + pageHeight); } List tableHeaders = cpr.getTableHeadersOfToLargeTables(); System.out.println("******tableHeaders.size()=" + tableHeaders.size()); for(int i = 0; i < tableHeaders.size(); i++) { JTableHeader tableHeader = tableHeaders.get(i); String thH = "!!! cannot calculate height since tableHeader is null"; if(tableHeader != null) thH = "" + tableHeader.getHeight(); System.out.println("i=" + i + "; tableHeader.getHeight()=" + thH); } // int pageY = 0; // for(int i = 0; i < pageHeights.size(); i++) // { // pageY += pageHeights.get(i); // System.out.println("i=" + i + "; pageY=" + pageY); // } //p must be visible for myprint utility p.setVisible(false); f.validate(); f.repaint(); pj.setPrintable(cpr, pageFormat); if(pj.printDialog()) try { ContainerPageRenderer.disableDoubleBuffering(p); System.out.println("Calling PrintJob.print()"); //w ten sposob wydruk bedzie wycentrowany do rozmiarow strony pj.print();//print(new HashPrintRequestAttributeSet());//printJob.print(); System.out.println("End PrintJob.print()"); }catch(PrinterException e) { System.out.println(e); }finally // whatever happend (exception or not) turn back on the double buffering { ContainerPageRenderer.enableDoubleBuffering(p); } // f.dispose(); } }