home *** CD-ROM | disk | FTP | other *** search
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Container;
- import java.awt.Event;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Frame;
- import java.awt.Insets;
- import java.awt.Label;
- import java.awt.List;
- import java.awt.Menu;
- import java.awt.MenuBar;
- import java.awt.MenuItem;
- import java.awt.Rectangle;
- import java.awt.TextField;
- import java.awt.Window;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowEvent;
- import java.awt.event.WindowListener;
- import java.util.Enumeration;
- import java.util.EventObject;
- import powersoft.powerj.event.AWTEvent;
- import powersoft.powerj.event.EventData;
- import powersoft.powerj.ui.ResizePercentLayout;
- import powersoft.powerj.util.CompareRoutine;
- import powersoft.powerj.util.SortableVector;
- import powersoft.powerj.util.StringUtility;
-
- class Form_SortableVector extends Frame implements WindowListener, ActionListener {
- boolean __mainForm;
- private MenuBar menu_SortableVector = new MenuBar();
- private Menu menu_SortableVector_Sort = new Menu("");
- private MenuItem menu_SortableVector_AlphaSort = new MenuItem("");
- private MenuItem menu_SortableVector_NameLengthSort = new MenuItem("");
- private MenuItem menu_SortableVector_Scramble = new MenuItem("");
- private Menu menu_SortableVector_Name = new Menu("");
- private MenuItem menu_SortableVector_AddWaldo = new MenuItem("");
- private MenuItem menu_SortableVector_RemoveWaldo = new MenuItem("");
- private MenuItem menu_SortableVector_FindWaldo = new MenuItem("");
- private MenuItem menu_SortableVector_RemoveThirdName = new MenuItem("");
- private List lb_nameList = new List();
- private TextField textf_messages = new TextField();
- private Label label_1 = new Label();
- private Label label_2 = new Label();
- private Label label_3 = new Label();
- final int ELEMENTS;
- SortableVector _nameVector = new SortableVector();
- String _sortType = new String("none");
- String[] _nameList = new String[]{"John", "Brad", "Jim", "Greg", "Dan", "Dave", "Wesley", "Eric", "Chris", "Geno", "Rob", "David", "Ivan", "Waldo", "Mark", "Kevin", "Paul", "Bill", "Eugene", "Tim", "Asif", "Michael", "Thorsten", "Graham", "Jack", "Lisa", "Anthony", "Brian"};
-
- public Rectangle DURectangle(int var1, int var2, int var3, int var4) {
- String var5 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
- FontMetrics var6 = ((Component)this).getFontMetrics(((Component)this).getFont());
- double var7 = (double)(var6 != null ? var6.stringWidth(var5) / var5.length() : 0);
- double var9 = var6 != null ? (double)var6.getHeight() / (double)2.0F : (double)0.0F;
- return new Rectangle((int)Math.round(var7 * (double)var1 / (double)4.0F), (int)Math.round(var9 * (double)var2 / (double)4.0F), (int)Math.round(var7 * (double)var3 / (double)4.0F), (int)Math.round(var9 * (double)var4 / (double)4.0F));
- }
-
- public void DUPositionComponent(Component var1, int var2, int var3, int var4, int var5, Insets var6) {
- Rectangle var7 = this.DURectangle(var2, var3, var4, var5);
- if (var1 != this && var6 != null) {
- var7.x += var6.left;
- var7.y += var6.top;
- }
-
- if (var1 == this && var6 != null && var6.bottom > 0) {
- var7.height += var6.bottom;
- }
-
- var1.setBounds(var7);
- }
-
- public void setMainForm(boolean var1) {
- this.__mainForm = var1;
- }
-
- public boolean isMainForm() {
- return this.__mainForm;
- }
-
- public boolean create() throws Exception {
- ((Frame)this).setTitle("Sortable Vector");
- ((Frame)this).setResizable(true);
- Container var1 = this.getContentPane();
- Font var2 = new Font("Dialog", 0, 12);
- ((Component)this).setFont(var2);
- boolean var3 = true;
- ((Frame)this).addNotify();
- this.menu_SortableVector_Sort.setLabel("Sort");
- this.menu_SortableVector.add(this.menu_SortableVector_Sort);
- this.menu_SortableVector_AlphaSort.setLabel("Alpha Sort");
- this.menu_SortableVector_Sort.add(this.menu_SortableVector_AlphaSort);
- this.menu_SortableVector_NameLengthSort.setLabel("Name Length Sort");
- this.menu_SortableVector_Sort.add(this.menu_SortableVector_NameLengthSort);
- this.menu_SortableVector_Scramble.setLabel("Scramble");
- this.menu_SortableVector_Sort.add(this.menu_SortableVector_Scramble);
- this.menu_SortableVector_Name.setLabel("Name");
- this.menu_SortableVector.add(this.menu_SortableVector_Name);
- this.menu_SortableVector_AddWaldo.setLabel("Add Waldo");
- this.menu_SortableVector_Name.add(this.menu_SortableVector_AddWaldo);
- this.menu_SortableVector_RemoveWaldo.setLabel("Remove Waldo");
- this.menu_SortableVector_Name.add(this.menu_SortableVector_RemoveWaldo);
- this.menu_SortableVector_FindWaldo.setLabel("Find Waldo");
- this.menu_SortableVector_Name.add(this.menu_SortableVector_FindWaldo);
- this.menu_SortableVector_RemoveThirdName.setLabel("Remove Third Name");
- this.menu_SortableVector_Name.add(this.menu_SortableVector_RemoveThirdName);
- ((Frame)this).setMenuBar(this.menu_SortableVector);
- Insets var4 = (Insets)var1.getInsets().clone();
- ((Component)var1).setBackground(Color.lightGray);
- ((Component)var1).setForeground(Color.black);
- ResizePercentLayout var5 = new ResizePercentLayout();
- var1.setLayout(var5);
- var1.add(this.lb_nameList);
- var1.add(this.textf_messages);
- var1.add(this.label_1);
- var1.add(this.label_2);
- var1.add(this.label_3);
- this.DUPositionComponent(this, 59, 36, 293, 224, var4);
- ((Window)this).addWindowListener(this);
- this.menu_SortableVector_AlphaSort.addActionListener(this);
- this.menu_SortableVector_NameLengthSort.addActionListener(this);
- this.menu_SortableVector_Scramble.addActionListener(this);
- this.menu_SortableVector_AddWaldo.addActionListener(this);
- this.menu_SortableVector_RemoveWaldo.addActionListener(this);
- this.menu_SortableVector_FindWaldo.addActionListener(this);
- this.menu_SortableVector_RemoveThirdName.addActionListener(this);
- this.DUPositionComponent(this.lb_nameList, 5, 40, 275, 135, var4);
- this.lb_nameList.setFont(var2);
- this.lb_nameList.setBackground(Color.white);
- this.lb_nameList.setForeground(Color.black);
- this.lb_nameList.setEnabled(true);
- this.lb_nameList.setVisible(true);
- this.DUPositionComponent(this.textf_messages, 5, 180, 275, 13, var4);
- this.textf_messages.setFont(var2);
- this.textf_messages.setBackground(Color.white);
- this.textf_messages.setForeground(Color.black);
- this.textf_messages.setEnabled(true);
- this.textf_messages.setVisible(true);
- this.DUPositionComponent(this.label_1, 5, 5, 275, 10, var4);
- this.label_1.setText("This sample demonstrates some of the use of the SortableVector class.");
- this.label_1.setAlignment(0);
- this.label_1.setFont(var2);
- this.label_1.setBackground(Color.lightGray);
- this.label_1.setForeground(Color.black);
- this.label_1.setEnabled(true);
- this.label_1.setVisible(true);
- this.DUPositionComponent(this.label_2, 5, 15, 275, 10, var4);
- this.label_2.setText("The menu items perform operations on the vector and display the results");
- this.label_2.setAlignment(0);
- this.label_2.setFont(var2);
- this.label_2.setBackground(Color.lightGray);
- this.label_2.setForeground(Color.black);
- this.label_2.setEnabled(true);
- this.label_2.setVisible(true);
- this.DUPositionComponent(this.label_3, 5, 25, 275, 10, var4);
- this.label_3.setText("in the list box below.");
- this.label_3.setAlignment(0);
- this.label_3.setFont(var2);
- this.label_3.setBackground(Color.lightGray);
- this.label_3.setForeground(Color.black);
- this.label_3.setEnabled(true);
- this.label_3.setVisible(true);
- var5.setResizePercent(this.lb_nameList, new Rectangle(0, 0, 0, 0));
- var5.setResizePercent(this.textf_messages, new Rectangle(0, 0, 0, 0));
- var5.setResizePercent(this.label_1, new Rectangle(0, 0, 0, 0));
- var5.setResizePercent(this.label_2, new Rectangle(0, 0, 0, 0));
- var5.setResizePercent(this.label_3, new Rectangle(0, 0, 0, 0));
- ((Component)this).setVisible(true);
- this.Form_SortableVector_objectCreated(new EventData(this));
- return var3;
- }
-
- public synchronized boolean destroy() {
- if (this instanceof Window) {
- ((Window)this).dispose();
- } else {
- ((Container)this).removeNotify();
- }
-
- if (this.isMainForm()) {
- System.gc();
- System.runFinalization();
- System.exit(0);
- }
-
- return true;
- }
-
- public boolean defaultHandleEvent(Event var1) {
- this.defaultProcessEvent(new AWTEvent(var1));
- return false;
- }
-
- public void defaultProcessEvent(java.awt.AWTEvent var1) {
- super.processEvent(var1);
- }
-
- public void windowClosed(WindowEvent var1) {
- }
-
- public void windowOpened(WindowEvent var1) {
- }
-
- public void windowClosing(WindowEvent var1) {
- Object var2 = ((EventObject)var1).getSource();
- if (var2 == this) {
- this.Form_SortableVector_windowClosing(var1);
- } else {
- this.unhandledEvent("java.awt.event.WindowListener", "windowClosing", var1);
- }
-
- }
-
- public void windowIconified(WindowEvent var1) {
- }
-
- public void windowActivated(WindowEvent var1) {
- }
-
- public void windowDeiconified(WindowEvent var1) {
- }
-
- public void windowDeactivated(WindowEvent var1) {
- }
-
- public void actionPerformed(ActionEvent var1) {
- Object var2 = ((EventObject)var1).getSource();
- if (var2 == this.menu_SortableVector_AlphaSort) {
- this.menu_SortableVector_AlphaSort_actionPerformed(var1);
- } else if (var2 == this.menu_SortableVector_NameLengthSort) {
- this.menu_SortableVector_NameLengthSort_actionPerformed(var1);
- } else if (var2 == this.menu_SortableVector_Scramble) {
- this.menu_SortableVector_Scramble_actionPerformed(var1);
- } else if (var2 == this.menu_SortableVector_AddWaldo) {
- this.menu_SortableVector_AddWaldo_actionPerformed(var1);
- } else if (var2 == this.menu_SortableVector_RemoveWaldo) {
- this.menu_SortableVector_RemoveWaldo_actionPerformed(var1);
- } else if (var2 == this.menu_SortableVector_FindWaldo) {
- this.menu_SortableVector_FindWaldo_actionPerformed(var1);
- } else if (var2 == this.menu_SortableVector_RemoveThirdName) {
- this.menu_SortableVector_RemoveThirdName_actionPerformed(var1);
- } else {
- this.unhandledEvent("java.awt.event.ActionListener", "actionPerformed", var1);
- }
-
- }
-
- public Form_SortableVector() {
- }
-
- public void unhandledEvent(String var1, String var2, Object var3) {
- }
-
- public void processEvent(java.awt.AWTEvent var1) {
- this.defaultProcessEvent(var1);
- }
-
- public Container getContentPane() {
- return this;
- }
-
- void loadVector() {
- this._nameVector.clear();
-
- for(int var1 = 0; var1 < 28; ++var1) {
- this._nameVector.append(this._nameList[var1]);
- }
-
- }
-
- public int sequentialSearch(String var1) {
- int var3 = 0;
-
- for(Enumeration var2 = this._nameVector.getElements(); var2.hasMoreElements(); ++var3) {
- String var4 = (String)var2.nextElement();
- if (var4.equals(var1)) {
- return var3;
- }
- }
-
- return -1;
- }
-
- public void updateListbox() {
- this.lb_nameList.clear();
- Enumeration var1 = this._nameVector.getElements();
-
- while(var1.hasMoreElements()) {
- String var2 = (String)var1.nextElement();
- this.lb_nameList.addItem(var2);
- }
-
- }
-
- public boolean Form_SortableVector_objectCreated(EventData var1) {
- this.loadVector();
- this.updateListbox();
- return false;
- }
-
- public void Form_SortableVector_windowClosing(WindowEvent var1) {
- ((Component)this).setVisible(false);
- this.destroy();
- }
-
- public void menu_SortableVector_AlphaSort_actionPerformed(ActionEvent var1) {
- CompareRoutine var2 = StringUtility.getCompareRoutine(false);
- this._nameVector.setCompareRoutine(var2);
- this._sortType = "alpha";
- this._nameVector.sort();
- this.updateListbox();
- }
-
- public void menu_SortableVector_NameLengthSort_actionPerformed(ActionEvent var1) {
- this._nameVector.setCompareRoutine(new StringLengthSort());
- this._sortType = "namelength";
- this._nameVector.sort();
- this.updateListbox();
- }
-
- public void menu_SortableVector_Scramble_actionPerformed(ActionEvent var1) {
- this._nameVector = new SortableVector();
- this.loadVector();
- this.updateListbox();
- this._sortType = "none";
- }
-
- public void menu_SortableVector_AddWaldo_actionPerformed(ActionEvent var1) {
- if (this._sortType.equals("none")) {
- this._nameVector.insert((int)(Math.random() * (double)this._nameVector.getCount()), "Waldo");
- } else {
- this._nameVector.insert("Waldo");
- }
-
- this.updateListbox();
- }
-
- public void menu_SortableVector_RemoveWaldo_actionPerformed(ActionEvent var1) {
- int var2 = -1;
- if (!this._sortType.equals("alpha")) {
- var2 = this.sequentialSearch("Waldo");
- } else {
- var2 = this._nameVector.binarySearch("Waldo");
- }
-
- if (var2 > -1) {
- this._nameVector.removeAt(var2);
- this.updateListbox();
- }
-
- }
-
- public void menu_SortableVector_FindWaldo_actionPerformed(ActionEvent var1) {
- int var2 = 0;
- if (!this._sortType.equals("alpha")) {
- var2 = this.sequentialSearch("Waldo") + 1;
- } else {
- var2 = this._nameVector.binarySearch("Waldo") + 1;
- }
-
- if (var2 != 0) {
- this.textf_messages.setText("Waldo found at position " + var2 + ".");
- } else {
- this.textf_messages.setText("Waldo not found.");
- }
-
- }
-
- public void menu_SortableVector_RemoveThirdName_actionPerformed(ActionEvent var1) {
- if (this._nameVector.getCount() > 2) {
- this._nameVector.removeAt(2);
- this.updateListbox();
- }
-
- }
- }
-