PageRenderTime 298ms CodeModel.GetById 289ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/jedit/Java14.java

#
Java | 189 lines | 130 code | 16 blank | 43 comment | 29 complexity | 2bda261d4f126bf8928ed5e97c5d9558 MD5 | raw file
  1/*
  2 * Java14.java - Java 2 version 1.4 API calls
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2001, 2002 Slava Pestov
  7 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; if not, write to the Free Software
 20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 21 */
 22
 23package org.gjt.sp.jedit;
 24
 25//{{{ Imports
 26import javax.swing.*;
 27import java.awt.datatransfer.*;
 28import java.awt.event.*;
 29import java.awt.*;
 30import org.gjt.sp.jedit.msg.*;
 31import org.gjt.sp.jedit.textarea.JEditTextArea;
 32import org.gjt.sp.jedit.EditBus;
 33import org.gjt.sp.util.Log;
 34//}}}
 35
 36/**
 37 * This file must be compiled with a JDK 1.4 or higher javac. If you are using
 38 * an older Java version and wish to compile from source, you can safely leave
 39 * this file out.
 40 * @since jEdit 4.0pre4
 41 * @author Slava Pestov
 42 * @version $Id: Java14.java 4568 2003-03-23 18:44:35Z spestov $
 43 */
 44class Java14
 45{
 46	//{{{ init() method
 47	public static void init()
 48	{
 49		JFrame.setDefaultLookAndFeelDecorated(
 50			jEdit.getBooleanProperty("decorate.frames"));
 51		JDialog.setDefaultLookAndFeelDecorated(
 52			jEdit.getBooleanProperty("decorate.dialogs"));
 53
 54		KeyboardFocusManager.setCurrentKeyboardFocusManager(
 55			new MyFocusManager());
 56
 57		EditBus.addToBus(new EBComponent()
 58		{
 59			public void handleMessage(EBMessage msg)
 60			{
 61				if(msg instanceof ViewUpdate)
 62				{
 63					ViewUpdate vu = (ViewUpdate)msg;
 64					if(vu.getWhat() == ViewUpdate.CREATED)
 65					{
 66						vu.getView().setFocusTraversalPolicy(
 67							new MyFocusTraversalPolicy());
 68					}
 69				}
 70				else if(msg instanceof EditPaneUpdate)
 71				{
 72					EditPaneUpdate eu = (EditPaneUpdate)msg;
 73					if(eu.getWhat() == EditPaneUpdate.CREATED)
 74					{
 75						eu.getEditPane().getTextArea()
 76							.addMouseWheelListener(
 77							new MouseWheelHandler());
 78					}
 79				}
 80			}
 81		});
 82
 83		Clipboard selection = Toolkit.getDefaultToolkit().getSystemSelection();
 84		if(selection != null)
 85		{
 86			Log.log(Log.DEBUG,Java14.class,"Setting % register"
 87				+ " to system selection");
 88			Registers.setRegister('%',new Registers.ClipboardRegister(selection));
 89		}
 90	} //}}}
 91
 92	//{{{ MyFocusManager class
 93	static class MyFocusManager extends DefaultKeyboardFocusManager
 94	{
 95		MyFocusManager()
 96		{
 97			setDefaultFocusTraversalPolicy(new LayoutFocusTraversalPolicy());
 98		}
 99
100		public boolean postProcessKeyEvent(KeyEvent evt)
101		{
102			if(!evt.isConsumed())
103			{
104				Component comp = (Component)evt.getSource();
105				if(!comp.isShowing())
106					return true;
107
108				for(;;)
109				{
110					if(comp instanceof View)
111					{
112						((View)comp).processKeyEvent(evt);
113						return true;
114					}
115					else if(comp == null || comp instanceof Window
116						|| comp instanceof JEditTextArea)
117					{
118						break;
119					}
120					else
121						comp = comp.getParent();
122				}
123			}
124
125			return super.postProcessKeyEvent(evt);
126		}
127	} //}}}
128
129	//{{{ MyFocusTraversalPolicy class
130	static class MyFocusTraversalPolicy extends LayoutFocusTraversalPolicy
131	{
132		public Component getDefaultComponent(Container focusCycleRoot)
133		{
134			return GUIUtilities.getView(focusCycleRoot).getTextArea();
135		}
136	} //}}}
137
138	//{{{ WheelScrollListener class
139	static class MouseWheelHandler implements MouseWheelListener
140	{
141		public void mouseWheelMoved(MouseWheelEvent e)
142		{
143			JEditTextArea textArea = (JEditTextArea)e.getSource();
144
145			/****************************************************
146			 * move caret depending on pressed control-keys:
147			 * - Alt: move cursor, do not select
148			 * - Alt+(shift or control): move cursor, select
149			 * - shift: scroll page
150			 * - control: scroll single line
151			 * - <else>: scroll 3 lines
152			 ****************************************************/
153			if(e.isAltDown())
154			{
155				moveCaret(textArea,e.getWheelRotation(),
156					e.isShiftDown() || e.isControlDown());
157			}
158			else if(e.isShiftDown())
159				scrollPage(textArea,e.getWheelRotation());
160			else if(e.isControlDown())
161				scrollLine(textArea,e.getWheelRotation());
162			else if(e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL)
163				scrollLine(textArea,e.getUnitsToScroll());
164			else
165				scrollLine(textArea,3 * e.getWheelRotation());
166		}
167
168		private void scrollLine(JEditTextArea textArea, int amt)
169		{
170			textArea.setFirstLine(textArea.getFirstLine() + amt);
171		}
172
173		private void scrollPage(JEditTextArea textArea, int amt)
174		{
175			if(amt > 0)
176				textArea.scrollDownPage();
177			else
178				textArea.scrollUpPage();
179		}
180
181		private void moveCaret(JEditTextArea textArea, int amt, boolean select)
182		{
183			if (amt < 0)
184				textArea.goToPrevLine(select);
185			else
186				textArea.goToNextLine(select);
187		}
188	} //}}}
189}