FileDocCategorySizeDatePackage
Applet1.javaAPI DocExample13241Sat Sep 12 03:01:00 BST 1998borland.samples.jbcl.listview

Applet1

public class Applet1 extends JApplet implements Runnable, VectorModelListener, CustomItemListener

Fields Summary
boolean
isStandalone
ListView
listView
BevelPanel
bevelPanel1
ButtonControl
restartButton
ButtonControl
cancelButton
GridBagLayout
gridBagLayout1
GridBagLayout
gridBagLayout2
LabelControl
testNumberText
LabelControl
primeNumberText
GridLayout
gridLayout1
GridLayout
gridLayout2
JLabel
testNumberLabel
JLabel
primeNumberLabel
PrimeFactory
primeFactory
Thread
uiThread
Color
defaultColor
JPanel
panel1
XYLayout
xYLayout2
JLabel
jLabel1
JTextField
jTextField1
BorderLayout
borderLayout1
Checkbox
checkbox1
boolean
restart
boolean
started
LabelControl
timeText
JLabel
timeLabel
Checkbox
checkBox1
Checkbox
checkbox2
int
max
int
oldmax
Constructors Summary
public Applet1()


  //Construct the applet
    
  
Methods Summary
voidcancelButton_actionPerformed(java.awt.event.ActionEvent e)

    primeFactory.stop();
    uiThread.stop();
    if (isStandalone)
      System.exit(0);
  
voidcheckbox2_itemStateChanged(java.awt.event.ItemEvent e)

    listView.setUniformWidth(checkbox2.getState());
  
public voidcustomizeItem(java.lang.Object parm1, java.lang.Object data, int parm3, CustomPaintSite paintSite)

    //method for interface borland.jbcl.view.CustomViewManagerListener
    try {
      long value = ((Long)data).longValue();
      if ((value | (value+1)) == value + value + 1)
        paintSite.setForeground(Color.red);
      else
        paintSite.setForeground(Color.black);
        }
      catch(Exception e) {
        System.err.println("painterRequested() - " + e);
      }
  
public voideditorRequested(java.lang.Object parm1, java.lang.Object parm2, int parm3, CustomPaintSite parm4)

    //method for interface borland.jbcl.view.CustomViewManagerListener
  
public java.lang.StringgetAppletInfo()

    return "Applet Information";
  
public java.lang.String[][]getParameterInfo()

    return null;
  
public voidinit()

    try { jbInit(); } catch (Exception e) { e.printStackTrace(); }
    primeFactory.setPriority(Thread.MIN_PRIORITY);
    //primeFactory.start();
    updateUI(0);
    uiThread = new Thread(this);
    uiThread.setPriority(Thread.NORM_PRIORITY - 1);

  
voidjTextField1_actionPerformed(java.awt.event.ActionEvent e)

    //System.err.println("action performed");
    try {
      int subscript = primeFactory.find(new Long(jTextField1.getText()));
      listView.setTopIndex(subscript);
    }
    catch (Exception exp) {}
  
private voidjbInit()

    primeFactory.addModelListener(this);
    bevelPanel1.setBevelInner(BevelPanel.FLAT);
    bevelPanel1.setLayout(gridLayout1);
    restartButton.setLabel("Start");
    restartButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        restartButton_actionPerformed(e);
      }
    });
    cancelButton.setLabel("Cancel");
    cancelButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        cancelButton_actionPerformed(e);
      }
    });
    testNumberText.setAlignment(Label.RIGHT);
    testNumberText.setFont(new Font("Dialog", 0, 30));
    testNumberText.setForeground(testNumberLabel.getForeground());
    testNumberText.setText("testing");
    primeNumberText.setAlignment(Label.RIGHT);
    primeNumberText.setFont(new Font("Dialog", 0, 30));
    primeNumberText.setForeground(testNumberLabel.getForeground());
    primeNumberText.setText("3");
    gridLayout1.setHgap(8);
    testNumberLabel.setHorizontalAlignment(SwingConstants.RIGHT);
    testNumberLabel.setText("Largest so far:");
    primeNumberLabel.setHorizontalAlignment(SwingConstants.RIGHT);
    primeNumberLabel.setText("Prime:");
    jLabel1.setText("Go To:");
    timeLabel.setHorizontalAlignment(SwingConstants.RIGHT);
    checkbox2.setLabel("UniformWidth");
    checkbox2.setState(true);
    checkbox2.addItemListener(new java.awt.event.ItemListener() {
      public void itemStateChanged(ItemEvent e) {
        checkbox2_itemStateChanged(e);
      }
    });
    checkbox2.setForeground(testNumberLabel.getForeground());
    checkbox1.setLabel("BatchMode");
    checkbox1.setState(true);
    checkbox1.setForeground(testNumberLabel.getForeground());
    timeText.setText("");
    timeText.setAlignment(Label.RIGHT);
    timeText.setForeground(testNumberLabel.getForeground());
    timeLabel.setText("Primes found:");
    jTextField1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jTextField1_actionPerformed(e);
      }
    });
    panel1.setLayout(borderLayout1);
    this.setSize(new Dimension(420, 300));
    listView.setSelection(new NullVectorSelection ());
    listView.setModel(primeFactory);
    listView.setSubfocus(-1);
    listView.setShowFocus(false);
    listView.setBatchMode(true);
    listView.addCustomItemListener(this);
    //When either uniformWidth or uniformHeight are false, ListView looks through the entire model to
    //calculate preferredSize
    listView.setUniformWidth(true); //practically required for large models
    this.getContentPane().setLayout(gridBagLayout1);
    this.getContentPane().add(bevelPanel1, new GridBagConstraints2(2, 7, 2, 1, 0.0, 0.0
            ,GridBagConstraints.SOUTHEAST, GridBagConstraints.NONE, new Insets(0, 30, 10, 12), 50, 0));
    bevelPanel1.add(restartButton, null);
    bevelPanel1.add(cancelButton, null);
    this.getContentPane().add(testNumberText, new GridBagConstraints2(2, 0, 2, 1, 0.0, 0.0
            ,GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 12), 0, 0));
    this.getContentPane().add(primeNumberText, new GridBagConstraints2(2, 1, 2, 1, 0.0, 0.0
            ,GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 12), 0, 0));
    this.getContentPane().add(testNumberLabel, new GridBagConstraints2(1, 0, 1, 1, 0.0, 0.0
            ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 10), 0, 0));
    this.getContentPane().add(primeNumberLabel, new GridBagConstraints2(1, 1, 1, 1, 0.0, 0.0
            ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 10), 0, 0));
    this.getContentPane().add(panel1, new GridBagConstraints2(0, 0, 1, 8, 1.0, 1.0
            ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 12, 10, 21), 0, 0));
    panel1.add(listView, BorderLayout.CENTER);
    this.getContentPane().add(jLabel1, new GridBagConstraints2(1, 3, 1, 2, 0.0, 0.0
            ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 10), 0, 0));
    this.getContentPane().add(jTextField1, new GridBagConstraints2(2, 3, 2, 1, 0.0, 0.0
            ,GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 12), 23, 0));
    this.getContentPane().add(timeText, new GridBagConstraints2(2, 2, 2, 1, 0.0, 0.0
            ,GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 12), 0, 0));
    this.getContentPane().add(timeLabel, new GridBagConstraints2(1, 2, 1, 1, 0.0, 0.0
            ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 10), 0, 0));
    this.getContentPane().add(checkbox1, new GridBagConstraints2(2, 5, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
    this.getContentPane().add(checkbox2, new GridBagConstraints2(2, 6, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

    // add listView scrollbar listener
      listView.getVerticalScrollBar().addAdjustmentListener(
       new AdjustmentListener() {
         public void adjustmentValueChanged(AdjustmentEvent e) {
           listViewHorizontalScrollBar_adjustmentValueChanged(e);
         }
      }
    );
  
voidlistViewHorizontalScrollBar_adjustmentValueChanged(java.awt.event.AdjustmentEvent e)

     
    max = e.getAdjustable().getMaximum();
    if (max == oldmax)
      listView.setSubfocus(listView.getTopIndex());
    else
      oldmax = max;
  
public static voidmain(java.lang.String[] args)

    Applet1 applet = new Applet1();
    applet.isStandalone = true;
    DecoratedFrame frame = new DecoratedFrame();
    frame.setTitle("Prime Numbers");
    frame.add(applet, BorderLayout.CENTER);
    applet.init();
    applet.start();

    frame.pack();
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
    frame.setVisible(true);
  
public voidmodelContentChanged(VectorModelEvent parm1)

    listView.setBatchMode(checkbox1.getState());
  
public voidmodelStructureChanged(VectorModelEvent parm1)

    //UpdateUI will setBatchMode to false after it finishes waiting;
    listView.setBatchMode(checkbox1.getState());   //nothing much happens when the state stays the same
  
voidrestartButton_actionPerformed(java.awt.event.ActionEvent e)

    restart = !restart;
    if (restart) {
      primeFactory.suspend();
      restartButton.setLabel("Resume");

    }
    else {
      restartButton.setLabel("Pause");
      if (started)
        primeFactory.resume();
      else {
        started = true;
        primeFactory.start();
        uiThread.start();
      }  
    }
  
public voidrun()

    //update the ui on this thread every so often
    int count = 0;
    while (count < primeFactory.getCount()){
      updateUI(count++);
      synchronized (uiThread) {
        try  {
          uiThread.wait(200);
        }
        catch (Exception e) {}
      }
    }
  
voidupdateUI(int location)

    //System.err.println("updateUI");
    listView.setBatchMode(false);  //let the list update itself occasionally even when setBatchMode is on
    int count = primeFactory.getCount();
    Object largest = primeFactory.get(location);
    long prime = ((Long)largest).longValue();
    if ((prime | (prime + 1)) == prime + prime + 1) {
      defaultColor = primeNumberText.getForeground();
      primeNumberText.setForeground(Color.red);
    }
    else  {
      if (defaultColor != null) {
        primeNumberText.setForeground(defaultColor);
        defaultColor = null;
      }
    }
    primeNumberText.setText(" " + largest.toString());
    testNumberText.setText(" " + primeFactory.getCheckNumber());
    timeText.setText(" " + count );
    //validate();