View Javadoc
1   /*
2    * Created on 2005-maj-21
3    */
4   package net.sourceforge.jsh3modtool.gui.imagetable;
5   
6   import java.awt.event.ComponentAdapter;
7   import java.awt.event.ComponentEvent;
8   
9   import javax.swing.JScrollPane;
10  
11  /***
12   * @author redsolo
13   */
14  public class ImagePane extends JScrollPane 
15  {
16      private final ImageTable table;
17  
18      /***
19       * 
20       */
21      public ImagePane() {
22          this(400,300);
23      }
24      
25      /***
26       * 
27       */
28      public ImagePane(final int imageWidth, final int imageHeight) {
29          super();
30          table = new ImageTable(imageHeight,imageHeight);
31          setViewportView(table);
32          addComponentListener(new ImagePaneResizer(table));
33          getViewport().setBackground(table.getBackground());
34      }
35      
36      /***
37       * Component resizer that updates the column count depending on the viewport dimension.
38       * @author redsolo
39       */
40      private class ImagePaneResizer extends ComponentAdapter
41  	{
42      	private final ImageTable table;
43      	
44  		/***
45  		 * @param table the image table to resize
46  		 */
47  		public ImagePaneResizer(ImageTable table) {
48  			super();
49  			this.table = table;
50  		}
51  		
52      	public void componentResized(final ComponentEvent arg0) {
53          	//if (arg0.getComponent() == table)
54          	{
55          		final int componentWidth = getViewport().getWidth();
56      	        final int columnWidth = table.getColumnWidth() + table.getIntercellSpacing().width;
57      	        int nrOfCols = componentWidth / columnWidth;
58      	        if (nrOfCols == 0)
59      	        {
60      	            nrOfCols = 1;
61      	        }
62      	        table.setColumnCount(nrOfCols);
63          	}
64          }	
65  	}
66  
67      /***
68       * @return the table model
69       */
70      public ImageTableModel getModel() {
71          return (ImageTableModel) table.getModel();
72      }
73  }