PageRenderTime 214ms CodeModel.GetById 171ms app.highlight 25ms RepoModel.GetById 15ms app.codeStats 0ms

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

#
Java | 320 lines | 210 code | 50 blank | 60 comment | 28 complexity | 7366addde7a50102bcb5909a1107883c MD5 | raw file
  1/*
  2 * SessionManagerDialog.java - a dialog for managing 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.Component;
 27import java.awt.Dimension;
 28import java.awt.GridBagLayout;
 29import java.awt.GridBagConstraints;
 30import java.awt.Insets;
 31import java.awt.event.ActionEvent;
 32import java.awt.event.ActionListener;
 33import java.awt.event.MouseAdapter;
 34import java.awt.event.MouseEvent;
 35import java.io.File;
 36import java.io.FilenameFilter;
 37import java.util.Vector;
 38import javax.swing.*;
 39import javax.swing.event.ListSelectionEvent;
 40import javax.swing.event.ListSelectionListener;
 41import org.gjt.sp.jedit.jEdit;
 42import org.gjt.sp.jedit.EditBus;
 43import org.gjt.sp.jedit.GUIUtilities;
 44import org.gjt.sp.jedit.View;
 45import org.gjt.sp.jedit.gui.EnhancedDialog;
 46import org.gjt.sp.util.Log;
 47
 48
 49/**
 50 * A modal dialog for managing jEdit sessions and session files.
 51 *
 52 * @author Dirk Moebius
 53 */
 54class SessionManagerDialog
 55		extends EnhancedDialog
 56		implements ActionListener, ListSelectionListener
 57{
 58
 59	public SessionManagerDialog(View view, final String currentSession)
 60	{
 61		super(view, jEdit.getProperty("sessions.manager.title"), true);
 62		this.currentSession = currentSession;
 63
 64		lSessions = new JList(SessionManager.getInstance().getSessionNames());
 65		lSessions.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 66		lSessions.addListSelectionListener(this);
 67		lSessions.addMouseListener(new MouseHandler()); // for double-clicks
 68		SwingUtilities.invokeLater(new Runnable()
 69		{
 70			public void run()
 71			{
 72				lSessions.setSelectedValue(currentSession, true);
 73			}
 74		});
 75		// try to show as many sessions as possible (maximum 25)
 76		int numSessions = lSessions.getModel().getSize();
 77		if (numSessions > 25)
 78			numSessions = 25;
 79		lSessions.setVisibleRowCount(numSessions);
 80
 81		JScrollPane scrSessions = new JScrollPane(lSessions);
 82
 83		bRename = new JButton(jEdit.getProperty("sessions.manager.rename"));
 84		bRename.addActionListener(this);
 85
 86		bDelete = new JButton(jEdit.getProperty("sessions.manager.delete"));
 87		bDelete.addActionListener(this);
 88
 89		bChangeTo = new JButton(jEdit.getProperty("sessions.manager.changeTo"));
 90		bChangeTo.setDefaultCapable(true);
 91		bChangeTo.addActionListener(this);
 92
 93		bClose = new JButton(jEdit.getProperty("sessions.manager.close"));
 94		bClose.addActionListener(this);
 95
 96		Insets inset10 = new Insets(10, 10, 10, 10);
 97		Insets insetButton = new Insets(10, 0, 0, 10);
 98		Insets insetLast = new Insets(10, 0, 10, 10);
 99
100		getContentPane().setLayout(new GridBagLayout());
101		addComponent(scrSessions, 1, 3, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, inset10);
102		addComponent(bRename, GridBagConstraints.REMAINDER, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, insetButton);
103		addComponent(bDelete, GridBagConstraints.REMAINDER, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, insetButton);
104		addComponent(bChangeTo, GridBagConstraints.REMAINDER, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, insetButton);
105		addComponent(new JSeparator(), GridBagConstraints.REMAINDER, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, inset10);
106		addComponent(new JPanel(), 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, inset10);
107		addComponent(bClose, GridBagConstraints.REMAINDER, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, insetLast);
108		getRootPane().setDefaultButton(bChangeTo);
109
110		pack();
111		setLocationRelativeTo(view);
112		setVisible(true);
113	}
114
115
116	/**
117	 * Invoked if ENTER or the Ok button are pressed.
118	 * Switches the session and closes the dialog.
119	 * You shouldn't show it again afterwards, because it is disposed.
120	 */
121	public void ok()
122	{
123		if (lSessions.getSelectedValue() != null)
124			selectedSession = lSessions.getSelectedValue().toString();
125		setVisible(false);
126		dispose();
127	}
128
129
130	/**
131	 * Invoked if ESC or the Cancel button are pressed.
132	 * Closes the dialog.
133	 * You shouldn't show it again afterwards, because it is disposed.
134	 */
135	public void cancel()
136	{
137		setVisible(false);
138		dispose();
139	}
140
141
142	/**
143	 * Return the selected session, or null if the dialog has been cancelled.
144	 */
145	public String getSelectedSession()
146	{
147		return selectedSession;
148	}
149
150
151	/**
152	 * Return true if the list of sessions has been modified.
153	 * The list has been modified if at least one session has been renamed
154	 * or deleted.
155	 */
156	public boolean isListModified()
157	{
158		return listModified;
159	}
160
161
162	/**
163	 * Invoked if one of the buttons is pressed.
164	 */
165	public void actionPerformed(ActionEvent evt)
166	{
167		if (evt.getSource() == bClose)
168			cancel();
169		else if (evt.getSource() == bChangeTo)
170			ok();
171		else if (evt.getSource() == bRename)
172			rename();
173		else if (evt.getSource() == bDelete)
174			delete();
175	}
176
177
178	/**
179	 * Invoked when the selected item in the sessions list changes.
180	 */
181	public void valueChanged(ListSelectionEvent evt)
182	{
183		Object[] values = lSessions.getSelectedValues();
184		if (values == null || values.length == 0)
185		{
186			// no selection
187			bRename.setEnabled(false);
188			bDelete.setEnabled(false);
189			bChangeTo.setEnabled(false);
190		}
191		else
192		{
193			boolean isCurrentSession = values[0].toString().equals(currentSession);
194			boolean isDefaultSession = values[0].toString().equalsIgnoreCase("default");
195			bChangeTo.setEnabled(!isCurrentSession);
196			bRename.setEnabled(!isDefaultSession);
197			bDelete.setEnabled(!isDefaultSession);
198		}
199	}
200
201
202	private void rename()
203	{
204		// TODO: Move this functionality to SessionManager
205		
206		String oldName = lSessions.getSelectedValue().toString();
207		String newName = SessionManager.inputSessionName(this, oldName);
208
209		if (newName == null)
210			return;
211
212		// Load the session to be re-named
213		Session renameSession;
214		if (oldName.equals(currentSession))
215		{
216			renameSession = SessionManager.getInstance().getCurrentSessionInstance();
217		} else {
218			renameSession = new Session(oldName);
219			try {
220				renameSession.loadXML();
221			} catch (Exception e) {
222				GUIUtilities.error(this, "sessions.manager.error.rename", new Object[] { oldName, newName });
223				return;
224			}
225		}
226		
227		if (renameSession.rename(newName))	// rename succeeded
228		{
229			setNewListModel();
230			lSessions.setSelectedValue(newName, true);
231			if (oldName.equals(currentSession))
232				currentSession = newName;
233		}
234		else	// rename failed
235			GUIUtilities.error(this, "sessions.manager.error.rename", new Object[] { oldName, newName });
236	}
237
238
239	private void delete()
240	{
241		// TODO: Move this functionality to SessionManager
242		
243		String name = lSessions.getSelectedValue().toString();
244		File file = new File(SessionManager.createSessionFileName(name));
245
246		if (file.delete())
247		{
248			if (name.equals(currentSession))
249				currentSession = null; // mark the current session as deleted
250			setNewListModel();
251			lSessions.setSelectedValue("default", true);
252		}
253		else
254			GUIUtilities.error(this, "sessions.manager.error.delete",	new Object[] { file });
255	}
256
257
258	private void setNewListModel()
259	{
260		listModified = true;
261		final String[] listData = SessionManager.getInstance().getSessionNames();
262
263		lSessions.setModel(
264			new AbstractListModel()
265			{
266				public int getSize() { return listData.length; }
267				public Object getElementAt(int i) { return listData[i]; }
268			}
269		);
270	}
271
272
273	/**
274	 * Convenience method for adding components to the GridBagLayout of this dialog.
275	 */
276	private void addComponent(Component comp,
277			int gridwidth, int gridheight,
278			double weightx, double weighty,
279			int anchor, int fill,
280			Insets insets)
281	{
282		GridBagConstraints constraints = new GridBagConstraints();
283		constraints.gridwidth = gridwidth;
284		constraints.gridheight = gridheight;
285		constraints.weightx = weightx;
286		constraints.weighty = weighty;
287		constraints.anchor = anchor;
288		constraints.fill = fill;
289		constraints.insets = insets;
290
291		GridBagLayout gridBag = (GridBagLayout) getContentPane().getLayout();
292		gridBag.setConstraints(comp, constraints);
293		getContentPane().add(comp);
294	}
295
296
297	/**
298	 * A <tt>MouseListener</tt> for handling double-clicks on the sessions list.
299	 */
300	private class MouseHandler extends MouseAdapter
301	{
302		public void mousePressed(MouseEvent evt)
303		{
304			if (evt.getClickCount() == 2)
305				ok();
306		}
307	}
308
309
310	private JList lSessions;
311	private JButton bRename;
312	private JButton bDelete;
313	private JButton bChangeTo;
314	private JButton bClose;
315	private String selectedSession;
316	private String currentSession;
317	private boolean listModified;
318
319}
320