home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-08-27 | 6.0 KB | 207 lines |
- /*
- A basic extension of the java.awt.Dialog class
- */
-
- import java.awt.*;
-
- public class FindDialog extends Dialog {
- void cancelButton_Clicked(java.awt.event.ActionEvent event) {
- // to do: place event handler code here.
- statuslabel.setText("");
- hide();
- dispose();
- }
-
- void findButton_Clicked(java.awt.event.ActionEvent event, boolean has_dialog) {
-
- int suspect_length;
- int foundit_index;
- String found_string;
-
- m_findme = findText.getText();
- suspect_length = m_findme.length();
- foundit_index = m_body_string.indexOf(m_findme, m_current_index);
- if(foundit_index == -1) {
- if(has_dialog) {
- statuslabel.setText("End of File");
- statuslabel.setVisible(true);
- }
- }
- else {
- if(m_MatchCase.getState()) {
- found_string = m_body_string.substring(foundit_index, foundit_index + suspect_length);
- if(found_string.equals(m_findme)) {
- if(has_dialog) {
- statuslabel.setText("Found");
- statuslabel.setVisible(true);
- }
- m_body.select(foundit_index, foundit_index + suspect_length);
- m_current_index = foundit_index + suspect_length;
- }
- }
- else {
- statuslabel.setText("Found");
- statuslabel.setVisible(true);
- m_body.select(foundit_index, foundit_index + suspect_length);
- m_current_index = foundit_index + suspect_length;
- }
- }
- }
-
-
- public void setBody(TextArea thebody)
- {
- m_body = thebody;
- m_body_string = m_body.getText();
- }
-
- public void setSuspect(String thesuspect)
- {
- m_findme = thesuspect;
- findText.setText(m_findme);
- }
-
- public void setStart(int start)
- {
- m_current_index = start;
- }
-
- public boolean matchCase()
- {
- return m_MatchCase.getState();
- }
-
- public String getFindString()
- {
- return findText.getText();
- }
-
- public void addNotify()
- {
- super.addNotify();
-
- if (fComponentsAdjusted)
- return;
-
- // Adjust components according to the insets
- setSize(insets().left + insets().right + getSize().width, insets().top + insets().bottom + getSize().height);
- Component components[] = getComponents();
- for (int i = 0; i < components.length; i++)
- {
- Point p = components[i].getLocation();
- p.translate(insets().left, insets().top);
- components[i].setLocation(p);
- }
- fComponentsAdjusted = true;
- }
-
- boolean fComponentsAdjusted = false;
-
- public FindDialog(Frame parent, boolean modal) {
-
- super(parent, modal);
-
- //{{INIT_CONTROLS
- setLayout(null);
- setVisible(false);
- setSize(insets().left + insets().right + 297,insets().top + insets().bottom + 98);
- setBackground(new Color(12632256));
- label2 = new java.awt.Label("Find What");
- label2.setBounds(insets().left + 12,insets().top + 12,80,16);
- add(label2);
- findText = new java.awt.TextField();
- findText.setBounds(insets().left + 12,insets().top + 36,190,20);
- add(findText);
- findText.setCursor(new Cursor(Cursor.TEXT_CURSOR));
- findButton = new java.awt.Button();
- findButton.setLabel("Find");
- findButton.setBounds(insets().left + 216,insets().top + 12,70,20);
- add(findButton);
- cancelButton = new java.awt.Button();
- cancelButton.setLabel("Cancel");
- cancelButton.setBounds(insets().left + 216,insets().top + 36,70,20);
- cancelButton.setFont(new Font("Dialog", Font.PLAIN, 12));
- add(cancelButton);
- statuslabel = new java.awt.Label("");
- statuslabel.setVisible(false);
- statuslabel.setBounds(insets().left + 168,insets().top + 60,120,24);
- add(statuslabel);
- m_MatchCase = new java.awt.Checkbox("Match case");
- m_MatchCase.setBounds(insets().left + 12,insets().top + 60,84,20);
- m_MatchCase.setBackground(new Color(12632256));
- add(m_MatchCase);
- setTitle("Find");
- setResizable(false);
- //}}
-
- //{{REGISTER_LISTENERS
- Action lAction = new Action();
- findButton.addActionListener(lAction);
- cancelButton.addActionListener(lAction);
- //}}
- }
-
- public FindDialog(JavaPad parent, String title, boolean modal, String findString) {
- this(parent, modal);
- setTitle(title);
- findText.setText(findString);
- }
-
- public synchronized void show() {
- Rectangle bounds = getParent().bounds();
- Rectangle abounds = bounds();
-
- move(bounds.x + (bounds.width - abounds.width)/ 2,
- bounds.y + (bounds.height - abounds.height)/2);
-
- super.show();
- }
- //-------------------------------------------------------------
- // This file has been migrated from the 1.0 to 1.1 event model.
- // This method is not used with the new 1.1 event model. You can
- // move any code you need to keep, then remove this method.
- //-------------------------------------------------------------
- //
- // public boolean handleEvent(Event event) {
- // if(event.id == Event.WINDOW_DESTROY) {
- // hide();
- // return true;
- // }
- // if (event.target == findButton && event.id == Event.ACTION_EVENT) {
- // findButton_Clicked(event);
- // }
- // if (event.target == cancelButton && event.id == Event.ACTION_EVENT) {
- // cancelButton_Clicked(event);
- // }
- // return super.handleEvent(event);
- // }
- //-------------------------------------------------------------
-
- //{{DECLARE_CONTROLS
- java.awt.Label label2;
- java.awt.TextField findText;
- java.awt.Button findButton;
- java.awt.Button cancelButton;
- java.awt.Label statuslabel;
- java.awt.Checkbox m_MatchCase;
- //}}
-
- TextArea m_body;
- String m_body_string;
- String m_findme;
- int m_current_index;
-
-
- class Action implements java.awt.event.ActionListener
- {
- public void actionPerformed(java.awt.event.ActionEvent event)
- {
- Object object = event.getSource();
- if (object == findButton)
- findButton_Clicked(event, true);
- else if (object == cancelButton)
- cancelButton_Clicked(event);
- }
- }
- }
-