download Editor.java
Language: Java
License: GPL
LOC: 332
Project Info
Jipe
Server: SourceForge
Type: cvs
...\j\jipe\jipe\jipe\src\jipe\
   BoxComment.java
   BrowserLauncher.java
   ClassBrowser.java
   ClassFilter.java
   DeleteLine.java
   Editor.java
   ExtensionFilter.java
   Find.java
   FindInFiles.java
   HelpConsole.java
   HtmlFilter.java
   IDEOptions.java
   JarFileManager.java
   JavaFilter.java
   JavaOptions.java
   Jipe.java
   JipeCodeCompleter.java
   JipeCompiler.java
   JipeConsole.java
   JipeDecompiler.java
   JipeMethodFinder.java
   JipeProjectManager.java
   JipeUtilities.java
   JSBeautifier.java
   JSFormatter.java
   JSLineBreaker.java
   JspFilter.java
   LeftIndent.java
   ListAllFiles.java
   MasterFilter.java
   Print.java
   PrintManager.java
   ProjectFilter.java
   PropertiesManager.java
   RemoveSpaces.java
   RightIndent.java
   SimpleComment.java
   SpecialFileFilters.java
   StatusBar.java
   StringParser.java
   TextFilter.java
   ToLowerCase.java
   ToUpperCase.java
   WingComment.java

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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/**
 * Editor.java - Editor class for use with the Jipe Project Copyright (c) 
 * 1996-1999 Steve Lawson.  E-Mail steve@e-i-s.co.uk Latest version can be 
 * found at http://e-i-s.co.uk/jipe
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

package jipe;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.undo.*;
import java.net.URL;
import java.util.*;

import org.gjt.sp.jedit.syntax.*;
import org.gjt.sp.jedit.textarea.*;


public class Editor extends JTextArea
{
    private JSFormatter jsbeautifier;
    private CompoundEdit compoundEdit;
    protected static int num_windows = 0;
    protected static int window_num = 0;
    JEditTextArea textarea;
    protected UndoableEditListener undoHandler = new UndoHandler();

    /**
    * UndoManager that we add edits to.
    */
    protected UndoManager undo = new UndoManager();

    File file;
    String filename;
    String title;
    static boolean dontopen = false;
    String path;
    Jipe jipe;
    public Editor(Jipe parent, String str, File importfile)
    {
        super(str);
        jipe = parent;

        if(importfile!=null)file = importfile;
        else file=null;
        textarea=new JEditTextArea();
        textarea.setBackground(Color.white);
        textarea.getPainter().setFont(new Font(jipe.props.FONTSTYLE, 0, jipe.props.FONTSIZE));
        setTabSize(getTabSize());
        textarea.setEditable(true);
        if (str.startsWith("Html"))
            setDocumentStyle(".html");
        else if (str.startsWith("Java"))
            setDocumentStyle(".java");
        else if (str.startsWith("Untitled"))
            setDocumentStyle("");
        jsbeautifier=new JSFormatter();
        textarea.setRightClickPopup(jipe.popup);
        textarea.addCaretListener(new CaretHandler());
        textarea.getDocument().addUndoableEditListener(undoHandler);
        if(jipe.props.AUTOINDENT)textarea.setAutoIndentOn();
        	else textarea.setAutoIndentOff();

    }

    public String getTitle()
    {
        return (title);
    }
    public String setTitle(String setTitle)
    {
        title = setTitle;
        return title;
    }

    void SetActiveWindow()
    {
        jipe.activeChild = this;
        jipe.statusBar.msgline.setText("Current Window : " + this.getTitle());
        if (jipe.activeChild.getTitle().endsWith(".java"))
            jipe.speedbar.parse();
        Find.editor = textarea;
    }
    public void setDirtyTitle(){
        jipe.tabbedPane2.setForegroundAt(jipe.tabbedPane2.getSelectedIndex(), Color.red.darker());
    }
    void load(String filename)
    {
        StringBuffer sb = new StringBuffer();
        try
        {
            FileReader fis = new FileReader(file);
            BufferedReader br = new BufferedReader(fis);
            String str = br.readLine();
            while (str != null)
            {
                sb.append(str);
                sb.append("\n");
                str = br.readLine();
            }
            fis.close();
            setDocumentStyle(file.getName());
            setTitle(file.getName());
            textarea.setText(sb.toString());
        }
        catch (Exception err)
        {
        }
    }

    void close()
    {
        int selectedValue = 1;
        if (textarea.isDirty() == true)
            selectedValue = SaveConfirm(
                                this.getTitle(), "File not Saved, Save Now?");
        if (selectedValue == 0)
        {
            if (file != null)
                SaveFile(false);
            else
                SaveFile(true);
            selectedValue = 1;
        }
        if (selectedValue == 1)
        {
            String str;
            for (int i = 0; i < jipe.window.getItemCount(); i++)
            {
                if (jipe.window.getItem(i) == null)
                {
                    continue;
                }
                str = jipe.window.getItem(i).getText();
                if (str == this.getTitle())
                    jipe.window.remove(i);
            }
            jipe.children.removeElement((Object)this);
            jipe.tabbedPane2.removeTabAt(jipe.tabbedPane2.indexOfTab(
                                             this.getTitle()));
            if (jipe.children.isEmpty())
            {
                jipe.statusBar.msgline.setText("No current Window Set");
                jipe.activeChild = null;
                jipe.speedbar.removeList();
            }
            else
                jipe.GetWindow(new File(jipe.tabbedPane2.getTitleAt(
                                            jipe.tabbedPane2.getSelectedIndex())));
        }
    }
    public void SaveFile(boolean saveas)
    {
        boolean saveAs=saveas;
        if(file==null)saveAs=true;
        JFileChooser fileChooser = new JFileChooser(jipe.CURRENTDIR);
        if (saveAs)
        {
            fileChooser.addChoosableFileFilter(new TextFilter());
            fileChooser.addChoosableFileFilter(new HtmlFilter());
            fileChooser.addChoosableFileFilter(new JspFilter());
            fileChooser.setFileFilter(new JavaFilter());

            int selected = fileChooser.showSaveDialog(this);
            if (selected == JFileChooser.APPROVE_OPTION){
                file = fileChooser.getSelectedFile();
                fileChooser.setCurrentDirectory(file);
                jipe.CURRENTDIR=fileChooser.getCurrentDirectory();
            }
            else
                return;
        }
        try
        {
            jipe.statusBar.msgline.setText("Saving Document " + file.getName());
            FileWriter fos = new FileWriter(file.getPath());
            fos.write(this.textarea.getText());
            fos.close();
            jipe.statusBar.msgline.setText( file.getName()+" Saved.");
            textarea.clean();
            jipe.tabbedPane2.setForegroundAt(jipe.tabbedPane2.getSelectedIndex(), Color.black);

        }
        catch (Exception err)
        {
            jipe.statusBar.msgline.setText("Error : " + err);
        }
        if (saveAs){

            for (int i = 0; i < jipe.window.getItemCount(); i++)
            {

                if (jipe.window.getItem(i)==null)
                {
                    continue;
                }
                if (this.getTitle().equals(jipe.window.getItem(i).getText()))
                    jipe.window.remove(i);
            }
            jipe.children.removeElement((Object)this);
            jipe.tabbedPane2.setTitleAt(
                jipe.tabbedPane2.getSelectedIndex(), file.getName());
            setTitle(file.getName());
            jipe.addChild(jipe, this);
        }
    }
    int SaveConfirm(String confirmTitle, String confirmText)
    {
        int value;
        value = JOptionPane.showConfirmDialog(
                    this, confirmText, confirmTitle, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
        return value;
    }

    public void Goto()
    {
        String nameExtension = JOptionPane.showInputDialog("Goto Line:");

        if(nameExtension==null)System.out.println("");
        if ((nameExtension!=null)&&(nameExtension.trim().length() >0))
        {
            try
            {
                int lineno = Integer.valueOf(nameExtension).intValue() - 1;
                System.out.println(""+lineno );
                if (lineno <= textarea.getLineCount() && lineno>0)
                    gotoLine(lineno);
            }
            catch (NumberFormatException e)
            {
            }
        }
    }
    public void gotoLine(int line)
    {

        Element elemen3 = textarea.getDocument().getDefaultRootElement();
        Element elemen4 = elemen3.getElement(line);
        textarea.select(
            ((int)elemen4.getStartOffset()), elemen4.getEndOffset());
    }
    public int getTabSize()
    {
        int tabSize = jipe.props.TABSIZE;
        return tabSize;
    }

    /**
     * See getTabSize().
     * 
     * @param size The new tab size (in amount of spaces)
     */
    public void setTabSize(int size)
    {
        try
        {
            ((PlainDocument)textarea.getDocument()).putProperty(((PlainDocument)textarea.getDocument()).tabSizeAttribute, new Integer
                    (size));
            textarea.setTabSize(size);
        }
        catch (NumberFormatException nfe)
        {
            ((PlainDocument)textarea.getDocument()).putProperty(((PlainDocument)textarea.getDocument()).tabSizeAttribute, new Integer
                    (4));
            textarea.setTabSize(4);
        }
    }
  
    public void indent()
    {
        try{
            JSFormatter format = new JSFormatter();
            format.setBracketBreak(true);
            format.beautifier.setSpaceIndentation(getTabSize());
            format.init();
            Document doc = textarea.getDocument();
            Element map = doc.getDefaultRootElement();

            String line;
            int i = 0, start, end;
            StringBuffer buf = new StringBuffer(doc.getLength());
            try
            {
                while (true)
                {
                    while (!format.hasMoreFormattedLines())
                    {
                        if (i > map.getElementCount()) throw new NullPointerException();
                        Element lineElement = map.getElement(i);
                        start = lineElement.getStartOffset();
                        end = lineElement.getEndOffset() - 1;
                        end -= start;
                        line = textarea.getText(start, end);
                        if (line == null) throw new NullPointerException();
                        format.formatLine(line);
                        i++;
                    }
                    while (format.hasMoreFormattedLines())
                        buf.append(format.nextFormattedLine() + '\n');
                }
            } catch (NullPointerException npe) { }

            doc.remove(0, doc.getLength());
            doc.insertString(0, buf.toString(), null);

            format.summerize();
            while (format.hasMoreFormattedLines())
                doc.insertString(doc.getLength(), format.nextFormattedLine() + '\n', null);
        } catch (BadLocationException ble) { }
    }
    public void setDocumentStyle(String str)
    {
        if (str.toLowerCase().endsWith(".java") || str.toLowerCase().endsWith(
                    ".jav")) textarea.setTokenMarker(new JavaTokenMarker());
        else if (str.toLowerCase().endsWith(".htm") || str.toLowerCase().endsWith
                 (".html"))
            textarea.setTokenMarker(new HTMLTokenMarker());
        else if (str.toLowerCase().endsWith(".jsp"))
            textarea.setTokenMarker(new JSPTokenMarker());
        else if (str.toLowerCase().endsWith(".c") || str.toLowerCase().endsWith
                 (".cpp"))
            textarea.setTokenMarker(new CTokenMarker());
        else
            textarea.setTokenMarker(new TextTokenMarker());
    }
    class UndoHandler implements UndoableEditListener
    {

        /**
         * Messaged when the Document has created an edit, the edit is added 
         * to <code>undo</code>, an instance of UndoManager.
         */
        public void undoableEditHappened(UndoableEditEvent e)
        {
            undo.addEdit(e.getEdit());

            // undoAction.update();
            // redoAction.update();

        }
    }
    void redoAction()
    {
        try
        {
            undo.redo();
        }
        catch (CannotRedoException ex)
        {
            System.out.println("Unable to redo: " + ex);
            ex.printStackTrace();
        }
    }
    void undoAction()
    {
        try
        {
            undo.undo();
        }
        catch (CannotUndoException ex)
        {
            System.out.println("Unable to undo: " + ex);
            ex.printStackTrace();
        }
    }
    class CaretHandler implements CaretListener
    {
        public void caretUpdate(CaretEvent evt)
        {
            jipe.statusBar.updateStatus(textarea);
            if(textarea.isDirty() && jipe.tabbedPane2.getForegroundAt(jipe.tabbedPane2.getSelectedIndex())!=Color.red.darker())jipe.tabbedPane2.setForegroundAt(jipe.tabbedPane2.getSelectedIndex(), Color.red.darker());
            
        //    if(jipe.props.AUTOINDENT)textarea.setAutoIndentOn();
       //     	else textarea.setAutoIndentOff();
        }
    }
}

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us