001: /*******************************************************************************
002: * Copyright (c) 2003, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal;
011:
012: import java.util.HashMap;
013: import java.util.Map;
014:
015: import org.eclipse.core.runtime.IStatus;
016: import org.eclipse.jface.resource.ImageDescriptor;
017: import org.eclipse.jface.window.WindowManager;
018: import org.eclipse.ui.IMemento;
019: import org.eclipse.ui.IWorkbench;
020: import org.eclipse.ui.IWorkbenchWindow;
021: import org.eclipse.ui.PlatformUI;
022: import org.eclipse.ui.WorkbenchException;
023: import org.eclipse.ui.application.IWorkbenchConfigurer;
024: import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
025: import org.eclipse.ui.application.WorkbenchAdvisor;
026:
027: /**
028: * Internal class providing special access for configuring the workbench.
029: * <p>
030: * Note that these objects are only available to the main application
031: * (the plug-in that creates and owns the workbench).
032: * </p>
033: * <p>
034: * This class is not intended to be instantiated or subclassed by clients.
035: * </p>
036: *
037: * @since 3.0
038: */
039: public final class WorkbenchConfigurer implements IWorkbenchConfigurer {
040:
041: /**
042: * Table to hold arbitrary key-data settings (key type: <code>String</code>,
043: * value type: <code>Object</code>).
044: * @see #setData
045: */
046: private Map extraData = new HashMap();
047:
048: /**
049: * Indicates whether workbench state should be saved on close and
050: * restored on subsequent open.
051: */
052: private boolean saveAndRestore = false;
053:
054: /**
055: * Indicates whether the workbench is being force to close. During
056: * an emergency close, no interaction with the user should be done.
057: */
058: private boolean isEmergencyClosing = false;
059:
060: /**
061: * Indicates the behaviour when the last window is closed.
062: * If <code>true</code>, the workbench will exit (saving the last window's state,
063: * if configured to do so).
064: * If <code>false</code> the window will be closed, leaving the workbench running.
065: *
066: * @since 3.1
067: */
068: private boolean exitOnLastWindowClose = true;
069:
070: /**
071: * Creates a new workbench configurer.
072: * <p>
073: * This method is declared package-private. Clients are passed an instance
074: * only via {@link WorkbenchAdvisor#initialize WorkbenchAdvisor.initialize}
075: * </p>
076: */
077: /* package */WorkbenchConfigurer() {
078: super ();
079: }
080:
081: /* (non-javadoc)
082: * @see org.eclipse.ui.application.IWorkbenchConfigurer#getWorkbench
083: */
084: public IWorkbench getWorkbench() {
085: return PlatformUI.getWorkbench();
086: }
087:
088: /* (non-javadoc)
089: * @see org.eclipse.ui.application.IWorkbenchConfigurer#getWorkbenchWindowManager
090: */
091: public WindowManager getWorkbenchWindowManager() {
092: // return the global workbench window manager
093: return ((Workbench) getWorkbench()).getWindowManager();
094: }
095:
096: /* (non-javadoc)
097: * @see org.eclipse.ui.application.IWorkbenchConfigurer#declareImage
098: */
099: public void declareImage(String symbolicName,
100: ImageDescriptor descriptor, boolean shared) {
101: if (symbolicName == null || descriptor == null) {
102: throw new IllegalArgumentException();
103: }
104: WorkbenchImages.declareImage(symbolicName, descriptor, shared);
105: }
106:
107: /* (non-javadoc)
108: * @see org.eclipse.ui.application.IWorkbenchConfigurer#getWindowConfigurer
109: */
110: public IWorkbenchWindowConfigurer getWindowConfigurer(
111: IWorkbenchWindow window) {
112: if (window == null) {
113: throw new IllegalArgumentException();
114: }
115: return ((WorkbenchWindow) window).getWindowConfigurer();
116: }
117:
118: /* (non-Javadoc)
119: * @see org.eclipse.ui.application.IWorkbenchConfigurer#getSaveAndRestore()
120: */
121: public boolean getSaveAndRestore() {
122: return saveAndRestore;
123: }
124:
125: /* (non-Javadoc)
126: * @see org.eclipse.ui.application.IWorkbenchConfigurer#setSaveAndRestore(boolean)
127: */
128: public void setSaveAndRestore(boolean enabled) {
129: saveAndRestore = enabled;
130: }
131:
132: /* (non-Javadoc)
133: * @see org.eclipse.ui.application.IWorkbenchConfigurer#getData
134: */
135: public Object getData(String key) {
136: if (key == null) {
137: throw new IllegalArgumentException();
138: }
139: return extraData.get(key);
140: }
141:
142: /* (non-Javadoc)
143: * @see org.eclipse.ui.application.IWorkbenchConfigurer#setData(String, Object)
144: */
145: public void setData(String key, Object data) {
146: if (key == null) {
147: throw new IllegalArgumentException();
148: }
149: if (data != null) {
150: extraData.put(key, data);
151: } else {
152: extraData.remove(key);
153: }
154: }
155:
156: /* (non-Javadoc)
157: * @see org.eclipse.ui.application.IWorkbenchConfigurer#emergencyClose()
158: */
159: public void emergencyClose() {
160: if (!isEmergencyClosing) {
161: isEmergencyClosing = true;
162: if (Workbench.getInstance() != null
163: && !Workbench.getInstance().isClosing()) {
164: Workbench.getInstance().close(
165: PlatformUI.RETURN_EMERGENCY_CLOSE, true);
166: }
167: }
168:
169: }
170:
171: /* (non-Javadoc)
172: * @see org.eclipse.ui.application.IWorkbenchConfigurer#emergencyClosing()
173: */
174: public boolean emergencyClosing() {
175: return isEmergencyClosing;
176: }
177:
178: /* (non-Javadoc)
179: * @see org.eclipse.ui.application.IWorkbenchConfigurer#restoreState()
180: */
181: public IStatus restoreState() {
182: return ((Workbench) getWorkbench()).restoreState();
183: }
184:
185: /* (non-Javadoc)
186: * @see org.eclipse.ui.application.IWorkbenchConfigurer#openFirstTimeWindow()
187: */
188: public void openFirstTimeWindow() {
189: ((Workbench) getWorkbench()).openFirstTimeWindow();
190: }
191:
192: /* (non-Javadoc)
193: * @see org.eclipse.ui.application.IWorkbenchConfigurer#restoreWorkbenchWindow(org.eclipse.ui.IMemento)
194: */
195: public IWorkbenchWindowConfigurer restoreWorkbenchWindow(
196: IMemento memento) throws WorkbenchException {
197: return getWindowConfigurer(((Workbench) getWorkbench())
198: .restoreWorkbenchWindow(memento));
199: }
200:
201: /* (non-Javadoc)
202: * @see org.eclipse.ui.application.IWorkbenchConfigurer#getExitOnLastWindowClose()
203: */
204: public boolean getExitOnLastWindowClose() {
205: return exitOnLastWindowClose;
206: }
207:
208: /* (non-Javadoc)
209: * @see org.eclipse.ui.application.IWorkbenchConfigurer#setExitOnLastWindowClose(boolean)
210: */
211: public void setExitOnLastWindowClose(boolean enabled) {
212: exitOnLastWindowClose = enabled;
213: }
214: }
|