TextEditor2.java
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
/** ファイルの編集を行う TextEditorクラス @Ver 1.1 */
public class TextEditor2 extends TextEditor {
/** トップレベルのウィンドウとなる Frameのオブジェクト */
public Frame toplevel;
/** 警告のためにポップアップする Dialogのオブジェクト */
public Dialog dialog;
/** 警告メッセージを表示する Labelのオブジェクト */
public Label dialogMessage;
/** 確認のための Buttonのオブジェクト */
public Button dialogOk, dialogCancel;
/** Panelのオブジェクト */
public Panel dialogPanel1, dialogPanel2;
/** Dialog のアクションを処理するアダプタのオブジェクト */
public DialogAdapter dialogAdapter;
/** コンストラクタ */
public TextEditor2( Frame toplevel ){
super(); // TextEditorの生成
this.toplevel = toplevel;
dialog = new Dialog( toplevel, "警告", true );
dialogPanel1 = new Panel();
dialogPanel2 = new Panel();
dialogMessage = new Label(
"ファイルが既に存在します。上書きしていいですか?" );
dialogPanel1.add( dialogMessage );
dialogOk = new Button( "実行" );
dialogCancel = new Button( "取り消し" );
dialogPanel2.add( dialogOk );
dialogPanel2.add( dialogCancel );
dialog.add( dialogPanel1, "North" );
dialog.add( dialogPanel2, "Center" );
dialog.pack();
dialogAdapter = new DialogAdapter();
dialogOk.addActionListener( dialogAdapter );
dialogCancel.addActionListener( dialogAdapter );
}
/** テキストのファイルへのセーブのメソッドの再定義 */
public void saveText() {
fileName = fileNameField.getText();
if ( fileName == null || fileName.equals( "" ) ) {
messageLabel.setText( "ファイル名が入力されていません" );
return;
}
try {
file = new File( fileName );
if( file.exists() )
dialog.show(); // 警告ダイアログを出す
else {
String text = textArea.getText();
FileSaver.saveText( file, text );
messageLabel.setText( fileName + "に保存しました" );
}
}
catch( Exception e ) {
messageLabel.setText( e.toString() );
}
}
/** テキストのファイルへの上書き保存のメソッド */
public void overWriteText() {
try {
String text = textArea.getText();
FileSaver.saveText( file, text );
messageLabel.setText( fileName + "に保存しました" );
}
catch( Exception e ) {
messageLabel.setText( e.toString() );
}
}
/** 単独で起動した時に呼び出されるメソッド */
public static void main( String argv[] ) {
ExitableFrame frame
= new ExitableFrame("Text Editor Ver 1.1");
TextEditor2 textEditor = new TextEditor2( frame );
frame.add( textEditor, "Center" );
frame.pack();
frame.show();
}
/** Dialogのアクションを処理する innerクラス */
class DialogAdapter implements ActionListener {
/** アクション処理のメソッド */
public void actionPerformed( ActionEvent evt ) {
if( (Button)evt.getSource() == dialogOk )
overWriteText();
dialog.setVisible( false );
}
} // DialogAdapterクラスの定義の終わり
} // TextEditor2クラスの定義の終わり