Text editor

mail@pastecode.io avatar
unknown
java
24 days ago
4.2 kB
2
Indexable
Never
1. TextEditor Interface
java
Copy code
public interface TextEditor {
    void openDocument(String filePath);
    void saveDocument(String filePath);
    void formatText(FormattingOptions options);
    void insertText(String text);
    void deleteText(int start, int end);
    String getText();
}
2. Document Interface
java
Copy code
public interface Document {
    void load(String filePath);
    void save(String filePath);
    String getContent();
    void setContent(String content);
}
3. Formatting Interface
java
Copy code
public interface Formatting {
    void applyFormatting(FormattingOptions options);
}
4. FormattingOptions Class
java
Copy code
public class FormattingOptions {
    private boolean bold;
    private boolean italic;
    private boolean underline;

    // Constructors, getters, setters
    public FormattingOptions(boolean bold, boolean italic, boolean underline) {
        this.bold = bold;
        this.italic = italic;
        this.underline = underline;
    }

    public boolean isBold() { return bold; }
    public void setBold(boolean bold) { this.bold = bold; }

    public boolean isItalic() { return italic; }
    public void setItalic(boolean italic) { this.italic = italic; }

    public boolean isUnderline() { return underline; }
    public void setUnderline(boolean underline) { this.underline = underline; }
}
5. Concrete Implementations
TextDocument Class
java
Copy code
public class TextDocument implements Document {
    private String content;

    @Override
    public void load(String filePath) {
        // Simulate loading document from file
        this.content = "Sample Document Content";
    }

    @Override
    public void save(String filePath) {
        // Simulate saving document to file
        System.out.println("Document saved to: " + filePath);
    }

    @Override
    public String getContent() {
        return content;
    }

    @Override
    public void setContent(String content) {
        this.content = content;
    }
}
BasicTextEditor Class
java
Copy code
public class BasicTextEditor implements TextEditor {
    private Document document;
    private Formatting formatting;

    public BasicTextEditor(Document document, Formatting formatting) {
        this.document = document;
        this.formatting = formatting;
    }

    @Override
    public void openDocument(String filePath) {
        document.load(filePath);
    }

    @Override
    public void saveDocument(String filePath) {
        document.save(filePath);
    }

    @Override
    public void formatText(FormattingOptions options) {
        formatting.applyFormatting(options);
    }

    @Override
    public void insertText(String text) {
        String currentContent = document.getContent();
        document.setContent(currentContent + text);
    }

    @Override
    public void deleteText(int start, int end) {
        String currentContent = document.getContent();
        if (start >= 0 && end <= currentContent.length()) {
            document.setContent(currentContent.substring(0, start) + currentContent.substring(end));
        }
    }

    @Override
    public String getText() {
        return document.getContent();
    }
}
TextFormatting Class
java
Copy code
public class TextFormatting implements Formatting {
    @Override
    public void applyFormatting(FormattingOptions options) {
        // Simulate applying formatting
        System.out.println("Formatting applied: Bold=" + options.isBold() +
                           ", Italic=" + options.isItalic() +
                           ", Underline=" + options.isUnderline());
    }
}
6. Main Class
java
Copy code
public class Main {
    public static void main(String[] args) {
        Document document = new TextDocument();
        Formatting formatting = new TextFormatting();
        TextEditor editor = new BasicTextEditor(document, formatting);

        editor.openDocument("example.txt");
        editor.insertText("Hello, World!");
        FormattingOptions options = new FormattingOptions(true, false, false);
        editor.formatText(options);
        System.out.println("Current Document Text: " + editor.getText());
        editor.saveDocument("example.txt");
    }
}
Leave a Comment