001: /*******************************************************************************
002: * Copyright (c) 2003, 2006 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.actions;
011:
012: import org.eclipse.jface.action.IContributionItem;
013: import org.eclipse.ui.IWorkbenchWindow;
014: import org.eclipse.ui.internal.ChangeToPerspectiveMenu;
015: import org.eclipse.ui.internal.PinEditorAction;
016: import org.eclipse.ui.internal.ReopenEditorMenu;
017: import org.eclipse.ui.internal.ShowInMenu;
018: import org.eclipse.ui.internal.ShowViewMenu;
019: import org.eclipse.ui.internal.SwitchToWindowMenu;
020: import org.eclipse.ui.internal.actions.HelpSearchContributionItem;
021: import org.eclipse.ui.internal.actions.PinEditorContributionItem;
022:
023: /**
024: * Access to standard contribution items provided by the workbench.
025: * <p>
026: * Most of the functionality of this class is provided by
027: * static methods and fields.
028: * Example usage:
029: * <pre>
030: * MenuManager menu = ...;
031: * IContributionItem reEdit
032: * = ContributionItemFactory.REOPEN_EDITORS.create(window);
033: * menu.add(reEdit);
034: * </pre>
035: * </p>
036: * <p>
037: * Clients may declare subclasses that provide additional application-specific
038: * contribution item factories.
039: * </p>
040: *
041: * @since 3.0
042: */
043: public abstract class ContributionItemFactory {
044:
045: /**
046: * Id of contribution items created by this factory.
047: */
048: private final String contributionItemId;
049:
050: /**
051: * Creates a new workbench contribution item factory with the given id.
052: *
053: * @param contributionItemId the id of contribution items created by this factory
054: */
055: protected ContributionItemFactory(String contributionItemId) {
056: this .contributionItemId = contributionItemId;
057: }
058:
059: /**
060: * Creates a new standard contribution item for the given workbench window.
061: * <p>
062: * A typical contribution item automatically registers listeners against the
063: * workbench window so that it can keep its enablement state up to date.
064: * Ordinarily, the window's references to these listeners will be dropped
065: * automatically when the window closes. However, if the client needs to get
066: * rid of a contribution item while the window is still open, the client must
067: * call IContributionItem#dispose to give the item an
068: * opportunity to deregister its listeners and to perform any other cleanup.
069: * </p>
070: *
071: * @param window the workbench window
072: * @return the workbench contribution item
073: */
074: public abstract IContributionItem create(IWorkbenchWindow window);
075:
076: /**
077: * Returns the id of this contribution item factory.
078: *
079: * @return the id of contribution items created by this factory
080: */
081: public String getId() {
082: return contributionItemId;
083: }
084:
085: /**
086: * Workbench action (id "pinEditor"): Toggle whether the editor is pinned.
087: * This action maintains its enablement state.
088: */
089: public static final ContributionItemFactory PIN_EDITOR = new ContributionItemFactory(
090: "pinEditor") { //$NON-NLS-1$
091: /* (non-javadoc) method declared on ContributionItemFactory */
092: public IContributionItem create(IWorkbenchWindow window) {
093: if (window == null) {
094: throw new IllegalArgumentException();
095: }
096: PinEditorAction action = new PinEditorAction(window);
097: action.setId(getId());
098: return new PinEditorContributionItem(action, window);
099: }
100: };
101:
102: /**
103: * Workbench contribution item (id "openWindows"): A list of windows
104: * currently open in the workbench. Selecting one of the items makes the
105: * corresponding window the active window.
106: * This action dynamically maintains the list of windows.
107: */
108: public static final ContributionItemFactory OPEN_WINDOWS = new ContributionItemFactory(
109: "openWindows") { //$NON-NLS-1$
110: /* (non-javadoc) method declared on ContributionItemFactory */
111: public IContributionItem create(IWorkbenchWindow window) {
112: if (window == null) {
113: throw new IllegalArgumentException();
114: }
115: return new SwitchToWindowMenu(window, getId(), true);
116: }
117: };
118:
119: /**
120: * Workbench contribution item (id "viewsShortlist"): A list of views
121: * available to be opened in the window, arranged as a shortlist of
122: * promising views and an "Other" subitem. Selecting
123: * one of the items opens the corresponding view in the active window.
124: * This action dynamically maintains the view shortlist.
125: */
126: public static final ContributionItemFactory VIEWS_SHORTLIST = new ContributionItemFactory(
127: "viewsShortlist") { //$NON-NLS-1$
128: /* (non-javadoc) method declared on ContributionItemFactory */
129: public IContributionItem create(IWorkbenchWindow window) {
130: if (window == null) {
131: throw new IllegalArgumentException();
132: }
133: return new ShowViewMenu(window, getId());
134: }
135: };
136:
137: /**
138: * Workbench contribution item (id "viewsShowIn"): A list of views
139: * available to be opened in the window, arranged as a list of
140: * alternate views to show the same item currently selected. Selecting
141: * one of the items opens the corresponding view in the active window.
142: * This action dynamically maintains the view list.
143: */
144: public static final ContributionItemFactory VIEWS_SHOW_IN = new ContributionItemFactory(
145: "viewsShowIn") { //$NON-NLS-1$
146: /* (non-javadoc) method declared on ContributionItemFactory */
147: public IContributionItem create(IWorkbenchWindow window) {
148: if (window == null) {
149: throw new IllegalArgumentException();
150: }
151: return new ShowInMenu(window, getId());
152: }
153: };
154:
155: /**
156: * Workbench contribution item (id "reopenEditors"): A list of recent
157: * editors (with inputs) available to be reopened in the window. Selecting
158: * one of the items reopens the corresponding editor on its input in the
159: * active window. This action dynamically maintains the list of editors.
160: */
161: public static final ContributionItemFactory REOPEN_EDITORS = new ContributionItemFactory(
162: "reopenEditors") { //$NON-NLS-1$
163: /* (non-javadoc) method declared on ContributionItemFactory */
164: public IContributionItem create(IWorkbenchWindow window) {
165: if (window == null) {
166: throw new IllegalArgumentException();
167: }
168: return new ReopenEditorMenu(window, getId(), true);
169: }
170: };
171:
172: /**
173: * Workbench contribution item (id "perspectivesShortlist"): A list of
174: * perspectives available to be opened, arranged as a shortlist of
175: * promising perspectives and an "Other" subitem. Selecting
176: * one of the items makes the corresponding perspective active. Should a
177: * new perspective need to be opened, a workbench user preference controls
178: * whether the prespective is opened in the active window or a new window.
179: * This action dynamically maintains the perspectives shortlist.
180: */
181: public static final ContributionItemFactory PERSPECTIVES_SHORTLIST = new ContributionItemFactory(
182: "perspectivesShortlist") { //$NON-NLS-1$
183: /* (non-javadoc) method declared on ContributionItemFactory */
184: public IContributionItem create(IWorkbenchWindow window) {
185: if (window == null) {
186: throw new IllegalArgumentException();
187: }
188: return new ChangeToPerspectiveMenu(window, getId());
189: }
190: };
191:
192: /**
193: * Workbench contribution item (id "newWizardShortlist"): A list of
194: * new item wizards available to be opened, arranged as a shortlist of
195: * promising new item wizards and an "Other" subitem. Selecting
196: * one of the items invokes the corresponding new item wizard.
197: * This action dynamically maintains the new item wizard shortlist.
198: * @since 3.1
199: */
200: public static final ContributionItemFactory NEW_WIZARD_SHORTLIST = new ContributionItemFactory(
201: "newWizardShortlist") { //$NON-NLS-1$
202: /* (non-javadoc) method declared on ContributionItemFactory */
203: public IContributionItem create(IWorkbenchWindow window) {
204: if (window == null) {
205: throw new IllegalArgumentException();
206: }
207: return new BaseNewWizardMenu(window, getId());
208: }
209: };
210:
211: /**
212: * Workbench contribution item (id "helpSearch"): An editable field
213: * for entering help search queries.
214: * @since 3.1
215: */
216: public static final ContributionItemFactory HELP_SEARCH = new ContributionItemFactory(
217: "helpSearch") {//$NON-NLS-1$
218: public IContributionItem create(IWorkbenchWindow window) {
219: if (window == null) {
220: throw new IllegalArgumentException();
221: }
222: return new HelpSearchContributionItem(window, getId());
223: }
224: };
225:
226: }
|