PageRenderTime 32ms CodeModel.GetById 11ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/Sessions/sessions/SessionSwitcher.java

#
Java | 264 lines | 167 code | 57 blank | 40 comment | 30 complexity | c3d61b33f3862d679b9a5f768f726de0 MD5 | raw file
  1/*
  2 * SessionSwitcher.java - toolbar for switching between jEdit sessions
  3 * Copyright (c) 2001 Dirk Moebius
  4 *
  5 * :tabSize=4:indentSize=4:noTabs=false:maxLineLen=0:
  6 *
  7 * This program is free software; you can redistribute it and/or
  8 * modify it under the terms of the GNU General Public License
  9 * as published by the Free Software Foundation; either version 2
 10 * of the License, or any later version.
 11 *
 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 */
 21
 22
 23package sessions;
 24
 25
 26import java.awt.BorderLayout;
 27import java.awt.Dimension;
 28import java.awt.Insets;
 29import java.awt.event.ActionEvent;
 30import java.awt.event.ActionListener;
 31import java.awt.event.ItemEvent;
 32import java.awt.event.ItemListener;
 33import java.util.Vector;
 34import javax.swing.*;
 35import org.gjt.sp.jedit.*;
 36import org.gjt.sp.jedit.io.VFSManager;
 37import org.gjt.sp.jedit.msg.PropertiesChanged;
 38import org.gjt.sp.util.Log;
 39
 40
 41/**
 42 * A control panel for switching between jEdit sessions.
 43 *
 44 * @author Dirk Moebius
 45 */
 46public class SessionSwitcher
 47		extends JToolBar
 48		implements ActionListener, ItemListener, EBComponent
 49{
 50
 51	public SessionSwitcher(View view, boolean isInDefaultToolBar)
 52	{
 53		super();
 54		this.view = view;
 55
 56		Insets nullInsets = new Insets(0,0,0,0);
 57
 58		combo = new JComboBox(SessionManager.getInstance().getSessionNames());
 59		int maxEntries = jEdit.getIntegerProperty(
 60			"options.sessions.switcher.maxListSize", 8);
 61		combo.setMaximumRowCount(maxEntries);
 62		combo.setSelectedItem(SessionManager.getInstance().getCurrentSession());
 63		combo.setEditable(false);
 64		combo.addItemListener(this);
 65
 66		save = new JButton(new ImageIcon(getClass().getResource("Save24.gif")));
 67		save.setMargin(nullInsets);
 68		save.setToolTipText(jEdit.getProperty("sessions.switcher.save.tooltip"));
 69		save.setFocusPainted(false);
 70		save.addActionListener(this);
 71
 72		saveAs = new JButton(new ImageIcon(getClass().getResource("SaveAs24.gif")));
 73		saveAs.setMargin(nullInsets);
 74		saveAs.setToolTipText(jEdit.getProperty("sessions.switcher.saveAs.tooltip"));
 75		saveAs.setFocusPainted(false);
 76		saveAs.addActionListener(this);
 77
 78		reload = new JButton(new ImageIcon(getClass().getResource("Redo24.gif")));
 79		reload.setMargin(nullInsets);
 80		reload.setToolTipText(jEdit.getProperty("sessions.switcher.reload.tooltip"));
 81		reload.setFocusPainted(false);
 82		reload.addActionListener(this);
 83
 84		props = new JButton(new ImageIcon(getClass().getResource("History24.gif")));
 85		props.setMargin(nullInsets);
 86		props.setToolTipText(jEdit.getProperty("sessions.switcher.props.tooltip"));
 87		props.setFocusPainted(false);
 88		props.addActionListener(this);
 89
 90		prefs = new JButton(new ImageIcon(getClass().getResource("Preferences24.gif")));
 91		prefs.setMargin(nullInsets);
 92		prefs.setToolTipText(jEdit.getProperty("sessions.switcher.prefs.tooltip"));
 93		prefs.setFocusPainted(false);
 94		prefs.addActionListener(this);
 95
 96		setFloatable(false);
 97		putClientProperty("JToolBar.isRollover", Boolean.TRUE);
 98
 99		addSeparator(new Dimension(5,5));  // just add a little space at begin, looks better
100
101		add(combo);
102		addSeparator();
103		add(save);
104		add(saveAs);
105		add(reload);
106		//add(props);  // FIXME: not yet - EXPERIMENTAL
107		add(prefs);
108
109		// if we're not added to jEdit's default toolbar, then add some glue at
110		// the end of the toolbar, so that we're left aligned and the combo box
111		// doesn't get too long:
112		if(!isInDefaultToolBar)
113			add(Box.createGlue());
114
115		updateTitle();
116	}
117
118
119	/** adds itself to EditBus on display */
120	public void addNotify()
121	{
122		super.addNotify();
123		EditBus.addToBus(this);
124	}
125
126
127	/** removes itself from EditBus on undisplay */
128	public void removeNotify()
129	{
130		super.removeNotify();
131		EditBus.removeFromBus(this);
132	}
133
134
135	// BEGIN EBComponent implementation
136
137	public void handleMessage(EBMessage message)
138	{
139		if (message instanceof PropertiesChanged)
140			updateTitle();
141		else if (message instanceof SessionChanged)
142			handleSessionChanged((SessionChanged)message);
143		else if (message instanceof SessionListChanged)
144			handleSessionListChanged((SessionListChanged)message);
145	}
146
147	// END EBComponent implementation
148
149
150	// BEGIN ActionListener implementation
151
152	public void actionPerformed(ActionEvent evt)
153	{
154		if (evt.getSource() == save)
155			SessionManager.getInstance().saveCurrentSession(view);
156		else if (evt.getSource() == saveAs)
157			SessionManager.getInstance().saveCurrentSessionAs(view);
158		else if (evt.getSource() == reload)
159			SessionManager.getInstance().reloadCurrentSession(view);
160		else if (evt.getSource() == props)
161			SessionManager.getInstance().showSessionPropertiesDialog(view);
162		else if (evt.getSource() == prefs)
163			SessionManager.getInstance().showSessionManagerDialog(view);
164	}
165
166	// END ActionListener implementation
167
168
169	// BEGIN ItemListener implementation
170
171	public void itemStateChanged(ItemEvent e)
172	{
173		if(e.getStateChange() != ItemEvent.SELECTED || e.getItem() == null)
174			return;
175
176		String currentSession = SessionManager.getInstance().getCurrentSession();
177		final String selectedSession = e.getItem().toString();
178
179		if (selectedSession.equals(currentSession)) return;
180		
181		SessionManager.getInstance().setCurrentSession(view, selectedSession);
182		// The session may not have been changed (eg. if the session change 
183		// was cancelled by the user while closing all open buffers).
184		// Make sure the combo box is correct by calling ...
185		updateSessionComboBox();
186	}  
187
188	// END ItemListener implementation
189
190
191	private void handleSessionChanged(SessionChanged msg) {
192		updateSessionComboBox();
193	}
194	
195	private void updateSessionComboBox()
196	{
197		final String newSession = SessionManager.getInstance().getCurrentSession();
198
199		SwingUtilities.invokeLater(new Runnable()
200		{
201			public void run()
202			{
203				combo.setSelectedItem(newSession);
204			}
205		});
206	}
207
208
209	private void handleSessionListChanged(SessionListChanged msg)
210	{
211		final String[] sessions = msg.getSessionManager().getSessionNames();
212
213		SwingUtilities.invokeLater(new Runnable()
214		{
215			public void run()
216			{
217				combo.setModel(new DefaultComboBoxModel(sessions));
218			}
219		});
220	}
221
222
223	private void updateTitle()
224	{
225		if (jEdit.getBooleanProperty("sessions.switcher.showTitle", true))
226			addTitle();
227		else
228			removeTitle();
229	}
230
231
232	private void addTitle()
233	{
234		if (title != null)
235			return; // already added
236
237		title = new JLabel(jEdit.getProperty("sessions.switcher.title"));
238		add(title, 0);
239		revalidate();
240	}
241
242
243	private void removeTitle()
244	{
245		if(title == null)
246			return; // already removed
247
248		remove(title);
249		revalidate();
250		title = null;
251	}
252
253
254	private View view;
255	private JComboBox combo;
256	private JButton save;
257	private JButton saveAs;
258	private JButton reload;
259	private JButton props;
260	private JButton prefs;
261	private JLabel title;
262
263}
264