com.igormaznitsa.sciareto.ui.editors.TextEditor.java Source code

Java tutorial

Introduction

Here is the source code for com.igormaznitsa.sciareto.ui.editors.TextEditor.java

Source

/*
 * Copyright 2016 Igor Maznitsa.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.igormaznitsa.sciareto.ui.editors;

import java.io.File;
import java.io.IOException;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.swing.JComponent;
import javax.swing.JTextArea;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.filechooser.FileFilter;
import org.apache.commons.io.FileUtils;
import com.igormaznitsa.mindmap.model.logger.Logger;
import com.igormaznitsa.mindmap.model.logger.LoggerFactory;
import com.igormaznitsa.sciareto.Context;
import com.igormaznitsa.sciareto.ui.DialogProviderManager;
import com.igormaznitsa.sciareto.ui.SystemUtils;
import com.igormaznitsa.sciareto.ui.tabs.TabTitle;

public final class TextEditor extends AbstractScrollPane {

    private static final long serialVersionUID = -8551212562825517869L;

    private static final Logger LOGGER = LoggerFactory.getLogger(TextEditor.class);

    private final JTextArea editor;
    private final TabTitle title;

    private boolean ignoreChange;

    public static final FileFilter TXT_FILE_FILTER = new FileFilter() {

        @Override
        public boolean accept(@Nonnull final File f) {
            return f.isDirectory() || f.getName().endsWith(".txt");
        }

        @Override
        @Nonnull
        public String getDescription() {
            return "Text document (*.txt)";
        }
    };

    @Override
    @Nonnull
    public FileFilter getFileFilter() {
        return TXT_FILE_FILTER;
    }

    public TextEditor(@Nonnull final Context context, @Nullable File file) throws IOException {
        super();
        this.editor = new JTextArea();
        this.title = new TabTitle(context, this, file);

        this.editor.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(@Nonnull final DocumentEvent e) {
                if (!ignoreChange) {
                    title.setChanged(true);
                }
            }

            @Override
            public void removeUpdate(@Nonnull final DocumentEvent e) {
                if (!ignoreChange) {
                    title.setChanged(true);
                }
            }

            @Override
            public void changedUpdate(@Nonnull final DocumentEvent e) {
                if (!ignoreChange) {
                    title.setChanged(true);
                }
            }
        });

        setViewportView(this.editor);

        loadContent(file);
    }

    @Override
    public void loadContent(@Nullable final File file) throws IOException {
        this.ignoreChange = true;
        try {
            if (file != null) {
                this.editor.setText(FileUtils.readFileToString(file, "UTF-8"));
                this.editor.setCaretPosition(0);
            }
        } finally {
            this.ignoreChange = false;
        }
        this.revalidate();
    }

    @Override
    public boolean saveDocument() throws IOException {
        boolean result = false;
        if (this.title.isChanged()) {
            File file = this.title.getAssociatedFile();
            if (file == null) {
                file = DialogProviderManager.getInstance().getDialogProvider().msgSaveFileDialog("text-editor",
                        "Save Text document", null, true, getFileFilter(), "Save");
                if (file == null) {
                    return result;
                }
            }
            SystemUtils.saveUTFText(file, this.editor.getText());
            this.title.setChanged(false);
            result = true;
        } else {
            result = true;
        }
        return result;
    }

    @Override
    @Nonnull
    public TabTitle getTabTitle() {
        return this.title;
    }

    @Override
    @Nonnull
    public JComponent getMainComponent() {
        return this;
    }

}