PageRenderTime 53ms CodeModel.GetById 43ms app.highlight 8ms RepoModel.GetById 0ms app.codeStats 1ms

/bundles/plugins-trunk/Ancestor/src/gatchan/jedit/ancestor/AncestorPlugin.java

#
Java | 197 lines | 151 code | 14 blank | 32 comment | 24 complexity | 31c05d5a22e3e86c1bc6de4594ed8627 MD5 | raw file
  1/*
  2 * AncestorPlugin.java - The Ancestor plugin
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2007, 2011 Matthieu Casanova
  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 * This program is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15 * GNU General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU General Public License
 18 * along with this program; if not, write to the Free Software
 19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 20 */
 21package gatchan.jedit.ancestor;
 22
 23import java.awt.Component;
 24import java.awt.FlowLayout;
 25import java.lang.reflect.Field;
 26import java.util.HashMap;
 27import java.util.Map;
 28import javax.swing.JComponent;
 29import javax.swing.JPanel;
 30
 31import org.gjt.sp.jedit.EditBus;
 32import org.gjt.sp.jedit.EditPane;
 33import org.gjt.sp.jedit.EditPlugin;
 34import org.gjt.sp.jedit.View;
 35import org.gjt.sp.jedit.jEdit;
 36import org.gjt.sp.jedit.msg.BufferUpdate;
 37import org.gjt.sp.jedit.msg.EditPaneUpdate;
 38import org.gjt.sp.jedit.msg.ViewUpdate;
 39import org.gjt.sp.util.Log;
 40
 41/**
 42 * @author Matthieu Casanova
 43 * @version $Id: Server.java,v 1.33 2007/01/05 15:15:17 matthieu Exp $
 44 */
 45public class AncestorPlugin extends EditPlugin
 46{
 47	private final Map<View, AncestorToolBar> viewAncestorToolBar = new HashMap<View, AncestorToolBar>();
 48	private final Map<View, JComponent> topToolbars = new HashMap<View, JComponent>();
 49
 50	//{{{ start() method
 51	@Override
 52	public void start()
 53	{
 54		View[] views = jEdit.getViews();
 55		for (int i = 0; i < views.length; i++)
 56		{
 57			addAncestorToolBar(views[i]);
 58		}
 59		EditBus.addToBus(this);
 60	} //}}}
 61
 62	//{{{ addAncestorToolBar() method
 63	private void addAncestorToolBar(View view)
 64	{
 65		if (viewAncestorToolBar.containsKey(view))
 66			return;
 67		AncestorToolBar ancestorToolBar = new AncestorToolBar(view);
 68		EditPane editPane = view.getEditPane();
 69		ancestorToolBar.setBuffer(editPane.getBuffer());
 70
 71		JComponent toolBar = getViewToolbar(view);
 72		toolBar.add(ancestorToolBar);
 73		toolBar.validate();
 74		topToolbars.put(view, toolBar);
 75		viewAncestorToolBar.put(view, ancestorToolBar);
 76	} //}}}
 77
 78	private JComponent getViewToolbar(View view)
 79	{
 80		try
 81		{
 82			Field topToolBarsField = view.getClass().getDeclaredField("topToolBars");
 83			topToolBarsField.setAccessible(true);
 84			JPanel topToolBars = (JPanel) topToolBarsField.get(view);
 85			Component[] components = topToolBars.getComponents();
 86
 87			if (components.length > 1)
 88			{
 89				for (int i = 1; i < components.length; i++)
 90				{
 91					Component component = components[i];
 92					if (component instanceof JComponent)
 93					{
 94						JComponent toolBar = (JComponent) component;
 95						if (toolBar.getClientProperty("Ancestor-SmartOpen") == Boolean.TRUE)
 96							return toolBar;
 97
 98					}
 99				}
100			}
101		}
102		catch (NoSuchFieldException e)
103		{
104			Log.log(Log.ERROR, this, e);
105		}
106		catch (IllegalAccessException e)
107		{
108			Log.log(Log.ERROR, this, e);
109		}
110		JPanel customToolbar = new JPanel(new FlowLayout(FlowLayout.LEADING));
111		customToolbar.putClientProperty("Ancestor-SmartOpen", Boolean.TRUE);
112		view.addToolBar(customToolbar);
113		return customToolbar;
114	}
115
116	//{{{ removeAncestorToolBar() method
117	private void removeAncestorToolBar(View view)
118	{
119		AncestorToolBar toolBar = viewAncestorToolBar.get(view);
120		JComponent top = topToolbars.get(view);
121		top.remove(toolBar);
122		if (top.getComponentCount() == 0)
123		{
124			topToolbars.remove(view);
125			view.removeToolBar(top);
126		}
127		else
128		{
129			top.validate();
130			top.repaint();
131		}
132		viewAncestorToolBar.remove(view);
133	} //}}}
134
135	//{{{ handleViewUpdate() method
136	@EditBus.EBHandler
137	public void handleViewUpdate(ViewUpdate viewUpdate)
138	{
139		if (viewUpdate.getWhat() == ViewUpdate.CREATED)
140		{
141			View view = viewUpdate.getView();
142			addAncestorToolBar(view);
143		}
144		else if (viewUpdate.getWhat() == ViewUpdate.CLOSED)
145		{
146			viewAncestorToolBar.remove(viewUpdate.getView());
147		}
148		else if (viewUpdate.getWhat() == ViewUpdate.EDIT_PANE_CHANGED)
149		{
150			View view = viewUpdate.getView();
151			EditPane editPane = view.getEditPane();
152			AncestorToolBar bar = viewAncestorToolBar.get(view);
153			bar.setBuffer(editPane.getBuffer());
154		}
155	} //}}}
156
157	//{{{ handleEditPaneUpdate() method
158	@EditBus.EBHandler
159	public void handleEditPaneUpdate(EditPaneUpdate editPaneUpdate)
160	{
161		if (editPaneUpdate.getWhat() == EditPaneUpdate.BUFFER_CHANGED)
162		{
163			EditPane editPane = editPaneUpdate.getEditPane();
164			View view = editPane.getView();
165			AncestorToolBar bar = viewAncestorToolBar.get(view);
166			if (bar == null)
167				addAncestorToolBar(view);
168			bar = viewAncestorToolBar.get(view);
169			bar.setBuffer(editPane.getBuffer());
170		}
171	} //}}}
172
173	//{{{ handleMessage() method
174	@EditBus.EBHandler
175	public void handleBufferUpdate(BufferUpdate bufferUpdate)
176	{
177		// Needed to catch renaming of buffers / saving of new buffers
178		if (bufferUpdate.getWhat() == BufferUpdate.SAVED)
179		{
180			View view = bufferUpdate.getView();
181			AncestorToolBar bar = viewAncestorToolBar.get(view);
182			bar.setBuffer(bufferUpdate.getBuffer());
183		}
184	} //}}}
185
186	//{{{ stop() method
187	@Override
188	public void stop()
189	{
190		EditBus.removeFromBus(this);
191		View[] views = jEdit.getViews();
192		for (int i = 0; i < views.length; i++)
193		{
194			removeAncestorToolBar(views[i]);
195		}
196	} //}}}
197}