home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 1998 October
/
PCWorld_1998-10_cd.bin
/
software
/
prehled
/
inprise
/
JSAMPLES.Z
/
Applet1.java
< prev
next >
Wrap
Text File
|
1998-05-08
|
13KB
|
329 lines
/*
* Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
*
* This SOURCE CODE FILE, which has been provided by Borland as part
* of a Borland product for use ONLY by licensed users of the product,
* includes CONFIDENTIAL and PROPRIETARY information of Borland.
*
* USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS
* OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
* THE PRODUCT.
*
* IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
* COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
* OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
* OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
* OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
* OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
* CODE FILE.
*/
//Title: Prime Number List
//Version:
//Copyright: Copyright (c) 1998
//Author: Greg Hamer
//Company: Borland International
//Description: A Separate thread supplies the model with the prime numbers.
//Displays the model in a List View. Uses a CustomItemListener
//to display the Mersenne primes in red
//Also useful for seeing the effects of setBatchMode and setUniformWidth on performance
package borland.samples.jbcl.listview;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import borland.jbcl.layout.*;
import borland.jbcl.control.*;
import borland.jbcl.view.*;
import borland.jbcl.model.*;
import com.sun.java.swing.*;
public class Applet1 extends JApplet implements Runnable, VectorModelListener, CustomItemListener{
boolean isStandalone = false;
ListView listView = new ListControl();
BevelPanel bevelPanel1 = new BevelPanel();
ButtonControl restartButton = new ButtonControl();
ButtonControl cancelButton = new ButtonControl();
GridBagLayout gridBagLayout1 = new GridBagLayout();
GridBagLayout gridBagLayout2 = new GridBagLayout();
LabelControl testNumberText = new LabelControl();
LabelControl primeNumberText = new LabelControl();
GridLayout gridLayout1 = new GridLayout();
GridLayout gridLayout2 = new GridLayout();
JLabel testNumberLabel = new JLabel();
JLabel primeNumberLabel = new JLabel();
PrimeFactory primeFactory = new PrimeFactory();
Thread uiThread;
Color defaultColor;
JPanel panel1 = new JPanel();
XYLayout xYLayout2 = new XYLayout();
JLabel jLabel1 = new JLabel();
JTextField jTextField1 = new JTextField();
BorderLayout borderLayout1 = new BorderLayout();
Checkbox checkbox1 = new Checkbox();
boolean restart = true;
boolean started = false;
LabelControl timeText = new LabelControl();
JLabel timeLabel = new JLabel();
Checkbox checkBox1 = new Checkbox();
Checkbox checkbox2 = new Checkbox();
//Construct the applet
public Applet1() {
}
//Initialize the applet
public void init() {
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);
}
//Component initialization
private void jbInit() throws Exception {
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);
}
}
);
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
}
//Main method
public static void main(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);
}
void updateUI(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();
}
public void run() {
//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) {}
}
}
}
public void customizeItem(Object parm1, 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 void editorRequested(Object parm1, Object parm2, int parm3, CustomPaintSite parm4) {
//method for interface borland.jbcl.view.CustomViewManagerListener
}
public void modelStructureChanged(VectorModelEvent parm1) {
//UpdateUI will setBatchMode to false after it finishes waiting;
listView.setBatchMode(checkbox1.getState()); //nothing much happens when the state stays the same
}
public void modelContentChanged(VectorModelEvent parm1) {
listView.setBatchMode(checkbox1.getState());
}
void cancelButton_actionPerformed(ActionEvent e) {
primeFactory.stop();
uiThread.stop();
if (isStandalone)
System.exit(0);
}
void restartButton_actionPerformed(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();
}
}
}
void jTextField1_actionPerformed(ActionEvent e) {
//System.err.println("action performed");
try {
int subscript = primeFactory.find(new Long(jTextField1.getText()));
listView.setTopIndex(subscript);
}
catch (Exception exp) {}
}
// Without this callback, the items that are shown are always the same
// even after you scroll. This is because when a new item is
// added to the list, ListView will make sure that the the item which
// has the subfocus is visible. During scrolling, however, the subfocus
// stays the same, only the topIndex is changed. So, we set the subfocus
// value to the topIndex value.
int max = 0;
int oldmax;
void listViewHorizontalScrollBar_adjustmentValueChanged(AdjustmentEvent e) {
max = e.getAdjustable().getMaximum();
if (max == oldmax)
listView.setSubfocus(listView.getTopIndex());
else
oldmax = max;
}
void checkbox2_itemStateChanged(ItemEvent e) {
listView.setUniformWidth(checkbox2.getState());
}
}