001: /*
002: * SpinnerExample.java
003: *
004: * Created on 6. September 2006, 08:53
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009:
010: package wingset;
011:
012: import org.wings.*;
013:
014: import java.util.List;
015: import java.util.LinkedList;
016:
017: import java.util.Date;
018: import java.util.Calendar;
019: import java.util.GregorianCalendar;
020:
021: import javax.swing.SpinnerListModel;
022: import javax.swing.SpinnerDateModel;
023: import javax.swing.SpinnerNumberModel;
024:
025: /**
026: *
027: * @author erik
028: */
029: public class SpinnerExample extends WingSetPane {
030: protected SComponent createControls() {
031: return null;
032: }
033:
034: public SComponent createExample() {
035:
036: SGridLayout layout = new SGridLayout(4, 2, 10, 10);
037: SPanel panel = new SPanel(layout);
038:
039: panel.add(new SLabel("Fruits : ", SConstants.RIGHT_ALIGN));
040: panel.add(getListSpinner());
041: panel.add(new SLabel("Month : ", SConstants.RIGHT_ALIGN));
042: panel.add(getDateSpinner());
043: panel.add(new SLabel("Percent : ", SConstants.RIGHT_ALIGN));
044: panel.add(getNumberSpinner());
045: panel.add(new SLabel("Calendar : ", SConstants.RIGHT_ALIGN));
046:
047: Calendar calendar = new GregorianCalendar();
048: Date initDate = calendar.getTime();
049:
050: calendar.add(Calendar.YEAR, -50);
051: Date earliestDate = calendar.getTime();
052:
053: calendar.add(Calendar.YEAR, 100);
054: Date latestDate = calendar.getTime();
055:
056: SSpinner spinner = new SSpinner(new SpinnerDateModel(initDate,
057: earliestDate, latestDate, Calendar.MONTH));
058: panel.add(spinner);
059:
060: return panel;
061:
062: }
063:
064: private SSpinner getListSpinner() {
065:
066: List fruits = new LinkedList();
067: fruits.add("Apple");
068: fruits.add("Banana");
069: fruits.add("Cherry");
070: fruits.add("Litchi");
071: fruits.add("Pineapple");
072:
073: SSpinner listSpinner = new SSpinner(
074: new SpinnerListModel(fruits));
075: listSpinner.setHorizontalAlignment(SConstants.RIGHT_ALIGN);
076:
077: getTextField(listSpinner).setColumns(10);
078:
079: return listSpinner;
080: }
081:
082: private SSpinner getDateSpinner() {
083:
084: Calendar calendar = new GregorianCalendar();
085: Date initDate = calendar.getTime();
086:
087: calendar.add(Calendar.YEAR, -50);
088: Date earliestDate = calendar.getTime();
089:
090: calendar.add(Calendar.YEAR, 100);
091: Date latestDate = calendar.getTime();
092:
093: SSpinner dateSpinner = new SSpinner(new SpinnerDateModel(
094: initDate, earliestDate, latestDate, Calendar.MONTH));
095: dateSpinner.setHorizontalAlignment(SConstants.RIGHT_ALIGN);
096: dateSpinner.setEditor(new SSpinner.DateEditor(dateSpinner,
097: "MM/yyyy"));
098:
099: getTextField(dateSpinner).setColumns(10);
100:
101: return dateSpinner;
102:
103: }
104:
105: private SSpinner getNumberSpinner() {
106:
107: SSpinner numberSpinner = new SSpinner();
108: SpinnerNumberModel numberSpinnerModel = new SpinnerNumberModel(
109: 50, 0, 100, 5);
110:
111: numberSpinner.setModel(numberSpinnerModel);
112:
113: SSpinner.NumberEditor numberEditor = new SSpinner.NumberEditor(
114: numberSpinner);
115:
116: numberSpinner.setEditor(numberEditor);
117:
118: numberSpinner.setHorizontalAlignment(SConstants.RIGHT_ALIGN);
119:
120: getTextField(numberSpinner).setColumns(10);
121:
122: return numberSpinner;
123:
124: }
125:
126: private STextField getTextField(SSpinner spinner) {
127:
128: STextField textField = null;
129:
130: SComponent editor = spinner.getEditor();
131: if (editor instanceof SSpinner.DefaultEditor) {
132: textField = ((SSpinner.DefaultEditor) editor)
133: .getTextField();
134: } else {
135: System.err.println("Error");
136: }
137: return textField;
138:
139: }
140:
141: /*
142: public static class CalendarEditor extends SSpinner.DefaultEditor {
143:
144: XCalendar calendar = null;
145:
146: public CalendarEditor ( SSpinner spinner, SDateFormatter formatter ) {
147: super( spinner );
148:
149: removeAll();
150:
151: calendar = new XCalendar( formatter );
152: calendar.getFormattedTextField().setColumns( 15 );
153:
154: add( calendar );
155:
156: }
157:
158: public SFormattedTextField getTextField() {
159: return calendar.getFormattedTextField();
160: }
161:
162: }
163: */
164:
165: }
|