001: /*
002: * Copyright 2006-2007 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt.
007: *
008: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
009: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
010: * the license for the specific language governing your rights and limitations.
011: *
012: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
013: */
014: package org.pentaho.reportdesigner.lib.client.commands;
015:
016: import org.jetbrains.annotations.NonNls;
017: import org.jetbrains.annotations.NotNull;
018: import org.jetbrains.annotations.Nullable;
019: import org.pentaho.reportdesigner.lib.client.components.Category;
020: import org.pentaho.reportdesigner.lib.client.components.favoritespanel.FavoritesPanel;
021:
022: import javax.swing.*;
023: import java.awt.*;
024: import java.beans.PropertyChangeEvent;
025: import java.beans.PropertyChangeListener;
026:
027: /**
028: * User: Martin
029: * Date: 19.11.2004
030: * Time: 11:03:30
031: */
032: public class CommandManager {
033:
034: @NotNull
035: private static final CommandRegistryManager commandRegistryManager = new CommandRegistryManager();
036:
037: @NotNull
038: private static Component lastFocusedComponent;
039:
040: @NotNull
041: private static final TaskCoalescHelper taskCoalescHelper = new TaskCoalescHelper(
042: 50, true);
043:
044: private static int requestedRefreshes;
045: private static int executedRefreshes;
046: private static long usedRefreshNanos;
047:
048: @NotNull
049: private static final String PERMANENT_FOCUS_OWNER = "permanentFocusOwner";
050:
051: private CommandManager() {
052: }
053:
054: public static void addCommandToGroup(@NotNull
055: CommandApplicationRoot commandApplicationRoot, @NotNull
056: @NonNls
057: String place, @NotNull
058: Command command, @NotNull
059: CommandConstraint commandConstraint) {
060: ApplicationCommandRegistry applicationCommandRegistry = commandRegistryManager
061: .getApplicationCommandRegistry(commandApplicationRoot
062: .getApplicationID());
063: DefaultCommandGroup defaultCommandGroup = applicationCommandRegistry
064: .getDefaultCommandGroup(place);
065: defaultCommandGroup.addChild(command, commandConstraint);
066: }
067:
068: public static void registerCommandGroup(@NotNull
069: CommandApplicationRoot commandApplicationRoot, @NotNull
070: @NonNls
071: String place, @NotNull
072: DefaultCommandGroup defaultCommandGroup) {
073: ApplicationCommandRegistry applicationCommandRegistry = commandRegistryManager
074: .getApplicationCommandRegistry(commandApplicationRoot
075: .getApplicationID());
076: applicationCommandRegistry.registerDefaultCommandGroup(place,
077: defaultCommandGroup);
078: }
079:
080: public static void removeCommandFromGroup(@NotNull
081: CommandApplicationRoot commandApplicationRoot, @NotNull
082: @NonNls
083: String place, @NotNull
084: Command command) {
085: ApplicationCommandRegistry applicationCommandRegistry = commandRegistryManager
086: .getApplicationCommandRegistry(commandApplicationRoot
087: .getApplicationID());
088: DefaultCommandGroup defaultCommandGroup = applicationCommandRegistry
089: .getDefaultCommandGroup(place);
090: defaultCommandGroup.removeChild(command);
091: }
092:
093: @NotNull
094: public static DefaultCommandGroup getCommandGroup(@NotNull
095: CommandApplicationRoot commandApplicationRoot, @NotNull
096: @NonNls
097: String place) {
098: ApplicationCommandRegistry applicationCommandRegistry = commandRegistryManager
099: .getApplicationCommandRegistry(commandApplicationRoot
100: .getApplicationID());
101: return applicationCommandRegistry.getDefaultCommandGroup(place);
102: }
103:
104: public static void initAWTListeners() {
105: KeyboardFocusManager.getCurrentKeyboardFocusManager()
106: .addPropertyChangeListener(
107: new PropertyChangeListener() {
108: public void propertyChange(@NotNull
109: PropertyChangeEvent evt) {
110: if (PERMANENT_FOCUS_OWNER.equals(evt
111: .getPropertyName())) {
112: lastFocusedComponent = KeyboardFocusManager
113: .getCurrentKeyboardFocusManager()
114: .getPermanentFocusOwner();
115: dataProviderChanged();
116: }
117: }
118: });
119: }
120:
121: @NotNull
122: public static DataProvider getCurrentCommandProvider() {
123: if (CommandManager.lastFocusedComponent != null) {
124: Component cc = CommandManager.lastFocusedComponent;
125:
126: while (cc != null) {
127: if (cc instanceof DataProvider) {
128: return (DataProvider) cc;
129: }
130: cc = cc.getParent();
131: }
132: }
133:
134: return new NullDataProvider();
135: }
136:
137: //ToolBars
138: @NotNull
139: private static final WeakList<CommandToolBar> commandToolBars = new WeakList<CommandToolBar>();
140:
141: @NotNull
142: public static CommandToolBar createCommandToolBar(@NotNull
143: CommandApplicationRoot commandApplicationRoot, @NotNull
144: DataProvider dataProvider, @NotNull
145: @NonNls
146: String place) {
147: ApplicationCommandRegistry applicationCommandRegistry = commandRegistryManager
148: .getApplicationCommandRegistry(commandApplicationRoot
149: .getApplicationID());
150: DefaultCommandGroup defaultCommandGroup = applicationCommandRegistry
151: .getDefaultCommandGroup(place);
152: return createCommandToolBar(commandApplicationRoot,
153: dataProvider, defaultCommandGroup);
154: }
155:
156: @NotNull
157: public static CommandToolBar createCommandToolBar(@NotNull
158: CommandApplicationRoot commandApplicationRoot, @NotNull
159: @NonNls
160: String place) {
161: ApplicationCommandRegistry applicationCommandRegistry = commandRegistryManager
162: .getApplicationCommandRegistry(commandApplicationRoot
163: .getApplicationID());
164: DefaultCommandGroup defaultCommandGroup = applicationCommandRegistry
165: .getDefaultCommandGroup(place);
166: return createCommandToolBar(commandApplicationRoot, null,
167: defaultCommandGroup);
168: }
169:
170: @NotNull
171: private static CommandToolBar createCommandToolBar(@NotNull
172: CommandApplicationRoot commandApplicationRoot, @Nullable
173: DataProvider dataProvider, @NotNull
174: Command command) {
175: CommandToolBar commandToolBar = new CommandToolBar(
176: commandApplicationRoot, dataProvider, command);
177: commandToolBars.add(commandToolBar);
178: return commandToolBar;
179: }
180:
181: //MenuBars
182: @NotNull
183: private static final WeakList<CommandMenuBar> commandMenuBars = new WeakList<CommandMenuBar>();
184:
185: @NotNull
186: public static CommandMenuBar createCommandMenuBar(@NotNull
187: CommandApplicationRoot commandApplicationRoot, @NotNull
188: @NonNls
189: String place) {
190: ApplicationCommandRegistry applicationCommandRegistry = commandRegistryManager
191: .getApplicationCommandRegistry(commandApplicationRoot
192: .getApplicationID());
193: DefaultCommandGroup defaultCommandGroup = applicationCommandRegistry
194: .getDefaultCommandGroup(place);
195: return createCommandMenuBar(commandApplicationRoot,
196: defaultCommandGroup);
197: }
198:
199: @NotNull
200: private static CommandMenuBar createCommandMenuBar(@NotNull
201: CommandApplicationRoot commandApplicationRoot, @NotNull
202: Command command) {
203: CommandMenuBar commandMenuBar = new CommandMenuBar(
204: commandApplicationRoot, command);
205: commandMenuBars.add(commandMenuBar);
206: return commandMenuBar;
207: }
208:
209: //CommandCategories
210: @NotNull
211: private static final WeakList<CommandCategories> commandCategoriesList = new WeakList<CommandCategories>();
212:
213: @NotNull
214: public static CommandCategories createCommandCategories(@NotNull
215: CommandApplicationRoot commandApplicationRoot, @NotNull
216: @NonNls
217: String place, @NotNull
218: FavoritesPanel favoritesPanel, @NotNull
219: String iconKey) {
220: ApplicationCommandRegistry applicationCommandRegistry = commandRegistryManager
221: .getApplicationCommandRegistry(commandApplicationRoot
222: .getApplicationID());
223: DefaultCommandGroup defaultCommandGroup = applicationCommandRegistry
224: .getDefaultCommandGroup(place);
225:
226: CommandCategories commandCategories = new CommandCategories(
227: commandApplicationRoot, defaultCommandGroup,
228: favoritesPanel, iconKey);
229: commandCategoriesList.add(commandCategories);
230: return commandCategories;
231: }
232:
233: //Popup
234: @NotNull
235: public static CommandPopupMenu createCommandPopupMenu(@NotNull
236: CommandApplicationRoot commandApplicationRoot, @NotNull
237: @NonNls
238: String place, @NotNull
239: String iconKey) {
240: ApplicationCommandRegistry applicationCommandRegistry = commandRegistryManager
241: .getApplicationCommandRegistry(commandApplicationRoot
242: .getApplicationID());
243: DefaultCommandGroup defaultCommandGroup = applicationCommandRegistry
244: .getDefaultCommandGroup(place);
245: return createCommandPopupMenu(commandApplicationRoot,
246: defaultCommandGroup, iconKey);
247: }
248:
249: @NotNull
250: public static CommandPopupMenu createCommandPopupMenu(@NotNull
251: CommandApplicationRoot commandApplicationRoot, @NotNull
252: Command command, @NotNull
253: String iconKey) {
254: //a popup menu is temporary, commandproviders should not change while a popup is visible
255: return new CommandPopupMenu(commandApplicationRoot, command,
256: iconKey);
257: }
258:
259: @NotNull
260: private static final WeakList<CommandButton> commandButtons = new WeakList<CommandButton>();
261:
262: @NotNull
263: public static JButton createCommandButtonContextual(@NotNull
264: CommandApplicationRoot commandApplicationRoot, @NotNull
265: Command command, @NotNull
266: String iconKey, @NotNull
267: ButtonType buttonType) {
268: CommandButton commandButton = new CommandButton(
269: commandApplicationRoot, null, command, iconKey,
270: buttonType);
271: commandButtons.add(commandButton);
272: return commandButton;
273: }
274:
275: @NotNull
276: public static JButton createCommandButton(@NotNull
277: CommandApplicationRoot commandApplicationRoot, @NotNull
278: DataProvider dataProvider, @NotNull
279: Command command, @NotNull
280: String iconKey, @NotNull
281: ButtonType buttonType) {
282: CommandButton commandButton = new CommandButton(
283: commandApplicationRoot, dataProvider, command, iconKey,
284: buttonType);
285: commandButtons.add(commandButton);
286: return commandButton;
287: }
288:
289: //ActionComponentsCommandHelper
290: @NotNull
291: private static final WeakList<ActionComponentsCommandHelper> commandActionComponentsCommandHelperList = new WeakList<ActionComponentsCommandHelper>();
292:
293: @NotNull
294: public static ActionComponentsCommandHelper createActionComponentsCommandHelperContextual(
295: @NotNull
296: CommandApplicationRoot commandApplicationRoot, @NotNull
297: @NonNls
298: String place, @NotNull
299: Category<?> category, @NotNull
300: String iconKey) {
301: ApplicationCommandRegistry applicationCommandRegistry = commandRegistryManager
302: .getApplicationCommandRegistry(commandApplicationRoot
303: .getApplicationID());
304: DefaultCommandGroup defaultCommandGroup = applicationCommandRegistry
305: .getDefaultCommandGroup(place);
306:
307: ActionComponentsCommandHelper commandCategories = new ActionComponentsCommandHelper(
308: commandApplicationRoot, null, defaultCommandGroup,
309: category, iconKey);
310: commandActionComponentsCommandHelperList.add(commandCategories);
311: return commandCategories;
312: }
313:
314: @NotNull
315: public static ActionComponentsCommandHelper createActionComponentsCommandHelper(
316: @NotNull
317: CommandApplicationRoot commandApplicationRoot, @NotNull
318: DataProvider dataProvider, @NotNull
319: @NonNls
320: String place, @NotNull
321: Category<?> category, @NotNull
322: String iconKey) {
323: ApplicationCommandRegistry applicationCommandRegistry = commandRegistryManager
324: .getApplicationCommandRegistry(commandApplicationRoot
325: .getApplicationID());
326: DefaultCommandGroup defaultCommandGroup = applicationCommandRegistry
327: .getDefaultCommandGroup(place);
328:
329: ActionComponentsCommandHelper commandCategories = new ActionComponentsCommandHelper(
330: commandApplicationRoot, dataProvider,
331: defaultCommandGroup, category, iconKey);
332: commandActionComponentsCommandHelperList.add(commandCategories);
333: return commandCategories;
334: }
335:
336: /**
337: * Use when a selection within a light weight component like JTree, JList, JTable changes, and focus does not change.
338: * It is safe to call this method whenever/as many times as you like.
339: * Refresh requests are coalesced to prevent performance issues.
340: */
341: public static void dataProviderChanged() {
342: requestedRefreshes++;
343: taskCoalescHelper
344: .addTask(new TaskCoalescHelper.CoalesceableTask() {
345: public void run() {
346: refresh();
347: }
348: });
349: }
350:
351: private static void refresh() {
352: long l1 = System.nanoTime();
353: DataProvider dataProvider = getCurrentCommandProvider();
354:
355: //update toolbars
356: for (CommandToolBar commandToolBar : commandToolBars) {
357: commandToolBar.update(dataProvider);
358: }
359:
360: //update menubars
361: for (CommandMenuBar commandMenuBar : commandMenuBars) {
362: commandMenuBar.update(dataProvider);
363: }
364:
365: //update commandCategories
366: for (CommandCategories commandCategories : commandCategoriesList) {
367: commandCategories.update(dataProvider);
368: }
369:
370: for (CommandButton commandButton : commandButtons) {
371: commandButton.update(dataProvider);
372: }
373:
374: for (ActionComponentsCommandHelper actionComponentsCommandHelper : commandActionComponentsCommandHelperList) {
375: actionComponentsCommandHelper.update(dataProvider);
376: }
377: long l2 = System.nanoTime();
378:
379: usedRefreshNanos += l2 - l1;
380: executedRefreshes++;
381: }
382:
383: public static int getRequestedRefreshes() {
384: return requestedRefreshes;
385: }
386:
387: public static int getExecutedRefreshes() {
388: return executedRefreshes;
389: }
390:
391: public static long getUsedRefreshNanos() {
392: return usedRefreshNanos;
393: }
394:
395: public static void resetStatistics() {
396: requestedRefreshes = 0;
397: executedRefreshes = 0;
398: usedRefreshNanos = 0;
399: }
400: }
|