Lines 10-15
Link Here
|
10 |
*******************************************************************************/ |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.ui.examples.rcp.browser; |
11 |
package org.eclipse.ui.examples.rcp.browser; |
12 |
|
12 |
|
|
|
13 |
import java.io.File; |
14 |
import java.io.FileInputStream; |
15 |
import java.io.FileNotFoundException; |
16 |
import java.io.FileOutputStream; |
17 |
import java.io.IOException; |
18 |
import java.io.InputStreamReader; |
19 |
import java.io.OutputStreamWriter; |
20 |
import java.io.UnsupportedEncodingException; |
21 |
|
22 |
import org.eclipse.core.runtime.IPath; |
23 |
import org.eclipse.core.runtime.IStatus; |
24 |
import org.eclipse.ui.IMemento; |
25 |
import org.eclipse.ui.WorkbenchException; |
26 |
import org.eclipse.ui.XMLMemento; |
13 |
import org.eclipse.ui.application.IWorkbenchConfigurer; |
27 |
import org.eclipse.ui.application.IWorkbenchConfigurer; |
14 |
import org.eclipse.ui.application.IWorkbenchWindowConfigurer; |
28 |
import org.eclipse.ui.application.IWorkbenchWindowConfigurer; |
15 |
import org.eclipse.ui.application.WorkbenchAdvisor; |
29 |
import org.eclipse.ui.application.WorkbenchAdvisor; |
Lines 25-30
Link Here
|
25 |
* @since 3.0 |
39 |
* @since 3.0 |
26 |
*/ |
40 |
*/ |
27 |
public class BrowserAdvisor extends WorkbenchAdvisor { |
41 |
public class BrowserAdvisor extends WorkbenchAdvisor { |
|
|
42 |
|
43 |
private File stateLocation = null; |
44 |
private String role = null; |
45 |
|
46 |
public IStatus restoreState(IMemento memento) { |
47 |
// restore state using the role based memento |
48 |
if (role != null) { |
49 |
File stateLocation = getStateLocation(); |
50 |
String stateFileName = role + ".config.xml"; |
51 |
File stateFile = new File(stateLocation.toString() + File.separator + stateFileName); |
52 |
if (stateFile.exists()) { |
53 |
IMemento roleMemento = null; |
54 |
try { |
55 |
roleMemento = XMLMemento.createReadRoot( new InputStreamReader ( |
56 |
new FileInputStream(stateFile),"UTF-8")); |
57 |
} catch (WorkbenchException e) { |
58 |
e.printStackTrace(); |
59 |
} catch (FileNotFoundException e) { |
60 |
// won't happen because we already checked it exists |
61 |
} catch (UnsupportedEncodingException e) { |
62 |
// not possible - UTF8 is required |
63 |
} |
64 |
if (roleMemento != null) |
65 |
return super.restoreState(roleMemento); |
66 |
} |
67 |
} |
68 |
return super.restoreState(memento); |
69 |
} |
70 |
|
71 |
public IStatus saveState(IMemento memento) { |
72 |
IStatus status = super.saveState(memento); |
73 |
|
74 |
// save state to store based on role |
75 |
if (role != null) { |
76 |
File stateLocation = getStateLocation(); |
77 |
ensureDirectoryExists(stateLocation); |
78 |
String stateFileName = stateLocation.getPath() + File.separator + role + ".config.xml"; |
79 |
|
80 |
OutputStreamWriter writer = null; |
81 |
try { |
82 |
writer = new OutputStreamWriter(new FileOutputStream(stateFileName),"UTF-8"); |
83 |
|
84 |
} catch (UnsupportedEncodingException e1) { |
85 |
// not possible, UTF-8 is required to be implemented by all JVMs |
86 |
} catch (FileNotFoundException e1) { |
87 |
// creating a new file, won't happen unless the path eclipse |
88 |
// specifies is totally wrong, or its read-only |
89 |
} |
90 |
XMLMemento xmlm = (XMLMemento)memento; |
91 |
try { |
92 |
xmlm.save(writer); |
93 |
writer.close(); |
94 |
} catch (IOException e) { |
95 |
e.printStackTrace(); |
96 |
} |
97 |
|
98 |
} |
99 |
|
100 |
|
101 |
return status; |
102 |
} |
28 |
|
103 |
|
29 |
/** |
104 |
/** |
30 |
* Constructs a new <code>BrowserAdvisor</code>. |
105 |
* Constructs a new <code>BrowserAdvisor</code>. |
Lines 38-44
Link Here
|
38 |
*/ |
113 |
*/ |
39 |
public void initialize(IWorkbenchConfigurer configurer) { |
114 |
public void initialize(IWorkbenchConfigurer configurer) { |
40 |
super.initialize(configurer); |
115 |
super.initialize(configurer); |
41 |
// configurer.setSaveAndRestore(true); |
116 |
String[] args = org.eclipse.core.runtime.Platform.getApplicationArgs(); |
|
|
117 |
processCommandLine(args); |
118 |
configurer.setSaveAndRestore(true); |
42 |
} |
119 |
} |
43 |
|
120 |
|
44 |
/* (non-Javadoc) |
121 |
/* (non-Javadoc) |
Lines 54-58
Link Here
|
54 |
public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor( |
131 |
public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor( |
55 |
IWorkbenchWindowConfigurer configurer) { |
132 |
IWorkbenchWindowConfigurer configurer) { |
56 |
return new BrowserWindowAdvisor(configurer); |
133 |
return new BrowserWindowAdvisor(configurer); |
|
|
134 |
} |
135 |
|
136 |
private synchronized File getStateLocation() { |
137 |
if (stateLocation == null) { |
138 |
IPath path = BrowserPlugin.getDefault().getStateLocation(); |
139 |
StringBuffer fileName = new StringBuffer(); |
140 |
fileName.append(File.separator); |
141 |
fileName.append("BrowserWindowState"); |
142 |
fileName.append(File.separator); |
143 |
stateLocation = path.append(fileName.toString()).toFile(); |
144 |
} |
145 |
return stateLocation; |
146 |
} |
147 |
|
148 |
private void ensureDirectoryExists(File dir) { |
149 |
dir.mkdirs(); |
150 |
} |
151 |
|
152 |
private void processCommandLine(String[] args) { |
153 |
for (int i = 0; i < args.length; i++) { |
154 |
// check role |
155 |
if (args[i].equalsIgnoreCase("-role")) { //$NON-NLS-1$ |
156 |
if ((i + 1) < args.length) |
157 |
role = args[i + 1]; |
158 |
} |
159 |
} |
57 |
} |
160 |
} |
58 |
} |
161 |
} |