JThreadKit Home
Source File(s):
SyncIntegerDemo.java
SyncIntDemoApplet.java
source.zip (all files)

SyncIntegerDemo.java

  1: package com.jtkdemo;
  2: 
  3: import java.util.*;
  4: import java.awt.*;
  5: import java.awt.event.*;
  6: import javax.swing.*;
  7: import javax.swing.border.*;
  8: import com.jthreadkit.*;
  9: import com.jthreadkit.swing.*;
 10: 
 11: public class SyncIntegerDemo extends JPanel {
 12: 
 13:     private int minVal;
 14:     private int maxVal;
 15:     private int newVal;
 16:     private long timeout;
 17:     private boolean finished;
 18:     private JButton startB;
 19:     private JButton increaseB;
 20:     private JButton decreaseB;
 21:     private JButton chgValueB;
 22:     private JButton chgRangeB;
 23:     private JTKLabel valueLabel;
 24:     private JTKLabel maxValueL;
 25:     private JTKLabel minValueL;
 26:     private JTextField newValueTF;
 27:     private JTKTextArea resultsTA;
 28:     private JList testList;
 29:     private SyncInteger syncInt;
 30:     private FocusListener fl;
 31:     private Runnable enableScreenRunnable;
 32:     private Runnable runDemoRunnable;
 33:     
 34:     public SyncIntegerDemo() {
 35:         setFinished(false);
 36:         // create a SyncInteger that will be used througout the demo
 37:         syncInt = new SyncInteger();
 38:         // enable timeout exception to demo it
 39:         syncInt.setUseTimedOutException(true);
 40:         // re-usable runnable to enable the screen...
 41:         enableScreenRunnable = new Runnable() {
 42:                 public void run() {
 43:                     enableScreen();
 44:                 }
 45:             };
 46:         // re-usable runnable to run the demo
 47:         runDemoRunnable = new Runnable() {
 48:                 public void run() {
 49:                     Thread.currentThread().setName("Helper-Thread");
 50:                     getMethod();
 51:                 }
 52:             };
 53:         // shared by right panel text fields    
 54:         fl = new FocusListener() {
 55:                 public void focusGained(FocusEvent e) {
 56:                     try {
 57:                         JTextField tf = (JTextField) e.getSource();
 58:                         tf.selectAll();
 59:                     } catch ( ClassCastException x ) {
 60:                         // ignore
 61:                     }
 62:                 }
 63: 
 64:                 public void focusLost(FocusEvent e) {
 65:                     try {
 66:                         JTextField tf = (JTextField) e.getSource();
 67:                         tf.select(0, 0);
 68:                     } catch ( ClassCastException x ) {
 69:                         // ignore
 70:                     }
 71:                 }
 72:             };
 73:         // separate the panels with a split pane
 74:         JSplitPane splitPane = 
 75:             new JSplitPane(
 76:                     JSplitPane.HORIZONTAL_SPLIT, 
 77:                     true, 
 78:                     buildLeftPanel(), 
 79:                     buildRightPanel()
 80:                 );
 81:         this.setLayout(new BorderLayout(5, 5));
 82:         this.add(splitPane, BorderLayout.CENTER);
 83:     }
 84:     
 85:     private Component buildLeftPanel() {
 86:         startB = new JButton("Start");
 87:         startB.setMnemonic('S');
 88:         startB.addActionListener(new ActionListener() {
 89:                 public void actionPerformed(ActionEvent e) {
 90:                     // disable the appropriate buttons
 91:                     startB.setEnabled(false);
 92:                     testList.setEnabled(false);
 93:                     runDemo();
 94:                 }
 95:             });
 96:         JPanel buttonPanel = new JPanel();
 97:         buttonPanel.add(startB);
 98:         testList = new JList(getMethodNames());
 99:         testList.setSelectedIndex(0);
100:         // allow list items to be double clicked
101:         testList.addMouseListener(new MouseAdapter() {
102:                  public void mouseClicked(MouseEvent e) {
103:                      if (e.getClickCount() == 2) {
104:                         // disable the appropriate buttons
105:                         startB.setEnabled(false);
106:                         testList.setEnabled(false);
107:                         runDemo();
108:                      }
109:                  }
110:             });
111:         JScrollPane scrollPane = new JScrollPane(testList);
112:         JPanel leftPanel = new JPanel(new BorderLayout());
113:         leftPanel.add(buttonPanel, BorderLayout.NORTH);
114:         leftPanel.add(scrollPane, BorderLayout.CENTER);
115:         return leftPanel;
116:     }
117:     
118:     private Component buildRightPanel() {
119:         // build value change items
120:         JLabel valueL = new JLabel("New Value:", JLabel.RIGHT);
121:         newValueTF = new JTextField(10);
122:         newValueTF.addActionListener(new ActionListener() {
123:                 public void actionPerformed(ActionEvent e) {
124:                     getNewValue();
125:                 }
126:             });
127:         newValueTF.addFocusListener(fl);
128:         chgValueB = new JButton("Change");
129:         chgValueB.addActionListener(new ActionListener() {
130:                 public void actionPerformed(ActionEvent e) {
131:                     getNewValue();
132:                 }
133:             });
134:         GridBagLayout gbl = new GridBagLayout();
135:         GridBagConstraints gbc = new GridBagConstraints();
136:         gbc.insets = new Insets(2, 2, 2, 2);
137:         JPanel topPanel = new JPanel(gbl);
138:         decreaseB = new JButton("<");
139:         decreaseB.addActionListener(new ActionListener() {
140:                 public void actionPerformed(ActionEvent e) {
141:                     decreaseValue(1);
142:                 }
143:             });
144:         increaseB = new JButton(">");
145:         increaseB.addActionListener(new ActionListener() {
146:                 public void actionPerformed(ActionEvent e) {
147:                     increaseValue(1);
148:                 }
149:             });
150:         valueLabel = new JTKLabel(
151:             new JLabel(Integer.toString(syncInt.getValue()), JLabel.CENTER));
152:         valueLabel.getLabel().setBorder(
153:             BorderFactory.createLoweredBevelBorder());
154:         JLabel description = 
155:             new JLabel("SyncInteger's Current Value:", JLabel.CENTER);
156:         gbc.gridwidth = gbc.REMAINDER;
157:         gbc.gridheight = 1;
158:         gbl.setConstraints(description, gbc);
159:         topPanel.add(description);
160:         // add decrease button
161:         gbc.fill = gbc.NONE;
162:         gbc.anchor = gbc.EAST;
163:         gbc.gridwidth = 1;
164:         gbc.gridheight = 1;
165:         gbl.setConstraints(decreaseB, gbc);
166:         topPanel.add(decreaseB);
167:         // add size label
168:         gbc.fill = gbc.BOTH;
169:         gbc.anchor = gbc.CENTER;
170:         gbc.gridwidth = 3;
171:         gbc.gridheight = 1;
172:         gbl.setConstraints(valueLabel.getLabel(), gbc);
173:         topPanel.add(valueLabel.getLabel());
174:         // add increase button
175:         gbc.fill = gbc.REMAINDER;
176:         gbc.anchor = gbc.WEST;
177:         gbc.gridwidth = 1;
178:         gbc.gridheight = 1;
179:         gbl.setConstraints(increaseB, gbc);
180:         topPanel.add(increaseB);
181:         gbc.gridy = 2;
182:         gbc.fill = GridBagConstraints.HORIZONTAL;
183:         // set constraints and add label
184:         gbc.gridwidth = 1;
185:         gbl.setConstraints(valueL, gbc);
186:         topPanel.add(valueL);
187:         // set constraints and add text field
188:         gbc.gridwidth = 3;
189:         gbl.setConstraints(newValueTF, gbc);
190:         topPanel.add(newValueTF);
191:         // set constraints and add chg button
192:         gbc.gridwidth = gbc.REMAINDER;
193:         gbc.fill = gbc.NONE;
194:         gbl.setConstraints(chgValueB, gbc);
195:         topPanel.add(chgValueB);
196:         // build range change items
197:         JLabel minL = new JLabel("Min:", JLabel.RIGHT);
198:         JLabel tmpL = new JLabel("XXXXXXXXXXX");
199:         Dimension minSize = tmpL.getPreferredSize();
200:         minValueL = new JTKLabel(new JLabel(
201:             Integer.toString(syncInt.getMinValue()), JLabel.CENTER));
202:         minValueL.getLabel().setBorder(
203:             BorderFactory.createLoweredBevelBorder());
204:         minValueL.getLabel().setPreferredSize(minSize);
205:         minValueL.getLabel().setMinimumSize(minSize);
206:         JLabel maxL = new JLabel("Max:", JLabel.RIGHT);
207:         maxValueL = new JTKLabel(new JLabel(
208:             Integer.toString(syncInt.getMaxValue()), JLabel.CENTER));
209:         maxValueL.getLabel().setBorder(BorderFactory.createLoweredBevelBorder());
210:         maxValueL.getLabel().setPreferredSize(minSize);
211:         maxValueL.getLabel().setMinimumSize(minSize);
212:         chgRangeB = new JButton("Change");
213:         chgRangeB.addActionListener(new ActionListener() {
214:                 public void actionPerformed(ActionEvent e) {
215:                     changeRange();
216:                 }
217:             });
218:         // set constraints and add min label
219:         gbc.gridy = 3;
220:         gbc.fill = gbc.HORIZONTAL;
221:         gbc.gridwidth = 1;
222:         gbl.setConstraints(minL, gbc);
223:         topPanel.add(minL);
224:         // set constraints and add min text field
225:         gbc.gridwidth = 1;
226:         gbc.fill = gbc.BOTH;
227:         gbl.setConstraints(minValueL.getLabel(), gbc);
228:         topPanel.add(minValueL.getLabel());
229:         // set constraints and add max label
230:         gbc.gridwidth = 1;
231:         gbc.fill = gbc.HORIZONTAL;
232:         gbl.setConstraints(maxL, gbc);
233:         topPanel.add(maxL);
234:         // set constraints and add min text field
235:         gbc.gridwidth = 1;
236:         gbc.fill = gbc.BOTH;
237:         gbl.setConstraints(maxValueL.getLabel(), gbc);
238:         topPanel.add(maxValueL.getLabel());
239:         // set constraints and add range chg button
240:         gbc.gridwidth = gbc.REMAINDER;
241:         gbc.fill = gbc.HORIZONTAL;
242:         gbl.setConstraints(chgRangeB, gbc);
243:         topPanel.add(chgRangeB);
244:         JPanel centerPanel = new JPanel(new BorderLayout(5, 1));
245:         JButton clearB = new JButton("Clear");
246:         clearB.setMnemonic('C');
247:         clearB.addActionListener(new ActionListener() {
248:                 public void actionPerformed(ActionEvent e) {
249:                     resultsTA.setTextSafely("");
250:                 }
251:             });
252:         JPanel lowerP = new JPanel();
253:         lowerP.add(clearB);
254:         centerPanel.add(topPanel, BorderLayout.NORTH);
255:         centerPanel.add(buildResultsTA(), BorderLayout.CENTER);
256:         centerPanel.add(lowerP, BorderLayout.SOUTH);
257:         return centerPanel;
258:     }
259:     
260:     private JPanel buildResultsTA() {
261:         resultsTA = new JTKTextArea(new JTextArea());
262:         resultsTA.getTextArea().setEditable(false);
263:         JScrollPane sp = new JScrollPane(resultsTA.getTextArea());
264:         JPanel rightPanel = new JPanel(new BorderLayout());
265:         rightPanel.add(sp, BorderLayout.CENTER);
266:         return rightPanel;
267:     }
268:     
269:     private String[] getMethodNames() {
270:         String[] methods = {"waitForValueToChange( )", 
271:                             "waitForValueToClimbTo( )",
272:                             "waitForValueToFallTo( )",
273:                             "waitUntilMaxValue( )",
274:                             "waitUntilMinValue( )",
275:                             "waitUntilValueInRange( )",
276:                             "waitUntilValueIs( )",
277:                             "waitUntilZero( )",
278:                             "waitWhileMaxValue( )",
279:                             "waitWhileMinValue( )",
280:                             "waitWhileValueInRange( )",
281:                             "waitWhileValueIs( )",
282:                             "waitWhileZero( )"};
283:         return methods;
284:     }
285:     
286:     private void changeRange() {
287:         setFinished(false);
288:         JDialog newRangeD = buildNewRangeDialog("Change Range");
289:         newRangeD.setSize(400, 120);
290:         newRangeD.setLocationRelativeTo(getTopContainer(newRangeD));
291:         newRangeD.setVisible(true);
292:         if ( isFinished() ) {
293:             updateRange(minVal, maxVal);
294:         }
295:         // reset finished toggle
296:         setFinished(false);
297:     }
298:     
299:     private JDialog buildNewRangeDialog(String name) {
300:         // get range values
301:         final JDialog d = buildDialog(name);
302:         final JPanel rangeP = new JPanel(new BorderLayout());
303:         JLabel min = new JLabel("New Min:", JLabel.RIGHT);
304:         JLabel max = new JLabel("New Max:", JLabel.RIGHT);
305:         final JTextField minTF = new JTextField(8);
306:         final JTextField maxTF = new JTextField(8);
307:         JButton submitB = new JButton("Submit");
308:         submitB.addActionListener(new ActionListener() {
309:                 public void actionPerformed(ActionEvent e) {
310:                     try {
311:                         minVal = 
312:                             Integer.valueOf(minTF.getText().trim()).intValue();
313:                         maxVal =
314:                             Integer.valueOf(maxTF.getText().trim()).intValue();
315:                         if ( minVal > maxVal || minVal > syncInt.getValue() ||
316:                              maxVal < syncInt.getValue() ) {
317:                             showErrorMessage("Invalid Range, please re-enter!");
318:                             minTF.setText("");
319:                             maxTF.setText("");
320:                             minTF.requestFocus();
321:                             return;
322:                         }
323:                         d.dispose();
324:                         setFinished(true);
325:                     } catch ( NumberFormatException nfe ) {
326:                         showErrorMessage("Please enter an integer value!");
327:                         minTF.setText("");
328:                         maxTF.setText("");
329:                         minTF.requestFocus();
330:                         return;
331:                     }
332:                 }
333:             });
334:         JPanel topP = new JPanel();
335:         topP.add(min);
336:         topP.add(minTF);
337:         topP.add(max);
338:         topP.add(maxTF);
339:         JPanel bottomP = new JPanel();
340:         bottomP.add(submitB);
341:         rangeP.add(topP, BorderLayout.NORTH);
342:         rangeP.add(bottomP, BorderLayout.SOUTH);
343:         d.getContentPane().add(rangeP);
344:         minTF.getRootPane().setDefaultButton(submitB);
345:         
346:         return d;
347:     }
348:     
349:     private void updateRange(final int min, final int max) {
350:         syncInt.setValueRange(min, max);
351:         updateRangeFields(min, max);
352:     }
353:     
354:     private void updateRangeFields(int min, int max) {
355:         // call setTextSafely on the JTKLabel to safel set 
356:         // the text with the event thread
357:         minValueL.setTextSafely(Integer.toString(min));
358:         maxValueL.setTextSafely(Integer.toString(max));
359:     }
360:     
361:     private void changeValue(int value) {
362:         syncInt.setValue(value);
363:         setValueLabel();
364:     }
365:     
366:     private void increaseValue(int value) {
367:         if ( syncInt.isMaxValue() ) {
368:             // do nothing...
369:             return;
370:         } else { 
371:             syncInt.increaseValueBy(value);
372:         }
373:         setValueLabel();
374:     }
375:     
376:     private void decreaseValue(int value) {
377:         if ( syncInt.isMinValue() ) {
378:             // do nothing...
379:             return;
380:         } else { 
381:             syncInt.decreaseValueBy(value);
382:         }
383:         setValueLabel();
384:     }
385:     
386:     private void setValueLabel() {
387:         // call setTextSafely on the JTKLabel to safel set 
388:         // the text with the event thread
389:         valueLabel.setTextSafely(Integer.toString(syncInt.getValue()));
390:     }
391:     
392:     private void getNewValue() {
393:         // get the value from the newValueTF
394:         int value = 0;
395:         try {
396:             value = Integer.valueOf(newValueTF.getText().trim()).intValue();
397:             if ( (value > syncInt.getMaxValue()) || 
398:                  (value < syncInt.getMinValue()) ) {
399:                 showErrorMessage("New value outside of range!");
400:                 newValueTF.setText("");
401:                 newValueTF.requestFocus();
402:                 return;     
403:             }
404:         } catch ( NumberFormatException nfe ) {
405:             showErrorMessage("Please enter an integer value!");
406:             newValueTF.setText("");
407:             newValueTF.requestFocus();
408:             return;
409:         }
410:         changeValue(value);
411:         newValueTF.setText("");
412:     }
413:     
414:     private void showErrorMessage(String msg){
415:         JOptionPane pane = new JOptionPane();
416:         JOptionPane.showMessageDialog(
417:                     this, 
418:                     msg, 
419:                     "Alert", 
420:                     JOptionPane.ERROR_MESSAGE
421:                 ); 
422:     }
423:     
424:     private void runDemo() {
425:         // Use a swing helper to create a helper thread.  The 
426:         // SwingHelper will create a thread to first run the demo, 
427:         // and at completion, the screen will be safely reactivated 
428:         // by the event thread.  The SwingHelper will recycle the 
429:         // helper thread behind the scenes so we do not need to 
430:         // constantly recreate threads to execute the demo...
431:         SwingHelper.execute(runDemoRunnable, enableScreenRunnable);
432:     }
433:     
434:     private void getMethod() {
435:         // get the method name...
436:         String method = (String) testList.getSelectedValue();
437:         // call the appropriate method
438:         callMethod(method);
439:     }
440:     
441:     private void callMethod(String method) {
442:         // determine what method to call
443:         long startTime = 0;
444:         long endTime = 0;
445:         long elapsedTime = 0;
446:         setFinished(false);
447:         if ( method.equals("waitForValueToChange( )") ) {
448:             getTimeout();
449:             if ( isFinished() ) {
450:                 startTime = System.currentTimeMillis();
451:                 try {
452:                     syncInt.waitForValueToChange(timeout);
453:                     elapsedTime = System.currentTimeMillis() - startTime;
454:                     updateResults("waited " + elapsedTime + "ms for" +
455:                         " the value to change to: " + syncInt.getValue() + 
456:                         "\n");
457:                 } catch ( TimedOutException toe ) {
458:                     elapsedTime = System.currentTimeMillis() - startTime;
459:                     updateResults("timed out after " + elapsedTime + 
460:                         "ms waiting for the value to change...\n");
461:                 } catch ( Exception x ) {
462:                     x.printStackTrace();
463:                 }
464:             }
465:         } else if ( method.equals("waitForValueToClimbTo( )") ) {
466:             getValue();
467:             if ( isFinished() ) {
468:                 startTime = System.currentTimeMillis();
469:                 try {
470:                     syncInt.waitForValueToClimbTo(newVal, timeout);
471:                     elapsedTime = System.currentTimeMillis() - startTime;
472:                     updateResults("waited " + elapsedTime + "ms for" +
473:                         " the value to climb to: " + syncInt.getValue() + 
474:                         "\n");
475:                 } catch ( TimedOutException toe ) {
476:                     elapsedTime = System.currentTimeMillis() - startTime;
477:                     updateResults("timed out after " + elapsedTime + 
478:                         "ms waiting for the value to climb to " + newVal +
479:                         "...\n");
480:                 } catch ( Exception x ) {
481:                     x.printStackTrace();
482:                 }
483:             }
484:         } else if ( method.equals("waitForValueToFallTo( )") ) {
485:             getValue();
486:             if ( isFinished() ) {
487:                 startTime = System.currentTimeMillis();
488:                 try {
489:                     syncInt.waitForValueToFallTo(newVal, timeout);
490:                     elapsedTime = System.currentTimeMillis() - startTime;
491:                     updateResults("waited " + elapsedTime + "ms for" +
492:                         " the value to fall to: " + syncInt.getValue() + 
493:                         "\n");
494:                 } catch ( TimedOutException toe ) {
495:                     elapsedTime = System.currentTimeMillis() - startTime;
496:                     updateResults("timed out after " + elapsedTime + 
497:                         "ms waiting for the value to fall to " + newVal +
498:                         "...\n");
499:                 } catch ( Exception x ) {
500:                     x.printStackTrace();
501:                 }
502:             }
503:         } else if ( method.equals("waitUntilMaxValue( )") ) {
504:             getTimeout();
505:             if ( isFinished() ) {
506:                 startTime = System.currentTimeMillis();
507:                 try {
508:                     syncInt.waitUntilMaxValue(timeout);
509:                     elapsedTime = System.currentTimeMillis() - startTime;
510:                     updateResults("waited " + elapsedTime + "ms for" +
511:                         " the value to reach the max value..." + "\n");
512:                 } catch ( TimedOutException toe ) {
513:                     elapsedTime = System.currentTimeMillis() - startTime;
514:                     updateResults("timed out after " + elapsedTime + 
515:                         "ms waiting for the value to reach the max value" +
516:                         "...\n");
517:                 } catch ( Exception x ) {
518:                     x.printStackTrace();
519:                 }
520:             }
521:         } else if ( method.equals("waitUntilMinValue( )") ) {
522:             getTimeout();
523:             if ( isFinished() ) {
524:                 startTime = System.currentTimeMillis();
525:                 try {
526:                     syncInt.waitUntilMinValue(timeout);
527:                     elapsedTime = System.currentTimeMillis() - startTime;
528:                     updateResults("waited " + elapsedTime + "ms for" +
529:                         " the value to reach the min value..." + "\n");
530:                 } catch ( TimedOutException toe ) {
531:                     elapsedTime = System.currentTimeMillis() - startTime;
532:                     updateResults("timed out after " + elapsedTime + 
533:                         "ms waiting for the value to reach the min value" +
534:                         "...\n");
535:                 } catch ( Exception x ) {
536:                     x.printStackTrace();
537:                 }
538:             }
539:         } else if ( method.equals("waitUntilValueInRange( )") ) {
540:             getRange();
541:             if ( isFinished() ) {
542:                 startTime = System.currentTimeMillis();
543:                 try {
544:                     syncInt.waitUntilValueInRange(minVal, maxVal, timeout);
545:                     elapsedTime = System.currentTimeMillis() - startTime;
546:                     updateResults("waited " + elapsedTime + 
547:                         "ms for the value to reach the range " + minVal + 
548:                         " - " + maxVal + "\n");
549:                 } catch ( TimedOutException toe ) {
550:                     elapsedTime = System.currentTimeMillis() - startTime;
551:                     updateResults("timed out after " + elapsedTime + 
552:                         "ms waiting for the value to reach the range " + 
553:                         minVal + " to " + maxVal + "\n");
554:                 } catch ( Exception x ) {
555:                     x.printStackTrace();
556:                 }
557:             }
558:         } else if ( method.equals("waitUntilValueIs( )") ) {
559:             getValue();
560:             if ( isFinished() ) {
561:                 startTime = System.currentTimeMillis();
562:                 try {
563:                     syncInt.waitUntilValueIs(newVal, timeout);
564:                     elapsedTime = System.currentTimeMillis() - startTime;
565:                     updateResults("waited " + elapsedTime + "ms for" +
566:                         " the value to = " + syncInt.getValue() + 
567:                         "\n");
568:                 } catch ( TimedOutException toe ) {
569:                     elapsedTime = System.currentTimeMillis() - startTime;
570:                     updateResults("timed out after " + elapsedTime + 
571:                         "ms waiting for the value = " + newVal +
572:                         "...\n");
573:                 } catch ( Exception x ) {
574:                     x.printStackTrace();
575:                 }
576:             }
577:         } else if ( method.equals("waitUntilZero( )") ) {
578:             getTimeout();
579:             if ( isFinished() ) {
580:                 startTime = System.currentTimeMillis();
581:                 try {
582:                     syncInt.waitUntilZero(timeout);
583:                     elapsedTime = System.currentTimeMillis() - startTime;
584:                     updateResults("waited " + elapsedTime + "ms " +
585:                         "for the value to = zero..." + "\n");
586:                 } catch ( TimedOutException toe ) {
587:                     elapsedTime = System.currentTimeMillis() - startTime;
588:                     updateResults("timed out after " + elapsedTime + 
589:                         "ms waiting for the value to = zero...\n");
590:                 } catch ( Exception x ) {
591:                     x.printStackTrace();
592:                 }
593:             }
594:         } else if ( method.equals("waitWhileMaxValue( )") ) {
595:             getTimeout();
596:             if ( isFinished() ) {
597:                 startTime = System.currentTimeMillis();
598:                 try {
599:                     syncInt.waitWhileMaxValue(timeout);
600:                     elapsedTime = System.currentTimeMillis() - startTime;
601:                     updateResults("waited " + elapsedTime + "ms " +
602:                         "while the value was = to the max value..." + "\n");
603:                 } catch ( TimedOutException toe ) {
604:                     elapsedTime = System.currentTimeMillis() - startTime;
605:                     updateResults("timed out after " + elapsedTime + 
606:                         "ms while the value was = to the max value...\n");
607:                 } catch ( Exception x ) {
608:                     x.printStackTrace();
609:                 }
610:             }
611:         } else if ( method.equals("waitWhileMinValue( )") ) {
612:             getTimeout();
613:             if ( isFinished() ) {
614:                 startTime = System.currentTimeMillis();
615:                 try {
616:                     syncInt.waitWhileMinValue(timeout);
617:                     elapsedTime = System.currentTimeMillis() - startTime;
618:                     updateResults("waited " + elapsedTime + "ms " +
619:                         "while the value was = to the min value..." + "\n");
620:                 } catch ( TimedOutException toe ) {
621:                     elapsedTime = System.currentTimeMillis() - startTime;
622:                     updateResults("timed out after " + elapsedTime + 
623:                         "ms while the value was = to the min value...\n");
624:                 } catch ( Exception x ) {
625:                     x.printStackTrace();
626:                 }
627:             }
628:         } else if ( method.equals("waitWhileValueInRange( )") ) {
629:             getRange();
630:             if ( isFinished() ) {
631:                 startTime = System.currentTimeMillis();
632:                 try {
633:                     syncInt.waitWhileValueInRange(minVal, maxVal, timeout);
634:                     elapsedTime = System.currentTimeMillis() - startTime;
635:                     updateResults("waited " + elapsedTime + 
636:                         "ms while the value was in the range " + minVal + 
637:                         " - " + maxVal + "\n");
638:                 } catch ( TimedOutException toe ) {
639:                     elapsedTime = System.currentTimeMillis() - startTime;
640:                     updateResults("timed out after waiting " + 
641:                         elapsedTime + "ms while the value was within the " +
642:                         "range " + minVal + " to " + maxVal + "\n");
643:                 } catch ( Exception x ) {
644:                     x.printStackTrace();
645:                 }
646:             }
647:         } else if ( method.equals("waitWhileValueIs( )") ) {
648:             getValue();
649:             if ( isFinished() ) {
650:                 startTime = System.currentTimeMillis();
651:                 try {
652:                     syncInt.waitWhileValueIs(newVal, timeout);
653:                     elapsedTime = System.currentTimeMillis() - startTime;
654:                     updateResults("waited " + elapsedTime + "ms while" +
655:                         " the value was = to: " + newVal + "\n");
656:                 } catch ( TimedOutException toe ) {
657:                     elapsedTime = System.currentTimeMillis() - startTime;
658:                     updateResults("timed out after " + elapsedTime + 
659:                         "ms while the value = " + newVal + "...\n");
660:                 } catch ( Exception x ) {
661:                     x.printStackTrace();
662:                 }
663:             }
664:         } else if ( method.equals("waitWhileZero( )") ) {
665:             getTimeout();
666:             if ( isFinished() ) {
667:                 startTime = System.currentTimeMillis();
668:                 try {
669:                     syncInt.waitWhileZero(timeout);
670:                     elapsedTime = System.currentTimeMillis() - startTime;
671:                     updateResults("waited " + elapsedTime + "ms " +
672:                         "while the value was = to zero..." + "\n");
673:                 } catch ( TimedOutException toe ) {
674:                     elapsedTime = System.currentTimeMillis() - startTime;
675:                     updateResults("timed out after " + elapsedTime + 
676:                         "ms while the value was = to zero...\n");
677:                 } catch ( Exception x ) {
678:                     x.printStackTrace();
679:                 }
680:             }
681:         }
682:         // reset finished flag
683:         setFinished(false);         
684:     }
685:     
686:     private synchronized void updateResults(String msg) {
687:         String name = Thread.currentThread().getName();
688:         resultsTA.appendSafely(name + ": " + msg);
689:     }
690:     
691:     private void enableScreen() {
692:         startB.setEnabled(true);
693:         testList.setEnabled(true);
694:     }
695:     
696:     private synchronized void setFinished(boolean result) {
697:         finished = result;
698:     }
699:     
700:     private synchronized boolean isFinished() {
701:         return finished;
702:     }
703:     
704:     private JDialog buildDialog(String title) {
705:         JDialog jd = 
706:             new JDialog((Frame)this.getParent().getParent().getParent(), 
707:                 title, true);
708:         jd.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
709:         return jd;
710:     }
711:     
712:     private void getTimeout() {
713:         JDialog timeoutD = buildTimeoutDialog("Enter Timeout");
714:         timeoutD.setSize(400, 120);
715:         timeoutD.setLocationRelativeTo(
716:             this.getParent().getParent().getParent());
717:         timeoutD.setVisible(true);
718:     }
719:     
720:     private JDialog buildTimeoutDialog(String name) {
721:         final JDialog d = buildDialog(name);
722:         final JPanel timeoutP = new JPanel(new BorderLayout());
723:         JLabel timeoutL = new JLabel("Timeout (in seconds, 0 for no timeout):",
724:             JLabel.RIGHT);
725:         final JTextField timeoutTF = new JTextField(6);
726:         JButton submitB = new JButton("Submit");
727:         submitB.addActionListener(new ActionListener() {
728:                 public void actionPerformed(ActionEvent e) {
729:                     try {
730:                         timeout = 
731:                             Integer.valueOf(
732:                                 timeoutTF.getText().trim()).intValue() * 1000;
733:                         setFinished(true);
734:                         d.dispose();
735:                     } catch ( NumberFormatException nfe ) {
736:                         showErrorMessage("Please enter an integer value!");
737:                         return;
738:                     }
739:                 }
740:             });
741:         JPanel topP = new JPanel();
742:         topP.add(timeoutL);
743:         topP.add(timeoutTF);
744:         JPanel bottomP = new JPanel();
745:         bottomP.add(submitB);
746:         timeoutP.add(topP, BorderLayout.NORTH);
747:         timeoutP.add(bottomP, BorderLayout.SOUTH);
748:         d.getContentPane().add(timeoutP);
749:         timeoutTF.getRootPane().setDefaultButton(submitB);
750:         
751:         return d;
752:     }
753:     
754:     private void getRange() {
755:         JDialog rangeD = buildRangeDialog("Enter Range");
756:         rangeD.setSize(550, 120);
757:         rangeD.setLocationRelativeTo(getTopContainer(rangeD));
758:         rangeD.setVisible(true);    
759:     }
760: 
761:     private JDialog buildRangeDialog(String name) {
762:         final JDialog d = buildDialog(name);
763:         final JPanel rangeP = new JPanel(new BorderLayout());
764:         JLabel min = new JLabel("Min:", JLabel.RIGHT);
765:         JLabel max = new JLabel("Max:", JLabel.RIGHT);
766:         final JTextField minTF = new JTextField(6);
767:         final JTextField maxTF = new JTextField(6);
768:         JLabel timeoutL = new JLabel("Timeout (in seconds, 0 for no timeout):",
769:             JLabel.RIGHT);
770:         final JTextField timeoutTF = new JTextField(6);
771:         JButton submitB = new JButton("Submit");
772:         submitB.addActionListener(new ActionListener() {
773:                 public void actionPerformed(ActionEvent e) {
774:                     try {
775:                         minVal = 
776:                             Integer.valueOf(minTF.getText().trim()).intValue();
777:                         maxVal =
778:                             Integer.valueOf(maxTF.getText().trim()).intValue();
779:                         if ( minVal > maxVal ) {
780:                             showErrorMessage("Invalid Range, please re-enter!");
781:                             minTF.setText("");
782:                             maxTF.setText("");
783:                             minTF.requestFocus();
784:                             return;
785:                         }
786:                         timeout = 
787:                             Integer.valueOf(
788:                                 timeoutTF.getText().trim()).intValue() * 1000;
789:                         setFinished(true);
790:                         d.dispose();
791:                     } catch ( NumberFormatException nfe ) {
792:                         showErrorMessage("Please enter an integer value!");
793:                         return;
794:                     }
795:                 }
796:             });
797:         JPanel topP = new JPanel();
798:         topP.add(min);
799:         topP.add(minTF);
800:         topP.add(max);
801:         topP.add(maxTF);
802:         topP.add(timeoutL);
803:         topP.add(timeoutTF);
804:         JPanel bottomP = new JPanel();
805:         bottomP.add(submitB);
806:         rangeP.add(topP, BorderLayout.NORTH);
807:         rangeP.add(bottomP, BorderLayout.SOUTH);
808:         d.getContentPane().add(rangeP);
809:         timeoutTF.getRootPane().setDefaultButton(submitB);
810:         
811:         return d;
812:     }
813:     
814:     private void getValue() {
815:         JDialog valueD = buildValueDialog("Enter Value");
816:         valueD.setSize(450, 120);
817:         valueD.setLocationRelativeTo(getTopContainer(valueD));
818:         valueD.setVisible(true);
819:     }
820:     
821:     private JDialog buildValueDialog(String name) {
822:         final JDialog d = buildDialog(name);
823:         final JPanel valueP = new JPanel(new BorderLayout());
824:         JLabel valueL = new JLabel("Value:", JLabel.RIGHT);
825:         final JTextField valueTF = new JTextField(6);
826:         JLabel timeoutL = new JLabel("Timeout (in seconds, 0 for no timeout):",
827:             JLabel.RIGHT);
828:         final JTextField timeoutTF = new JTextField(6);
829:         JButton submitB = new JButton("Submit");
830:         submitB.addActionListener(new ActionListener() {
831:                 public void actionPerformed(ActionEvent e) {
832:                     try {
833:                         newVal =
834:                             Integer.valueOf(
835:                                 valueTF.getText().trim()).intValue();
836:                         timeout = 
837:                             Integer.valueOf(
838:                                 timeoutTF.getText().trim()).intValue() * 1000;
839:                         setFinished(true);
840:                         d.dispose();
841:                     } catch ( NumberFormatException nfe ) {
842:                         showErrorMessage("Please enter an integer value!");
843:                         return;
844:                     }
845:                 }
846:             });
847:         JPanel topP = new JPanel();
848:         topP.add(valueL);
849:         topP.add(valueTF);
850:         topP.add(timeoutL);
851:         topP.add(timeoutTF);
852:         JPanel bottomP = new JPanel();
853:         bottomP.add(submitB);
854:         valueP.add(topP, BorderLayout.NORTH);
855:         valueP.add(bottomP, BorderLayout.SOUTH);
856:         d.getContentPane().add(valueP);
857:         timeoutTF.getRootPane().setDefaultButton(submitB);
858:         
859:         return d;
860:     }
861:     
862:     private Container getTopContainer(Component comp) {
863:         Container currContainer = comp.getParent();
864:         Container lastContainer = null;
865: 
866:         while ( ( currContainer != null ) && 
867:                 ( currContainer != lastContainer ) ) {
868:             lastContainer = currContainer;
869:             if ( lastContainer instanceof JDialog ) {
870:                 break;
871:             }
872:             currContainer = currContainer.getParent();
873:         }
874:         return lastContainer;
875:     }
876:     
877:     public static JFrame createFramedInstance(final boolean exitOnClose) {
878:         final SyncIntegerDemo demo = new SyncIntegerDemo();
879:         final JFrame f = new JFrame("SyncInteger Demo");
880:         f.setContentPane(demo);
881:         f.addWindowListener(new WindowAdapter() {
882:                 public void windowClosing(WindowEvent w) {
883:                     f.setVisible(false);
884:                     f.dispose();
885:                     // supports applet so the vm is only shutdown when 
886:                     // run as an application
887:                     if ( exitOnClose ) {
888:                         System.exit(0);
889:                     }
890:                 }
891:             });
892:         f.setSize(new Dimension(700, 350));
893:         Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
894:         f.setLocation((screenDim.width - f.getSize().width) / 2,
895:                       (screenDim.height - f.getSize().height) / 2);
896: 
897:         return f;
898:     }
899:     
900:     public static void main(String[] args) {
901:         JFrame f = SyncIntegerDemo.createFramedInstance(true);
902:         f.setVisible(true);
903:     }
904: }
      

SyncIntDemoApplet.java

 1: package com.jtkdemo;
 2: 
 3: import java.awt.*;
 4: import java.applet.*;
 5: import java.awt.event.*;
 6: import javax.swing.*;
 7: import com.jtkdemo.*;
 8: import com.jthreadkit.*;
 9: import com.jthreadkit.fifo.*;
10: import com.jthreadkit.collection.*;
11: 
12: public class SyncIntDemoApplet extends Applet {
13:     public void init() {
14:         this.setBackground(Color.white);
15:         JButton button = new JButton("Run Demo");
16:         JPanel buttonP = new JPanel();
17:         buttonP.setBackground(Color.white);
18:         buttonP.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
19:         buttonP.add(button);
20:         this.add(buttonP, BorderLayout.SOUTH);
21:         button.addActionListener(new ActionListener() {
22:                 public void actionPerformed(ActionEvent e) {
23:                     startDemo();
24:                 }
25:             });
26:     }
27:     
28:     private void startDemo() {
29:         SyncIntegerDemo demo = new SyncIntegerDemo();
30:         JFrame f = new JFrame("SyncIntegerDemo");
31:                 
32:         f.setContentPane(demo);
33:         f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
34:         Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
35:         f.setSize(700, 350);
36:         f.setLocation((screenDim.width - f.getSize().width) / 2,
37:                       (screenDim.height - f.getSize().height) / 2);
38:         f.setVisible(true);
39:     }
40: }
      
Back to top