-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSessionComponent.java
318 lines (269 loc) · 10.7 KB
/
SessionComponent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package com.squiek.idea.plugin.tabsession;
import com.squiek.idea.plugin.tabsession.ui.LoadSessionDialog;
import com.squiek.idea.plugin.tabsession.ui.SaveSessionDialog;
import com.squiek.idea.plugin.tabsession.ui.SessionConfiguration;
import com.intellij.openapi.components.*;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.ui.popup.Balloon;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.WindowManager;
import com.intellij.ui.awt.RelativePoint;
import org.jdom.Element;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* created by alp (29.03.2013)
*/
@State(
name = "SessionConfiguration",
storages = {
@Storage(id = "default", file = StoragePathMacros.PROJECT_FILE),
@Storage(id = "dir", file = StoragePathMacros.PROJECT_CONFIG_DIR + "/tabsession.xml", scheme = StorageScheme.DIRECTORY_BASED)
}
)
public class SessionComponent implements ProjectComponent, Configurable, PersistentStateComponent<Element> {
/*
* Initialization and Component Configuration
*/
public static final int STATE_VERSION = 1;
public static final String ERROR_MESSAGE_NAME_EMPTY = "Session name can't be empty";
public static final String ERROR_MESSAGE_NAME_EXISTS = "Session with that name already exists";
public static final String ERROR_MESSAGE_NAME_EXISTS_OVERWRITE = "Session with that name already exists and will be overwritten";
public static final String ERROR_MESSAGE_FILE_EMPTY = "File path can't be empty";
public static final String ERROR_MESSAGE_FILE_EXISTS = "File path already exists in Session";
public static final String ERROR_MESSAGE_FILE_WRONG_PATH = "Entered path is not an existing file";
private static Logger LOG = Logger.getInstance(SessionComponent.class);
private Project project;
private SessionState sessionState;
private SessionConfiguration form;
public SessionComponent(Project project) {
this.project = project;
sessionState = new SessionState();
}
@NotNull
public String getComponentName() {
return "SessionComponent";
}
public void initComponent() {
// TODO: insert component initialization logic here
}
public void disposeComponent() {
// TODO: insert component disposal logic here
}
public void projectOpened() {
// called when project is opened
}
public void projectClosed() {
// called when project is being closed
}
public SessionState getSessionState() {
return sessionState;
}
/*
* Component Persistence
*/
@Override
public void loadState(Element element) {
LOG.debug("Loading state");
sessionState = new SessionState();
// meta configuration information
Element stateElement = element.getChild("state");
// version not needed yet
// Integer stateVersion = Integer.valueOf(stateElement.getAttributeValue("version"));
// session data
Element sessionsElement = element.getChild("sessions");
for (Object se : sessionsElement.getChildren("session")) {
Element sessionElement = (Element) se;
if(sessionElement != null) {
Element filesElement = sessionElement.getChild("files");
if(filesElement != null) {
SessionState.Session session = sessionState.getOrCreateSessionByName(sessionElement.getAttributeValue("name"));
for (Object fe : filesElement.getChildren("file")) {
Element fileElement = (Element) fe;
session.files.add(fileElement.getText());
}
session.focusedFile = filesElement.getAttributeValue("focusedFile");
}
}
}
}
@Nullable
@Override
public Element getState() {
LOG.debug("Saving state");
final Element element = new Element("tabsession");
// meta configuration information
final Element stateElement = new Element("state");
stateElement.setAttribute("version", Integer.toString(STATE_VERSION));
element.addContent(stateElement);
// session data
final Element sessionsElement = new Element("sessions");
for(SessionState.Session session : sessionState.sessions) {
final Element sessionElement = new Element("session");
sessionElement.setAttribute("name", session.name);
final Element filesElement = new Element("files");
for(String path : session.files) {
final Element fileElement = new Element("file");
fileElement.setText(path);
filesElement.addContent(fileElement);
}
filesElement.setAttribute("focusedFile", session.focusedFile);
sessionElement.addContent(filesElement);
sessionsElement.addContent(sessionElement);
}
element.addContent(sessionsElement);
return element;
}
/*
* Save/Load logic
*/
public int saveCurrentTabs(String sessionName) {
Editor[] editors = getOpenEditors();
FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
VirtualFile[] selectedFiles = fileEditorManager.getSelectedFiles();
FileDocumentManager fileDocManager = FileDocumentManager.getInstance();
SessionState.Session session = sessionState.getOrCreateSessionByName(sessionName);
session.files.clear();
for(Editor editor : editors) {
VirtualFile vf = fileDocManager.getFile(editor.getDocument());
if(vf != null) {
String path = vf.getCanonicalPath();
if(path.equals(selectedFiles[0].getCanonicalPath())) {
session.focusedFile = path;
}
session.addFileIfNew(path);
}
}
return editors.length;
}
public int loadSession(String sessionName) {
closeCurrentTabs();
FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
VirtualFile focusedFile = null;
SessionState.Session session = sessionState.getOrCreateSessionByName(sessionName);
for(String path : session.files) {
VirtualFile vf = LocalFileSystem.getInstance().findFileByPath(path);
fileEditorManager.openFile(vf, false, true);
if(path.equals(session.focusedFile)) {
focusedFile = vf;
}
}
if(focusedFile != null) {
fileEditorManager.openFile(focusedFile, true, true);
}
return session.files.size();
}
public void closeCurrentTabs() {
Editor[] editors = getOpenEditors();
FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
FileDocumentManager fileDocManager = FileDocumentManager.getInstance();
for(Editor editor : editors) {
VirtualFile vf = fileDocManager.getFile(editor.getDocument());
if(vf != null) {
fileEditorManager.closeFile(vf);
}
}
}
private Editor[] getOpenEditors() {
return EditorFactory.getInstance().getAllEditors();
}
/*
* UI elements
*/
public void showMessage(String htmlText) {
JMenuBar menuBar = WindowManager.getInstance().getFrame(project).getJMenuBar();
JBPopupFactory.getInstance()
.createHtmlTextBalloonBuilder(htmlText, MessageType.INFO, null)
.setFadeoutTime(7500)
.setCloseButtonEnabled(true)
.createBalloon()
.show(RelativePoint.getCenterOf(menuBar), Balloon.Position.atRight);
}
public void showSaveDialog() {
JFrame frame = WindowManager.getInstance().getFrame(project);
final SaveSessionDialog saveDialog = new SaveSessionDialog();
saveDialog.setSavedSessions(sessionState.getSessionNames());
saveDialog.addOnOKListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String sessionName = saveDialog.getSessionName();
int tabCount = saveCurrentTabs(sessionName);
String htmlText = "Saved " + String.valueOf(tabCount) + " tabs in session \"" + sessionName + "\"";
showMessage(htmlText);
}
});
saveDialog.display(frame);
}
public void showLoadDialog() {
JFrame frame = WindowManager.getInstance().getFrame(project);
final LoadSessionDialog loadDialog = new LoadSessionDialog();
loadDialog.setSessionState(sessionState);
loadDialog.setProject(project);
loadDialog.setConfigurable(this);
loadDialog.addOnOKListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String sessionName = loadDialog.getSessionName();
int tabCount = loadSession(sessionName);
String htmlText = "Loaded " + String.valueOf(tabCount) + " tabs from session \"" + sessionName + "\"";
showMessage(htmlText);
}
});
loadDialog.display(frame);
}
/*
* IDEA Settings
*/
@Nls
@Override
public String getDisplayName() {
return "Tab Sessions";
}
@Nullable
@Override
public String getHelpTopic() {
return null;
}
@Nullable
@Override
public JComponent createComponent() {
if(form == null) {
form = new SessionConfiguration(project);
}
return form.getRootComponent();
}
@Override
public boolean isModified() {
return form != null && form.isModified();
}
@Override
public void apply() throws ConfigurationException {
if(form != null) {
sessionState = form.getData();
reset();
}
}
@Override
public void reset() {
if(form != null) {
form.setInitialState(sessionState);
form.mapStateToUI();
}
}
@Override
public void disposeUIResources() {
form = null;
}
}