001: package hero.client.grapheditor;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import java.util.Calendar;
006: import java.text.SimpleDateFormat;
007:
008: import javax.swing.*;
009: import javax.swing.event.PopupMenuListener;
010: import javax.swing.event.PopupMenuEvent;
011: import javax.swing.plaf.ComboBoxUI;
012: import javax.swing.plaf.basic.ComboPopup;
013: import javax.swing.plaf.metal.MetalComboBoxUI;
014: import javax.swing.border.Border;
015: import javax.swing.border.EtchedBorder;
016: import javax.swing.border.EmptyBorder;
017:
018: import com.sun.java.swing.plaf.motif.MotifComboBoxUI;
019: import com.sun.java.swing.plaf.windows.WindowsComboBoxUI;
020:
021: /**
022: * @version 1.0 11/02/2000
023: */
024:
025: //////////////////////////////////////////////////////////////
026: public class DateComboBox extends JComboBox {
027:
028: static java.util.ResourceBundle resource = java.util.ResourceBundle
029: .getBundle("resources.Traduction")/*#BundleType=List*/;
030:
031: protected SimpleDateFormat dateFormat = new SimpleDateFormat(
032: "MMM d, yyyy");
033:
034: public void setDateFormat(SimpleDateFormat dateFormat) {
035: this .dateFormat = dateFormat;
036: }
037:
038: public void setSelectedItem(Object item) {
039: // Could put extra logic here or in renderer when item is instanceof Date, Calendar, or String
040: // Dont keep a list ... just the currently selected item
041: removeAllItems(); // hides the popup if visible
042: addItem(item);
043: super .setSelectedItem(item);
044: }
045:
046: public void updateUI() {
047: ComboBoxUI cui = (ComboBoxUI) UIManager.getUI(this );
048: if (cui instanceof MetalComboBoxUI) {
049: cui = new MetalDateComboBoxUI();
050: } else if (cui instanceof MotifComboBoxUI) {
051: cui = new MotifDateComboBoxUI();
052: } else if (cui instanceof WindowsComboBoxUI) {
053: cui = new WindowsDateComboBoxUI();
054: }
055: setUI(cui);
056: }
057:
058: // Inner classes are used purely to keep DateComboBox component in one file
059: //////////////////////////////////////////////////////////////
060: // UI Inner classes -- one for each supported Look and Feel
061: //////////////////////////////////////////////////////////////
062:
063: class MetalDateComboBoxUI extends MetalComboBoxUI {
064: protected ComboPopup createPopup() {
065: return new DatePopup(comboBox);
066: }
067: }
068:
069: class WindowsDateComboBoxUI extends WindowsComboBoxUI {
070: protected ComboPopup createPopup() {
071: return new DatePopup(comboBox);
072: }
073: }
074:
075: class MotifDateComboBoxUI extends MotifComboBoxUI {
076: protected ComboPopup createPopup() {
077: return new DatePopup(comboBox);
078: }
079: }
080:
081: //////////////////////////////////////////////////////////////
082: // DatePopup inner class
083: //////////////////////////////////////////////////////////////
084:
085: class DatePopup implements ComboPopup, MouseMotionListener,
086: MouseListener, KeyListener, PopupMenuListener {
087:
088: protected JComboBox comboBox;
089: protected Calendar calendar;
090: protected JPopupMenu popup;
091: protected JLabel monthLabel;
092: protected JPanel days = null;
093: protected SimpleDateFormat monthFormat = new SimpleDateFormat(
094: "MMM yyyy");
095:
096: protected Color selectedBackground;
097: protected Color selectedForeground;
098: protected Color background;
099: protected Color foreground;
100:
101: public DatePopup(JComboBox comboBox) {
102: this .comboBox = comboBox;
103: calendar = Calendar.getInstance();
104: // check Look and Feel
105: background = UIManager.getColor("ComboBox.background");
106: foreground = UIManager.getColor("ComboBox.foreground");
107: selectedBackground = UIManager
108: .getColor("ComboBox.selectionBackground");
109: selectedForeground = UIManager
110: .getColor("ComboBox.selectionForeground");
111:
112: initializePopup();
113: }
114:
115: //========================================
116: // begin ComboPopup method implementations
117: //
118: public void show() {
119: try {
120: // if setSelectedItem() was called with a valid date, adjust the calendar
121: calendar.setTime(dateFormat.parse(comboBox
122: .getSelectedItem().toString()));
123: } catch (Exception e) {
124: }
125: updatePopup();
126: popup.show(comboBox, 0, comboBox.getHeight());
127: }
128:
129: public void hide() {
130: popup.setVisible(false);
131: }
132:
133: protected JList list = new JList();
134:
135: public JList getList() {
136: return list;
137: }
138:
139: public MouseListener getMouseListener() {
140: return this ;
141: }
142:
143: public MouseMotionListener getMouseMotionListener() {
144: return this ;
145: }
146:
147: public KeyListener getKeyListener() {
148: return this ;
149: }
150:
151: public boolean isVisible() {
152: return popup.isVisible();
153: }
154:
155: public void uninstallingUI() {
156: popup.removePopupMenuListener(this );
157: }
158:
159: //
160: // end ComboPopup method implementations
161: //======================================
162:
163: //===================================================================
164: // begin Event Listeners
165: //
166:
167: // MouseListener
168:
169: public void mousePressed(MouseEvent e) {
170: }
171:
172: public void mouseReleased(MouseEvent e) {
173: }
174:
175: // something else registered for MousePressed
176: public void mouseClicked(MouseEvent e) {
177: if (!SwingUtilities.isLeftMouseButton(e))
178: return;
179: if (!comboBox.isEnabled())
180: return;
181: if (comboBox.isEditable()) {
182: comboBox.getEditor().getEditorComponent()
183: .requestFocus();
184: } else {
185: comboBox.requestFocus();
186: }
187: togglePopup();
188: }
189:
190: protected boolean mouseInside = false;
191:
192: public void mouseEntered(MouseEvent e) {
193: mouseInside = true;
194: }
195:
196: public void mouseExited(MouseEvent e) {
197: mouseInside = false;
198: }
199:
200: // MouseMotionListener
201: public void mouseDragged(MouseEvent e) {
202: }
203:
204: public void mouseMoved(MouseEvent e) {
205: }
206:
207: // KeyListener
208: public void keyPressed(KeyEvent e) {
209: }
210:
211: public void keyTyped(KeyEvent e) {
212: }
213:
214: public void keyReleased(KeyEvent e) {
215: if (e.getKeyCode() == KeyEvent.VK_SPACE
216: || e.getKeyCode() == KeyEvent.VK_ENTER) {
217: togglePopup();
218: }
219: }
220:
221: /**
222: * Variables hideNext and mouseInside are used to
223: * hide the popupMenu by clicking the mouse in the JComboBox
224: */
225: public void popupMenuCanceled(PopupMenuEvent e) {
226: }
227:
228: protected boolean hideNext = false;
229:
230: public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
231: hideNext = mouseInside;
232: }
233:
234: public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
235: }
236:
237: //
238: // end Event Listeners
239: //=================================================================
240:
241: //===================================================================
242: // begin Utility methods
243: //
244:
245: protected void togglePopup() {
246: if (isVisible() || hideNext) {
247: hide();
248: } else {
249: show();
250: }
251: hideNext = false;
252: }
253:
254: //
255: // end Utility methods
256: //=================================================================
257:
258: // Note *** did not use JButton because Popup closes when pressed
259: protected JLabel createUpdateButton(final int field,
260: final int amount) {
261: final JLabel label = new JLabel();
262: final Border selectedBorder = new EtchedBorder();
263: final Border unselectedBorder = new EmptyBorder(
264: selectedBorder.getBorderInsets(new JLabel()));
265: label.setBorder(unselectedBorder);
266: label.setForeground(foreground);
267: label.addMouseListener(new MouseAdapter() {
268: public void mouseReleased(MouseEvent e) {
269: calendar.add(field, amount);
270: updatePopup();
271: }
272:
273: public void mouseEntered(MouseEvent e) {
274: label.setBorder(selectedBorder);
275: }
276:
277: public void mouseExited(MouseEvent e) {
278: label.setBorder(unselectedBorder);
279: }
280: });
281: return label;
282: }
283:
284: protected void initializePopup() {
285: JPanel header = new JPanel(); // used Box, but it wasn't Opaque
286: header.setLayout(new BoxLayout(header, BoxLayout.X_AXIS));
287: header.setBackground(background);
288: header.setOpaque(true);
289:
290: JLabel label;
291: label = createUpdateButton(Calendar.YEAR, -1);
292: label.setText("<<");
293: label.setToolTipText(resource
294: .getString("datecombobox.prevyear"));
295:
296: header.add(Box.createHorizontalStrut(12));
297: header.add(label);
298: header.add(Box.createHorizontalStrut(12));
299:
300: label = createUpdateButton(Calendar.MONTH, -1);
301: label.setText("<");
302: label.setToolTipText(resource
303: .getString("datecombobox.prevmonth"));
304: header.add(label);
305:
306: monthLabel = new JLabel("", JLabel.CENTER);
307: monthLabel.setForeground(foreground);
308: header.add(Box.createHorizontalGlue());
309: header.add(monthLabel);
310: header.add(Box.createHorizontalGlue());
311:
312: label = createUpdateButton(Calendar.MONTH, 1);
313: label.setText(">");
314: label.setToolTipText(resource
315: .getString("datecombobox.nextmonth"));
316: header.add(label);
317:
318: label = createUpdateButton(Calendar.YEAR, 1);
319: label.setText(">>");
320: label.setToolTipText(resource
321: .getString("datecombobox.nextyear"));
322:
323: header.add(Box.createHorizontalStrut(12));
324: header.add(label);
325: header.add(Box.createHorizontalStrut(12));
326:
327: popup = new JPopupMenu();
328: popup
329: .setBorder(BorderFactory
330: .createLineBorder(Color.black));
331: popup.setLayout(new BorderLayout());
332: popup.setBackground(background);
333: popup.addPopupMenuListener(this );
334: popup.add(BorderLayout.NORTH, header);
335: }
336:
337: // update the Popup when either the month or the year of the calendar has been changed
338: protected void updatePopup() {
339: monthLabel.setText(monthFormat.format(calendar.getTime()));
340: if (days != null) {
341: popup.remove(days);
342: }
343: days = new JPanel(new GridLayout(0, 7));
344: days.setBackground(background);
345: days.setOpaque(true);
346:
347: Calendar setupCalendar = (Calendar) calendar.clone();
348: setupCalendar.set(Calendar.DAY_OF_WEEK, setupCalendar
349: .getFirstDayOfWeek());
350: for (int i = 0; i < 7; i++) {
351: int dayInt = setupCalendar.get(Calendar.DAY_OF_WEEK);
352: JLabel label = new JLabel();
353: label.setHorizontalAlignment(JLabel.CENTER);
354: label.setForeground(foreground);
355: if (dayInt == Calendar.SUNDAY) {
356: label.setText(resource
357: .getString("datecombobox.sun"));
358: } else if (dayInt == Calendar.MONDAY) {
359: label.setText(resource
360: .getString("datecombobox.mon"));
361: } else if (dayInt == Calendar.TUESDAY) {
362: label.setText(resource
363: .getString("datecombobox.tue"));
364: } else if (dayInt == Calendar.WEDNESDAY) {
365: label.setText(resource
366: .getString("datecombobox.wed"));
367: } else if (dayInt == Calendar.THURSDAY) {
368: label.setText(resource
369: .getString("datecombobox.thu"));
370: } else if (dayInt == Calendar.FRIDAY) {
371: label.setText(resource
372: .getString("datecombobox.fri"));
373: } else if (dayInt == Calendar.SATURDAY) {
374: label.setText(resource
375: .getString("datecombobox.sat"));
376: }
377: days.add(label);
378: setupCalendar.roll(Calendar.DAY_OF_WEEK, true);
379: }
380:
381: setupCalendar = (Calendar) calendar.clone();
382: setupCalendar.set(Calendar.DAY_OF_MONTH, 1);
383: int first = setupCalendar.get(Calendar.DAY_OF_WEEK);
384: for (int i = 0; i < (first - 1); i++) {
385: days.add(new JLabel(""));
386: }
387: for (int i = 1; i <= setupCalendar
388: .getActualMaximum(Calendar.DAY_OF_MONTH); i++) {
389: final int day = i;
390: final JLabel label = new JLabel(String.valueOf(day));
391: label.setHorizontalAlignment(JLabel.CENTER);
392: label.setForeground(foreground);
393: label.addMouseListener(new MouseListener() {
394: public void mousePressed(MouseEvent e) {
395: }
396:
397: public void mouseClicked(MouseEvent e) {
398: }
399:
400: public void mouseReleased(MouseEvent e) {
401: label.setOpaque(false);
402: label.setBackground(background);
403: label.setForeground(foreground);
404: calendar.set(Calendar.DAY_OF_MONTH, day);
405: comboBox.setSelectedItem(dateFormat
406: .format(calendar.getTime()));
407: // hide();
408: // hide is called with setSelectedItem() ... removeAll()
409: comboBox.requestFocus();
410: }
411:
412: public void mouseEntered(MouseEvent e) {
413: label.setOpaque(true);
414: label.setBackground(selectedBackground);
415: label.setForeground(selectedForeground);
416: }
417:
418: public void mouseExited(MouseEvent e) {
419: label.setOpaque(false);
420: label.setBackground(background);
421: label.setForeground(foreground);
422: }
423: });
424:
425: days.add(label);
426: }
427:
428: popup.add(BorderLayout.CENTER, days);
429: popup.pack();
430: }
431: }
432:
433: //////////////////////////////////////////////////////////////
434: // This is only included to provide a sample GUI
435: //////////////////////////////////////////////////////////////
436: public static void main(String args[]) {
437: JFrame f = new JFrame();
438: Container c = f.getContentPane();
439: c.setLayout(new FlowLayout());
440: c.add(new JLabel("Date 1:"));
441: c.add(new DateComboBox());
442: c.add(new JLabel("Date 2:"));
443: DateComboBox dcb = new DateComboBox();
444: dcb.setEditable(true);
445: c.add(dcb);
446: f.addWindowListener(new WindowAdapter() {
447: public void windowClosing(WindowEvent e) {
448: System.exit(0);
449: }
450: });
451: f.setSize(500, 200);
452: f.show();
453: }
454:
455: }
|