1 /*
2 * Created on Oct 27, 2003
3 *
4 * To change the template for this generated file go to
5 * Window>Preferences>Java>Code Generation>Code and Comments
6 */
7 package portaview.view;
8
9 import java.awt.Dimension;
10 import java.awt.FlowLayout;
11 import java.util.Iterator;
12 import java.util.List;
13 import java.util.Observable;
14 import java.util.Observer;
15 import java.util.Vector;
16
17 import javax.swing.ButtonGroup;
18 import javax.swing.JPanel;
19 import javax.swing.event.ChangeEvent;
20 import javax.swing.event.ChangeListener;
21
22 import portaview.model.ObservableCollection;
23
24 /***
25 * Generic implementation for the content that lives in the IconPanel. This contains
26 * the collection of views and is scrollable.
27 *
28 * @author William Lee
29 */
30 public class IconPanelContent
31 extends JPanel
32 implements ChangeListener, Observer
33 {
34 /***
35 * Try to fit the icons based on the width of the viewport.
36 */
37 public static int FIT_WIDTH = 0;
38 /***
39 * Try to fit the icons based on the height of the viewport.
40 */
41 public static int FIT_HEIGHT = 1;
42
43 private int mode = FIT_WIDTH;
44 private ObservableCollection models = null;
45 private List views = null;
46 private IconPanel panel = null;
47
48 /***
49 * The button group used to control the select behavior.
50 */
51 private ButtonGroup buttonGroup = new ButtonGroup();
52
53 /***
54 * Selects the first view.
55 *
56 */
57 public void selectFirst()
58 {
59 select(0);
60 }
61
62 /***
63 * Selects the nth element.
64 * @param n the index to select.
65 */
66 public void select(int n)
67 {
68 int sel = n;
69 if (views == null)
70 return;
71 if (views.size() == 0)
72 return;
73 if (sel >= views.size())
74 sel = 0;
75 IconView iv = (IconView) views.get(sel);
76 if (iv == null)
77 return;
78 iv.doClick();
79 }
80
81 public IconPanelContent(IconPanel panel, ObservableCollection models)
82 {
83 this.panel = panel;
84 this.models = models;
85 this.views = new Vector();
86 FlowLayout layout = new FlowLayout();
87 layout.setAlignment(FlowLayout.LEFT);
88 setLayout(layout);
89 models.addObserver(this);
90 refresh();
91 }
92
93 /***
94 * Refreshes the content of this view.
95 */
96 public void refresh()
97 {
98 int lastSelected = 0;
99 int ctr = 0;
100 IconView sampleIcon = null;
101 for (Iterator iter = views.iterator(); iter.hasNext();)
102 {
103 IconView iv = (IconView) iter.next();
104 if (iv.isSelected())
105 lastSelected = ctr;
106 panel.removeView(iv);
107 buttonGroup.remove(iv);
108 ctr++;
109 }
110 // remove all the content
111 removeAll();
112 boolean isFirst = true;
113 boolean isSecond = false;
114 for (Iterator iter = models.iterator(); iter.hasNext();)
115 {
116 IconView icon =
117 (IconView) panel.getViewFromModel(iter.next(), panel);
118 views.add(icon);
119 if (isFirst)
120 {
121 add(panel.decorateFirstView(icon));
122 isFirst = false;
123 isSecond = true;
124 }
125 else if (isSecond)
126 {
127 add(panel.decorateSecondView(icon));
128 isSecond = false;
129 }
130 else
131 {
132 add(icon);
133 }
134 buttonGroup.add(icon);
135 icon.refresh();
136 sampleIcon = icon;
137 }
138 select(lastSelected);
139 /*
140 setPreferredSize(
141 new Dimension(panel.getViewport().getWidth(), getDesiredHeight()));
142 */
143 int width = 100;
144 int height = 80 * models.size();
145 if (sampleIcon != null)
146 {
147 width = sampleIcon.getWidth();
148 height = (sampleIcon.getHeight() * models.size()) + 20;
149 }
150 setPreferredSize(new Dimension(width, height));
151 }
152
153 /***
154 * Recalculate the row of icons in order to fit the view port better.
155 *
156 * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
157 */
158 public void stateChanged(ChangeEvent e)
159 {
160 //refresh();
161 }
162
163 /***
164 * Listen to the event if the observable has changed.
165 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
166 */
167 public void update(Observable o, Object arg)
168 {
169 refresh();
170 }
171
172 }
This page was automatically generated by Maven