PageRenderTime 119ms CodeModel.GetById 87ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/RubyPlugin/src/org/jedit/ruby/RubyPlugin.java

#
Java | 301 lines | 231 code | 45 blank | 25 comment | 49 complexity | e6ab3ec45e20a5162f698431d6132737 MD5 | raw file
  1/*
  2 * RubyPlugin.java - Ruby editor plugin for jEdit
  3 *
  4 * Copyright 2005 Robert McKinnon
  5 *
  6 * This program is free software; you can redistribute it and/or
  7 * modify it under the terms of the GNU General Public License
  8 * as published by the Free Software Foundation; either version 2
  9 * of the License, or any later version.
 10 *
 11 * This program is distributed in the hope that it will be useful,
 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14 * GNU General Public License for more details.
 15 *
 16 * You should have received a copy of the GNU General Public License
 17 * along with this program; if not, write to the Free Software
 18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 19 */
 20package org.jedit.ruby;
 21
 22import org.gjt.sp.jedit.*;
 23import org.gjt.sp.jedit.msg.ViewUpdate;
 24import org.gjt.sp.jedit.msg.EditPaneUpdate;
 25import org.gjt.sp.jedit.msg.BufferUpdate;
 26import org.gjt.sp.jedit.msg.PropertiesChanged;
 27import org.gjt.sp.jedit.textarea.JEditTextArea;
 28import org.gjt.sp.util.Log;
 29import org.jedit.ruby.parser.JRubyParser;
 30import org.jedit.ruby.ri.RiParser;
 31import org.jedit.ruby.completion.RubyKeyBindings;
 32import org.jedit.ruby.structure.RubyStructureMatcher;
 33import org.jedit.ruby.structure.BufferChangeHandler;
 34import org.jedit.ruby.utils.CharCaretListener;
 35import org.jedit.ruby.utils.EditorView;
 36import org.jedit.ruby.utils.ViewWrapper;
 37import org.jedit.ruby.utils.CommandUtils;
 38
 39import javax.swing.*;
 40import java.awt.*;
 41import java.awt.event.KeyEvent;
 42import java.io.*;
 43import java.util.Map;
 44import java.util.HashMap;
 45
 46/**
 47 * @author robmckinnon at users,sourceforge,net
 48 */
 49public final class RubyPlugin extends EBPlugin {
 50
 51    private static final boolean debug = System.getProperty("user.home").equals("/Users/x");
 52    private static final CharCaretListener CHAR_CARET_LISTENER = new CharCaretListener();
 53    private static final Map<View, EditorView> views = new HashMap<View, EditorView>();
 54
 55    public final void start() {
 56        super.start();
 57        JRubyParser.setExpectedLabel(jEdit.getProperty("ruby.syntax-error.expected.label"));
 58        JRubyParser.setFoundLabel(jEdit.getProperty("ruby.syntax-error.found.label"));
 59        JRubyParser.setNothingLabel(jEdit.getProperty("ruby.syntax-error.nothing.label"));
 60
 61        RiParser.parseRdoc();
 62
 63        View view = jEdit.getFirstView();
 64        while (view != null) {
 65            EditPane[] panes = view.getEditPanes();
 66            for (EditPane pane : panes) {
 67                addKeyListener(pane.getTextArea());
 68            }
 69            view = view.getNext();
 70        }
 71    }
 72
 73    public void handleMessage(EBMessage message) {
 74        if (message instanceof ViewUpdate) {
 75            handleViewUpdate((ViewUpdate)message);
 76        } else if (message instanceof EditPaneUpdate) {
 77            handleEditUpdate((EditPaneUpdate)message);
 78        } else if (message instanceof BufferUpdate) {
 79            handleBufferUpdate((BufferUpdate)message);
 80        } else if (message instanceof PropertiesChanged) {
 81//            SideKickActions.propertiesChanged();
 82        }
 83    }
 84
 85    private static void handleViewUpdate(ViewUpdate update) {
 86        if (ViewUpdate.CREATED == update.getWhat()) {
 87            //
 88        } else if (ViewUpdate.CLOSED == update.getWhat()) {
 89            views.remove(update.getView());
 90        }
 91    }
 92
 93    private static void handleBufferUpdate(BufferUpdate update) {
 94        if (BufferUpdate.LOADED == update.getWhat()) {
 95            update.getBuffer().addBufferListener(BufferChangeHandler.instance());
 96
 97        } if (BufferUpdate.CLOSED == update.getWhat()) {
 98            update.getBuffer().addBufferListener(BufferChangeHandler.instance());
 99        }
100    }
101
102    private void handleEditUpdate(EditPaneUpdate update) {
103        JEditTextArea textArea = update.getEditPane().getTextArea();
104
105        if (EditPaneUpdate.CREATED == update.getWhat()) {
106            addKeyListener(textArea);
107        } else if (EditPaneUpdate.DESTROYED == update.getWhat()) {
108            removeKeyListener(textArea);
109        }
110    }
111
112    private static void addKeyListener(JEditTextArea textArea) {
113        RubyKeyBindings bindings = new RubyKeyBindings(textArea);
114        RubyStructureMatcher structureMatcher = new RubyStructureMatcher();
115
116        textArea.putClientProperty(RubyKeyBindings.class, bindings);
117        textArea.putClientProperty(RubyStructureMatcher.class, structureMatcher);
118
119        textArea.addKeyListener(bindings);
120        textArea.addStructureMatcher(structureMatcher);
121        textArea.addCaretListener(CHAR_CARET_LISTENER);
122    }
123
124    private static void removeKeyListener(JEditTextArea textArea) {
125        RubyKeyBindings bindings = (RubyKeyBindings)textArea.getClientProperty(RubyKeyBindings.class);
126        RubyStructureMatcher structureMatcher = (RubyStructureMatcher)textArea.getClientProperty(RubyStructureMatcher.class);
127
128        textArea.putClientProperty(RubyKeyBindings.class, null);
129        textArea.putClientProperty(RubyStructureMatcher.class, null);
130
131        textArea.removeKeyListener(bindings);
132        textArea.removeStructureMatcher(structureMatcher);
133        textArea.removeCaretListener(CHAR_CARET_LISTENER);
134    }
135
136    public static void log(String message, Class clas) {
137        if (debug) {
138            try {
139                Log.log(Log.MESSAGE, clas, message);
140            } catch (Exception e) {
141                System.out.println(message);
142            }
143        }
144    }
145
146    public static void error(Exception e, Class clas) {
147        String message = e.getClass().getName();
148        message = message.substring(message.lastIndexOf('.') + 1);
149        if (e.getMessage() != null && e.getMessage().length() > 0) {
150            message += ": " + e.getMessage();
151        }
152        e.printStackTrace();
153        error(message, clas);
154    }
155
156    public static EditorView getActiveView() {
157        final View view = jEdit.getActiveView();
158
159        if (!views.containsKey(view)) {
160            EditorView editorView = view != null ? new ViewWrapper(view) : EditorView.NULL;
161            views.put(view, editorView);
162        }
163
164        return views.get(view);
165    }
166
167    public static void error(String message, Class clas) {
168        try {
169            Log.log(Log.ERROR, clas, message);
170            View view = jEdit.getActiveView();
171            if (view != null) {
172                if (debug) {
173                    show("", message, view, JOptionPane.ERROR_MESSAGE);
174                }
175            }
176        } catch (Exception e) {
177            System.err.println(message);
178        }
179    }
180
181    public static int getNonSpaceStartOffset(int line) {
182        return getActiveView().getNonSpaceStartOffset(line);
183    }
184
185    public static int getEndOffset(int line) {
186        return getActiveView().getEndOffset(line);
187    }
188
189    public static int getStartOffset(int line) {
190        return getActiveView().getStartOffset(line);
191    }
192
193    public static int getEndOfFileOffset() {
194        return getActiveView().getEndOfFileOffset();
195    }
196
197    public static String readFile(File file) {
198        StringBuffer buffer = new StringBuffer();
199
200        try {
201            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
202            try {
203                char[] chars = new char[1024];
204                int length;
205                while (-1 != (length = bufferedReader.read(chars))) {
206                    buffer.append(chars, 0, length);
207                }
208            } catch (IOException e) {
209                error(e, RubyPlugin.class);
210            }
211        } catch (FileNotFoundException e) {
212            error(e, RubyPlugin.class);
213        }
214
215        return buffer.toString();
216    }
217
218    public static Point getCaretPopupLocation(View view) {
219        JEditTextArea textArea = view.getTextArea();
220        textArea.scrollToCaret(false);
221        Point location = textArea.offsetToXY(textArea.getCaretPosition());
222        location.y += textArea.getPainter().getFontMetrics().getHeight();
223        SwingUtilities.convertPointToScreen(location, textArea.getPainter());
224        return location;
225    }
226
227    public static Point getCenteredPopupLocation(View view) {
228        JEditTextArea textArea = view.getTextArea();
229        textArea.scrollToCaret(false);
230        return new Point(textArea.getSize().width / 3, textArea.getSize().height / 5);
231    }
232
233    public static boolean isRuby(org.gjt.sp.jedit.textarea.TextArea textArea) {
234        return isRuby(CommandUtils.getBuffer(textArea));
235    }
236
237    private static boolean isRuby(Object buffer) {
238        if (buffer instanceof Buffer) {
239            return isRuby((Buffer)buffer);
240        } else {
241            throw new IllegalArgumentException("expecting buffer to be of type Buffer");
242        }
243    }
244
245    public static boolean isRuby(Buffer buffer) {
246        boolean isRubyBuffer = buffer.getMode() == rubyMode();
247        return isRubyBuffer || isRuby(new File(buffer.getPath()));
248    }
249
250    public static boolean isRuby(File file) {
251        return file.isFile() && rubyMode().accept(file.getPath(), "");
252    }
253
254    private static Mode rubyMode() {
255        return jEdit.getMode("ruby");
256    }
257
258    public static void showMessage(String titleKey, String messageKey, View view) {
259        String title = titleKey == null ? null : jEdit.getProperty(titleKey);
260        String message = jEdit.getProperty(messageKey);
261        show(title, message, view, JOptionPane.INFORMATION_MESSAGE);
262    }
263
264    public static void showMessage(String messageKey, View view) {
265        showMessage(null, messageKey, view);
266    }
267
268    private static void show(String title, String message, View view, int type) {
269        GUIUtilities.hideSplashScreen();
270        JOptionPane.showMessageDialog(view, message, title, type);
271    }
272
273    public static boolean ignoreKeyTyped(int keyCode, char keyChar, KeyEvent event) {
274        boolean ignore;
275
276        switch (keyCode) {
277            case KeyEvent.VK_ENTER:
278            case KeyEvent.VK_ESCAPE:
279            case KeyEvent.VK_UP:
280            case KeyEvent.VK_DOWN:
281            case KeyEvent.VK_PAGE_UP:
282            case KeyEvent.VK_PAGE_DOWN:
283            case KeyEvent.VK_HOME:
284            case KeyEvent.VK_END:
285            case KeyEvent.VK_LEFT:
286            case KeyEvent.VK_RIGHT:
287                ignore = true;
288                break;
289            default:
290                // for some reason have to match backspace and tab using keyChar
291                ignore = keyChar == KeyEvent.VK_BACK_SPACE
292                        || keyChar == KeyEvent.VK_TAB
293                        || event.isActionKey()
294                        || event.isControlDown()
295                        || event.isAltDown()
296                        || event.isMetaDown();
297        }
298        return ignore;
299    }
300
301}