001: package org.wingx;
002:
003: import org.wings.*;
004: import org.wingx.table.TruncatableModel;
005:
006: import javax.swing.*;
007: import javax.swing.table.TableModel;
008: import java.awt.*;
009: import java.awt.event.*;
010: import java.text.MessageFormat;
011: import java.util.*;
012:
013: /**
014: * XScrollPane
015: *
016: * @author jdenzel
017: */
018: public class XScrollPane extends SScrollPane {
019:
020: private ExtentComboModel extentModel = new ExtentComboModel(
021: new Vector<Integer>(Arrays.asList(8, 10, 12, 14, 16, 18,
022: 20, 22, 24, 26, 28, 30, 32)));
023:
024: protected SComboBox extentCombo = new SComboBox(extentModel);
025: protected final XPageScroller pageScroller = new XPageScroller();
026: protected final SLabel extentComboLabel = new SLabel();
027: protected final SLabel totalLabel = new SLabel();
028: private STable tableComponent;
029: private String visibleSectionLabel = "{0} .. {1} of {2}";
030:
031: public XScrollPane() {
032: this (null);
033: }
034:
035: public XScrollPane(STable tableComponent) {
036: this (tableComponent, 10);
037: }
038:
039: public XScrollPane(STable tableComponent, int verticalExtent) {
040: setPreferredSize(SDimension.FULLAREA);
041: setVerticalAlignment(SConstants.TOP_ALIGN);
042:
043: extentCombo.addActionListener(new ExtentComboActionListener());
044:
045: pageScroller.add(totalLabel);
046: pageScroller.add(extentComboLabel);
047: pageScroller.add(extentCombo);
048: pageScroller.add(new SLabel(" "), 1d);
049:
050: pageScroller
051: .addAdjustmentListener(new PageAdjustmentListener());
052: pageScroller.setExtent(verticalExtent);
053: pageScroller.setHorizontalAlignment(SConstants.LEFT_ALIGN);
054:
055: setHorizontalScrollBar(pageScroller);
056: setHorizontalScrollBarPolicy(SScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
057: setVerticalScrollBar(null);
058: setVerticalScrollBarPolicy(SScrollPane.VERTICAL_SCROLLBAR_NEVER);
059: setMode(SScrollPane.MODE_PAGING);
060:
061: setHorizontalExtent(20);
062: setVerticalExtent(verticalExtent);
063:
064: if (tableComponent != null)
065: setViewportView(tableComponent);
066: }
067:
068: /**
069: * Sets the horizontal scrollbar.
070: *
071: * @param sb the scrollbar that controls the viewport's horizontal view position
072: * @param constraint the constraint for the {@link LayoutManager} of this {@link SContainer}.
073: * The {@link LayoutManager} is per default {@link SScrollPaneLayout}.
074: */
075: public void setHorizontalScrollBar(Adjustable sb, String constraint) {
076: if (horizontalScrollBar != null) {
077: if (horizontalScrollBar instanceof SAbstractAdjustable)
078: ((SAbstractAdjustable) horizontalScrollBar)
079: .setModel(new SDefaultBoundedRangeModel());
080: else if (horizontalScrollBar instanceof XPageScroller)
081: ((XPageScroller) horizontalScrollBar)
082: .setModel(new SDefaultBoundedRangeModel());
083:
084: if (horizontalScrollBar instanceof SComponent)
085: remove((SComponent) horizontalScrollBar);
086: }
087:
088: horizontalScrollBar = sb;
089:
090: if (horizontalScrollBar != null) {
091: if (horizontalScrollBar instanceof SComponent)
092: addComponent((SComponent) horizontalScrollBar,
093: constraint, getComponentCount());
094:
095: if (horizontalScrollBar instanceof SAbstractAdjustable) {
096: SAbstractAdjustable scrollbar = (SAbstractAdjustable) horizontalScrollBar;
097: if (scrollbar.getOrientation() == SConstants.HORIZONTAL)
098: scrollbar.setModel(horizontalModel);
099: else
100: scrollbar.setModel(verticalModel);
101: } else if (horizontalScrollBar instanceof XPageScroller) {
102: XPageScroller scrollbar = (XPageScroller) horizontalScrollBar;
103: if (scrollbar.getOrientation() == SConstants.HORIZONTAL)
104: scrollbar.setModel(horizontalModel);
105: else
106: scrollbar.setModel(verticalModel);
107: }
108:
109: adoptScrollBarVisibility(horizontalScrollBar,
110: horizontalScrollBarPolicy);
111: }
112:
113: reload();
114: }
115:
116: /**
117: * Sets the vertical scrollbar.
118: *
119: * @param sb the scrollbar that controls the viewport's vertical view position
120: * @param constraint the constraint for the {@link LayoutManager} of this {@link SContainer}.
121: * The {@link LayoutManager} is per default {@link SScrollPaneLayout}.
122: */
123: public void setVerticalScrollBar(Adjustable sb, String constraint) {
124: if (verticalScrollBar != null) {
125: if (verticalScrollBar instanceof SAbstractAdjustable)
126: ((SAbstractAdjustable) verticalScrollBar)
127: .setModel(new SDefaultBoundedRangeModel());
128: else if (verticalScrollBar instanceof XPageScroller)
129: ((XPageScroller) verticalScrollBar)
130: .setModel(new SDefaultBoundedRangeModel());
131:
132: if (verticalScrollBar instanceof SComponent)
133: remove((SComponent) verticalScrollBar);
134: }
135:
136: verticalScrollBar = sb;
137:
138: if (verticalScrollBar != null) {
139: if (verticalScrollBar instanceof SComponent)
140: addComponent((SComponent) verticalScrollBar,
141: constraint, getComponentCount());
142:
143: if (verticalScrollBar instanceof SAbstractAdjustable) {
144: SAbstractAdjustable scrollbar = (SAbstractAdjustable) verticalScrollBar;
145: if (scrollbar.getOrientation() == SConstants.HORIZONTAL)
146: scrollbar.setModel(horizontalModel);
147: else
148: scrollbar.setModel(verticalModel);
149: } else if (verticalScrollBar instanceof XPageScroller) {
150: XPageScroller scrollbar = (XPageScroller) verticalScrollBar;
151: if (scrollbar.getOrientation() == SConstants.HORIZONTAL)
152: scrollbar.setModel(horizontalModel);
153: else
154: scrollbar.setModel(verticalModel);
155: }
156:
157: adoptScrollBarVisibility(verticalScrollBar,
158: verticalScrollBarPolicy);
159: }
160:
161: reload();
162: }
163:
164: public void setExtentLabel(String label) {
165: extentComboLabel.setText(label);
166: }
167:
168: public String getVisibleSectionLabel() {
169: return visibleSectionLabel;
170: }
171:
172: public void setVisibleSectionLabel(String visibleSectionLabel) {
173: this .visibleSectionLabel = visibleSectionLabel;
174: }
175:
176: /**
177: * @inheritDoc
178: */
179: public void setViewportView(SComponent view) {
180: if (view == null) {
181: throw new NullPointerException();
182: }
183: if (getScrollable() != null) {
184: throw new RuntimeException(
185: "error: this component is not reinitializable");
186: }
187: if (!(view instanceof XTable)) {
188: throw new RuntimeException(
189: "the inner component must be of type XTable");
190: }
191: tableComponent = (XTable) view;
192: tableComponent.setVerticalAlignment(SConstants.TOP_ALIGN);
193:
194: super .setViewportView(view);
195: refresh();
196: }
197:
198: public void setExtents(Integer[] extents) {
199: extentModel.setExtents(extents);
200: reload();
201: }
202:
203: public void setVerticalExtent(int extent) {
204: super .setVerticalExtent(extent);
205: extentModel.addExtent(extent);
206: extentCombo.setSelectedItem(extent);
207: }
208:
209: public void addAdjustmentListener(AdjustmentListener al) {
210: pageScroller.addAdjustmentListener(al);
211: }
212:
213: public void refresh() {
214: scrollable.getViewportSize().height = verticalExtent;
215: refreshTotalLabel();
216: }
217:
218: private void refreshTotalLabel() {
219: if (tableComponent == null)
220: return;
221:
222: Rectangle viewportSize = tableComponent.getViewportSize();
223: if (viewportSize == null) {
224: totalLabel.setText(null);
225: return;
226: }
227:
228: int startRow = viewportSize.y;
229: int endRow = tableComponent.getRowCount();
230: endRow = Math.min(startRow + viewportSize.height, endRow);
231:
232: String text;
233: if (endRow > 0) {
234: TableModel tableModel = tableComponent.getModel();
235: int rowCount = tableModel.getRowCount();
236: text = MessageFormat.format(visibleSectionLabel,
237: startRow + 1, endRow, rowCount);
238:
239: if (tableModel instanceof TruncatableModel) {
240: if (((TruncatableModel) tableModel).isTruncated())
241: text += " (+)";
242: }
243: } else {
244: text = null;
245: }
246:
247: totalLabel.setText(text);
248: }
249:
250: class PageAdjustmentListener implements AdjustmentListener {
251: public void adjustmentValueChanged(AdjustmentEvent e) {
252: refreshTotalLabel();
253: //pageScroller.reload();
254: }
255: }
256:
257: class ExtentComboActionListener implements ActionListener {
258: public void actionPerformed(ActionEvent e) {
259: if (tableComponent == null)
260: return;
261: Integer extent = (Integer) extentCombo.getSelectedItem();
262: assert extent != null;
263: setVerticalExtent(extent.intValue());
264: }
265: }
266:
267: class ExtentComboModel extends DefaultComboBoxModel {
268: private Vector<Integer> extents;
269:
270: public ExtentComboModel(Vector<Integer> extents) {
271: super (extents);
272: this .extents = extents;
273: }
274:
275: public void setExtents(Integer[] extents) {
276: this .extents.removeAllElements();
277: this .extents.addAll(Arrays.asList(extents));
278: if (!addExtent((Integer) getSelectedItem())) {
279: ;
280: fireContentsChanged(this , -1, -1);
281: }
282: }
283:
284: public boolean addExtent(int extent) {
285: if (!extents.contains(extent)) {
286: extents.add(extent);
287: Collections.sort(extents);
288: fireContentsChanged(this , -1, -1);
289: return true;
290: }
291: return false;
292: }
293: }
294:
295: public XPageScroller getPageScroller() {
296: return pageScroller;
297: }
298: }
|