Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 12th, 2013  |  syntax: None  |  size: 6.06 KB  |  views: 32  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public void movingElements()
  2. {
  3.     //gets the new filler height to compare
  4.     int newFillerHeight = getFiller().getHeight();
  5.  
  6.     //chapter contains an array of pages I need to deal with here
  7.     Chapter chapter = getChapter();
  8.  
  9.     //element removed/shrunk
  10.     //compares the oldFillerHeight which contains the height of the filler
  11.     //prior to this particular resizing
  12.     else if (newFillerHeight >= oldFillerHeight)
  13.     {  
  14.             //fetches the next and previous page of this page (getPage()
  15.             //returns page we are dealing with)
  16.         Page previousPage = chapter.getPreviousPage(getPage());
  17.         Page nextPage = chapter.getNextPage(getPage());
  18.  
  19.             //here is where it gets tricky
  20.             //I didn't want to check if first (few) element(s) can be
  21.             //moved to previous page (which can happen if the first, large
  22.             //element followed by several small ones is removed) (CHECK A) AND
  23.             //if any elements on the next page can be moved to this one
  24.             //(CHECK B). What I did instead was to always do the CHECK A.
  25.  
  26.             //if this is the first page of the chapter, I cannot perform the
  27.             //CHECK A
  28.         if (previousPage == null)
  29.         {
  30.                     //I have to invoke this method on the next page, if it
  31.                     //exists. If it doesn't, then this is the only page of
  32.                     //the chapter and I have nothing to do here.
  33.             if (nextPage != null)
  34.             {
  35.                             //this is explained bellow this method
  36.                 nextPage.dummy.setVisible(true);
  37.             }
  38.         }
  39.  
  40.             //if previous page exists, we preform CHECK A
  41.         else
  42.         {
  43.             Element mover = getElement(1);
  44.  
  45.                     //I have to check if the first element on this page fits
  46.                     //onto the free space of the previous one
  47.                     //-2 is required to prevent infinite loops
  48.             if (mover.getHeight() < previousPage.getFiller().getHeight()-2)
  49.             {
  50.                             //I move the element
  51.                 removeElement(mover);
  52.                 previousPage.addElement(mover, previousPage.getElementCount()+1);
  53.  
  54.                             //This is a flag that tells that an object was
  55.                             //moved, you'll se why I need it soon enough
  56.                 chapter.setMoved(true);
  57.             }
  58.  
  59.                     //If I can't move the object, I have move onto the next
  60.                     //page (if it exists) and repeat the process. I also
  61.                     //check for isMoved flag because maybe nothing was moved
  62.                     //on the previous page and there is no need to keep the
  63.                     //this chain of execution going
  64.             else if ((nextPage != null) && (chapter.isMoved()))
  65.             {
  66.                             //sets isMoved flag to false so the above code
  67.                             //would work
  68.                             chapter.setMoved(false);
  69.                 nextPage.dummy.setVisible(true);
  70.             }
  71.         }
  72.     }
  73.  
  74.     //saves the new filer height for further use
  75.     oldFillerHeight = newFillerHeight;
  76. }
  77.        
  78. dummy = new JPanel();
  79. dummy.setVisible(false);
  80.  
  81. dummy.addComponentListener(new ComponentAdapter()
  82. {
  83.     @Override
  84.     public void componentShown(ComponentEvent arg0)
  85.     {
  86.             dummy.setVisible(false);
  87.         movingElements();  
  88.     }          
  89. });
  90.        
  91. filler.addComponentListener(new ComponentAdapter()
  92.     {
  93.         @Override
  94.         public void componentResized(ComponentEvent arg0)
  95.         {  
  96.             if (!getChapter().isChaining())
  97.             {
  98.                 getChapter().setChaining(true);
  99.                 movingElements();
  100.             }
  101.  
  102.             oldFillerHeight = getFiller().getHeight();
  103.         }
  104.     });
  105.        
  106. dummy.addComponentListener(new ComponentAdapter()
  107.     {
  108.         @Override
  109.         public void componentShown(ComponentEvent arg0)
  110.         {
  111.             dummy.setVisible(false);
  112.             movingElements();
  113.         }          
  114.     });
  115.        
  116. public void movingElements()
  117. {
  118.     int newFillerHeight = getFiller().getHeight();
  119.     Document document = getDocument();
  120.     Chapter chapter = getChapter();
  121.  
  122.     //element added/enlarged
  123.     if (newFillerHeight == 0)
  124.     {  
  125.         Page nextPage = chapter.getNextPage(getPage());
  126.  
  127.         if (nextPage == null)
  128.         {
  129.             nextPage = new Page();
  130.             chapter.addPage(nextPage, chapter.getPageIndex(getPage())+1);
  131.         }
  132.  
  133.         Element mover = getPage().getElement(getPage().getElementCount());
  134.  
  135.         removeElement(mover);
  136.         nextPage.addElement(mover, 1);
  137.  
  138.         getPage().dummy.setVisible(true);
  139.     }
  140.  
  141.     //element removed/shrunk
  142.     else if (newFillerHeight >= oldFillerHeight)
  143.     {  
  144.         Page previousPage = chapter.getPreviousPage(getPage());
  145.         Page nextPage = chapter.getNextPage(getPage());
  146.  
  147.         if (previousPage == null)
  148.         {
  149.             if (nextPage != null)
  150.             {
  151.                 nextPage.dummy.setVisible(true);
  152.             }
  153.  
  154.             else
  155.             {
  156.                 //chain end
  157.                 chapter.setChaining(false);
  158.             }
  159.         }
  160.  
  161.         else
  162.         {
  163.             Element mover = getElement(1);
  164.  
  165.             if (mover.getHeight() < previousPage.getFiller().getHeight()-2)  //-2 is required to prevent infinite loops
  166.             {
  167.                 removeElement(mover);
  168.                 previousPage.addElement(mover, previousPage.getElementCount()+1);
  169.  
  170.                 chapter.setMoved(true);
  171.  
  172.                 getPage().dummy.setVisible(true);
  173.             }
  174.  
  175.             else if ((nextPage != null) && (chapter.isMoved()))
  176.             {
  177.                 nextPage.dummy.setVisible(true);
  178.             }
  179.  
  180.             else
  181.             {
  182.                 //chain end
  183.                 chapter.setChaining(false);
  184.             }
  185.         }
  186.     }
  187.  
  188.     else
  189.     {
  190.         //chain end
  191.         chapter.setChaining(false);
  192.     }
  193. }
  194.        
  195. private AtomicBoolean chaining= new AtomicBoolean(false);
  196.  
  197. public boolean isChaining()
  198. {
  199.     return chaining.get();
  200. }
  201.  
  202. public void setChaining(boolean chaining)
  203. {
  204.     this.chaining.set(chaining);
  205. }