1 package org.objectweb.proactive.p2p.peerconfiguration; 2 import javax.swing.*; 3 import java.awt.Container ; 4 import java.util.Calendar ; 5 6 public class SpinnerSet extends JPanel { 7 protected JFormattedTextField ftf = null; 8 protected JSpinner spinnerMinutes = null; 9 protected JSpinner spinnerHour = null; 10 protected JSpinner spinnerDay = null; 11 protected JSpinner spinnerMonth = null; 12 protected JSpinner spinnerYear = null; 13 14 public SpinnerSet(boolean cycleMonths) { 15 super(new SpringLayout()); 16 17 String [] labels = {"Day: ", "Month: ", "Year: "}; 18 int numPairs = labels.length; 19 Calendar calendar = Calendar.getInstance(); 20 21 45 int currentDay = calendar.get(Calendar.DAY_OF_MONTH); 47 String [] days = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15", 48 "16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"}; 49 SpinnerModel dayModel = new CyclingSpinnerListModel(days); 50 spinnerDay = addLabeledSpinner(this, labels[0], dayModel); 51 ftf = getTextField(spinnerDay); 52 if (ftf != null ) { 53 ftf.setColumns(8); ftf.setHorizontalAlignment(JTextField.RIGHT); 55 } 56 57 String [] monthStrings = {"1","2","3","4","5","6","7","8","9","10","11","12"}; 59 SpinnerListModel monthModel = null; 60 if (cycleMonths) { monthModel = new CyclingSpinnerListModel(monthStrings); 62 } else { monthModel = new SpinnerListModel(monthStrings); 64 } 65 spinnerMonth = addLabeledSpinner(this, 66 labels[1], 67 monthModel); 68 ftf = getTextField(spinnerMonth); 69 if (ftf != null ) { 70 ftf.setColumns(8); ftf.setHorizontalAlignment(JTextField.RIGHT); 72 } 73 74 75 int currentYear = calendar.get(Calendar.YEAR); 77 SpinnerModel yearModel = new SpinnerNumberModel(currentYear, currentYear - 100, currentYear + 100, 1); if (monthModel instanceof CyclingSpinnerListModel) { 83 84 87 ((CyclingSpinnerListModel)dayModel).setLinkedModel(monthModel); 88 ((CyclingSpinnerListModel)monthModel).setLinkedModel(yearModel); 89 } 90 spinnerYear = addLabeledSpinner(this, labels[2], yearModel); 91 spinnerYear.setEditor(new JSpinner.NumberEditor(spinnerYear, "#")); 93 94 SpringUtilities.makeCompactGrid(this, 96 numPairs, 2, 10, 10, 6, 10); } 100 101 105 public JFormattedTextField getTextField(JSpinner spinner) { 106 JComponent editor = spinner.getEditor(); 107 if (editor instanceof JSpinner.DefaultEditor) { 108 return ((JSpinner.DefaultEditor)editor).getTextField(); 109 } else { 110 System.err.println("Unexpected editor type: " 111 + spinner.getEditor().getClass() 112 + " isn't a descendant of DefaultEditor"); 113 return null; 114 } 115 } 116 117 121 static protected String [] getMonthStrings() { 122 String [] months = new java.text.DateFormatSymbols ().getMonths(); 123 int lastIndex = months.length - 1; 124 125 if (months[lastIndex] == null 126 || months[lastIndex].length() <= 0) { String [] monthStrings = new String [lastIndex]; 128 System.arraycopy(months, 0, 129 monthStrings, 0, lastIndex); 130 return monthStrings; 131 } else { return months; 133 } 134 } 135 136 static protected JSpinner addLabeledSpinner(Container c, 137 String label, 138 SpinnerModel model) { 139 JLabel l = new JLabel(label); 140 c.add(l); 141 142 JSpinner spinner = new JSpinner(model); 143 l.setLabelFor(spinner); 144 c.add(spinner); 145 146 return spinner; 147 } 148 149 154 private static void createAndShowGUI() { 155 JFrame.setDefaultLookAndFeelDecorated(true); 157 158 JFrame frame = new JFrame("SpinnerSet"); 160 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 161 162 JComponent newContentPane = new SpinnerSet(false); 164 newContentPane.setOpaque(true); frame.setContentPane(newContentPane); 166 167 frame.pack(); 169 frame.setVisible(true); 170 } 171 172 public int getYear() { 173 return (((Integer ) spinnerYear.getValue()).intValue()); 174 } 175 public int getMonth() { 176 String monthString = (String ) spinnerMonth.getValue(); 177 if (monthString.compareTo("January")==0) return (1); 178 if (monthString.compareTo("February")==0) return (2); 179 if (monthString.compareTo("March")==0) return (3); 180 if (monthString.compareTo("April")==0) return (4); 181 if (monthString.compareTo("May")==0) return (5); 182 if (monthString.compareTo("June")==0) return (6); 183 if (monthString.compareTo("July")==0) return (7); 184 if (monthString.compareTo("August")==0) return (8); 185 if (monthString.compareTo("September")==0) return (9); 186 if (monthString.compareTo("October")==0) return (10); 187 if (monthString.compareTo("November")==0) return (11); 188 if (monthString.compareTo("December")==0) return (12); 189 return(1); 190 } 191 public int getDay() { 192 return ((new Integer ((String ) spinnerDay.getValue())).intValue()); 193 } 194 195 public JSpinner[] getSpinners() { 196 JSpinner[] result = {spinnerDay, spinnerMonth, spinnerYear}; 197 return (result); 198 } 199 200 } | Popular Tags |