import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; import javax.swing.text.*; import java.util.*; /*ssssssssssssssssssssssssssssssssssssssssssssssssssssssss 100% Pure Javaテキストエディター CoffeeMaker.java k.suzuki 2000.5 ssssssssssssssssssssssssssssssssssssssssssssssssssssssss*/ public class CoffeeMaker extends SwingBaseWindow2 { /* 変数一覧 */ int tabsize; //タブの大きさを格納します(全角)。 String filename; //使用しているファイル名を格納 まだ実際の動作はせず String title = "CoffeeMaker"; public static void main(String[] args) { CoffeeMaker w = new CoffeeMaker("CoffeeMaker", 0); Profiles profs = new Profiles(); w.setLocation(profs.locateLoadlocx(), profs.locateLoadlocy()); w.setSize(profs.locateLoad(), profs.locateLoady()); w.setVisible(true); } /*内部クラス集*/ class Save implements ActionListener { JFrame frame; JTextArea textArea; Save( JFrame frame, JTextArea textArea ) { this.frame = frame; this.textArea = textArea; } public void actionPerformed(ActionEvent evt) { JFileChooser fileChooser = new JFileChooser("."); int ret = fileChooser.showSaveDialog(frame); File file = fileChooser.getSelectedFile(); if(ret != JFileChooser.APPROVE_OPTION || file == null) return; frame.setTitle(title + "-" + file.getName()); try { FileWriter writer = new FileWriter(file); textArea.write(writer); writer.close(); } catch(FileNotFoundException ex) {} catch(IOException ex) {} } } class Load implements ActionListener { JFrame frame; JTextArea textArea; Load( JFrame frame, JTextArea textArea ) { this.frame = frame; this.textArea = textArea; } public void actionPerformed(ActionEvent evt) { JFileChooser fileChooser = new JFileChooser("."); int ret = fileChooser.showOpenDialog(frame); File file = fileChooser.getSelectedFile(); if( ret != JFileChooser.APPROVE_OPTION || file==null) return; /*ファイル名の処理をしています。*/ filename = file.getName(); frame.setTitle(title + "-" + file.getName()); /*ファイル名の処理・終了*/ try { FileReader reader = new FileReader(file); textArea.read(reader, new PlainDocument()); } catch(FileNotFoundException ex) {} catch(IOException ex) {} textArea.setTabSize(tabsize); } } class Exit implements ActionListener { public void actionPerformed(ActionEvent evt) { goodBye(); System.exit(0); } } class TabDialog implements ActionListener { JFrame frame; JTextArea textArea; TabDialog( JFrame frame, JTextArea textArea ) { this.frame = frame; this.textArea = textArea; } String tatxt; //テキストエリアのテキストを格納 String msg = "タブサイズを設定できます。"; ButtonGroup bg = new ButtonGroup(); JRadioButton rb1 = new JRadioButton("タブサイズ:2"); JRadioButton rb2 = new JRadioButton("タブサイズ:4"); JRadioButton rb3 = new JRadioButton("タブサイズ:6"); JRadioButton rb4 = new JRadioButton("タブサイズ:8"); public void actionPerformed (ActionEvent evt) { tatxt = textArea.getText(); bg.add(rb1); bg.add(rb2); bg.add(rb3); bg.add(rb4); Object[] obj = {rb1, rb2, rb3, rb4}; JOptionPane.showConfirmDialog(frame, obj, msg, JOptionPane.OK_CANCEL_OPTION); if (rb1.isSelected()) { tabsize = 2; } else if (rb2.isSelected()) { tabsize = 4; } else if (rb3.isSelected()) { tabsize = 6; } else if (rb4.isSelected()) { tabsize = 8; } else tabsize = 4; textArea.setTabSize(tabsize); //実はタブサイズはキチンと変更されていたらしい textArea.setText(tatxt); //再描画させる為に設定(もっとスリムな方法を模索する事) System.out.println("タブサイズを"+tabsize+"に変更しました"); //お決まりの変更時の変数外部出力 } } /*ウィンドウの位置座標等を獲得し、Profilesクラスに渡すメソッドです*/ public void getWindowLocation() { int x; int y; int endx; int endy; x = getX(); y = getY(); endx = getWidth(); endy = getHeight(); System.out.println("保存座標を表示します"); System.out.println("始点x座標="+x); System.out.println("始点y座標="+y); System.out.println("終点x座標="+endx); System.out.println("終点y座標="+endy); System.out.println("タブサイズ="+tabsize); Profiles profs = new Profiles(); profs.locateSave(x,y,endx,endy,tabsize); } /*終了時の動作を設定しています。*/ public void goodBye() { getWindowLocation(); } /*起動時の初期設定用メソッドです。*/ CoffeeMaker(String title, int ui) { super(title, ui); JTextArea textArea = new JTextArea(); JScrollPane textPane = new JScrollPane(textArea); Profiles profs = new Profiles(); tabsize = profs.tabSizeLoad(); textArea.setTabSize(tabsize); textArea.setLineWrap(true); pane.add(textPane); addMenu("ファイル"); // addMenuItem("新規作成",); //拡張予定 addMenuItem("保存", new Save(this, textArea)); addMenuItem("読込", new Load(this, textArea)); // addSeparator(); addMenuItem("終了", new Exit()); /*各設定 拡張予定*/ addMenu("設定"); addMenuItem("タブ設定", new TabDialog(this, textArea)); } } /*ssssssssssssssssssssssssssssssssssssssssssssssssssssssss Profiles クラス 2000.5 k.suzuki 機能概要 ハッシュテーブルの機能を元に、CoffeeMakerに必要な各プロパティを 保存・読込します。 読込動作…***Load()メソッド 各プロパティを個別に読み込んでいます。 書出し動作…locateSaveメソッド CoffeeMakerクラスで読み出したプロパティを全て保存します。 setting.iniというファイルに保存しています。 ssssssssssssssssssssssssssssssssssssssssssssssssssssssss*/ class Profiles extends Properties { void locateSave (int x, int y, int endx, int endy, int tabsize) { String string = new String(); Properties profile = new Properties(); profile.setProperty("locx",string.valueOf(x)); profile.setProperty("locy",string.valueOf(y)); profile.setProperty("width",string.valueOf(endx)); profile.setProperty("height",string.valueOf(endy)); profile.setProperty("tab",string.valueOf(tabsize)); try { profile.store(new FileOutputStream("setting.ini"),null); }catch(IOException err) {} } int locateLoad () { Properties profile = new Properties(); try { profile.load(new FileInputStream("setting.ini")); }catch(IOException err) {} String strx = profile.getProperty("width","100"); Integer integer = new Integer(strx); int x = integer.parseInt(strx,10); System.out.println("xの値は、"+x); return x; } int locateLoady () { Properties profile = new Properties(); try { profile.load(new FileInputStream("setting.ini")); }catch(IOException err) {} String strx = profile.getProperty("height","100"); Integer integer = new Integer(strx); int y = integer.parseInt(strx,10); System.out.println("yの値は、"+y); return y; } int locateLoadlocx () { Properties profile = new Properties(); try { profile.load(new FileInputStream("setting.ini")); }catch(IOException err) {} String strx = profile.getProperty("locx","100"); Integer integer = new Integer(strx); int locx = integer.parseInt(strx,10); System.out.println("locxの値は、"+locx); return locx; } int locateLoadlocy () { Properties profile = new Properties(); try { profile.load(new FileInputStream("setting.ini")); }catch(IOException err) {} String strx = profile.getProperty("locy","100"); Integer integer = new Integer(strx); int locy = integer.parseInt(strx,10); System.out.println("locyの値は、"+locy); return locy; } /*目標:keyの値と出力する変数名を入力することで、valueを取り出したい int locateLoad (String key, String name) { Properties profile = new Properties(); int name = new int(); try { profile.load(new FileInputStream("setting.ini")); }catch(IOException err) {} String strx = profile.getProperty("key","100"); Integer integer = new Integer(strx); int name = integer.parseInt(strx,10); System.out.println(name+"の値は、"+name); return name; } */ int tabSizeLoad () { Properties profile = new Properties(); try { profile.load(new FileInputStream("setting.ini")); }catch(IOException err) {} String strx = profile.getProperty("tab","8"); Integer integer = new Integer(strx); int tabsize = integer.parseInt(strx,10); System.out.println("タブサイズは"+tabsize); return tabsize; } void tabSizeSave (int tabsize) { Properties profile = new Properties(); String string = new String(); profile.setProperty("tab",string.valueOf(tabsize)); try { profile.store(new FileOutputStream("setting.ini"),null); }catch(IOException err) {} } }