[Jreepad-CVS] jreepad/src/jreepad/editor ArticleView.java, NONE, 1.1 AbstractArticleView.java, NONE
Brought to you by:
danstowell
From: PeWu <pe...@us...> - 2007-02-07 20:37:36
|
Update of /cvsroot/jreepad/jreepad/src/jreepad/editor In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv25085/src/jreepad/editor Modified Files: PlainTextEditor.java Added Files: ArticleView.java AbstractArticleView.java Log Message: refactoring: Started new ArticleView interface. Moved PlainTextEditor to new interface --- NEW FILE: AbstractArticleView.java --- package jreepad.editor; import jreepad.JreepadArticle; public abstract class AbstractArticleView implements ArticleView { protected JreepadArticle article; // The following boolean should be TRUE while we're changing from node to // node, and false otherwise protected boolean editLocked = false; public AbstractArticleView(JreepadArticle article) { this.article = article; } public void lockEdits() { editLocked = true; } public void unlockEdits() { editLocked = false; } public void saveArticle() { article.setContent(getText()); } public void setArticle(JreepadArticle article) { this.article = article; reloadArticle(); } } Index: PlainTextEditor.java =================================================================== RCS file: /cvsroot/jreepad/jreepad/src/jreepad/editor/PlainTextEditor.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** PlainTextEditor.java 7 Feb 2007 17:39:16 -0000 1.2 --- PlainTextEditor.java 7 Feb 2007 20:16:20 -0000 1.3 *************** *** 20,23 **** --- 20,24 ---- package jreepad.editor; + import javax.swing.JComponent; import javax.swing.JEditorPane; import javax.swing.event.CaretEvent; *************** *** 45,82 **** * The plain text editor pane. */ ! public class PlainTextEditor extends JEditorPane implements CaretListener, UndoableEditListener { ! private JreepadArticle article; private ContentChangeListener contentChangeListener = null; - // The following boolean should be TRUE while we're changing from node to - // node, and false otherwise - private boolean editLocked = false; - public PlainTextEditor(JreepadArticle article) { ! super("text/plain", article.getContent()); ! this.article = article; ! setEditable(true); if (getPrefs().wrapToWindow) ! setEditorKit(new JPEditorKit()); // Add a listener to make sure the editorpane's content is always stored // when it changes ! addCaretListener(this); ! getDocument().addUndoableEditListener(this); } public void insertText(String text) { ! Document doc = getDocument(); ! int here = getCaretPosition(); try { ! setText(doc.getText(0, here) + text + doc.getText(here, doc.getLength() - here)); ! setCaretPosition(here + text.length()); } catch (BadLocationException e) --- 46,79 ---- * The plain text editor pane. */ ! public class PlainTextEditor extends AbstractArticleView implements CaretListener, UndoableEditListener { ! private JEditorPane editorPane; private ContentChangeListener contentChangeListener = null; public PlainTextEditor(JreepadArticle article) { ! super(article); ! editorPane = new JEditorPane("text/plain", article.getContent()); ! editorPane.setEditable(true); if (getPrefs().wrapToWindow) ! editorPane.setEditorKit(new JPEditorKit()); // Add a listener to make sure the editorpane's content is always stored // when it changes ! editorPane.addCaretListener(this); ! editorPane.getDocument().addUndoableEditListener(this); } public void insertText(String text) { ! Document doc = editorPane.getDocument(); ! int here = editorPane.getCaretPosition(); try { ! editorPane.setText(doc.getText(0, here) + text + doc.getText(here, doc.getLength() - here)); ! editorPane.setCaretPosition(here + text.length()); } catch (BadLocationException e) *************** *** 88,113 **** public void reloadArticle() { ! setText(article.getContent()); } ! public void setArticle(JreepadArticle article) ! { ! this.article = article; ! setText(article.getContent()); ! } ! ! public void lockEdits() { ! editLocked = true; } ! public void unlockEdits() { ! editLocked = false; } ! public boolean isEditLocked() { ! return editLocked; } --- 85,104 ---- public void reloadArticle() { ! editorPane.setText(article.getContent()); } ! public JComponent getComponent() { ! return editorPane; } ! public String getText() { ! return editorPane.getText(); } ! public String getSelectedText() { ! return editorPane.getSelectedText(); } *************** *** 127,132 **** try { ! String text = getText(); ! int startpos = getCaretPosition(); int endpos = startpos; if (text.length() > 0) --- 118,123 ---- try { ! String text = editorPane.getText(); ! int startpos = editorPane.getCaretPosition(); int endpos = startpos; if (text.length() > 0) *************** *** 134,145 **** // Select the character before/after the current position, and // grow it until we hit whitespace... ! while (startpos > 0 && !Character.isWhitespace(getText(startpos - 1, 1).charAt(0))) startpos--; ! while (endpos < (text.length()) && !Character.isWhitespace(getText(endpos, 1).charAt(0))) endpos++; if (endpos > startpos) { ! setSelectionStart(startpos); ! setSelectionEnd(endpos); return getSelectedText(); } --- 125,136 ---- // Select the character before/after the current position, and // grow it until we hit whitespace... ! while (startpos > 0 && !Character.isWhitespace(editorPane.getText(startpos - 1, 1).charAt(0))) startpos--; ! while (endpos < (text.length()) && !Character.isWhitespace(editorPane.getText(endpos, 1).charAt(0))) endpos++; if (endpos > startpos) { ! editorPane.setSelectionStart(startpos); ! editorPane.setSelectionEnd(endpos); return getSelectedText(); } *************** *** 159,167 **** return; // i.e. we are only relevant when in plain-text mode ! if (!getText().equals(article.getContent())) { // System.out.println("UPDATE - I'd now overwrite node content with // editorpane content"); ! article.setContent(getText()); notifyContentChangeListener(); } --- 150,158 ---- return; // i.e. we are only relevant when in plain-text mode ! if (!editorPane.getText().equals(article.getContent())) { // System.out.println("UPDATE - I'd now overwrite node content with // editorpane content"); ! article.setContent(editorPane.getText()); notifyContentChangeListener(); } --- NEW FILE: ArticleView.java --- package jreepad.editor; import javax.swing.JComponent; import jreepad.JreepadArticle; /** * Interface for an article viewer or editor. * * @author <a href="mailto:pe...@lo...">Przemek WiÄch</a> * @version $Id: ArticleView.java,v 1.1 2007/02/07 20:16:20 pewu Exp $ */ public interface ArticleView { /** * Returns the GUI viewer/editor component. */ public JComponent getComponent(); /** * Returns selected text in view. */ public String getSelectedText(); /** * Reloads the article. * Transfers data from article to view. */ public void reloadArticle(); /** * Saves the current article. * Transfers data from view to article. */ public void saveArticle(); /** * Returns the content text. */ public String getText(); /** * Sets new article content. */ public void setArticle(JreepadArticle article); /** * Lock editor - no edits will be allowed. */ public void lockEdits(); /** * Unlock editor - edits will now be allowed. */ public void unlockEdits(); } |