HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"JOutlookBar"
Volume: 3 Issue: 8 p. 22

Download files assoicated with this article

	

Listing 1.
 
protected Vector tabs = new Vector(); 
protected Vector panels = new Vector(); 

public void setIndex(Container parent, int index) 
{ 
  this.index = index; 
  layoutContainer(parent); 
} 

public void addLayoutComponent(Component tab, Object panel) 
{ 
  if (panel == null) return; 
  tabs.addElement(tab); 
  panels.addElement(panel); 
} 

public void removeLayoutComponent(Component comp) 
{ 
  for (int i = 0; i < tabs.size(); i++) 
  { 
    if (tabs.elementAt(i) == comp) 
    { 
      tabs.removeElementAt(i); 
      panels.removeElementAt(i); 
      return; 
    } 
  } 
} 

Listing 2.
 
public Dimension preferredLayoutSize(Container target) 
{ 
  Insets insets = target.getInsets(); 
  Dimension tab = getPreferredTabSize(); 
  int h = tab.height * 
    (tabs.size() + 1) + (tabs.size() * hgap); 
  return new Dimension( 
    tab.width + insets.left + insets.right + (hgap * 2), 
    h + insets.top + insets.bottom); 
} 

private Dimension getPreferredTabSize() 
{ 
  int w = 0; 
  int h = 0; 
  Dimension size; 
  Component comp; 
  for (int i = 0; i < tabs.size(); i++) 
  { 
    comp = (Component)tabs.elementAt(i); 
    size = comp.getPreferredSize(); 
    if (size.width > w) w = size.width; 
    if (size.height > h) h = size.height; 
  } 
  return new Dimension(w, h); 
} 
  

Listing 3.
 
public void layoutContainer(Container target) 
{ 
  Dimension size = getPreferredTabSize(); 
  layoutTabs(target, index, size, target.getSize()); 
  layoutCenter(target, index, size, target.getSize()); 
} 

private void layoutCenter(Container cont, int index, Dimension size, Dimension parent) 
{ 
  Insets insets = cont.getInsets(); 
  int top = size.height * index + insets.top + 1 + 
    vgap * index + vgap; 
  int h = parent.height - (size.height + vgap ) * 
    tabs.size() - insets.top - insets.bottom - 2 - 
    vgap * 2; 
  
  if (center != null) cont.remove(center); 
  center = (Component)panels.elementAt(index - 1); 
  
  center.setBounds(insets.left + 1 + hgap, top, 
    parent.width - insets.left - 
    insets.right - 2 - (hgap * 2), h); 
  cont.add(center); 
  center.paintAll(center.getGraphics()); 
} 

private void layoutTabs(Container cont, int index, Dimension size, Dimension parent) 
{ 
  Insets insets = cont.getInsets(); 
  Component comp; 
  // Top tabs 
  int top = insets.top + 1 + vgap; 
  for (int i = 0; i < index; i++) 
  { 
    comp = (Component)tabs.elementAt(i); 
    comp.setBounds(insets.left + 1 + hgap, top, 
      parent.width - insets.left - 
      insets.right - 2 - (hgap * 2), 
      size.height); 
    top += size.height + vgap; 
  } 
  // Bottom tabs 
  top = parent.height - insets.bottom - 1 - 
    (size.height + vgap) * (tabs.size() - index); 
  for (int i = index; i < tabs.size(); i++) 
  { 
    comp = (Component)tabs.elementAt(i); 
    comp.setBounds(insets.left + 1 + hgap, top, 
      parent.width - insets.left - 
      insets.right - 2 - (hgap * 2), 
      size.height); 
    top += size.height + vgap; 
  } 
} 

Listing 4.
 
protected Vector buttons = new Vector(); 

public ContextPanel() 
{ 
  setLayout(new ContextLayout()); 
  setBorder(new BevelBorder(BevelBorder.LOWERED)); 
  setPreferredSize(new Dimension(80, 80)); 
} 

public void setIndex(int index) 
{ 
  ((ContextLayout)getLayout()).setIndex(this, index); 
} 

public void addTab(String name, Component comp) 
{ 
  JButton button = new TabButton(name); 
  add(button, comp); 
  buttons.addElement(button); 
  button.addActionListener(this); 
} 
  
public void removeTab(JButton button) 
{ 
  button.removeActionListener(this); 
  buttons.removeElement(button); 
  remove(button); 
} 
  
public void actionPerformed(ActionEvent event) 
{ 
  Object source = event.getSource(); 
  for (int i = 0; i < buttons.size(); i++) 
  { 
    if (source == buttons.elementAt(i)) 
    { 
      setIndex(i + 1); 
      return; 
    } 
  } 
} 

Listing 5.
 
public ScrollingPanel(Component component) 
{ 
  setLayout(new BorderLayout()); 
  north = new BasicArrowButton(BasicArrowButton.NORTH); 
  south = new BasicArrowButton(BasicArrowButton.SOUTH); 
  viewport = new JViewport(); 
  add("Center", viewport); 
  viewport.setView(component); 
  north.addActionListener(this); 
  south.addActionListener(this); 
} 

public void setBounds(int x, int y, int w, int h) 
{ 
  super.setBounds(x, y, w, h); 
  Dimension view = new Dimension(w, h); 
  Dimension pane = viewport.getView().getPreferredSize(); 
  viewport.setViewPosition(new Point(0, 0)); 
  remove(north); 
  if (pane.height >= view.height) 
  { 
    add("South", south); 
  } 
  else 
  { 
    remove(south); 
  } 
  doLayout(); 
} 
  
public void actionPerformed(ActionEvent event) 
{ 
  Dimension view = getSize(); 
  Dimension pane = viewport.getView().getPreferredSize(); 
  Point top = viewport.getViewPosition(); 
  if (event.getSource() == north) 
  { 
    if (pane.height > view.height) 
      add("South", south); 
    if (top.y < incr) 
    { 
      viewport.setViewPosition(new Point(0, 0)); 
      remove(north); 
    } 
    else 
    { 
      viewport.setViewPosition(new Point(0, top.y - incr)); 
    } 
    doLayout(); 
  } 
  if (event.getSource() == south) 
  { 
    if (pane.height > view.height) 
      add("North", north); 
    int max = pane.height - view.height; 
    if (top.y > (max - incr)) 
    { 
      remove(south); 
      doLayout(); 
      view = viewport.getExtentSize(); 
      max = pane.height - view.height; 
      viewport.setViewPosition(new Point(0, max)); 
    } 
    else 
    { 
      viewport.setViewPosition(new Point(0, top.y + incr)); 
    } 
    doLayout(); 
  } 
} 

Listing 6.
 
public RolloverButton(String name, Icon icon) 
{ 
  super(name, icon); 
  setOpaque(true); 
  setBackground(Color.gray); 
  setForeground(Color.white); 
  setMargin(new Insets(2, 2, 2, 2)); 
  setBorderPainted(false); 
  setFocusPainted(false); 
  setVerticalAlignment(TOP); 
  setHorizontalAlignment(CENTER); 
  setVerticalTextPosition(BOTTOM); 
  setHorizontalTextPosition(CENTER); 
  addMouseListener(this); 
} 

public void mouseEntered(MouseEvent event) 
{ 
  setBorderPainted(true); 
  setForeground(Color.black); 
  setBackground(Color.lightGray); 
  repaint(); 
} 

public void mouseExited(MouseEvent event) 
{ 
  setBorderPainted(false); 
  setForeground(Color.white); 
  setBackground(Color.gray); 
  repaint(); 
} 

Listing 7.
 
public void layoutContainer(Container parent) 
{ 
  Insets insets = parent.getInsets(); 
  int w = parent.getSize().width; 
  Component comp; 
  Dimension size; 
  int position = insets.top; 
  int ncomponents = parent.getComponentCount(); 
  for (int i = 0; i < ncomponents; i++) 
  { 
    comp = parent.getComponent(i); 
    size = comp.getPreferredSize(); 
    int h = size.height - insets.top - insets.bottom; 
    switch (alignment) 
    { 
      case CENTER: 
      { 
        int l = (w - size.width) / 2; 
        comp.setBounds(insets.left + hgap + l, position, 
          size.width - insets.left - insets.right - (hgap * 2), h); 
        break; 
      } 
      case LEFT: 
      { 
        comp.setBounds(insets.left + hgap, position, 
          size.width - insets.left - insets.right - (hgap * 2), h); 
        break; 
      } 
      case RIGHT: 
      { 
        int l = w - size.width; 
        comp.setBounds(insets.left + hgap + l, position, 
          size.width - insets.left - insets.right - (hgap * 2), h); 
        break; 
      } 
      default: 
      { 
        comp.setBounds(insets.left + hgap, position, 
          w - insets.left - insets.right - (hgap * 2), h); 
        break; 
      } 
    } 
    position += h + vgap; 
  } 
} 

Listing 8.
 
protected Vector names = new Vector(); 
protected Vector views = new Vector(); 
  
public void addIcon(String context, Action action) 
{ 
  int index; 
  JPanel view; 
  if ((index = names.indexOf(context)) > -1) 
  { 
    view = (JPanel)views.elementAt(index); 
  } 
  else 
  { 
    view = new JPanel(); 
    view.setBackground(Color.gray); 
    view.setLayout(new ListLayout()); 
    names.addElement(context); 
    views.addElement(view); 
    addTab(context, new ScrollingPanel(view)); 
  } 
  RolloverButton button = new RolloverButton( 
    (String)action.getValue(Action.NAME), 
    (Icon)action.getValue("LargeIcon")); 
  button.addActionListener(action); 
  view.add(button); 
  doLayout(); 
} 
  
      
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.