Changeset 1885

Show
Ignore:
Timestamp:
05/15/09 12:04:10 (3 years ago)
Author:
eitan
Message:

improved calendar layout algorithm to address last case presented by okmijn42

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • jmatter-complet/trunk/modules/swingvm/src/com/u2d/view/swing/calendar/PositionedLayout.java

    r1859 r1885  
    114114   } 
    115115 
     116   static int event_padding = 4; 
     117 
    116118    public void layoutContainer(Container parent) 
    117119    { 
     
    123125         Collections.sort(_events); 
    124126         int xPos = 0; 
    125          for (CalEvent event : _events) 
     127 
     128         CalEvent prevEvt = null; 
     129         for (CalEvent calEvent : _events) 
    126130         { 
    127             List<CalEvent> overlappingEvents = calcOverlappingEventsFor(event); 
    128             xPos = layoutEvent(event, overlappingEvents, xPos); 
     131            List<CalEvent> overlappingEvents = calcOverlappingEventsFor(calEvent); 
     132 
     133            Rectangle bounds = _view.getBounds(calEvent); 
     134            int numOverlapping = overlappingEvents.size(); 
     135            int width = bounds.width / numOverlapping; 
     136            if (xPos + width > bounds.width) 
     137            { 
     138               // first, correct x position of previous event (align right to avoid overlap conflict 
     139               // with next overlapping items) 
     140               if (prevEvt != null && overlappingEvents.contains(prevEvt)) 
     141               { 
     142                  Component prevComp = _components.get(prevEvt); 
     143                  Point location = prevComp.getLocation(); 
     144                  int x = bounds.x + bounds.width - (prevComp.getWidth() + event_padding/2); 
     145                  prevComp.setLocation(x, location.y); 
     146               } 
     147 
     148               // second, carriage-return logic 
     149               xPos = 0; 
     150               for (CalEvent e : overlappingEvents) 
     151               { 
     152                  if (e == calEvent) break; 
     153                  Rectangle ebounds = _components.get(e).getBounds(); 
     154                  int pos = xPos + (event_padding / 2); 
     155                  if (ebounds.getX() - bounds.x == pos) 
     156                  { 
     157                     xPos += _components.get(e).getWidth() + event_padding; 
     158                  } 
     159               } 
     160            } 
     161 
     162            Rectangle eventBounds = new Rectangle(bounds.x + xPos + event_padding/2, bounds.y, width - event_padding, bounds.height); 
     163            _components.get(calEvent).setBounds(eventBounds); 
     164             
     165            xPos += xPos + width; 
     166 
     167            prevEvt = calEvent; 
    129168         } 
     169 
     170 
    130171      } 
    131172   } 
    132173 
    133    static int event_padding = 4; 
    134  
    135    /** 
    136     * @param event event to layout 
    137     * @param overlappingEvents list of overlapping events for event 
    138     * @param xPos current x position 
    139     * @return next xPos 
    140     */ 
    141    private int layoutEvent(CalEvent event, List<CalEvent> overlappingEvents, int xPos) 
    142    { 
    143       Component comp = _components.get(event); 
    144       Rectangle bounds = _view.getBounds(event); 
    145       int numOverlapping = overlappingEvents.size(); 
    146       int width = bounds.width / numOverlapping; 
    147       if (xPos + width > bounds.getWidth()) 
    148       { 
    149          xPos = 0; 
    150          for (CalEvent e : overlappingEvents) 
    151          { 
    152             if (e == event) break; 
    153             Rectangle ebounds = _components.get(e).getBounds(); 
    154             int pos = xPos + (event_padding / 2); 
    155             if (ebounds.getX() - bounds.x == pos) 
    156             { 
    157                xPos += _components.get(e).getWidth() + event_padding; 
    158             } 
    159          } 
    160       } 
    161       comp.setBounds(new Rectangle(bounds.x + xPos + event_padding/2, bounds.y, width - event_padding, bounds.height)); 
    162       return xPos + width; 
    163     } 
    164     
    165174}