Menu

NoBreakPrint question

Help
2008-04-28
2013-04-10
  • Nobody/Anonymous

    I am trying to force my column headers to NOT wrap.

    Here's my code:

    GridPrint grid = new GridPrint("d,d,d" new DefaultGridLook(10,5));
    grid.add(new NoBreakPrint(new TextPrint(colHeaderString, font)));

    But this does not work.  My colHeaderString is one long word (no spaces, just one word) that is getting split mid-word into 2 lines.  Is the code wrong?  I shouldn't need to specify "p" for preferred when setting up the GridPrint, right?  What is wrong with my use of the NoBreakPrint?

    Thanks for your help...

     
    • Matthew Hall

      Matthew Hall - 2008-04-28

      NoBreakPrint doesn't prevent line breaks, it prevents a Print's content from being broken across multiple pages.

      I generally discourage preferred size columns, as they can lead to failed layouts if there are too many of them, or if the preferred column is too big.  What we need is a way to tell individual Prints (like those in the headers) to lay out at their preferred sizes, and let everything else layout at default size.  Try wrapping your header cells in this class and tell me if it makes a difference:

      [begin source]

      /************************************************************************************************************
      * Copyright (c) 2008 Woodcraft Mill & Cabinet Corporation. All rights reserved.
      *
      * This program and the accompanying materials are made available under the terms of the Eclipse Public
      * License v1.0 which accompanies this distribution, and is available at
      * http://www.eclipse.org/legal/epl-v10.html
      *
      * Contributors: Woodcraft Mill & Cabinet Corporation - initial API and implementation
      ***********************************************************************************************************/
      package net.sf.paperclips;

      import net.sf.paperclips.internal.Util;

      import org.eclipse.swt.graphics.Device;
      import org.eclipse.swt.graphics.GC;
      import org.eclipse.swt.graphics.Point;

      /**
      * A Print wrapper which causes its target to be laid out as if its minimum size were its preferred size.
      * @author Matthew Hall
      */
      public class PreferredSizePrint implements Print {
        private final Print target;

        /**
         * Constructs a PreferredSizePrint wrapping the given target
         * @param target the Print which will be laid out at its preferred size.
         */
        public PreferredSizePrint( Print target ) {
          Util.notNull( target );
          this.target = target;
        }

        public PrintIterator iterator( Device device, GC gc ) {
          return new PreferredSizeIterator( target, device, gc );
        }

        public int hashCode() {
          final int prime = 31;
          int result = 1;
          result = prime * result + ( ( target == null ) ? 0 : target.hashCode() );
          return result;
        }

        public boolean equals( Object obj ) {
          if ( this == obj )
            return true;
          if ( obj == null )
            return false;
          if ( getClass() != obj.getClass() )
            return false;
          PreferredSizePrint other = (PreferredSizePrint) obj;
          if ( target == null ) {
            if ( other.target != null )
              return false;
          } else if ( !target.equals( other.target ) )
            return false;
          return true;
        }
      }

      class PreferredSizeIterator implements PrintIterator {
        private final PrintIterator target;

        PreferredSizeIterator( Print target, Device device, GC gc ) {
          this.target = target.iterator( device, gc );
        }

        private PreferredSizeIterator( PreferredSizeIterator that ) {
          this.target = that.target.copy();
        }

        public boolean hasNext() {
          return target.hasNext();
        }

        public Point minimumSize() {
          return target.preferredSize(); // TA DA
        }

        public Point preferredSize() {
          return target.preferredSize();
        }

        public PrintPiece next( int width, int height ) {
          return PaperClips.next( target, width, height );
        }

        public PrintIterator copy() {
          return new PreferredSizeIterator( this );
        }
      }

      [end source]

      If this fixes your problem, I'll go ahead and add it to Subversion and it will be included in the next release.

      Matthew

       
    • Nobody/Anonymous

      OK, I'll try that and let you know what happens.  Thanks for the quick response.

       
    • Nobody/Anonymous

      Nope.  It has no effect.  Does it matter that I don't have version 1.0.3 yet?  (I only had to fix one compile problem because of Util.notNull, which I assume is in 1.0.3.)

      Any other ideas?

       
      • Matthew Hall

        Matthew Hall - 2008-04-29

        Could you provide a standalone snippet that demonstrates the problem?  It's difficult to know what you're seeing without something to run on my side.

        Matthew

         
    • Nobody/Anonymous

      OK.  What I'm doing is printing the metadata associated with an IPropertySource.  So I have 3 columns: CATEGORY, PROPERTY, and VALUE.  What you should see happen is the "Y" in PROPERTY get shoved over to a second line.  Obviously, we can't control how the very long URL gets split up, because there are no spaces on which to split.  The same is true for the cell containing "URLDatabase".  Here's some code:

      //********************************************************************************
      public class PropertiesGrid
      {
          private static FontData colHeaderFont - new FontData("courier", 12, SWT.BOLD);
          private static FontData dataFont - new FontData("courier", 10, SWT.NORMAL);

          GridPrint grid = null;

          public GridPrint createGrid()
          {
              DefaultGridLook gridLook = new DefaultGridLook(10,5);
              grid = new GridPrint("d,d,d", gridLook);

              return (grid);
          }

          public GridPrint addDataToGrid(List<IPropertyDescriptor> descriptorsList,
                                         List<Object> valuesList)
          {
              //and here i'm just using some dummy data that produces the bad layout
             
              //headers
              grid.add(new TextPrint("CATEGORY", colHeaderFont);
              grid.add(new TextPrint("PROPERTY", colHeaderFont);
              grid.add(new TextPrint("VALUE", colHeaderFont);

              //data
              grid.add(new TextPrint("URLDatabase", dataFont);
              grid.add(new TextPrint("URL", dataFont);
              grid.add(new TextPrint("http://averylonglonglong/URL/thatcauseswrappingwhereIdontwantit", dataFont);

              return (grid);
          }
      }
      //***********************************************************************************

      ...and then I altered your PrintPreview code from snippet7 to change the margins:
      //***********************************************************************************

      public Shell createShell(Print print)
      {
          printJob = new PrintJob("PrintProperties", print);

          //reduce the top/bottom margins to 1/4 inch (ie. 18 points)
          Margins margins = new Margins();
          margins.bottom = 18;
          margins.top = 18;
          printJob.setMargins(margins);

          // and the rest of your code is the same...
      }
      //*******************************************************************************

      Thanks very much.

       
    • Nobody/Anonymous

      Any progress on this one?

       
      • Matthew Hall

        Matthew Hall - 2008-05-07

        I see the problem.  When you have default sized column, GridPrint calculates the minimum and preferred width of each column.  Then, depending on whether the available width is relative to the overall minimum width and overall preferred width, it divides up the space between the columns. 

        In your situation, the URLs in the VALUE column have a very large minimum width, which causes the overall minimum width of the grid to be wider than the page.  In this situation, GridPrint scales down every column proportionate to its minimum size.  This is why your other column headers are wrapping.

        Try creating the grid with "d,d,0:g" instead of "d,d,d" and see if that fixes your problem.

         
        • Matthew Hall

          Matthew Hall - 2008-05-20

          So.. did it work?

          Matthew

           
    • Nobody/Anonymous

      Nope.  No good.  Here's what I ended up doing:  "108PT,108PT,0:g".  That gave 1.5 inches to the first 2 columns and the rest of the space to the 3rd column.  In the absence of a more generic solution, this seems to be workable.

      Thanks for your help.

       

Log in to post a comment.

MongoDB Logo MongoDB