1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.message.ui;
16 import org.eclipse.core.runtime.Platform;
17 import org.eclipse.jface.action.*;
18 import org.eclipse.swt.widgets.Control;
19 import org.eclipse.swt.widgets.Menu;
20 import org.eclipse.ui.IMemento;
23 * Imports log to Log View from such sources as file in local file system, file in workspace,
24 * files from log files manager.
26 public class ImportLogAction extends Action implements IMenuCreator {
28 private Menu toolbarMenu = null;
29 private Menu popupMenu = null;
32 * View to import logs to.
34 private final LogView logView;
35 private ImportConfigurationLogAction[] actions;
36 private IMemento fMemento;
39 * Action imports log file from given location to Log View.
41 private class ImportConfigurationLogAction extends Action {
43 private String location;
45 public ImportConfigurationLogAction(String name, String location) {
46 super(name, AS_RADIO_BUTTON);
48 this.location = location;
49 setId(name + "#" + location); //$NON-NLS-1$
54 * @see org.eclipse.jface.action.Action#run()
57 logView.handleImportPath(location);
59 // remember we clicked on that item
60 fMemento.putString(LogView.P_IMPORT_LOG, getId());
66 * @see java.lang.Object#equals(java.lang.Object)
68 public boolean equals(Object o) {
69 if (o instanceof ImportConfigurationLogAction) {
70 ImportConfigurationLogAction action = (ImportConfigurationLogAction) o;
71 return name.equals(action.name) && location.equals(action.name);
78 public ImportLogAction(LogView logView, String text, IMemento memento) {
80 this.logView = logView;
81 this.fMemento = memento;
87 * @see org.eclipse.jface.action.Action#run()
90 // by default import file selected by user
91 logView.handleImport();
96 * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Control)
98 public Menu getMenu(Control parent) {
99 if (menuUpdateNeeded(toolbarMenu)) {
100 toolbarMenu = new Menu(parent);
101 createMenuItems(toolbarMenu);
108 * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Menu)
110 public Menu getMenu(Menu parent) {
111 if (menuUpdateNeeded(popupMenu)) {
112 popupMenu = new Menu(parent);
113 createMenuItems(popupMenu);
119 * Returns whether menu should be updated or not. Menu should be updated
120 * if either number of actions or any of actions has been changed.
121 * @return true if menu should be updated, false otherwise
123 private boolean menuUpdateNeeded(Menu menu) {
124 boolean result = false;
126 ImportConfigurationLogAction[] currActions = getLogActions();
130 } else if (actions == null) {
132 } else if (currActions.length != actions.length) {
135 for (int i = 0; i < currActions.length; i++) {
136 if (!currActions[i].equals(actions[i])) {
142 if (result == true) {
143 actions = currActions;
145 if (toolbarMenu != null) {
146 toolbarMenu.dispose();
149 if (popupMenu != null) {
159 * Returns list of all actions from LogFilesManager.
160 * @return list of all actions from LogFilesManager
162 private ImportConfigurationLogAction[] getLogActions() {
163 List<Object> result = new ArrayList<Object>();
164 Map<String, File> sources = LogFilesManager.getLogSources();
166 for (Iterator<String> j = sources.keySet().iterator(); j.hasNext();) {
167 // String name = j.next();
169 // File location = sources.get(name);
170 // result.add(new ImportConfigurationLogAction(name, location));
173 return (ImportConfigurationLogAction[]) result.toArray(new ImportConfigurationLogAction[result.size()]);
177 * Builds menu of ImportLogAction actions from log files provided by LogFilesManager.
179 * @see IMenuCreator#getMenu(Control)
181 private void createMenuItems(Menu menu) {
182 String previouslyCheckedActionId = fMemento.getString(LogView.P_IMPORT_LOG);
183 if (actions.length == 0) {
184 Action action = new Action(Messages.ImportLogAction_noLaunchHistory) {
187 action.setEnabled(false);
188 ActionContributionItem actionItem = new ActionContributionItem(action);
189 actionItem.fill(menu, -1);
191 for (int i = 0; i < actions.length; i++) {
192 actions[i].setChecked(actions[i].getId().equals(previouslyCheckedActionId) && !logView.isPlatformLogOpen());
193 ActionContributionItem item = new ActionContributionItem(actions[i]);
198 (new Separator()).fill(menu, -1);
199 ImportConfigurationLogAction importWorkspaceLogAction = new ImportConfigurationLogAction(Messages.ImportLogAction_reloadWorkspaceLog, Platform.getLogFileLocation().toFile().getAbsolutePath());
200 importWorkspaceLogAction.setChecked(logView.isPlatformLogOpen());
201 ActionContributionItem item = new ActionContributionItem(importWorkspaceLogAction);
207 * @see org.eclipse.jface.action.IMenuCreator#dispose()
209 public void dispose() {
210 if (toolbarMenu != null) {
211 toolbarMenu.dispose();
214 if (popupMenu != null) {