PageRenderTime 50ms CodeModel.GetById 44ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/CommonControls/common/gui/itemfinder/ItemFinderWindow.java

#
Java | 113 lines | 49 code | 8 blank | 56 comment | 0 complexity | 4738daf1c8a47d940a72adf836cc31a0 MD5 | raw file
  1/*
  2 * ItemFinderWindow.java
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 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 *
 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 */
 22package common.gui.itemfinder;
 23
 24//{{{ Imports
 25import java.awt.EventQueue;
 26import java.awt.event.WindowAdapter;
 27import java.awt.event.WindowEvent;
 28import javax.swing.JDialog;
 29
 30import org.gjt.sp.jedit.View;
 31//}}}
 32
 33/** A Window that contains a SearchField and a list of items.
 34 * When typing in the search field, the item list is updated,
 35 * and when selecting an item of that list, an action is
 36 * triggered.
 37 * To use it, you have to implement {@link ItemFinder}.
 38 *
 39 * Then you have two choices : instantiate {@link ItemFinderWindow} and make it visible,
 40 * or use {@link ItemFinderWindow#showWindow(View, ItemFinder)} that will create the window and show it.
 41 * @author Matthieu Casanova
 42 */
 43public class ItemFinderWindow<E> extends JDialog
 44{
 45	private final ItemFinderPanel<E> itemFinderPanel;
 46	public final Runnable requestFocusWorker;
 47
 48	//{{{ ItemFinderWindow constructor
 49	public ItemFinderWindow(ItemFinder<E> itemFinder)
 50	{
 51		setUndecorated(true);
 52		itemFinderPanel = new ItemFinderPanel<E>(this, itemFinder);
 53		requestFocusWorker = itemFinderPanel.requestFocusWorker;
 54		setContentPane(itemFinderPanel);
 55		pack();
 56		addWindowListener(new WindowAdapter()
 57		{
 58			@Override
 59			public void windowDeactivated(WindowEvent e)
 60			{
 61				dispose();
 62			}
 63		});
 64	} //}}}
 65
 66	//{{{ getItemFinderPanel() method
 67	/**
 68	 * Returns the ItemFinderPanel.
 69	 * This is a JPanel using BorderLayout. The textfield is in the Center,
 70	 * the NORTH is a label, you can modify that if you like.
 71	 * @return the itemFinderPanel
 72	 */
 73	public ItemFinderPanel<E> getItemFinderPanel()
 74	{
 75		return itemFinderPanel;
 76	} //}}}
 77
 78	//{{{ showWindow() methods
 79	/**
 80	 * Show the ItemFinderWindow
 81	 * @param view the parent view
 82	 * @param itemFinder the itemFinder object
 83	 * @param <E>
 84	 */
 85	public static <E> void showWindow(View view, ItemFinder<E> itemFinder)
 86	{
 87		showWindow(view, itemFinder, "");
 88	}
 89
 90	/**
 91	 * Show the ItemFinderWindow
 92	 * @param view the parent view
 93	 * @param itemFinder the itemFinder object
 94	 * @param defaultText the default text
 95	 * @param <E>
 96	 * @since CommonControls 1.4.1
 97	 */
 98	public static <E> void showWindow(View view, ItemFinder<E> itemFinder, final String defaultText)
 99	{
100		final ItemFinderWindow<E> itemFinderWindow = new ItemFinderWindow<E>(itemFinder);
101		itemFinderWindow.setLocationRelativeTo(view);
102		itemFinderWindow.setVisible(true);
103		EventQueue.invokeLater(new Runnable()
104		{
105			@Override
106			public void run()
107			{
108				itemFinderWindow.getItemFinderPanel().setText(defaultText);
109			}
110		});
111
112	} //}}}
113}