summaryrefslogtreecommitdiff
path: root/samples/textEditor/src/myDocumentFiles/MyVisualPanel.java
blob: 2a627a05af2af6821e4491af98aed77166dea9bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package myDocumentFiles;

import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

/**
 * Created by IntelliJ IDEA.
 * User: Alexey.Chursin
 * Date: Aug 30, 2010
 * Time: 1:42:38 PM
 */
public class MyVisualPanel extends DialogWrapper {
    private JTabbedPane viewFileTab;
    private JTextField fileName;
    private JButton buttonBrowse;
    private JTextField directoryName;
    private JFileChooser fileChooser = new JFileChooser("");
    private VirtualFile selectedVFile;
    private Document docFile;
    // Set maximum allowed number of lines in a text file to edit.
    private final int maxNumberofLines = 250;


    private JPanel myVisualUI;
    private JEditorPane myFileEditor;
    private JPanel viewFilePanel;
    private String initialFileContent = null;
    private boolean nFocus;


    public MyVisualPanel(boolean canBeParent) {
        super(canBeParent);
        init();


        // The Browse button listener.
        buttonBrowse.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                if (OpenFile()) {
                    if (docFile.getLineCount() > maxNumberofLines) {
                        Messages.showMessageDialog("File too long! Maximum allowed number of lines: " +
                                String.valueOf(maxNumberofLines) + ".\n"+
                                "To change this setting, modify the MyVisualPanel.maxNumberofLines field.", "Error", Messages.getErrorIcon());
                        return;
                    }
                    // Read the text file content.
                    initialFileContent = docFile.getText();
                    // Enable the View File tab
                    viewFileTab.setEnabled(true);
                    // Fill the "File name" and "Directory" text fields.
                    fileName.setText(selectedVFile.getName());
                    directoryName.setText(selectedVFile.getParent().getUrl());
                    myFileEditor.setText(initialFileContent);
                }


            }
        });

        // The file editor focus listener.
        myFileEditor.addFocusListener(new FocusAdapter() {
            public void focusGained(FocusEvent e) {
                if (!docFile.isWritable() && nFocus) {
                    nFocus = false;
                    Messages.showMessageDialog("This file is read-only. You cannot save your changes.", "Warning",
                            Messages.getWarningIcon());
                    return;

                } else return;
            }
        });
    }

    // Display the Open dialog and open the selected file.
    public boolean OpenFile() {
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Text files", "txt");
        fileChooser.setFileFilter(filter);
        //... Open a file dialog.
        int retval = fileChooser.showOpenDialog(null);
        if (retval == JFileChooser.APPROVE_OPTION) {
            // Get virtual file
            selectedVFile = LocalFileSystem.getInstance().findFileByIoFile(fileChooser.getSelectedFile());
            // Get document file
            docFile = FileDocumentManager.getInstance().getDocument(selectedVFile);
            nFocus = true;

            return true;
        }
        viewFilePanel.setEnabled(false);
        return false;
    }


    public JComponent createCenterPanel() {

        return (JComponent) myVisualUI;

    }
      // The OK button handler.
    protected void doOKAction() {
        if (initialFileContent == null) {
            this.close(0);
            return;
        }
        if (!initialFileContent.equals(myFileEditor.getText())) {
            if (Messages.showYesNoDialog("The file " + selectedVFile.getName() +
                    " has been changed. Are you sure you want to overwrite it?", "File Changed", Messages.getQuestionIcon()) == 0) {
                if (docFile.isWritable()) {
                    docFile.setText(myFileEditor.getText());
                } else {
                    Messages.showMessageDialog("This file is read-only! You cannot save your changes.", "Error", Messages.getErrorIcon());
                    return;
                }


            } else return;
        }

        this.close(0);
        this.dispose();


    }

}