1: package com.jtkdemo;
2:
3: import com.jthreadkit.*;
4: import com.jthreadkit.fifo.*;
5: import javax.swing.*;
6: import javax.swing.border.*;
7: import java.awt.*;
8: import java.awt.event.*;
9:
10: public class ObjectFIFODemo extends JPanel {
11: // Panels
12: private JPanel mainInnerPanel;
13: private JPanel labelPanel;
14: // Buttons
15: private JButton addButton;
16: private JButton changeButton;
17: private JButton removeOneButton;
18: private JButton removeButton;
19: private JButton clearRemoveTA;
20: private JButton clearAddTA;
21: private JButton clearAddElementTF;
22: // Radio buttons
23: private JRadioButton addNoTimeOutRB;
24: private JRadioButton addTimeOutAfterRB;
25: private JRadioButton remNoTimeOutRB;
26: private JRadioButton remTimeOutAfterRB;
27: // Check Boxes
28: private JCheckBox useTimeoutExceptionCB;
29: private JCheckBox useShutdownExceptionCB;
30: // Text fields
31: private JTextField addElementTF;
32: private JTextField addTimeoutTF;
33: private JTextField atLeastTF;
34: private JTextField atMostTF;
35: private JTextField remTimeoutTF;
36: private JTextField capacityTF;
37: // Text areas
38: private JTextArea addDetailsTA;
39: private JTextArea remDetailsTA;
40: // Counters
41: private int fifoCapacity;
42: // Labels
43: private JLabel currentFIFOSize;
44: private JLabel fifoSizeLabel;
45: // ObjectFIFO object
46: private ObjectFIFO fifo;
47: // Arrays
48: private JLabel[] labelArray;
49: // JScrollPane
50: private JScrollPane labelPanelSP;
51: private Runnable makeNewLabelsRunnable;
52: private Runnable updateLabelsRunnable;
53:
54: // Constructor
55: public ObjectFIFODemo(){
56: fifoCapacity = 10;
57: fifo = new ArrayObjectFIFO(fifoCapacity); // Create a ObjectFIFO
58: fifo.setUseTimedOutException(true); // turn time-out on
59: fifo.setElementType(String.class); // set the type = to String
60:
61: // Set this up here for use when new labels are needed.
62: makeNewLabelsRunnable = new Runnable() {
63: public void run() {
64: makeNewLabelsImpl();
65: }
66: };
67:
68: // Set this up here for use when the labels content has changed.
69: updateLabelsRunnable = new Runnable() {
70: public void run() {
71: updateLabelsImpl();
72: }
73: };
74:
75: mainInnerPanel = new JPanel(new BorderLayout(15, 15));
76: // call the helpers for building the ui
77: buildLeftPanel();
78: buildCenterPanel();
79: buildRightPanel();
80:
81: // set the layout of the main panel and add the inner panels
82: this.setLayout(new BorderLayout(5,5));
83: this.add(mainInnerPanel, BorderLayout.CENTER);
84: this.add(new JLabel(" "), BorderLayout.EAST);
85: this.add(new JLabel(" "), BorderLayout.WEST);
86: buildTopPanel();
87:
88: // Set the focus order...
89: addElementTF.setNextFocusableComponent(addButton);
90: addButton.setNextFocusableComponent(clearAddElementTF);
91: clearAddElementTF.setNextFocusableComponent(addNoTimeOutRB);
92: addNoTimeOutRB.setNextFocusableComponent(addTimeOutAfterRB);
93: addTimeOutAfterRB.setNextFocusableComponent(addTimeoutTF);
94: addTimeoutTF.setNextFocusableComponent(clearAddTA);
95: clearAddTA.setNextFocusableComponent(capacityTF);
96: capacityTF.setNextFocusableComponent(changeButton);
97: changeButton.setNextFocusableComponent(removeOneButton);
98: removeOneButton.setNextFocusableComponent(removeButton);
99: removeButton.setNextFocusableComponent(atLeastTF);
100: atLeastTF.setNextFocusableComponent(atMostTF);
101: atMostTF.setNextFocusableComponent(remNoTimeOutRB);
102: remNoTimeOutRB.setNextFocusableComponent(remTimeOutAfterRB);
103: remTimeOutAfterRB.setNextFocusableComponent(remTimeoutTF);
104: remTimeoutTF.setNextFocusableComponent(clearRemoveTA);
105: clearRemoveTA.setNextFocusableComponent(addElementTF);
106:
107: // Action Listeners
108: useShutdownExceptionCB.addItemListener(new ItemListener() {
109: public void itemStateChanged(ItemEvent evt) {
110: // call our helper method
111: shutDownFIFO();
112: }
113: });
114:
115: addDetailsTA.addMouseListener(new MouseAdapter() {
116: public void mouseClicked(MouseEvent e) {
117: addElementTF.requestFocus();
118: }
119:
120: public void mousePressed(MouseEvent e) {
121: addElementTF.requestFocus();
122: }
123: });
124:
125: remDetailsTA.addMouseListener(new MouseAdapter() {
126: public void mouseClicked(MouseEvent e) {
127: addElementTF.requestFocus();
128: }
129:
130: public void mousePressed(MouseEvent e) {
131: addElementTF.requestFocus();
132: }
133: });
134:
135: changeButton.addActionListener(new ActionListener() {
136: public void actionPerformed(ActionEvent e) {
137: // call our helper method
138: changeCapacity();
139: }
140: });
141:
142: capacityTF.addActionListener(new ActionListener() {
143: public void actionPerformed(ActionEvent e) {
144: changeCapacity();
145: }
146: });
147:
148: addButton.addActionListener(new ActionListener() {
149: public void actionPerformed(ActionEvent e) {
150: // call the add helper method
151: addAnItem();
152: }
153: });
154:
155: removeOneButton.addActionListener(new ActionListener() {
156: public void actionPerformed(ActionEvent e) {
157: long timeout = -1;
158: // find out if they want the remove to timeout
159: if ( remNoTimeOutRB.isSelected() ) {
160: timeout = 0L;
161: } else {
162: try {
163: timeout = Long.parseLong(remTimeoutTF.getText());
164: } catch ( NumberFormatException nfe) {
165: showErrorMessage("Timeout value must be " +
166: "numeric!");
167: remTimeoutTF.setText("");
168: remTimeoutTF.requestFocus();
169: return;
170: }
171: }
172: // call the addAnItem helper method
173: removeAnItem(timeout, -1);
174: }
175: });
176:
177: removeButton.addActionListener(new ActionListener() {
178: public void actionPerformed(ActionEvent e) {
179: long timeout = -1;
180: // find out if they want the remove to timeout
181: if ( remNoTimeOutRB.isSelected() ) {
182: timeout = 0L;
183: } else {
184: try {
185: timeout = Long.parseLong(remTimeoutTF.getText());
186: } catch ( NumberFormatException nfe) {
187: showErrorMessage("Timeout value must be " +
188: "numeric!");
189: remTimeoutTF.setText("");
190: remTimeoutTF.requestFocus();
191: return;
192: }
193: }
194: // call the addAnItem helper method
195: removeAnItem(timeout, 1);
196: }
197: });
198:
199: clearAddTA.addActionListener(new ActionListener() {
200: public void actionPerformed(ActionEvent e) {
201: addDetailsTA.setText("");
202: addElementTF.requestFocus();
203: }
204: });
205:
206: clearRemoveTA.addActionListener(new ActionListener() {
207: public void actionPerformed(ActionEvent e) {
208: remDetailsTA.setText("");
209: addElementTF.requestFocus();
210: }
211: });
212:
213: addElementTF.addActionListener(new ActionListener() {
214: public void actionPerformed(ActionEvent e) {
215: // call the add helper method
216: addAnItem();
217: }
218: });
219:
220: clearAddElementTF.addActionListener(new ActionListener() {
221: public void actionPerformed(ActionEvent e) {
222: // clear the add element TF
223: addElementTF.setText("");
224: addElementTF.requestFocus();
225: }
226: });
227:
228: addElementTF.requestFocus();
229: updateLabels(); // be sure that we're all set.
230: }
231:
232: private void showErrorMessage(String msg){
233: JOptionPane pane = new JOptionPane();
234: JOptionPane.showMessageDialog(
235: this,
236: msg,
237: "Alert",
238: JOptionPane.ERROR_MESSAGE
239: );
240: }
241:
242: // Helper methods
243: private void changeCapacity() {
244: changeButton.setEnabled(false);
245: try {
246: // get the new size
247: fifoCapacity = Integer.parseInt(capacityTF.getText().trim());
248: } catch ( NumberFormatException nfe ) {
249: showErrorMessage("Capacity must be numeric!");
250: capacityTF.setText("");
251: capacityTF.requestFocus();
252: return;
253: }
254: // change the capacity
255: int newCap = fifo.setCapacity(fifoCapacity);
256: // update the label grid
257: makeNewLabels();
258: // update the size label
259: updateFIFOSizeLabel();
260: addElementTF.requestFocus();
261: capacityTF.setText(Integer.toString(newCap));
262: changeButton.setEnabled(true);
263: }
264: private void addAnItem(){
265: String item = addElementTF.getText().trim();
266: if ( item.length() < 1 ) {
267: showErrorMessage("The Demo does not allow Zero length Strings!");
268: addElementTF.requestFocus();
269: addElementTF.setText("");
270: return;
271: }
272: long timeout = -1;
273: // find out if they want the add to timeout
274: if ( addNoTimeOutRB.isSelected() ) {
275: timeout = 0L;
276: } else {
277: try {
278: timeout = Long.parseLong(addTimeoutTF.getText());
279: } catch ( NumberFormatException nfe) {
280: showErrorMessage("Timeout value must be numeric!");
281: addTimeoutTF.setText("");
282: addTimeoutTF.requestFocus();
283: return;
284: }
285: }
286:
287: // start a new thread to add the item
288: Thread t = launchAddThread(
289: item,
290: addButton,
291: fifo,
292: addDetailsTA,
293: timeout
294: );
295: addElementTF.setText("");
296: }
297:
298: private Thread launchAddThread(
299: final String item,
300: final JButton button,
301: final ObjectFIFO fifo,
302: final JTextArea ta,
303: final long timeout
304: ) {
305:
306: button.setEnabled(false);
307: addElementTF.setEditable(false);
308: // fire off a new thread
309: Thread t = new Thread(new Runnable() {
310: public void run() {
311: final long startTime = System.currentTimeMillis();
312: try {
313: // add an item to the queue
314: fifo.add(item, timeout);
315: } catch ( TimedOutException toe ) {
316: long elapsedTime =
317: System.currentTimeMillis() - startTime;
318: String msg = "Add \'" + item + "\' timed out after " +
319: elapsedTime + "ms" + "\n";
320: // update the text area
321: updateTextArea(ta, msg, button);
322: return;
323: } catch ( ShutdownException se) {
324: // occurs if ShutdownException is on
325: } catch ( Exception e) {
326: // unexpected
327: System.out.println("Unexpectedly threw: " + e);
328: }
329:
330: // update the label grid
331: updateLabels();
332: long addTime = System.currentTimeMillis() - startTime;
333: String msg = "Added \'" + item + "\' in " +
334: addTime + "ms" + "\n";
335: // update the text area
336: updateTextArea(ta, msg, button);
337: }
338: }, "launchAddThread");
339: t.start();
340: return t;
341: }
342:
343: private void updateTextArea(
344: final JTextArea ta,
345: final String msg,
346: final JButton button
347: ) {
348:
349: Thread t = new Thread(new Runnable() {
350: public void run() {
351: runTextAreaWork(ta, msg, button);
352: }
353: });
354: t.start();
355: }
356:
357: private void updateTextArea(final JTextArea ta, final String msg) {
358: Thread t = new Thread(new Runnable() {
359: public void run() {
360: runTextAreaWork(ta, msg);
361: }
362: });
363: t.start();
364: }
365:
366: private void runTextAreaWork(
367: final JTextArea ta,
368: final String msg,
369: final JButton button
370: ) {
371:
372: Runnable updateTARunnable = new Runnable() {
373: public void run() {
374: updateTextAreaText(ta, msg, button);
375: }
376: };
377: // update the ui appropriately
378: SwingUtilities.invokeLater(updateTARunnable);
379: }
380:
381: private void runTextAreaWork(final JTextArea ta, final String msg) {
382: Runnable updateTARunnable = new Runnable() {
383: public void run() {
384: updateTextAreaText(ta, msg);
385: }
386: };
387: // update the ui appropriately
388: SwingUtilities.invokeLater(updateTARunnable);
389: }
390:
391: private void updateTextAreaText(
392: JTextArea ta,
393: String msg,
394: JButton button
395: ) {
396: // update the text area
397: ta.append(msg);
398: // re-enable the calling button
399: button.setEnabled(true);
400: addElementTF.setEditable(true);
401: }
402:
403: private void updateTextAreaText(JTextArea ta, String msg) {
404: // update the text area
405: ta.append(msg);
406: }
407:
408: /**
409: * Updates existing labels with the current contents of the FIFO.
410: * This method can be safely called by any thread, including the
411: * event thread. Use makeNewLabels() if the capacity changes.
412: */
413: private void updateLabels() {
414: SwingUtilities.invokeLater(updateLabelsRunnable);
415: }
416:
417: /**
418: * Creates new labels based on the current capacity and updates
419: * them with the current contents of the FIFO.
420: * This method can be safely called by any thread, including the
421: * event thread. Use updateLabels() if the capacity has NOT changed.
422: */
423: private void makeNewLabels() {
424: // This method can be safely called by any thread, incl event thread
425: SwingUtilities.invokeLater(makeNewLabelsRunnable);
426: }
427:
428: private void updateLabelsImpl() {
429: if ( SwingUtilities.isEventDispatchThread() == false ) {
430: throw new RuntimeException("updateLabelsImpl() should only " +
431: "be invoked by the event thread; use updateLabels() instead");
432: }
433:
434: // By synchronizing on the fifo's lock, we can be sure that the
435: // fifo doesn't change capacity, have items removed, or items
436: // added during this operation.
437: synchronized ( fifo.getLockObject() ) {
438: String[] item = (String[]) fifo.peekAll();
439: for ( int i = 0; i < labelArray.length; i++ ) {
440: String text = ( i < item.length ) ? item[i] : " ";
441: labelArray[i].setText(text);
442: }
443: updateFIFOSizeLabel();
444: }
445: }
446:
447: private void makeNewLabelsImpl() {
448: if ( SwingUtilities.isEventDispatchThread() == false ) {
449: throw new RuntimeException("makeNewLabelsImpl() should only " +
450: "be invoked by the event thread; " +
451: "use makeNewLabels() instead");
452: }
453: // By synchronizing on the fifo's lock, we can be sure that the
454: // fifo doesn't change capacity, have items removed, or items
455: // added during this operation.
456: synchronized ( fifo.getLockObject() ) {
457: labelPanel.removeAll();
458: labelArray = new JLabel[fifo.getCapacity()];
459: // Count backwards to get the labels added in 'reverse' order
460: for ( int i = labelArray.length - 1; i >= 0; i-- ) {
461: labelArray[i] = new JLabel();
462: labelArray[i].setHorizontalAlignment(SwingConstants.CENTER);
463: labelArray[i].setOpaque(true);
464: labelPanel.add(labelArray[i]);
465: }
466:
467: labelPanel.revalidate();
468: // Update these new labels to match the fifo's contents.
469: updateLabelsImpl();
470: }
471: }
472:
473: private void removeAnItem(long timeout, int indicator){
474: if ( indicator > 0 ) {
475: // this means we want to remove a range...
476: int atLeast = 0;
477: int atMost = 0;
478: // get at least value
479: try {
480: atLeast = Integer.parseInt(atLeastTF.getText());
481: if ( atLeast < 1 ) {
482: showErrorMessage("At Least value must be > 0!");
483: atLeastTF.setText("");
484: atLeastTF.requestFocus();
485: return;
486: }
487: } catch ( NumberFormatException nfe) {
488: showErrorMessage("At Least value must be numeric!");
489: atLeastTF.setText("");
490: atLeastTF.requestFocus();
491: return;
492: }
493: // get at most value
494: try {
495: atMost = Integer.parseInt(atMostTF.getText());
496: if ( atMost > fifo.getCapacity() || atMost < 1 ) {
497: showErrorMessage("At Most out of range!");
498: atMostTF.setText("");
499: atMostTF.requestFocus();
500: return;
501: }
502: } catch ( NumberFormatException nfe) {
503: showErrorMessage("At Most value must be numeric!");
504: atMostTF.setText("");
505: atMostTF.requestFocus();
506: return;
507: }
508:
509: // create a new thread to do the remove
510: Thread t = launchRemoveThread(
511: removeButton,
512: fifo,
513: remDetailsTA,
514: timeout,
515: atLeast,
516: atMost,
517: 1
518: );
519:
520: } else {
521: // just remove one, so start a new thread to do it
522: Thread t = launchRemoveThread(
523: removeOneButton,
524: fifo,
525: remDetailsTA,
526: timeout,
527: 0,
528: 0,
529: -1
530: );
531: }
532: }
533:
534: private Thread launchRemoveThread(
535: final JButton button,
536: final ObjectFIFO fifo,
537: final JTextArea ta,
538: final long timeout,
539: final int atLeast,
540: final int atMost,
541: final int indicator
542: ) {
543:
544: button.setEnabled(false);
545: Thread t = new Thread(new Runnable() {
546: public void run() {
547: long startTime = System.currentTimeMillis();
548: Object remObj = null;
549: Object[] remObjArr = null;
550:
551: try {
552: // determine if we remove 1 item or a range...
553: if ( indicator > 0 ) {
554: // remove a range
555: remObjArr = fifo.remove(atLeast, atMost, timeout);
556: } else {
557: // remove one
558: remObj = fifo.remove(timeout);
559: }
560: } catch ( TimedOutException toe ) {
561: long elapsedTime = System.currentTimeMillis() - startTime;
562: String msg = "Remove timed out after " + elapsedTime +
563: "ms" + "\n";
564: updateTextArea(ta, msg, button);
565: return;
566: } catch ( Exception e) {
567:
568: }
569: // update the label grid
570: updateLabels();
571: long removeTime = System.currentTimeMillis() - startTime;
572: // Print the appropriate detail information...
573: if ( indicator > 0 ) {
574: //synchronized ( ta ) {
575: String msg = "Removed " + remObjArr.length + " after " +
576: removeTime + "ms:\n";
577: for ( int i = 0; i < remObjArr.length; i++ ) {
578: msg += " " + remObjArr[i] + "\n";
579: }
580: updateTextArea(ta, msg, button);
581: } else {
582: String msg = "Removed \'" + remObj + "\' in " +
583: removeTime + "ms" + "\n";
584: updateTextArea(ta, msg, button);
585: }
586: }
587: }, "launchAddThread");
588: t.start();
589: return t;
590: }
591:
592: private void updateFIFOSizeLabel(){
593: synchronized ( fifo.getLockObject() ) {
594: String sizeStr = String.valueOf(fifo.getSize());
595: if ( fifo.isEmpty() ) {
596: sizeStr += " (empty)";
597: } else if ( fifo.isFull() ) {
598: sizeStr += " (full)";
599: }
600: fifoSizeLabel.setText(sizeStr);
601: }
602: }
603:
604: private void shutDownFIFO() {
605: // disable the checkbox
606: useShutdownExceptionCB.setEnabled(false);
607: // create a new thread which will wait until the FIFO is empty
608: Thread t = launchShutdownThread(fifo, addDetailsTA);
609: }
610:
611: private Thread launchShutdownThread(
612: final ObjectFIFO fifo,
613: final JTextArea ta
614: ) {
615:
616: Thread t = new Thread(new Runnable() {
617: public void run() {
618: final long startTime = System.currentTimeMillis();
619: try {
620: // block on shutdownWhenEmpty
621: boolean isShutDown = fifo.shutdownWhenEmpty(0L);
622: // once we are here, the FIFO is shutdown
623: long elapsedTime =
624: System.currentTimeMillis() - startTime;
625: String msg = "***FIFO Queue shutdown after " +
626: elapsedTime + "ms***";
627: // update the add TA with the shutdown message
628: updateTextArea(ta, msg);
629: // disable buttons
630: disableAfterShutdown();
631: } catch ( Exception e) {
632: // unexpected
633: System.out.println("Unexpectedly threw Exception: " + e);
634: }
635: }
636: }, "launchShutdownThread");
637: t.start();
638: return t;
639: }
640:
641: private void disableAfterShutdown() {
642: // fire off a thread to disable the appropriate buttons
643: Thread t = new Thread(new Runnable() {
644: public void run() {
645: runDisableWork();
646: }
647: });
648: t.start();
649: }
650:
651: private void runDisableWork() {
652: Runnable disableButtonsRunnable = new Runnable() {
653: public void run() {
654: disableButtonsAfterShutdown();
655: }
656: };
657: // update the ui appropriately
658: SwingUtilities.invokeLater(disableButtonsRunnable);
659: }
660:
661: private void disableButtonsAfterShutdown() {
662: // de-activate the add, change, remove and remove one buttons
663: addButton.setEnabled(false);
664: changeButton.setEnabled(false);
665: removeButton.setEnabled(false);
666: removeOneButton.setEnabled(false);
667: }
668:
669: private void buildLeftPanel(){
670: // build row one
671: addButton = new JButton("Add");
672: addButton.setToolTipText("Adds an element to the FIFO");
673: addElementTF = new JTextField(10);
674: clearAddElementTF = new JButton("Clear");
675: JPanel rowOne = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
676: rowOne.add(addElementTF);
677: rowOne.add(addButton);
678: rowOne.add(clearAddElementTF);
679:
680: // build row two
681: addNoTimeOutRB = new JRadioButton("No Timeout", true);
682: addTimeOutAfterRB = new JRadioButton("Timeout After");
683: addTimeoutTF = new JTextField(4);
684: JLabel timeoutMS = new JLabel("ms");
685:
686: // create a button group
687: ButtonGroup timeoutBG = new ButtonGroup();
688: timeoutBG.add(addNoTimeOutRB);
689: timeoutBG.add(addTimeOutAfterRB);
690: JPanel rowTwo = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
691: rowTwo.add(addNoTimeOutRB);
692:
693: // build row three
694: JPanel rowThree = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
695: rowThree.add(addTimeOutAfterRB);
696: rowThree.add(addTimeoutTF);
697: rowThree.add(timeoutMS);
698:
699: JPanel addEntryPanel = new JPanel(new GridLayout(0, 1, 0, 2));
700: addEntryPanel.setBorder(BorderFactory.
701: createTitledBorder("Add Entry"));
702: addEntryPanel.add(rowOne);
703: addEntryPanel.add(rowTwo);
704: addEntryPanel.add(rowThree);
705: // add text area
706: addDetailsTA = new JTextArea(15, 8);
707: addDetailsTA.setEditable(false);
708: JScrollPane jsp = new JScrollPane(addDetailsTA);
709: clearAddTA = new JButton("Clear");
710: JPanel clearButtonPanel = new JPanel(new FlowLayout());
711: clearButtonPanel.add(clearAddTA);
712: JPanel leftPanel = new JPanel(new BorderLayout(5, 5));
713: leftPanel.add(addEntryPanel, BorderLayout.NORTH);
714: leftPanel.add(jsp, BorderLayout.CENTER);
715: leftPanel.add(clearButtonPanel, BorderLayout.SOUTH);
716: mainInnerPanel.add(leftPanel, BorderLayout.WEST);
717: // more this exception panel stuff out...
718: useTimeoutExceptionCB = new JCheckBox("Use TimedOutException");
719: useShutdownExceptionCB = new JCheckBox("Use ShutdownException");
720:
721: JPanel exceptionPanel =
722: new JPanel(new FlowLayout(FlowLayout.LEFT,8,0));
723: exceptionPanel.setBorder(BorderFactory.createTitledBorder(
724: "Exceptions"));
725: exceptionPanel.add(useTimeoutExceptionCB);
726: exceptionPanel.add(useShutdownExceptionCB);
727: }
728:
729: private void buildCenterPanel(){
730: JPanel centerPanel = new JPanel(new BorderLayout(30, 10));
731: JPanel lowerCenterPanel = new JPanel();
732: JLabel fifoCountLabel = new JLabel("FIFO Size:");
733: fifoSizeLabel = new JLabel(String.valueOf(fifo.getSize()) + " EMPTY");
734: lowerCenterPanel.add(fifoCountLabel);
735: lowerCenterPanel.add(fifoSizeLabel);
736: labelPanel = new JPanel(new GridLayout(0, 1, 1, 1));
737: Color c = fifoSizeLabel.getForeground();
738: labelPanel.setBackground(c);
739: labelPanel.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
740: makeNewLabels();
741: JPanel outerLabelPanel = new JPanel(new BorderLayout());
742: outerLabelPanel.add(labelPanel, BorderLayout.NORTH);
743: labelPanelSP = new JScrollPane(outerLabelPanel);
744: // Use this temporary label to calculate a preferred size for the SP
745: JLabel tmpL = new JLabel("XXXXXXXXXXXXXXXXXXXX");
746: Dimension size = tmpL.getPreferredSize();
747: labelPanelSP.setPreferredSize(
748: new Dimension(size.width, 10 + size.height * 20));
749: centerPanel.add(labelPanelSP, BorderLayout.CENTER);
750: centerPanel.add(lowerCenterPanel, BorderLayout.SOUTH);
751: mainInnerPanel.add(centerPanel, BorderLayout.CENTER);
752: }
753:
754: private void buildRightPanel(){
755: // build row one
756: removeOneButton = new JButton("Remove One");
757: removeOneButton.setToolTipText("Removes one element");
758: JPanel rowOne = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
759: rowOne.add(removeOneButton);
760: // build row two
761: removeButton = new JButton("Remove");
762: removeButton.setToolTipText("Removes a range of elements");
763: JLabel atLeastL = new JLabel("at Least");
764: atLeastTF = new JTextField(2);
765: JLabel atMostL = new JLabel(", at Most");
766: atMostTF = new JTextField(2);
767: JPanel rowTwo = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
768: rowTwo.add(removeButton);
769: rowTwo.add(atLeastL);
770: rowTwo.add(atLeastTF);
771: rowTwo.add(atMostL);
772: rowTwo.add(atMostTF);
773: // build row three
774: remNoTimeOutRB = new JRadioButton("No Timeout", true);
775: remTimeOutAfterRB = new JRadioButton("Timeout After");
776: remTimeoutTF = new JTextField(4);
777: JLabel timeoutMS = new JLabel("ms");
778: // create a button group
779: ButtonGroup timeoutBG = new ButtonGroup();
780: timeoutBG.add(remNoTimeOutRB);
781: timeoutBG.add(remTimeOutAfterRB);
782: JPanel rowThree = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
783: rowThree.add(remNoTimeOutRB);
784: JPanel rowFour = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
785: rowFour.add(remTimeOutAfterRB);
786: rowFour.add(remTimeoutTF);
787: rowFour.add(timeoutMS);
788: JPanel removeP = new JPanel(new GridLayout(0, 1, 0, 2));
789: removeP.setBorder(
790: BorderFactory.createTitledBorder("Remove Entry(ies)"));
791: removeP.add(rowOne);
792: removeP.add(rowTwo);
793: removeP.add(rowThree);
794: removeP.add(rowFour);
795: // add text area
796: remDetailsTA = new JTextArea(15, 8);
797: remDetailsTA.setEditable(false);
798: JScrollPane jsp = new JScrollPane(remDetailsTA);
799: clearRemoveTA = new JButton("Clear");
800: JPanel clearRemButtonPanel = new JPanel(new FlowLayout());
801: clearRemButtonPanel.add(clearRemoveTA);
802: JPanel rightPanel = new JPanel(new BorderLayout(5, 5));
803: rightPanel.add(removeP, BorderLayout.NORTH);
804: rightPanel.add(jsp, BorderLayout.CENTER);
805: rightPanel.add(clearRemButtonPanel, BorderLayout.SOUTH);
806: mainInnerPanel.add(rightPanel, BorderLayout.EAST);
807: }
808:
809: private void buildTopPanel(){
810: JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
811: changeButton = new JButton("Change");
812: changeButton.setToolTipText("Changes the FIFO's capacity");
813: JLabel capacityLabel = new JLabel("Capacity: ");
814: capacityTF = new JTextField(String.valueOf(fifoCapacity), 3);
815: topPanel.add(capacityLabel);
816: topPanel.add(capacityTF);
817: topPanel.add(changeButton);
818: this.add(topPanel, BorderLayout.NORTH);
819: }
820:
821: public static void main(String[] args) {
822: ObjectFIFODemo demo = new ObjectFIFODemo();
823: Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
824: JFrame f = new JFrame("ObjectFIFODemo");
825: f.setContentPane(demo);
826: f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
827: f.pack();
828: f.setLocation((screenDim.width - f.getSize().width) / 2,
829: (screenDim.height - f.getSize().height) / 2);
830: f.setVisible(true);
831: }
832: }
|