@Override public void actionPerformed(ActionEvent e) { if (td.getTabCount() > 0) { TextDocument ta = (TextDocument) td.getComponentAt(td.getSelectedIndex()); Pattern pn = Pattern.compile(tf1.getText()); Matcher mt = pn.matcher(ta.getText()); if (e.getSource() == jb2) { // 取代 ta.setText(mt.replaceAll(tf2.getText())); } else if (e.getSource() == jb1) { // 尋找 Highlighter hl = ta.getHighlighter(); hl.removeAllHighlights(); while (mt.find()) { try { hl.addHighlight( mt.start(), mt.end(), new DefaultHighlighter.DefaultHighlightPainter(null)); } catch (Exception ex) { } } // 開啟及關閉介面 } else if (e.getSource() == replace_searchMenuItem) { System.out.println("Replace/Search is show:" + !show); if (show) { getContentPane().remove(jp); show = false; } else { getContentPane().add(jp, BorderLayout.SOUTH); show = true; } validate(); // 刷新容器 } } else if (e.getSource() == replace_searchMenuItem) { JOptionPane.showMessageDialog( null, "尚無檔案,無法使用!", "Repace/Search error", JOptionPane.ERROR_MESSAGE); } }
public void save(int i) { BufferedWriter brw = null; TextDocument ta = (TextDocument) td.getComponentAt(i); try { File file = null; if (ta.file == null) { // 判斷是否為新黨案,是的話使用預設路徑儲存 file = new File(default_auto_save_path + "\\" + ta.fileName); } else { file = ta.file; // 儲存於原檔案 } brw = new BufferedWriter(new FileWriter(file)); ta.write(brw); ta.save = true; td.setTitleAt(i, ta.fileName); // 更新標題名稱 } catch (FileNotFoundException ffe) { // 若預設路徑不存在,則跳出警告 JOptionPane.showMessageDialog( null, ta.fileName + "儲存失敗!\n請檢查Default AutoSave-Path是否存在,或是權限不足.", "Save error", JOptionPane.ERROR_MESSAGE); } catch (Exception exc) { exc.printStackTrace(); } finally { try { brw.close(); } catch (Exception exc) { } } }
@Override public void actionPerformed(ActionEvent e) { if (td.getTabCount() > 0) { JFileChooser f = new JFileChooser(); f.setFileFilter(new MyFileFilter()); int choose = f.showSaveDialog(getContentPane()); if (choose == JFileChooser.APPROVE_OPTION) { BufferedWriter brw = null; try { File file = f.getSelectedFile(); brw = new BufferedWriter(new FileWriter(file)); int i = td.getSelectedIndex(); TextDocument ta = (TextDocument) td.getComponentAt(i); ta.write(brw); ta.fileName = file.getName(); // 將檔案名稱更新為存檔的名稱 td.setTitleAt(td.getSelectedIndex(), ta.fileName); ta.file = file; ta.save = true; // 設定已儲存 System.out.println("Save as pass!"); td.setTitleAt(i, ta.fileName); // 更新標題名稱 } catch (Exception exc) { exc.printStackTrace(); } finally { try { brw.close(); } catch (Exception ecx) { ecx.printStackTrace(); } } } } else { JOptionPane.showMessageDialog(null, "沒有檔案可以儲存!"); } }
@Override public void actionPerformed(ActionEvent e) { JFileChooser f = new JFileChooser(); f.setFileFilter(new MyFileFilter()); // 設定檔案選擇器 int choose = f.showOpenDialog(getContentPane()); // 顯示檔案選取 if (choose == JFileChooser.OPEN_DIALOG) { // 有開啟檔案的話,開始讀檔 BufferedReader br = null; try { File file = f.getSelectedFile(); br = new BufferedReader(new FileReader(file)); TextDocument ta = new TextDocument(file.getName(), file); ta.addKeyListener(new SystemTrackSave()); ta.read(br, null); td.add(ta); td.setTitleAt(docCount++, file.getName()); } catch (Exception exc) { exc.printStackTrace(); } finally { try { br.close(); } catch (Exception ecx) { ecx.printStackTrace(); } } } }
@Override public void actionPerformed(ActionEvent a) { String str = "NewDoc_" + (NewDocCount++) + ".txt"; TextDocument text = new TextDocument(str); text.addKeyListener(new SystemTrackSave()); // 登記文字編譯區動作監聽,用於提示是否存檔 td.add(text); // 將新黨案放入td,並設定其標題。 td.setTitleAt(docCount++, str); }
@Override public void keyTyped(KeyEvent e) { int i = td.getSelectedIndex(); TextDocument text = (TextDocument) td.getComponentAt(i); if (text.save) { td.setTitleAt(i, " *" + td.getTitleAt(i)); text.save = false; } }
@Override public void actionPerformed(ActionEvent e) { if (td.getTabCount() > 0) { TextDocument ta = (TextDocument) td.getComponentAt(td.getSelectedIndex()); if (e.getSource() == cutMenuItem) { ta.cut(); } else if (e.getSource() == pasteMenuItem) { ta.paste(); } else if (e.getSource() == copyMenutem) { ta.copy(); } } }