]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.message.ui/src/org/simantics/message/ui/ImportLogAction.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.message.ui / src / org / simantics / message / ui / ImportLogAction.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.message.ui;
13
14 import java.io.File;
15 import java.util.*;
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;
21
22 /**
23  * Imports log to Log View from such sources as file in local file system, file in workspace,
24  * files from log files manager.
25  */
26 public class ImportLogAction extends Action implements IMenuCreator {
27
28         private Menu toolbarMenu = null;
29         private Menu popupMenu = null;
30
31         /**
32          * View to import logs to.
33          */
34         private final LogView logView;
35         private ImportConfigurationLogAction[] actions;
36         private IMemento fMemento;
37
38         /**
39          * Action imports log file from given location to Log View.
40          */
41         private class ImportConfigurationLogAction extends Action {
42                 private String name;
43                 private String location;
44
45                 public ImportConfigurationLogAction(String name, String location) {
46                         super(name, AS_RADIO_BUTTON);
47                         this.name = name;
48                         this.location = location;
49                         setId(name + "#" + location); //$NON-NLS-1$
50                 }
51
52                 /*
53                  * (non-Javadoc)
54                  * @see org.eclipse.jface.action.Action#run()
55                  */
56                 public void run() {
57                         logView.handleImportPath(location);
58                         if (isChecked()) {
59                                 // remember we clicked on that item
60                                 fMemento.putString(LogView.P_IMPORT_LOG, getId());
61                         }
62                 }
63
64                 /*
65                  * (non-Javadoc)
66                  * @see java.lang.Object#equals(java.lang.Object)
67                  */
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);
72                         }
73
74                         return false;
75                 }
76         }
77
78         public ImportLogAction(LogView logView, String text, IMemento memento) {
79                 super(text);
80                 this.logView = logView;
81                 this.fMemento = memento;
82                 setMenuCreator(this);
83         }
84
85         /*
86          * (non-Javadoc)
87          * @see org.eclipse.jface.action.Action#run()
88          */
89         public void run() {
90                 // by default import file selected by user
91                 logView.handleImport();
92         }
93
94         /*
95          * (non-Javadoc)
96          * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Control)
97          */
98         public Menu getMenu(Control parent) {
99                 if (menuUpdateNeeded(toolbarMenu)) {
100                         toolbarMenu = new Menu(parent);
101                         createMenuItems(toolbarMenu);
102                 }
103                 return toolbarMenu;
104         }
105
106         /*
107          * (non-Javadoc)
108          * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Menu)
109          */
110         public Menu getMenu(Menu parent) {
111                 if (menuUpdateNeeded(popupMenu)) {
112                         popupMenu = new Menu(parent);
113                         createMenuItems(popupMenu);
114                 }
115                 return popupMenu;
116         }
117
118         /**
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
122          */
123         private boolean menuUpdateNeeded(Menu menu) {
124                 boolean result = false;
125
126                 ImportConfigurationLogAction[] currActions = getLogActions();
127
128                 if (menu == null) {
129                         result = true;
130                 } else if (actions == null) {
131                         result = true;
132                 } else if (currActions.length != actions.length) {
133                         result = true;
134                 } else {
135                         for (int i = 0; i < currActions.length; i++) {
136                                 if (!currActions[i].equals(actions[i])) {
137                                         result = true;
138                                 }
139                         }
140                 }
141
142                 if (result == true) {
143                         actions = currActions;
144
145                         if (toolbarMenu != null) {
146                                 toolbarMenu.dispose();
147                                 toolbarMenu = null;
148                         }
149                         if (popupMenu != null) {
150                                 popupMenu.dispose();
151                                 popupMenu = null;
152                         }
153                 }
154
155                 return result;
156         }
157
158         /**
159          * Returns list of all actions from LogFilesManager.
160          * @return list of all actions from LogFilesManager
161          */
162         private ImportConfigurationLogAction[] getLogActions() {
163                 List<Object> result = new ArrayList<Object>();
164                 Map<String, File> sources = LogFilesManager.getLogSources();
165
166                 for (Iterator<String> j = sources.keySet().iterator(); j.hasNext();) {
167 //                      String name = j.next();
168             // FIXME: WTF ???
169 //                      File location = sources.get(name);
170 //                      result.add(new ImportConfigurationLogAction(name, location));
171                 }
172
173                 return (ImportConfigurationLogAction[]) result.toArray(new ImportConfigurationLogAction[result.size()]);
174         }
175
176         /**
177          * Builds menu of ImportLogAction actions from log files provided by LogFilesManager.
178          * 
179          * @see IMenuCreator#getMenu(Control)
180          */
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) {
185                                 // dummy action
186                         };
187                         action.setEnabled(false);
188                         ActionContributionItem actionItem = new ActionContributionItem(action);
189                         actionItem.fill(menu, -1);
190                 } else {
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]);
194                                 item.fill(menu, -1);
195                         }
196                 }
197
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);
202                 item.fill(menu, -1);
203         }
204
205         /*
206          * (non-Javadoc)
207          * @see org.eclipse.jface.action.IMenuCreator#dispose()
208          */
209         public void dispose() {
210                 if (toolbarMenu != null) {
211                         toolbarMenu.dispose();
212                         toolbarMenu = null;
213                 }
214                 if (popupMenu != null) {
215                         popupMenu.dispose();
216                         popupMenu = null;
217                 }
218         }
219 }