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

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/DockingLayoutManager.java

#
Java | 272 lines | 241 code | 26 blank | 5 comment | 59 complexity | 7dd1b8e817f1c2d2b6e5898844d12517 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1package org.gjt.sp.jedit.gui;
  2
  3import java.util.HashMap;
  4import java.util.Map;
  5
  6import javax.swing.JOptionPane;
  7
  8import org.gjt.sp.jedit.ActionSet;
  9import org.gjt.sp.jedit.Buffer;
 10import org.gjt.sp.jedit.EBComponent;
 11import org.gjt.sp.jedit.EBMessage;
 12import org.gjt.sp.jedit.EditAction;
 13import org.gjt.sp.jedit.EditBus;
 14import org.gjt.sp.jedit.Mode;
 15import org.gjt.sp.jedit.View;
 16import org.gjt.sp.jedit.jEdit;
 17import org.gjt.sp.jedit.gui.DockableWindowManager.DockingLayout;
 18import org.gjt.sp.jedit.msg.BufferUpdate;
 19import org.gjt.sp.jedit.msg.EditPaneUpdate;
 20import org.gjt.sp.jedit.msg.ViewUpdate;
 21import org.gjt.sp.jedit.options.DockingOptionPane;
 22
 23/** Saves and loads dockable layouts to disk
 24    @author Shlomy Reinstein
 25    @since jEdit 4.3pre16
 26*/
 27public class DockingLayoutManager implements EBComponent
 28{
 29
 30	private static final String NO_SETTINGS_MESSAGE = "no-settings.message";
 31	private static final String SAVE_LAYOUT_FAILED = "save-layout-failed.message";
 32	private static final String LOAD_LAYOUT_TITLE = "load-layout.title";
 33	private static final String LOAD_LAYOUT_MESSAGE = "load-layout.message";
 34	private static final String SAVE_LAYOUT_TITLE = "save-layout.title";
 35	private static final String SAVE_LAYOUT_MESSAGE = "save-layout.message";
 36	private static ActionSet actions;
 37	private static DockingLayoutManager instance;
 38	private Map<View, String> currentMode;
 39	
 40	private DockingLayoutManager()
 41	{
 42		currentMode = new HashMap<View, String>();
 43	}
 44	private static boolean save(View view, String layoutName)
 45	{
 46		DockingLayout docking = view.getViewConfig().docking; 
 47		if (docking != null)
 48		{
 49			boolean ret = docking.saveLayout(layoutName, DockingLayout.NO_VIEW_INDEX);
 50			if (! ret)
 51				return false;
 52			addAction(layoutName);
 53		}
 54		return true;
 55	}
 56	
 57	public static void saveAs(View view)
 58	{
 59		if (jEdit.getSettingsDirectory() == null)
 60		{
 61			JOptionPane.showMessageDialog(view, jEdit.getProperty(NO_SETTINGS_MESSAGE));
 62			return;
 63		}
 64		String layoutName = JOptionPane.showInputDialog(view,
 65			jEdit.getProperty(SAVE_LAYOUT_MESSAGE),
 66			jEdit.getProperty(SAVE_LAYOUT_TITLE),
 67			JOptionPane.QUESTION_MESSAGE);
 68		if (layoutName == null)
 69			return;
 70		if (! save(view, layoutName))
 71			JOptionPane.showMessageDialog(view, jEdit.getProperty(SAVE_LAYOUT_FAILED));
 72	}
 73	
 74	private static void load(View view, String layoutName)
 75	{
 76		DockingLayout docking = View.getDockingFrameworkProvider().createDockingLayout();
 77		if (docking.loadLayout(layoutName, DockingLayout.NO_VIEW_INDEX))
 78			view.getDockableWindowManager().setDockingLayout(docking);
 79	}
 80	
 81	public static void load(View view)
 82	{
 83		if (jEdit.getSettingsDirectory() == null)
 84		{
 85			JOptionPane.showMessageDialog(view, jEdit.getProperty(NO_SETTINGS_MESSAGE));
 86			return;
 87		}
 88		String layoutName = (String) JOptionPane.showInputDialog(view,
 89			jEdit.getProperty(LOAD_LAYOUT_MESSAGE),
 90			jEdit.getProperty(LOAD_LAYOUT_TITLE),
 91			JOptionPane.QUESTION_MESSAGE,
 92			null,
 93			getSavedLayouts(),
 94			null);
 95		if (layoutName == null)
 96			return;
 97		load(view, layoutName);
 98	}
 99
100	private static String[] getSavedLayouts()
101	{
102		DockingLayout docking = View.getDockingFrameworkProvider().createDockingLayout();
103		String[] layouts = null;
104		if (docking != null)
105			layouts = docking.getSavedLayouts();
106		if (layouts == null)
107			return new String[0];
108		return layouts;
109	}
110	
111	private static void addAction(String layoutName)
112	{
113		if ((actions != null) && (! actions.contains(layoutName)))
114			actions.addAction(new LoadPerspectiveAction(layoutName));
115	}
116	
117	public static void init()
118	{
119		createActions();
120		instance = new DockingLayoutManager();
121		EditBus.addToBus(instance);
122	}
123	
124	private static void createActions()
125	{
126		actions = new ActionSet("Docking Layouts");
127		String[] layouts = getSavedLayouts();
128		for (String layout: layouts)
129			addAction(layout);
130		jEdit.addActionSet(actions);
131		actions.initKeyBindings();
132	}
133	
134	public static void removeActions()
135	{
136		jEdit.removeActionSet(actions);
137	}
138
139	private static class LoadPerspectiveAction extends EditAction
140	{
141		private static final String LOAD_PREFIX = "load-";
142
143		public LoadPerspectiveAction(String layoutName)
144		{
145			super(LOAD_PREFIX + layoutName, new String[] { layoutName });
146			jEdit.setTemporaryProperty(LOAD_PREFIX + layoutName + ".label", LOAD_PREFIX + layoutName);
147		}
148		
149		@Override
150		public void invoke(View view)
151		{
152			DockingLayoutManager.load(view, (String) args[0]);
153		}
154	}
155
156	private boolean canChangeEditMode(EBMessage message)
157	{
158		if (message instanceof BufferUpdate)
159		{
160			BufferUpdate bu = (BufferUpdate) message;
161			Object what = bu.getWhat();
162			if ((what == BufferUpdate.CLOSED) ||
163				(what == BufferUpdate.CREATED) ||
164				(what == BufferUpdate.PROPERTIES_CHANGED))
165			{
166				return true;
167			}
168		}
169		else if (message instanceof EditPaneUpdate)
170		{
171			EditPaneUpdate ep = (EditPaneUpdate) message;
172			Object what = ep.getWhat();
173			if ((what == EditPaneUpdate.BUFFER_CHANGED) ||
174				(what == EditPaneUpdate.CREATED))
175			{
176				return true;
177			}
178		}
179		return false;
180	}
181
182	public void handleMessage(EBMessage message)
183	{
184		boolean autoLoadModeLayout = jEdit.getBooleanProperty(
185			DockingOptionPane.AUTO_LOAD_MODE_LAYOUT_PROP, false);
186		if (! autoLoadModeLayout)
187			return;
188		if (message instanceof ViewUpdate)
189		{
190			ViewUpdate vu = (ViewUpdate) message;
191			if (vu.getWhat() == ViewUpdate.CLOSED)
192			{
193				View view = jEdit.getActiveView();
194				String mode = currentMode.get(view);
195				saveModeLayout(view, mode);
196				return;
197			}
198		}
199		// Check for a change in the edit mode
200		View view = jEdit.getActiveView();
201		if (view == null)
202			return;
203		if (! canChangeEditMode(message))
204			return;
205		String newMode = getCurrentEditMode(view);
206		String mode = currentMode.get(view);
207		boolean sameMode =
208			(mode == null && newMode == null) ||
209			(mode != null && mode.equals(newMode));
210		if (! sameMode)
211		{
212			boolean autoSaveModeLayout = jEdit.getBooleanProperty(
213				DockingOptionPane.AUTO_SAVE_MODE_LAYOUT_PROP, false);
214			if (autoSaveModeLayout)
215				saveModeLayout(view, mode);
216			currentMode.put(view, newMode);
217			loadModeLayout(view, newMode);
218		}
219	}
220
221	private String getCurrentEditMode(View view)
222	{
223		Buffer buffer = view.getBuffer();
224		if (buffer == null)
225			return null;
226		Mode bufferMode = buffer.getMode();
227		if (bufferMode == null)
228			return null;
229		return bufferMode.getName();
230	}
231
232	private static final String GLOBAL_MODE = "DEFAULT";
233	
234	private void saveModeLayout(View view, String mode)
235	{
236		String modeLayout = getModePerspective(mode);
237		if (modeLayout == null)
238			return;
239		save(view, modeLayout);
240	}
241	
242	private void loadModeLayout(View view, String mode)
243	{
244		String modeLayout = getModePerspective(mode);
245		if (modeLayout == null)
246			return;
247		load(view, modeLayout);
248	}
249
250	public static void loadCurrentModeLayout(View view)
251	{
252		if (view == null)
253			return;
254		String mode = instance.getCurrentEditMode(view);
255		instance.loadModeLayout(view, mode);
256	}
257	
258	public static void saveCurrentModeLayout(View view)
259	{
260		if (view == null)
261			return;
262		String mode = instance.getCurrentEditMode(view);
263		instance.saveModeLayout(view, mode);
264	}
265	
266	private String getModePerspective(String mode)
267	{
268		if (mode == null)
269			mode = GLOBAL_MODE;
270		return "mode-" + mode;
271	}
272}